Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 76 |
-
if isinstance(file_input, str):
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
#
|
| 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)
|