From ebdbf75c2b033aec153c1e626b70d07ae76d962c Mon Sep 17 00:00:00 2001 From: anna-charlotte Date: Tue, 13 Dec 2022 10:25:12 +0100 Subject: [PATCH 1/2] test: add testing for remote audio file Signed-off-by: anna-charlotte --- tests/unit/document/test_converters.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/unit/document/test_converters.py b/tests/unit/document/test_converters.py index 3824bc9adf3..0a4f70e0785 100644 --- a/tests/unit/document/test_converters.py +++ b/tests/unit/document/test_converters.py @@ -36,16 +36,20 @@ def test_video_convert_pipe_key_frame_indices(pytestconfig, tmpdir): assert d.tags['keyframe_indices'] == [0, 95] -def test_audio_convert_pipe(pytestconfig, tmpdir): - num_d = 0 - for d in from_files(f'{cur_dir}/toydata/*.wav'): - fname = str(tmpdir / f'tmp{num_d}.wav') - d.load_uri_to_audio_tensor() - d.tensor = d.tensor[::-1] - d.save_audio_tensor_to_file(fname) - assert os.path.exists(fname) - num_d += 1 - assert num_d +@pytest.mark.parametrize( + 'file', + [ + 'https://www.kozco.com/tech/piano2.wav', + f'{cur_dir}/toydata/hello.wav', + f'{cur_dir}/toydata/olleh.wav', + ], +) +def test_audio_convert_pipe(file, pytestconfig, tmpdir): + d = Document(uri=file) + d.load_uri_to_audio_tensor() + fname = str(tmpdir / f'tmp.wav') + d.save_audio_tensor_to_file(fname) + assert os.path.exists(fname) def test_image_convert_pipe(pytestconfig): From f47f800896b0338f20dd9407806622bf8e2a5902 Mon Sep 17 00:00:00 2001 From: anna-charlotte Date: Tue, 13 Dec 2022 10:38:41 +0100 Subject: [PATCH 2/2] feat: add support for remote audio file Signed-off-by: anna-charlotte --- docarray/document/mixins/audio.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/docarray/document/mixins/audio.py b/docarray/document/mixins/audio.py index f6636950b52..a2f6b2e572a 100644 --- a/docarray/document/mixins/audio.py +++ b/docarray/document/mixins/audio.py @@ -43,8 +43,20 @@ def load_uri_to_audio_tensor(self: 'T') -> 'T': :return: Document itself after processed """ + if self.uri.startswith('http'): + import io + import requests + + resp = requests.get(self.uri) + resp.raise_for_status() + file = io.BytesIO() + file.write(resp.content) + file.seek(0) + else: + file = self.uri + with wave.open( - self.uri + file ) as ifile: #: note wave is Python built-in module https://docs.python.org/3/library/wave.html samples = ifile.getnframes() audio = ifile.readframes(samples)