BentoML icon indicating copy to clipboard operation
BentoML copied to clipboard

Make extracting audio file from file stream more intuitive.

Open zhangyilun opened this issue 3 years ago • 5 comments

Is your feature request related to a problem? Please describe. My use case is an audio classification service where the user will be sending audio file to the service for prediction. I'm using FileInput() and followed the doc and example at: https://docs.bentoml.org/en/latest/api/adapters.html#fileinput. In the given example, it was an image file and PIL can open it directly without any issue. However, when I applied the same with librosa, I was getting error.

After digging deeper and inspected the class and attributes of the file stream in BentoML, I noticed I have to extract file_stream._stream and send that to librosa.load(...) to make it work.

@bentoml.api(...)
def predict(self, file_streams):
    # Extract features
    feature_arrays = []
    for fs in file_streams:
        feature_arrays.append(extract_features(fs._stream))
    feature_arrays = np.array(feature_arrays)

def extract_features(fs):
    audio, sr = librosa.load(fs, AUDIO_SAMPLING_RATE)
    feat = exctract_feature(audio, sr)
    return feat 

Describe the solution you'd like I ended up making it work but it would be great to have more docs related to how to make it work with different file types. Or to return file_stream._stream if the input has been detected as audio. NOTE: I haven't tested other audio loading libraries, but for librosa, which is a fairly common audio processing library, the input has to be file_steam._stream.

Describe alternatives you've considered ...

Additional context ...

zhangyilun avatar Apr 27 '21 18:04 zhangyilun

cc @bojiang

parano avatar Jun 15 '21 20:06 parano

@sauyon is this similar to the issue you were encountering with XGBoost DMatrix?

parano avatar Jan 19 '22 01:01 parano

Proposed change:

Instead of the current FileLike object, we want to add additional return types to File, including the native BytesIO and TextIO.

File(type=bytesIO, textIO)

Discuss: should we deprecate the FileLike class? What if user code requires the file name?

parano avatar Jan 19 '22 02:01 parano

Update: with PR https://github.com/bentoml/BentoML/pull/2272, we can now add io.Text support to the File IO Descriptor

parano avatar May 05 '22 00:05 parano

Discussed with the team, we will address this with an audio gallery project.

ssheng avatar Jul 22 '22 20:07 ssheng