Spaces:
Building
Building
Update stt_google.py
Browse files- stt_google.py +127 -100
stt_google.py
CHANGED
@@ -110,120 +110,147 @@ class GoogleCloudSTT(STTInterface):
|
|
110 |
log_error(f"❌ Error queuing result: {e}")
|
111 |
|
112 |
def _run_stream(self):
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
log_warning(f"⚠️ Approaching 5-minute limit ({elapsed:.1f}s), ending stream gracefully")
|
128 |
-
break
|
129 |
-
|
130 |
-
# Get audio chunk with timeout
|
131 |
-
chunk = self.audio_queue.get(timeout=0.1)
|
132 |
-
if chunk is None: # Poison pill
|
133 |
-
log_info("📛 Poison pill received, stopping request generator")
|
134 |
-
break
|
135 |
-
chunk_count += 1
|
136 |
-
|
137 |
-
# Sadece önemli milestone'larda logla
|
138 |
-
if chunk_count == 1:
|
139 |
-
log_info(f"📤 First chunk sent to Google STT, size: {len(chunk)} bytes")
|
140 |
-
elif chunk_count % 100 == 0:
|
141 |
-
log_info(f"📤 Sent {chunk_count} chunks to Google STT (elapsed: {elapsed:.1f}s)")
|
142 |
-
|
143 |
-
yield speech.StreamingRecognizeRequest(audio_content=chunk)
|
144 |
-
except queue.Empty:
|
145 |
-
continue
|
146 |
-
except Exception as e:
|
147 |
-
log_error(f"❌ Error in request generator: {e}")
|
148 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
-
log_info("
|
|
|
|
|
|
|
|
|
154 |
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
if
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
empty_response_count += 1
|
173 |
-
if empty_response_count % 50 == 0:
|
174 |
-
log_warning(f"⚠️ Received {empty_response_count} empty responses from Google STT")
|
175 |
continue
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
is_final=result.is_final,
|
190 |
-
confidence=alternative.confidence if hasattr(alternative, 'confidence') and alternative.confidence else 0.0,
|
191 |
-
timestamp=datetime.now().timestamp()
|
192 |
-
)
|
193 |
-
|
194 |
-
# Put result in queue
|
195 |
-
self._put_result(transcription)
|
196 |
-
|
197 |
-
# SADECE final result'ları logla
|
198 |
-
if result.is_final:
|
199 |
-
log_info(f"🎯 GOOGLE STT FINAL: '{alternative.transcript}'")
|
200 |
|
201 |
-
|
|
|
202 |
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
log_error(f"❌ Google STT timeout - possibly network issue or slow connection")
|
215 |
-
elif "503" in error_msg or "Service Unavailable" in error_msg:
|
216 |
-
log_error(f"❌ Google STT service temporarily unavailable. Will retry...")
|
217 |
-
else:
|
218 |
-
log_error(f"❌ Google STT stream error: {error_msg}")
|
219 |
|
220 |
except Exception as e:
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
|
228 |
async def stream_audio(self, audio_chunk: bytes) -> AsyncIterator[TranscriptionResult]:
|
229 |
"""Stream audio chunk and get transcription results"""
|
|
|
110 |
log_error(f"❌ Error queuing result: {e}")
|
111 |
|
112 |
def _run_stream(self):
|
113 |
+
"""Run the streaming recognition in a separate thread"""
|
114 |
+
try:
|
115 |
+
log_info("🎤 Google STT stream thread started")
|
116 |
|
117 |
+
def request_generator():
|
118 |
+
"""Generate streaming requests"""
|
119 |
+
chunk_count = 0
|
120 |
+
total_bytes = 0 # Toplam byte sayısı
|
121 |
+
|
122 |
+
while not self.stop_event.is_set():
|
123 |
+
try:
|
124 |
+
chunk = self.audio_queue.get(timeout=0.1)
|
125 |
+
if chunk is None:
|
126 |
+
log_info("📛 Poison pill received, stopping request generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
break
|
128 |
+
|
129 |
+
chunk_count += 1
|
130 |
+
total_bytes += len(chunk)
|
131 |
+
|
132 |
+
# İlk chunk'ta audio format kontrolü
|
133 |
+
if chunk_count == 1:
|
134 |
+
log_info(f"📤 First chunk - size: {len(chunk)} bytes")
|
135 |
+
# Audio header kontrolü (WEBM magic bytes)
|
136 |
+
if chunk[:4] == b'\x1a\x45\xdf\xa3':
|
137 |
+
log_info("✅ Valid WEBM header detected")
|
138 |
+
else:
|
139 |
+
log_warning(f"⚠️ Unknown audio format, first 4 bytes: {chunk[:4].hex()}")
|
140 |
+
|
141 |
+
# Her 100 chunk'ta durum raporu
|
142 |
+
if chunk_count % 100 == 0:
|
143 |
+
avg_chunk_size = total_bytes / chunk_count
|
144 |
+
log_info(f"📤 Progress: {chunk_count} chunks, {total_bytes/1024:.1f}KB total, avg {avg_chunk_size:.0f} bytes/chunk")
|
145 |
+
|
146 |
+
yield speech.StreamingRecognizeRequest(audio_content=chunk)
|
147 |
+
|
148 |
+
except queue.Empty:
|
149 |
+
continue
|
150 |
+
except Exception as e:
|
151 |
+
log_error(f"❌ Error in request generator: {e}")
|
152 |
+
break
|
153 |
|
154 |
+
# Create streaming client
|
155 |
+
requests = request_generator()
|
156 |
+
|
157 |
+
log_info("🎤 Creating Google STT streaming client...")
|
158 |
+
|
159 |
+
try:
|
160 |
+
responses = self.client.streaming_recognize(
|
161 |
+
self.streaming_config,
|
162 |
+
requests,
|
163 |
+
timeout=300 # 5 dakika timeout
|
164 |
+
)
|
165 |
|
166 |
+
log_info("✅ Google STT streaming client created")
|
167 |
+
|
168 |
+
# Response timeout kontrolü
|
169 |
+
last_response_time = time.time()
|
170 |
+
RESPONSE_TIMEOUT = 30 # 30 saniye içinde response gelmezse
|
171 |
|
172 |
+
# Process responses
|
173 |
+
response_count = 0
|
174 |
+
empty_response_count = 0
|
175 |
+
|
176 |
+
for response in responses:
|
177 |
+
last_response_time = time.time()
|
178 |
+
response_count += 1
|
179 |
+
|
180 |
+
# Response type'ı logla
|
181 |
+
if response_count == 1:
|
182 |
+
log_info(f"📨 First response received from Google STT")
|
183 |
|
184 |
+
if self.stop_event.is_set():
|
185 |
+
log_info("🛑 Stop event detected, breaking response loop")
|
186 |
+
break
|
187 |
|
188 |
+
# Response içeriğini kontrol et
|
189 |
+
if not response.results:
|
190 |
+
empty_response_count += 1
|
191 |
+
if empty_response_count == 1:
|
192 |
+
log_debug("📭 Received empty response (no results)")
|
193 |
+
continue
|
194 |
+
|
195 |
+
for i, result in enumerate(response.results):
|
196 |
+
log_debug(f"📋 Result {i}: is_final={result.is_final}, alternatives={len(result.alternatives)}")
|
197 |
|
198 |
+
if not result.alternatives:
|
199 |
+
log_debug(f"📋 Result {i} has no alternatives")
|
|
|
|
|
|
|
200 |
continue
|
201 |
|
202 |
+
# İlk alternatifi al
|
203 |
+
alternative = result.alternatives[0]
|
204 |
+
|
205 |
+
# Sadece anlamlı text'leri işle
|
206 |
+
if alternative.transcript.strip():
|
207 |
+
# Create transcription result
|
208 |
+
transcription = TranscriptionResult(
|
209 |
+
text=alternative.transcript,
|
210 |
+
is_final=result.is_final,
|
211 |
+
confidence=alternative.confidence if hasattr(alternative, 'confidence') and alternative.confidence else 0.0,
|
212 |
+
timestamp=datetime.now().timestamp()
|
213 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
|
215 |
+
# Put result in queue
|
216 |
+
self._put_result(transcription)
|
217 |
|
218 |
+
# SADECE final result'ları logla
|
219 |
+
if result.is_final:
|
220 |
+
log_info(f"🎯 GOOGLE STT FINAL: '{alternative.transcript}'")
|
221 |
+
else:
|
222 |
+
log_debug(f"📋 Result {i} has empty transcript")
|
223 |
+
continue
|
224 |
+
|
225 |
+
if time.time() - last_response_time > RESPONSE_TIMEOUT:
|
226 |
+
log_error(f"❌ No response from Google STT for {RESPONSE_TIMEOUT} seconds")
|
227 |
+
|
228 |
+
log_info(f"📊 Google STT stream ended. Total responses: {response_count}, Empty: {empty_response_count}")
|
|
|
|
|
|
|
|
|
|
|
229 |
|
230 |
except Exception as e:
|
231 |
+
error_msg = str(e)
|
232 |
+
|
233 |
+
# Detaylı hata mesajları
|
234 |
+
if "Exceeded maximum allowed stream duration" in error_msg:
|
235 |
+
log_warning("⚠️ Stream duration limit exceeded (5 minutes). This is expected for long sessions.")
|
236 |
+
elif "Bad language code" in error_msg:
|
237 |
+
log_error(f"❌ Invalid language code in STT config. Check locale settings.")
|
238 |
+
elif "invalid_argument" in error_msg:
|
239 |
+
log_error(f"❌ Invalid STT configuration. Check encoding and sample rate.")
|
240 |
+
elif "Deadline Exceeded" in error_msg:
|
241 |
+
log_error(f"❌ Google STT response timeout - possibly network issue or slow connection")
|
242 |
+
elif "503" in error_msg or "Service Unavailable" in error_msg:
|
243 |
+
log_error(f"❌ Google STT service temporarily unavailable. Will retry...")
|
244 |
+
else:
|
245 |
+
log_error(f"❌ Google STT stream error: {error_msg}")
|
246 |
+
|
247 |
+
except Exception as e:
|
248 |
+
import traceback
|
249 |
+
log_error(f"❌ Fatal error in STT stream thread", error=str(e), traceback=traceback.format_exc())
|
250 |
+
finally:
|
251 |
+
log_info("🎤 Google STT stream thread ended")
|
252 |
+
# Thread bittiğinde streaming flag'ini kapat
|
253 |
+
self.is_streaming = False
|
254 |
|
255 |
async def stream_audio(self, audio_chunk: bytes) -> AsyncIterator[TranscriptionResult]:
|
256 |
"""Stream audio chunk and get transcription results"""
|