Spaces:
Building
Building
Update stt_google.py
Browse files- stt_google.py +84 -44
stt_google.py
CHANGED
@@ -121,71 +121,109 @@ class GoogleCloudSTT(STTInterface):
|
|
121 |
# Get audio chunk with timeout
|
122 |
chunk = self.audio_queue.get(timeout=0.1)
|
123 |
if chunk is None: # Poison pill
|
|
|
124 |
break
|
125 |
chunk_count += 1
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
yield speech.StreamingRecognizeRequest(audio_content=chunk)
|
128 |
except queue.Empty:
|
129 |
continue
|
|
|
|
|
|
|
130 |
|
131 |
# Create streaming client
|
132 |
requests = request_generator()
|
133 |
|
134 |
log_info("🎤 Creating Google STT streaming client...")
|
135 |
-
responses = self.client.streaming_recognize(self.streaming_config, requests)
|
136 |
-
log_info("✅ Google STT streaming client created")
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
response_count += 1
|
142 |
-
log_debug(f"📥 Google STT response {response_count} received")
|
143 |
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
# Put result in async queue - create task and add to list
|
158 |
-
task = loop.create_task(self._put_result(transcription))
|
159 |
-
pending_tasks.append(task)
|
160 |
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
-
#
|
164 |
-
if
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
except Exception as e:
|
172 |
-
|
173 |
-
log_error(f"❌ Google STT stream error", error=error_msg, traceback=traceback.format_exc())
|
174 |
-
|
175 |
-
# Send error to responses queue
|
176 |
-
if "Bad language code" in error_msg:
|
177 |
-
log_error(f"❌ Invalid language code in STT config. Check locale settings.")
|
178 |
-
elif "invalid_argument" in error_msg:
|
179 |
-
log_error(f"❌ Invalid STT configuration. Check encoding and sample rate.")
|
180 |
-
elif "Deadline Exceeded" in error_msg:
|
181 |
-
log_error(f"❌ Google STT timeout - possibly network issue or slow connection")
|
182 |
-
|
183 |
finally:
|
184 |
# Wait for remaining tasks
|
185 |
if pending_tasks:
|
186 |
-
|
|
|
|
|
|
|
187 |
log_info("🎤 Google STT stream thread ended")
|
188 |
-
|
|
|
|
|
|
|
189 |
|
190 |
async def _put_result(self, result: TranscriptionResult):
|
191 |
"""Helper to put result in queue"""
|
@@ -201,9 +239,11 @@ class GoogleCloudSTT(STTInterface):
|
|
201 |
self.audio_queue.put(audio_chunk)
|
202 |
|
203 |
# Check for any results (non-blocking)
|
|
|
204 |
try:
|
205 |
while True:
|
206 |
result = self.responses_queue.get_nowait()
|
|
|
207 |
yield result
|
208 |
except asyncio.QueueEmpty:
|
209 |
pass
|
|
|
121 |
# Get audio chunk with timeout
|
122 |
chunk = self.audio_queue.get(timeout=0.1)
|
123 |
if chunk is None: # Poison pill
|
124 |
+
log_info("📛 Poison pill received, stopping request generator")
|
125 |
break
|
126 |
chunk_count += 1
|
127 |
+
|
128 |
+
# Sadece önemli milestone'larda logla
|
129 |
+
if chunk_count == 1:
|
130 |
+
log_info(f"📤 First chunk sent to Google STT, size: {len(chunk)} bytes")
|
131 |
+
elif chunk_count % 100 == 0:
|
132 |
+
log_info(f"📤 Sent {chunk_count} chunks to Google STT")
|
133 |
+
|
134 |
yield speech.StreamingRecognizeRequest(audio_content=chunk)
|
135 |
except queue.Empty:
|
136 |
continue
|
137 |
+
except Exception as e:
|
138 |
+
log_error(f"❌ Error in request generator: {e}")
|
139 |
+
break
|
140 |
|
141 |
# Create streaming client
|
142 |
requests = request_generator()
|
143 |
|
144 |
log_info("🎤 Creating Google STT streaming client...")
|
|
|
|
|
145 |
|
146 |
+
try:
|
147 |
+
responses = self.client.streaming_recognize(self.streaming_config, requests)
|
148 |
+
log_info("✅ Google STT streaming client created")
|
|
|
|
|
149 |
|
150 |
+
# Process responses
|
151 |
+
response_count = 0
|
152 |
+
empty_response_count = 0
|
153 |
+
|
154 |
+
for response in responses:
|
155 |
+
response_count += 1
|
156 |
|
157 |
+
if self.stop_event.is_set():
|
158 |
+
log_info("🛑 Stop event detected, breaking response loop")
|
159 |
+
break
|
160 |
+
|
161 |
+
# Boş response'ları say ama loglamaa
|
162 |
+
if not response.results:
|
163 |
+
empty_response_count += 1
|
164 |
+
if empty_response_count % 10 == 0:
|
165 |
+
log_warning(f"⚠️ Received {empty_response_count} empty responses from Google STT")
|
166 |
+
continue
|
|
|
|
|
|
|
167 |
|
168 |
+
for result in response.results:
|
169 |
+
if not result.alternatives:
|
170 |
+
continue
|
171 |
+
|
172 |
+
# İlk alternatifi al
|
173 |
+
alternative = result.alternatives[0]
|
174 |
|
175 |
+
# Sadece anlamlı text'leri logla
|
176 |
+
if alternative.transcript.strip():
|
177 |
+
# Create transcription result
|
178 |
+
transcription = TranscriptionResult(
|
179 |
+
text=alternative.transcript,
|
180 |
+
is_final=result.is_final,
|
181 |
+
confidence=alternative.confidence if hasattr(alternative, 'confidence') and alternative.confidence else 0.0,
|
182 |
+
timestamp=datetime.now().timestamp()
|
183 |
+
)
|
184 |
+
|
185 |
+
# Put result in async queue - create task and add to list
|
186 |
+
task = loop.create_task(self._put_result(transcription))
|
187 |
+
pending_tasks.append(task)
|
188 |
+
|
189 |
+
# Önemli: Final result'ları her zaman logla
|
190 |
+
if result.is_final:
|
191 |
+
log_info(f"🎯 GOOGLE STT FINAL: '{alternative.transcript}'")
|
192 |
+
else:
|
193 |
+
# Interim result'ları sadece ilk harfi büyükse logla (cümle başı)
|
194 |
+
if alternative.transcript and alternative.transcript[0].isupper():
|
195 |
+
log_info(f"📝 Google STT interim: '{alternative.transcript}'")
|
196 |
+
|
197 |
+
log_info(f"📊 Google STT stream ended. Total responses: {response_count}, Empty: {empty_response_count}")
|
198 |
+
|
199 |
+
except Exception as e:
|
200 |
+
error_msg = str(e)
|
201 |
+
log_error(f"❌ Google STT stream error: {error_msg}")
|
202 |
+
|
203 |
+
# Detaylı hata mesajları
|
204 |
+
if "Exceeded maximum allowed stream duration" in error_msg:
|
205 |
+
log_error("❌ Stream duration limit exceeded (5 minutes). Need to restart stream.")
|
206 |
+
elif "Bad language code" in error_msg:
|
207 |
+
log_error(f"❌ Invalid language code in STT config. Check locale settings.")
|
208 |
+
elif "invalid_argument" in error_msg:
|
209 |
+
log_error(f"❌ Invalid STT configuration. Check encoding and sample rate.")
|
210 |
+
elif "Deadline Exceeded" in error_msg:
|
211 |
+
log_error(f"❌ Google STT timeout - possibly network issue or slow connection")
|
212 |
|
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 |
async def _put_result(self, result: TranscriptionResult):
|
229 |
"""Helper to put result in queue"""
|
|
|
239 |
self.audio_queue.put(audio_chunk)
|
240 |
|
241 |
# Check for any results (non-blocking)
|
242 |
+
results_found = 0
|
243 |
try:
|
244 |
while True:
|
245 |
result = self.responses_queue.get_nowait()
|
246 |
+
results_found += 1
|
247 |
yield result
|
248 |
except asyncio.QueueEmpty:
|
249 |
pass
|