ciyidogan commited on
Commit
d3f229a
·
verified ·
1 Parent(s): 0684b72

Update stt_google.py

Browse files
Files changed (1) hide show
  1. stt_google.py +77 -50
stt_google.py CHANGED
@@ -36,10 +36,9 @@ class GoogleCloudSTT(STTInterface):
36
 
37
  # Test credential'ları
38
  try:
39
- self.client = speech.SpeechClient()
40
- # Basit bir test çağrısı
41
  log_info("🔐 Testing Google credentials...")
42
- # Bu sadece client'ın oluşturulabildiğini test eder
43
  log_info("✅ Google credentials valid")
44
  except Exception as e:
45
  log_error(f"❌ Google credentials error", error=str(e))
@@ -48,19 +47,25 @@ class GoogleCloudSTT(STTInterface):
48
  log_error(f"❌ Google credentials path not found: {credentials_path}")
49
  raise FileNotFoundError(f"Credentials file not found: {credentials_path}")
50
 
51
- self.client = speech.SpeechClient()
 
52
  self.streaming_config = None
53
  self.is_streaming = False
54
  self.audio_queue = queue.Queue()
55
- self.responses_queue = queue.Queue() # Normal Queue, asyncio.Queue değil!
56
  self.stream_thread = None
57
  self.stop_event = threading.Event()
 
58
 
59
  async def start_streaming(self, config: dict) -> None:
60
  """Initialize streaming session"""
61
  try:
62
  log_info(f"🎤 Starting Google STT streaming with config: {config}")
63
 
 
 
 
 
64
  # Convert dict to STTConfig if needed
65
  if isinstance(config, dict):
66
  stt_config = STTConfig(
@@ -89,6 +94,19 @@ class GoogleCloudSTT(STTInterface):
89
  single_utterance=stt_config.single_utterance
90
  )
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  self.is_streaming = True
93
  self.stop_event.clear()
94
 
@@ -281,51 +299,60 @@ class GoogleCloudSTT(STTInterface):
281
  raise
282
 
283
  async def stop_streaming(self) -> Optional[TranscriptionResult]:
284
- """Stop streaming and get final result"""
285
- if not self.is_streaming:
286
- return None
287
-
288
- try:
289
- log_info("🛑 Stopping Google STT streaming...")
290
-
291
- self.is_streaming = False
292
- self.stop_event.set()
293
-
294
- # Send poison pill to queue
295
- self.audio_queue.put(None)
296
-
297
- # Wait for thread to finish with longer timeout
298
- if self.stream_thread and self.stream_thread.is_alive():
299
- self.stream_thread.join(timeout=10.0)
300
- if self.stream_thread.is_alive():
301
- log_warning("⚠️ STT thread did not stop gracefully")
302
-
303
- # Clear queues
304
- while not self.audio_queue.empty():
305
- try:
306
- self.audio_queue.get_nowait()
307
- except:
308
- pass
309
-
310
- final_result = None
311
- while not self.responses_queue.empty():
312
- try:
313
- result = self.responses_queue.get_nowait()
314
- if result.is_final:
315
- final_result = result
316
- except:
317
- pass
318
-
319
- # Reset thread reference
320
- self.stream_thread = None
321
-
322
- log_info("✅ Google STT streaming stopped")
323
- return final_result
324
-
325
- except Exception as e:
326
- log_error(f"❌ Failed to stop Google STT streaming", error=str(e))
327
- self.stream_thread = None
328
- return None
 
 
 
 
 
 
 
 
 
329
 
330
  def supports_realtime(self) -> bool:
331
  """Google Cloud STT supports real-time streaming"""
 
36
 
37
  # Test credential'ları
38
  try:
39
+ # Client'ı burada oluşturma, her seferinde yeni instance oluştur
40
+ test_client = speech.SpeechClient()
41
  log_info("🔐 Testing Google credentials...")
 
42
  log_info("✅ Google credentials valid")
43
  except Exception as e:
44
  log_error(f"❌ Google credentials error", error=str(e))
 
47
  log_error(f"❌ Google credentials path not found: {credentials_path}")
48
  raise FileNotFoundError(f"Credentials file not found: {credentials_path}")
49
 
50
+ # Client'ı burada oluşturma, start_streaming'de oluştur
51
+ self.client = None
52
  self.streaming_config = None
53
  self.is_streaming = False
54
  self.audio_queue = queue.Queue()
55
+ self.responses_queue = queue.Queue()
56
  self.stream_thread = None
57
  self.stop_event = threading.Event()
58
+ self.credentials_path = credentials_path # Sakla
59
 
60
  async def start_streaming(self, config: dict) -> None:
61
  """Initialize streaming session"""
62
  try:
63
  log_info(f"🎤 Starting Google STT streaming with config: {config}")
64
 
65
+ # Her start_streaming'de yeni client oluştur
66
+ self.client = speech.SpeechClient()
67
+ log_info("✅ Created new Google Speech client")
68
+
69
  # Convert dict to STTConfig if needed
70
  if isinstance(config, dict):
71
  stt_config = STTConfig(
 
94
  single_utterance=stt_config.single_utterance
95
  )
96
 
97
+ # Queue'ları temizle
98
+ while not self.audio_queue.empty():
99
+ try:
100
+ self.audio_queue.get_nowait()
101
+ except:
102
+ pass
103
+
104
+ while not self.responses_queue.empty():
105
+ try:
106
+ self.responses_queue.get_nowait()
107
+ except:
108
+ pass
109
+
110
  self.is_streaming = True
111
  self.stop_event.clear()
112
 
 
299
  raise
300
 
301
  async def stop_streaming(self) -> Optional[TranscriptionResult]:
302
+ """Stop streaming and get final result"""
303
+ if not self.is_streaming:
304
+ return None
305
+
306
+ try:
307
+ log_info("🛑 Stopping Google STT streaming...")
308
+
309
+ self.is_streaming = False
310
+ self.stop_event.set()
311
+
312
+ # Send poison pill to queue
313
+ self.audio_queue.put(None)
314
+
315
+ # Wait for thread to finish with longer timeout
316
+ if self.stream_thread and self.stream_thread.is_alive():
317
+ self.stream_thread.join(timeout=10.0)
318
+ if self.stream_thread.is_alive():
319
+ log_warning("⚠️ STT thread did not stop gracefully")
320
+
321
+ # Clear queues
322
+ while not self.audio_queue.empty():
323
+ try:
324
+ self.audio_queue.get_nowait()
325
+ except:
326
+ pass
327
+
328
+ final_result = None
329
+ while not self.responses_queue.empty():
330
+ try:
331
+ result = self.responses_queue.get_nowait()
332
+ if result.is_final:
333
+ final_result = result
334
+ except:
335
+ pass
336
+
337
+ # Reset thread reference
338
+ self.stream_thread = None
339
+
340
+ # Client'ı kapat
341
+ if self.client:
342
+ try:
343
+ self.client.transport.close()
344
+ except:
345
+ pass
346
+ self.client = None
347
+
348
+ log_info("✅ Google STT streaming stopped")
349
+ return final_result
350
+
351
+ except Exception as e:
352
+ log_error(f"❌ Failed to stop Google STT streaming", error=str(e))
353
+ self.stream_thread = None
354
+ self.client = None
355
+ return None
356
 
357
  def supports_realtime(self) -> bool:
358
  """Google Cloud STT supports real-time streaming"""