ciyidogan commited on
Commit
15d3e9a
Β·
verified Β·
1 Parent(s): b47c34d

Update stt/stt_deepgram.py

Browse files
Files changed (1) hide show
  1. stt/stt_deepgram.py +49 -35
stt/stt_deepgram.py CHANGED
@@ -110,11 +110,12 @@ class DeepgramSTT(STTInterface):
110
  options = LiveOptions(
111
  language="tr",
112
  model="nova-2",
113
- encoding="opus",
114
  sample_rate=16000,
115
- interim_results=True, # βœ… Enable interim results
116
- utterance_end_ms=1000, # 1 second silence
117
  punctuate=True,
 
118
  )
119
 
120
  log_info(f"πŸ”§ Deepgram options: language=tr, model=nova-2, encoding=opus, interim_results=True")
@@ -167,44 +168,57 @@ class DeepgramSTT(STTInterface):
167
  return
168
 
169
  # Results handler - handles transcription results
170
- def on_transcript(self, result, **kwargs):
 
171
  try:
172
- log_info(f"πŸ“ Deepgram Transcript event received")
173
-
174
- # Extract transcription
175
- channel = result.get("channel", {})
176
- alternatives = channel.get("alternatives", [])
177
 
178
- if alternatives and len(alternatives) > 0:
179
- transcript = alternatives[0].get("transcript", "")
180
- confidence = alternatives[0].get("confidence", 0.0)
181
- is_final = result.get("is_final", False)
182
-
183
- log_info(f"πŸ“ Transcript: '{transcript}' (is_final: {is_final}, confidence: {confidence})")
184
 
185
- # Process both interim and final results for debugging
186
- if transcript.strip(): # Only process non-empty transcripts
187
- transcription_result = TranscriptionResult(
188
- text=transcript,
189
- is_final=is_final,
190
- confidence=confidence,
191
- timestamp=datetime.now().timestamp()
192
- )
 
193
 
194
- # Queue result
195
- try:
196
- self.responses_queue.put(transcription_result)
197
- if is_final:
198
- self.final_result_received = True
199
- log_info(f"🎯 FINAL RESULT queued: '{transcript}'")
200
- else:
201
- log_debug(f"πŸ“ Interim result: '{transcript}'")
202
- except queue.Full:
203
- log_warning("⚠️ Response queue full")
204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  except Exception as e:
206
- log_error(f"❌ Error in Transcript handler: {e}")
207
- log_error(f"❌ Result data: {result}")
 
 
 
208
 
209
  # Speech started handler
210
  def on_speech_started(self, speech_started, **kwargs):
 
110
  options = LiveOptions(
111
  language="tr",
112
  model="nova-2",
113
+ encoding="linear16", # βœ… Change from opus to linear16
114
  sample_rate=16000,
115
+ interim_results=True,
116
+ utterance_end_ms=1000,
117
  punctuate=True,
118
+ channels=1,
119
  )
120
 
121
  log_info(f"πŸ”§ Deepgram options: language=tr, model=nova-2, encoding=opus, interim_results=True")
 
168
  return
169
 
170
  # Results handler - handles transcription results
171
+ def _on_transcript(self, *args, **kwargs):
172
+ """Handle transcript event - SDK calls this method directly"""
173
  try:
174
+ # SDK passes the result as second argument
175
+ result = args[1] if len(args) > 1 else kwargs.get("result")
 
 
 
176
 
177
+ if not result:
178
+ log_warning("⚠️ No result in transcript event")
179
+ return
 
 
 
180
 
181
+ # Access properties directly from the result object
182
+ is_final = result.is_final
183
+
184
+ # Get transcript from channel alternatives
185
+ if hasattr(result, 'channel') and result.channel:
186
+ alternatives = result.channel.alternatives
187
+ if alternatives and len(alternatives) > 0:
188
+ transcript = alternatives[0].transcript
189
+ confidence = alternatives[0].confidence
190
 
191
+ # Log all transcripts for debugging
192
+ log_debug(f"πŸ“ Raw transcript: '{transcript}' (is_final: {is_final}, confidence: {confidence})")
 
 
 
 
 
 
 
 
193
 
194
+ if transcript and transcript.strip(): # Only process non-empty transcripts
195
+ transcription_result = TranscriptionResult(
196
+ text=transcript,
197
+ is_final=is_final,
198
+ confidence=confidence,
199
+ timestamp=datetime.now().timestamp()
200
+ )
201
+
202
+ # Queue result
203
+ try:
204
+ self.responses_queue.put(transcription_result)
205
+ if is_final:
206
+ self.final_result_received = True
207
+ log_info(f"🎯 FINAL TRANSCRIPT: '{transcript}' (confidence: {confidence:.2f})")
208
+ else:
209
+ log_info(f"πŸ“ Interim transcript: '{transcript}'")
210
+ except queue.Full:
211
+ log_warning("⚠️ Response queue full")
212
+ else:
213
+ if is_final:
214
+ log_warning(f"⚠️ Empty final transcript received")
215
+
216
  except Exception as e:
217
+ log_error(f"❌ Error processing transcript: {e}")
218
+ log_error(f"❌ Args: {args}")
219
+ log_error(f"❌ Kwargs: {kwargs}")
220
+ import traceback
221
+ log_error(f"❌ Traceback: {traceback.format_exc()}")
222
 
223
  # Speech started handler
224
  def on_speech_started(self, speech_started, **kwargs):