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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -67,13 +67,19 @@ from io import BytesIO
67
  import librosa
68
 
69
  # Updated function for Streamlit-compatible audio processing
70
- def get_audio_embedding(uploaded_file):
71
- # Convert uploaded file to in-memory buffer
72
- audio_bytes = BytesIO(uploaded_file.read())
73
-
74
- # Load audio using librosa from the BytesIO buffer
75
- audio, sr = librosa.load(audio_bytes, sr=16000)
76
-
 
 
 
 
 
 
77
  # Extract features using the Wav2Vec2 model
78
  inputs = feature_extractor(audio, sampling_rate=sr, return_tensors="pt", padding=True)
79
  with torch.no_grad():
 
67
  import librosa
68
 
69
  # Updated function for Streamlit-compatible audio processing
70
+ import soundfile as sf
71
+ 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():