Spaces:
Sleeping
Sleeping
Update model_service.py
Browse files- model_service.py +15 -11
model_service.py
CHANGED
|
@@ -23,24 +23,28 @@ class ModelService:
|
|
| 23 |
print(f"Error loading model: {e}")
|
| 24 |
raise e
|
| 25 |
|
| 26 |
-
|
| 27 |
"""
|
| 28 |
-
Load audio bytes, resample to 16000 Hz
|
| 29 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
audio_file = io.BytesIO(audio_bytes)
|
| 34 |
-
|
| 35 |
-
# Load and resample to 16k
|
| 36 |
-
speech, sr = librosa.load(audio_file, sr=16000)
|
| 37 |
-
|
| 38 |
-
# Ensure it's mono (if multi-channel, average them) - librosa.load handles this by default (mono=True)
|
| 39 |
|
|
|
|
|
|
|
| 40 |
return speech
|
| 41 |
except Exception as e:
|
| 42 |
print(f"Error processing audio: {e}")
|
| 43 |
-
raise ValueError("Invalid audio format or corrupted file: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def predict(self, audio_bytes):
|
| 46 |
speech = self.preprocess_audio(audio_bytes)
|
|
|
|
| 23 |
print(f"Error loading model: {e}")
|
| 24 |
raise e
|
| 25 |
|
| 26 |
+
def preprocess_audio(self, audio_bytes):
|
| 27 |
"""
|
| 28 |
+
Load audio bytes, resample to 16000 Hz.
|
| 29 |
"""
|
| 30 |
+
import tempfile
|
| 31 |
+
import os
|
| 32 |
+
|
| 33 |
+
# Temp file handles high-quality WAV/MP3 better than memory buffers
|
| 34 |
+
fd, tmp_path = tempfile.mkstemp(suffix=".audio")
|
| 35 |
try:
|
| 36 |
+
with os.fdopen(fd, 'wb') as tmp:
|
| 37 |
+
tmp.write(audio_bytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Load and resample to 16kHz
|
| 40 |
+
speech, _ = librosa.load(tmp_path, sr=16000)
|
| 41 |
return speech
|
| 42 |
except Exception as e:
|
| 43 |
print(f"Error processing audio: {e}")
|
| 44 |
+
raise ValueError(f"Invalid audio format or corrupted file: {str(e)}")
|
| 45 |
+
finally:
|
| 46 |
+
if os.path.exists(tmp_path):
|
| 47 |
+
os.remove(tmp_path)
|
| 48 |
|
| 49 |
def predict(self, audio_bytes):
|
| 50 |
speech = self.preprocess_audio(audio_bytes)
|