DataMine commited on
Commit
b54a482
·
verified ·
1 Parent(s): e3f109a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -72,15 +72,15 @@ from io import BytesIO
72
 
73
  # Update the function to handle both file paths and Streamlit-uploaded files
74
  def get_audio_embedding(file_input):
75
- # Determine if input is a file path (string) or a file-like object (uploaded file)
76
- if isinstance(file_input, str):
77
- # If the input is a file path, use it directly with librosa
78
- audio, sr = librosa.load(file_input, sr=16000)
79
- else:
80
- # If the input is an uploaded file (BytesIO), use soundfile to read it
81
- audio, sr = sf.read(BytesIO(file_input.read()), dtype='float32')
82
-
83
- # Extract features using the Wav2Vec2 model
84
  inputs = feature_extractor(audio, sampling_rate=sr, return_tensors="pt", padding=True)
85
  with torch.no_grad():
86
  embeddings = model(**inputs).last_hidden_state.mean(dim=1)
 
72
 
73
  # Update the function to handle both file paths and Streamlit-uploaded files
74
  def get_audio_embedding(file_input):
75
+ # If the input is a Streamlit-uploaded file, convert it to bytes and then load using librosa
76
+ if not isinstance(file_input, str):
77
+ # Streamlit uploads the file as an in-memory file-like object, so we convert it into bytes
78
+ file_input = BytesIO(file_input.read())
79
+
80
+ # Load the audio using librosa
81
+ audio, sr = librosa.load(file_input, sr=16000)
82
+
83
+ # Convert audio to embeddings using Wav2Vec2
84
  inputs = feature_extractor(audio, sampling_rate=sr, return_tensors="pt", padding=True)
85
  with torch.no_grad():
86
  embeddings = model(**inputs).last_hidden_state.mean(dim=1)