-
Notifications
You must be signed in to change notification settings - Fork 234
feat(v2): add audio url and predefined document #940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
bebc9d4
6025c2f
9a599e5
04abdae
f8d700d
d58f804
bdf8e88
6572df8
9cd4baa
b3c1948
7774181
af840d4
797f488
8b48a77
e135438
14fcf6b
c623a13
1be8e3f
17786eb
97355f7
20e2344
7fc06e1
b34d783
130d8ab
2954351
61cb103
5943c0f
131c5ff
83ef649
eecca41
4762c3c
6948122
d174087
3a52303
a0be12e
9623d29
6efdcf2
5026543
703de43
83ece31
de079e2
2ef1350
d51d38e
3901cfa
a571898
71af630
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: anna-charlotte <[email protected]>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from docarray.typing.tensor.audio.audio_ndarray import AudioNdArray | ||
|
|
||
| __all__ = ['AudioNdArray'] | ||
|
|
||
| try: | ||
| import torch # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
| else: | ||
| from docarray.typing.tensor.audio.audio_torch_tensor import AudioTorchTensor # noqa | ||
|
|
||
| __all__.extend(['AudioTorchTensor']) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,20 @@ | ||
| import wave | ||
| from typing import TYPE_CHECKING, TypeVar, Union | ||
| from typing import TYPE_CHECKING, Any, Type, TypeVar, Union | ||
|
|
||
| import numpy as np | ||
|
|
||
| from docarray.typing.url.any_url import AnyUrl | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pydantic import BaseConfig | ||
| from pydantic.fields import ModelField | ||
|
|
||
| from docarray.proto import NodeProto | ||
|
|
||
| T = TypeVar('T', bound='AudioUrl') | ||
|
|
||
| AUDIO_FILE_FORMATS = ['wav'] | ||
|
|
||
|
|
||
| class AudioUrl(AnyUrl): | ||
| """ | ||
|
|
@@ -28,9 +33,25 @@ def _to_node_protobuf(self: T) -> 'NodeProto': | |
|
|
||
| return NodeProto(audio_url=str(self)) | ||
|
|
||
| @classmethod | ||
| def validate( | ||
| cls: Type[T], | ||
| value: Union[T, np.ndarray, Any], | ||
| field: 'ModelField', | ||
| config: 'BaseConfig', | ||
| ) -> T: | ||
| url = super().validate(value, field, config) # basic url validation | ||
| has_audio_extension = any(url.endswith(ext) for ext in AUDIO_FILE_FORMATS) | ||
| if not has_audio_extension: | ||
| raise ValueError( | ||
| f'Audio URL must have one of the following extensions:' | ||
| f'{AUDIO_FILE_FORMATS}' | ||
| ) | ||
| return cls(str(url), scheme=None) | ||
|
|
||
| def load(self: T) -> np.ndarray: | ||
|
||
| """ | ||
| Load the data from the url into a numpy.ndarray audio tensor. | ||
| Load the data from the url into a numpy.ndarray. | ||
|
|
||
| EXAMPLE USAGE | ||
|
|
||
|
|
@@ -46,7 +67,7 @@ class MyDoc(Document): | |
| audio_url: AudioUrl | ||
|
|
||
|
|
||
| doc = MyDoc(mesh_url="toydata/hello.wav") | ||
| doc = MyDoc(audio_url="toydata/hello.wav") | ||
|
|
||
| audio_tensor = doc.audio_url.load() | ||
| assert isinstance(audio_tensor, np.ndarray) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.