mariesig commited on
Commit
d99cd9d
·
1 Parent(s): cb37146

Update transcribe_audio function to include stream_name parameter and adjust usage in retrieve_audio_information

Browse files
Files changed (2) hide show
  1. offline_pipeline.py +2 -2
  2. utils.py +7 -13
offline_pipeline.py CHANGED
@@ -20,8 +20,8 @@ def retrieve_audio_information(
20
  enhanced_spec_path = f"/tmp/{sample_id}_enhanced_spectrogram.png"
21
  spec_image(original_array).save(noisy_spec_path)
22
  spec_image(enhanced_array).save(enhanced_spec_path)
23
- enhanced_transcript = transcribe_audio(enhanced_array, DEFAULT_SR, stt_model, mode="ENHANCED")
24
- noisy_transcript = transcribe_audio(original_array, DEFAULT_SR, stt_model, mode="NOISY")
25
  try:
26
  original_transcript = get_transcript(sample_id)
27
  wer_enhanced = compute_wer(original_transcript, enhanced_transcript)
 
20
  enhanced_spec_path = f"/tmp/{sample_id}_enhanced_spectrogram.png"
21
  spec_image(original_array).save(noisy_spec_path)
22
  spec_image(enhanced_array).save(enhanced_spec_path)
23
+ enhanced_transcript = transcribe_audio(enhanced_array, DEFAULT_SR, stt_model, stream_name=f"{sample_id}_enhanced")
24
+ noisy_transcript = transcribe_audio(original_array, DEFAULT_SR, stt_model, stream_name=f"{sample_id}_noisy")
25
  try:
26
  original_transcript = get_transcript(sample_id)
27
  wer_enhanced = compute_wer(original_transcript, enhanced_transcript)
utils.py CHANGED
@@ -6,8 +6,7 @@ from PIL import Image
6
  import io
7
  import matplotlib.pyplot as plt
8
  import resampy
9
- from stt_streamers import DeepgramStreamer, SonioxStreamer
10
- from constants import DEFAULT_SR
11
 
12
  def to_gradio_audio(x: np.ndarray, sr: int) -> tuple[int, np.ndarray]:
13
  x = np.asarray(x)
@@ -95,7 +94,7 @@ def compute_wer(reference: str, hypothesis: str) -> float:
95
 
96
 
97
 
98
- def transcribe_audio(audio_array: np.ndarray, sr: int, streamer_type: str = "Deepgram", mode: str = "RAW"):
99
  """
100
  Transcribe an audio array using the specified STT streamer.
101
 
@@ -103,7 +102,7 @@ def transcribe_audio(audio_array: np.ndarray, sr: int, streamer_type: str = "Dee
103
  audio_array (np.ndarray): Audio data array
104
  sr (int): Sample rate of the audio array
105
  streamer_type (str): "Soniox" or "Deepgram"
106
- mode (str): Optional label for streamer instance ("RAW", "ENHANCED", etc.)
107
 
108
  Returns:
109
  str: Transcript text
@@ -113,20 +112,15 @@ def transcribe_audio(audio_array: np.ndarray, sr: int, streamer_type: str = "Dee
113
  audio_array = resampy.resample(audio_array, sr, DEFAULT_SR)
114
  sr = DEFAULT_SR
115
 
116
- api_map = {
117
- "Soniox": SonioxStreamer,
118
- "Deepgram": DeepgramStreamer,
119
- }
120
 
121
- streamer_key = streamer_type
122
- if streamer_key not in api_map:
123
  raise ValueError(
124
  f"Invalid streamer_type '{streamer_type}'. "
125
- f"Choose from: {', '.join(api_map.keys())}"
126
  )
127
 
128
- StreamerClass = api_map[streamer_key]
129
- streamer = StreamerClass(sr, mode)
130
 
131
  transcript = streamer.stream_array(audio_array)
132
 
 
6
  import io
7
  import matplotlib.pyplot as plt
8
  import resampy
9
+ from constants import DEFAULT_SR, STREAMER_CLASSES
 
10
 
11
  def to_gradio_audio(x: np.ndarray, sr: int) -> tuple[int, np.ndarray]:
12
  x = np.asarray(x)
 
94
 
95
 
96
 
97
+ def transcribe_audio(audio_array: np.ndarray, sr: int, streamer_type: str = "Deepgram", stream_name: str = "RAW"):
98
  """
99
  Transcribe an audio array using the specified STT streamer.
100
 
 
102
  audio_array (np.ndarray): Audio data array
103
  sr (int): Sample rate of the audio array
104
  streamer_type (str): "Soniox" or "Deepgram"
105
+ stream_name (str): Optional label for streamer instance ("RAW", "ENHANCED", etc.)
106
 
107
  Returns:
108
  str: Transcript text
 
112
  audio_array = resampy.resample(audio_array, sr, DEFAULT_SR)
113
  sr = DEFAULT_SR
114
 
 
 
 
 
115
 
116
+ if streamer_type not in STREAMER_CLASSES:
 
117
  raise ValueError(
118
  f"Invalid streamer_type '{streamer_type}'. "
119
+ f"Choose from: {', '.join(STREAMER_CLASSES.keys())}"
120
  )
121
 
122
+ StreamerClass = STREAMER_CLASSES[streamer_type]
123
+ streamer = StreamerClass(sr, stream_name)
124
 
125
  transcript = streamer.stream_array(audio_array)
126