Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
c699992
1
Parent(s):
ac5f4c0
fix: add error handling in transcribe function for audio processing and resampling
Browse files
app.py
CHANGED
@@ -38,17 +38,22 @@ def transcribe(audio, state=""):
|
|
38 |
# Handle tuple of (sample_rate, audio_array)
|
39 |
print(f"Tuple contents: {audio}")
|
40 |
sample_rate, audio_data = audio
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Clear buffer
|
54 |
audio_buffer = []
|
|
|
38 |
# Handle tuple of (sample_rate, audio_array)
|
39 |
print(f"Tuple contents: {audio}")
|
40 |
sample_rate, audio_data = audio
|
41 |
+
try:
|
42 |
+
|
43 |
+
# Resample to 16kHz for NeMo
|
44 |
+
if sample_rate != 16000:
|
45 |
+
print(f"Resampling from {sample_rate}Hz to 16000Hz")
|
46 |
+
audio_data = librosa.resample(audio_data.astype(float), orig_sr=sample_rate, target_sr=16000)
|
47 |
+
# Save to temporary WAV file
|
48 |
+
temp_file = "temp_audio.wav"
|
49 |
+
sf.write(temp_file, audio_data, samplerate=16000)
|
50 |
+
print(f"Processing temporary audio file: {temp_file}")
|
51 |
+
transcription = model.transcribe([temp_file])[0]
|
52 |
+
os.remove(temp_file) # Clean up
|
53 |
+
print("Temporary file removed.")
|
54 |
+
except Exception as e:
|
55 |
+
print(f"Error processing audio: {e}")
|
56 |
+
# return state, state
|
57 |
|
58 |
# Clear buffer
|
59 |
audio_buffer = []
|