From 69777a97df7a4c1c172303b25aa0c6bcc9324b45 Mon Sep 17 00:00:00 2001 From: "bin.xue" Date: Wed, 27 Jul 2022 17:08:51 +0800 Subject: [PATCH] [to #42322933]feat: ANS pipeline can accept bytes as input and adjust processing order to reduce the amount of computation Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/9533946 --- modelscope/pipelines/audio/ans_pipeline.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/modelscope/pipelines/audio/ans_pipeline.py b/modelscope/pipelines/audio/ans_pipeline.py index 80b6bae1..699b67b3 100644 --- a/modelscope/pipelines/audio/ans_pipeline.py +++ b/modelscope/pipelines/audio/ans_pipeline.py @@ -1,4 +1,4 @@ -import os.path +import io from typing import Any, Dict import librosa @@ -46,14 +46,17 @@ class ANSPipeline(Pipeline): self.model.eval() def preprocess(self, inputs: Input) -> Dict[str, Any]: - assert isinstance(inputs, str) and os.path.exists(inputs) and os.path.isfile(inputs), \ - f'Input file do not exists: {inputs}' - data1, fs = sf.read(inputs) - data1 = audio_norm(data1) + if isinstance(inputs, bytes): + raw_data, fs = sf.read(io.BytesIO(inputs)) + elif isinstance(inputs, str): + raw_data, fs = sf.read(inputs) + else: + raise TypeError(f'Unsupported type {type(inputs)}.') + if len(raw_data.shape) > 1: + data1 = raw_data[:, 0] if fs != self.SAMPLE_RATE: data1 = librosa.resample(data1, fs, self.SAMPLE_RATE) - if len(data1.shape) > 1: - data1 = data1[:, 0] + data1 = audio_norm(data1) data = data1.astype(np.float32) inputs = np.reshape(data, [1, data.shape[0]]) return {'ndarray': inputs, 'nsamples': data.shape[0]}