GavinHuang commited on
Commit
c699992
·
1 Parent(s): ac5f4c0

fix: add error handling in transcribe function for audio processing and resampling

Browse files
Files changed (1) hide show
  1. app.py +16 -11
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
- # Resample to 16kHz for NeMo
42
- if sample_rate != 16000:
43
- print(f"Resampling from {sample_rate}Hz to 16000Hz")
44
- audio_data = librosa.resample(audio_data.astype(float), orig_sr=sample_rate, target_sr=16000)
45
- # Save to temporary WAV file
46
- temp_file = "temp_audio.wav"
47
- sf.write(temp_file, audio_data, samplerate=16000)
48
- print(f"Processing temporary audio file: {temp_file}")
49
- transcription = model.transcribe([temp_file])[0]
50
- os.remove(temp_file) # Clean up
51
- print("Temporary file removed.")
 
 
 
 
 
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 = []