Spaces:
Building
Building
Update stt_google.py
Browse files- stt_google.py +77 -50
stt_google.py
CHANGED
@@ -36,10 +36,9 @@ class GoogleCloudSTT(STTInterface):
|
|
36 |
|
37 |
# Test credential'ları
|
38 |
try:
|
39 |
-
|
40 |
-
|
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 |
-
|
|
|
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 |
|
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 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"""
|