Spaces:
Building
Building
Update stt_google.py
Browse files- stt_google.py +20 -33
stt_google.py
CHANGED
@@ -50,7 +50,7 @@ class GoogleCloudSTT(STTInterface):
|
|
50 |
self.streaming_config = None
|
51 |
self.is_streaming = False
|
52 |
self.audio_queue = queue.Queue()
|
53 |
-
self.responses_queue =
|
54 |
self.stream_thread = None
|
55 |
self.stop_event = threading.Event()
|
56 |
|
@@ -106,13 +106,6 @@ class GoogleCloudSTT(STTInterface):
|
|
106 |
try:
|
107 |
log_info("🎤 Google STT stream thread started")
|
108 |
|
109 |
-
# Create a new event loop for this thread
|
110 |
-
loop = asyncio.new_event_loop()
|
111 |
-
asyncio.set_event_loop(loop)
|
112 |
-
|
113 |
-
# Task listesi tut
|
114 |
-
pending_tasks = []
|
115 |
-
|
116 |
def request_generator():
|
117 |
"""Generate streaming requests"""
|
118 |
chunk_count = 0
|
@@ -158,7 +151,7 @@ class GoogleCloudSTT(STTInterface):
|
|
158 |
log_info("🛑 Stop event detected, breaking response loop")
|
159 |
break
|
160 |
|
161 |
-
# Boş response'ları say ama
|
162 |
if not response.results:
|
163 |
empty_response_count += 1
|
164 |
if empty_response_count % 10 == 0:
|
@@ -172,7 +165,7 @@ class GoogleCloudSTT(STTInterface):
|
|
172 |
# İlk alternatifi al
|
173 |
alternative = result.alternatives[0]
|
174 |
|
175 |
-
# Sadece anlamlı text'leri
|
176 |
if alternative.transcript.strip():
|
177 |
# Create transcription result
|
178 |
transcription = TranscriptionResult(
|
@@ -182,9 +175,8 @@ class GoogleCloudSTT(STTInterface):
|
|
182 |
timestamp=datetime.now().timestamp()
|
183 |
)
|
184 |
|
185 |
-
# Put result in
|
186 |
-
|
187 |
-
pending_tasks.append(task)
|
188 |
|
189 |
# Önemli: Final result'ları her zaman logla
|
190 |
if result.is_final:
|
@@ -213,21 +205,15 @@ class GoogleCloudSTT(STTInterface):
|
|
213 |
except Exception as e:
|
214 |
log_error(f"❌ Fatal error in STT stream thread", error=str(e), traceback=traceback.format_exc())
|
215 |
finally:
|
216 |
-
# Wait for remaining tasks
|
217 |
-
if pending_tasks:
|
218 |
-
try:
|
219 |
-
loop.run_until_complete(asyncio.gather(*pending_tasks, return_exceptions=True))
|
220 |
-
except Exception as e:
|
221 |
-
log_error(f"❌ Error waiting for pending tasks: {e}")
|
222 |
log_info("🎤 Google STT stream thread ended")
|
223 |
-
try:
|
224 |
-
loop.close()
|
225 |
-
except Exception as e:
|
226 |
-
log_error(f"❌ Error closing event loop: {e}")
|
227 |
|
228 |
-
|
229 |
"""Helper to put result in queue"""
|
230 |
-
|
|
|
|
|
|
|
|
|
231 |
|
232 |
async def stream_audio(self, audio_chunk: bytes) -> AsyncIterator[TranscriptionResult]:
|
233 |
"""Stream audio chunk and get transcription results"""
|
@@ -238,16 +224,17 @@ class GoogleCloudSTT(STTInterface):
|
|
238 |
# Put audio in queue for streaming thread
|
239 |
self.audio_queue.put(audio_chunk)
|
240 |
|
241 |
-
# Check for any results
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
result = self.responses_queue.get_nowait()
|
246 |
-
|
247 |
yield result
|
248 |
-
|
249 |
-
|
250 |
-
|
|
|
251 |
except Exception as e:
|
252 |
log_error(f"❌ Google STT streaming error", error=str(e))
|
253 |
raise
|
|
|
50 |
self.streaming_config = None
|
51 |
self.is_streaming = False
|
52 |
self.audio_queue = queue.Queue()
|
53 |
+
self.responses_queue = queue.Queue() # Normal Queue, asyncio.Queue değil!
|
54 |
self.stream_thread = None
|
55 |
self.stop_event = threading.Event()
|
56 |
|
|
|
106 |
try:
|
107 |
log_info("🎤 Google STT stream thread started")
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
def request_generator():
|
110 |
"""Generate streaming requests"""
|
111 |
chunk_count = 0
|
|
|
151 |
log_info("🛑 Stop event detected, breaking response loop")
|
152 |
break
|
153 |
|
154 |
+
# Boş response'ları say ama loglama
|
155 |
if not response.results:
|
156 |
empty_response_count += 1
|
157 |
if empty_response_count % 10 == 0:
|
|
|
165 |
# İlk alternatifi al
|
166 |
alternative = result.alternatives[0]
|
167 |
|
168 |
+
# Sadece anlamlı text'leri işle
|
169 |
if alternative.transcript.strip():
|
170 |
# Create transcription result
|
171 |
transcription = TranscriptionResult(
|
|
|
175 |
timestamp=datetime.now().timestamp()
|
176 |
)
|
177 |
|
178 |
+
# Put result in queue - direkt koy, task yaratma
|
179 |
+
self._put_result(transcription)
|
|
|
180 |
|
181 |
# Önemli: Final result'ları her zaman logla
|
182 |
if result.is_final:
|
|
|
205 |
except Exception as e:
|
206 |
log_error(f"❌ Fatal error in STT stream thread", error=str(e), traceback=traceback.format_exc())
|
207 |
finally:
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
log_info("🎤 Google STT stream thread ended")
|
|
|
|
|
|
|
|
|
209 |
|
210 |
+
def _put_result(self, result: TranscriptionResult):
|
211 |
"""Helper to put result in queue"""
|
212 |
+
try:
|
213 |
+
self.responses_queue.put(result)
|
214 |
+
log_debug(f"📥 Result queued: '{result.text}', final: {result.is_final}, queue size: {self.responses_queue.qsize()}")
|
215 |
+
except Exception as e:
|
216 |
+
log_error(f"❌ Error queuing result: {e}")
|
217 |
|
218 |
async def stream_audio(self, audio_chunk: bytes) -> AsyncIterator[TranscriptionResult]:
|
219 |
"""Stream audio chunk and get transcription results"""
|
|
|
224 |
# Put audio in queue for streaming thread
|
225 |
self.audio_queue.put(audio_chunk)
|
226 |
|
227 |
+
# Check for any results in queue
|
228 |
+
while True:
|
229 |
+
try:
|
230 |
+
# Non-blocking get from normal queue
|
231 |
result = self.responses_queue.get_nowait()
|
232 |
+
log_debug(f"🎤 Yielding result: '{result.text}', final: {result.is_final}")
|
233 |
yield result
|
234 |
+
except queue.Empty:
|
235 |
+
# No more results in queue
|
236 |
+
break
|
237 |
+
|
238 |
except Exception as e:
|
239 |
log_error(f"❌ Google STT streaming error", error=str(e))
|
240 |
raise
|