BentoML
BentoML copied to clipboard
Make extracting audio file from file stream more intuitive.
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 ...
cc @bojiang
@sauyon is this similar to the issue you were encountering with XGBoost DMatrix?
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?
Update: with PR https://github.com/bentoml/BentoML/pull/2272, we can now add io.Text support to the File IO Descriptor
Discussed with the team, we will address this with an audio gallery project.