GavinHuang commited on
Commit
8505a8f
·
1 Parent(s): 31b10c8

fix: enhance audio processing in transcribe function with librosa and improve transcription output

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -23,12 +23,13 @@ def load_model():
23
  print(f"Model loaded on device: {model.device}")
24
  return model
25
 
26
-
27
  @spaces.GPU(duration=120)
28
  def transcribe(audio, state=""):
29
  # Load the model inside the GPU worker process
30
  import numpy as np
31
  import soundfile as sf
 
 
32
  model = load_model()
33
 
34
  if audio is None or isinstance(audio, int):
@@ -36,17 +37,13 @@ def transcribe(audio, state=""):
36
  return state, state
37
  print(f"Received audio input of type: {type(audio)}")
38
  print(f"Audio shape: {audio.shape if isinstance(audio, np.ndarray) else 'N/A'}")
39
- # Append NumPy array to buffer
40
- # if isinstance(audio, tuple):
41
- # print(f"Tuple contents: {audio}")
42
- # # Try extracting the first element
43
- # audio = audio[1] if len(audio) > 1 else None
44
-
45
  if isinstance(audio, tuple) and len(audio) == 2 and isinstance(audio[1], np.ndarray):
46
  # Handle tuple of (sample_rate, audio_array)
47
  print(f"Tuple contents: {audio}")
48
  sample_rate, audio_data = audio
49
- try: # Resample to 16kHz for NeMo
 
50
  if sample_rate != 16000:
51
  print(f"Resampling from {sample_rate}Hz to 16000Hz")
52
  audio_data = librosa.resample(audio_data.astype(float), orig_sr=sample_rate, target_sr=16000)
@@ -55,17 +52,20 @@ def transcribe(audio, state=""):
55
  sf.write(temp_file, audio_data, samplerate=16000)
56
  print(f"Processing temporary audio file: {temp_file}")
57
 
58
- transcription = model.transcribe([temp_file])[0]
59
- print(type(transcription))
 
 
 
60
 
61
  os.remove(temp_file) # Clean up
62
  print("Temporary file removed.")
63
  except Exception as e:
64
  print(f"Error processing audio: {e}")
65
- # return state, state
66
 
67
  new_state = state + " " + transcription if state else transcription
68
- print(new_state)
69
  return new_state, new_state
70
  return state, state
71
 
 
23
  print(f"Model loaded on device: {model.device}")
24
  return model
25
 
 
26
  @spaces.GPU(duration=120)
27
  def transcribe(audio, state=""):
28
  # Load the model inside the GPU worker process
29
  import numpy as np
30
  import soundfile as sf
31
+ import librosa
32
+ import os
33
  model = load_model()
34
 
35
  if audio is None or isinstance(audio, int):
 
37
  return state, state
38
  print(f"Received audio input of type: {type(audio)}")
39
  print(f"Audio shape: {audio.shape if isinstance(audio, np.ndarray) else 'N/A'}")
40
+
 
 
 
 
 
41
  if isinstance(audio, tuple) and len(audio) == 2 and isinstance(audio[1], np.ndarray):
42
  # Handle tuple of (sample_rate, audio_array)
43
  print(f"Tuple contents: {audio}")
44
  sample_rate, audio_data = audio
45
+ try:
46
+ # Resample to 16kHz for NeMo
47
  if sample_rate != 16000:
48
  print(f"Resampling from {sample_rate}Hz to 16000Hz")
49
  audio_data = librosa.resample(audio_data.astype(float), orig_sr=sample_rate, target_sr=16000)
 
52
  sf.write(temp_file, audio_data, samplerate=16000)
53
  print(f"Processing temporary audio file: {temp_file}")
54
 
55
+ # Transcribe and extract only the text (string)
56
+ hypothesis = model.transcribe([temp_file])[0]
57
+ print(f"Hypothesis: {hypothesis}")
58
+ transcription = hypothesis.text # Extract the text attribute (string)
59
+ print(f"Transcription: {transcription}")
60
 
61
  os.remove(temp_file) # Clean up
62
  print("Temporary file removed.")
63
  except Exception as e:
64
  print(f"Error processing audio: {e}")
65
+ return state, state
66
 
67
  new_state = state + " " + transcription if state else transcription
68
+ print(f"New state: {new_state}")
69
  return new_state, new_state
70
  return state, state
71