Spaces:
Building
Building
Update websocket_handler.py
Browse files- websocket_handler.py +47 -49
websocket_handler.py
CHANGED
@@ -189,14 +189,27 @@ class RealtimeSession:
|
|
189 |
# Chunk counter için attribute
|
190 |
self.chunk_counter = 0
|
191 |
|
|
|
|
|
|
|
|
|
192 |
async def initialize_stt(self):
|
193 |
"""Initialize STT provider with clean state"""
|
194 |
try:
|
|
|
|
|
|
|
|
|
195 |
# Önce mevcut STT'yi tamamen temizle
|
196 |
await self.stop_stt_streaming()
|
197 |
|
198 |
-
#
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
# Tüm değişkenleri yeniden başlat
|
202 |
self.chunk_counter = 0
|
@@ -251,39 +264,19 @@ class RealtimeSession:
|
|
251 |
# Hata durumunda da temizlik yap
|
252 |
await self.stop_stt_streaming()
|
253 |
return False
|
254 |
-
|
255 |
-
async def restart_stt_if_needed(self):
|
256 |
-
"""Restart STT if it's not active"""
|
257 |
-
try:
|
258 |
-
# Sadece LISTENING state'inde ve WebSocket aktifse restart yap
|
259 |
-
if not self.is_streaming and self.is_websocket_active and self.state == ConversationState.LISTENING:
|
260 |
-
log_info(f"🔄 Restarting STT stream...", session_id=self.session.session_id)
|
261 |
-
|
262 |
-
# Önce mevcut stream'i temizle
|
263 |
-
await self.stop_stt_streaming()
|
264 |
-
|
265 |
-
# Sonra yeniden başlat
|
266 |
-
stt_initialized = await self.initialize_stt()
|
267 |
-
if stt_initialized:
|
268 |
-
log_info(f"✅ STT stream restarted successfully", session_id=self.session.session_id)
|
269 |
-
return True
|
270 |
-
else:
|
271 |
-
log_error(f"❌ Failed to restart STT stream", session_id=self.session.session_id)
|
272 |
-
return False
|
273 |
-
return True
|
274 |
-
except Exception as e:
|
275 |
-
log_error(f"❌ Error restarting STT", error=str(e), session_id=self.session.session_id)
|
276 |
-
return False
|
277 |
|
278 |
async def stop_stt_streaming(self):
|
279 |
"""Stop STT streaming completely and reset all STT-related variables"""
|
280 |
try:
|
|
|
|
|
|
|
281 |
if self.stt_manager:
|
282 |
-
log_info(f"🛑 Stopping STT stream and resetting all STT data", session_id=self.session.session_id)
|
283 |
-
|
284 |
-
# STT manager'ı durdur
|
285 |
if self.is_streaming:
|
286 |
-
|
|
|
|
|
|
|
287 |
|
288 |
# STT manager'ı tamamen sil
|
289 |
self.stt_manager = None
|
@@ -303,37 +296,33 @@ class RealtimeSession:
|
|
303 |
if hasattr(self, 'speech_started'):
|
304 |
delattr(self, 'speech_started')
|
305 |
|
306 |
-
|
|
|
|
|
|
|
307 |
|
308 |
except Exception as e:
|
309 |
-
|
310 |
# Hata olsa bile değişkenleri resetle
|
311 |
self.stt_manager = None
|
312 |
self.is_streaming = False
|
313 |
self.chunk_counter = 0
|
314 |
self.current_transcription = ""
|
315 |
-
|
316 |
-
|
|
|
|
|
|
|
317 |
|
318 |
async def restart_stt_if_needed(self):
|
319 |
"""Restart STT if it's not active"""
|
320 |
try:
|
321 |
# Sadece LISTENING state'inde ve WebSocket aktifse restart yap
|
322 |
if not self.is_streaming and self.is_websocket_active and self.state == ConversationState.LISTENING:
|
323 |
-
log_info(f"🔄 Restarting STT stream
|
|
|
324 |
|
325 |
-
#
|
326 |
-
await self.audio_buffer.clear()
|
327 |
-
self.chunk_counter = 0
|
328 |
-
|
329 |
-
# Önce mevcut stream'i temizle (eğer varsa)
|
330 |
-
if self.stt_manager:
|
331 |
-
await self.stop_stt_streaming()
|
332 |
-
|
333 |
-
# Biraz bekle - Google API'nin toparlanması için
|
334 |
-
await asyncio.sleep(0.5)
|
335 |
-
|
336 |
-
# Sonra yeniden başlat
|
337 |
stt_initialized = await self.initialize_stt()
|
338 |
if stt_initialized:
|
339 |
log_info(f"✅ STT stream restarted successfully", session_id=self.session.session_id)
|
@@ -343,7 +332,8 @@ class RealtimeSession:
|
|
343 |
return False
|
344 |
return True
|
345 |
except Exception as e:
|
346 |
-
log_error(f"❌ Error restarting STT", error=str(e), traceback=traceback.format_exc(),
|
|
|
347 |
return False
|
348 |
|
349 |
async def change_state(self, new_state: ConversationState):
|
@@ -364,13 +354,21 @@ class RealtimeSession:
|
|
364 |
|
365 |
async def reset_for_new_utterance(self):
|
366 |
"""Reset for new user utterance"""
|
|
|
|
|
|
|
367 |
await self.audio_buffer.clear()
|
368 |
self.silence_detector.reset()
|
|
|
|
|
369 |
self.current_transcription = ""
|
370 |
-
self.chunk_counter = 0
|
|
|
|
|
371 |
if hasattr(self, 'speech_started'):
|
372 |
delattr(self, 'speech_started')
|
373 |
-
|
|
|
374 |
|
375 |
async def cleanup(self):
|
376 |
"""Clean up resources"""
|
|
|
189 |
# Chunk counter için attribute
|
190 |
self.chunk_counter = 0
|
191 |
|
192 |
+
# Session management - YENİ
|
193 |
+
self.stt_session_count = 0
|
194 |
+
self.last_stt_stop_time = None
|
195 |
+
|
196 |
async def initialize_stt(self):
|
197 |
"""Initialize STT provider with clean state"""
|
198 |
try:
|
199 |
+
# Session numarasını artır
|
200 |
+
self.stt_session_count += 1
|
201 |
+
log_info(f"🎤 Initializing STT session #{self.stt_session_count}", session_id=self.session.session_id)
|
202 |
+
|
203 |
# Önce mevcut STT'yi tamamen temizle
|
204 |
await self.stop_stt_streaming()
|
205 |
|
206 |
+
# Önceki stop'tan bu yana yeterli zaman geçtiğinden emin ol
|
207 |
+
if self.last_stt_stop_time:
|
208 |
+
elapsed = (datetime.now() - self.last_stt_stop_time).total_seconds()
|
209 |
+
if elapsed < 0.5:
|
210 |
+
wait_time = 0.5 - elapsed
|
211 |
+
log_info(f"⏳ Waiting {wait_time:.2f}s for proper cleanup", session_id=self.session.session_id)
|
212 |
+
await asyncio.sleep(wait_time)
|
213 |
|
214 |
# Tüm değişkenleri yeniden başlat
|
215 |
self.chunk_counter = 0
|
|
|
264 |
# Hata durumunda da temizlik yap
|
265 |
await self.stop_stt_streaming()
|
266 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
|
268 |
async def stop_stt_streaming(self):
|
269 |
"""Stop STT streaming completely and reset all STT-related variables"""
|
270 |
try:
|
271 |
+
log_info(f"🛑 Stopping STT session #{self.stt_session_count}", session_id=self.session.session_id)
|
272 |
+
|
273 |
+
# STT manager varsa durdur
|
274 |
if self.stt_manager:
|
|
|
|
|
|
|
275 |
if self.is_streaming:
|
276 |
+
try:
|
277 |
+
await self.stt_manager.stop_streaming()
|
278 |
+
except Exception as e:
|
279 |
+
log_warning(f"⚠️ Error during STT stop_streaming: {e}", session_id=self.session.session_id)
|
280 |
|
281 |
# STT manager'ı tamamen sil
|
282 |
self.stt_manager = None
|
|
|
296 |
if hasattr(self, 'speech_started'):
|
297 |
delattr(self, 'speech_started')
|
298 |
|
299 |
+
# Stop zamanını kaydet
|
300 |
+
self.last_stt_stop_time = datetime.now()
|
301 |
+
|
302 |
+
log_info(f"✅ STT session #{self.stt_session_count} stopped and all data reset", session_id=self.session.session_id)
|
303 |
|
304 |
except Exception as e:
|
305 |
+
log_error(f"❌ Error in stop_stt_streaming", error=str(e), session_id=self.session.session_id)
|
306 |
# Hata olsa bile değişkenleri resetle
|
307 |
self.stt_manager = None
|
308 |
self.is_streaming = False
|
309 |
self.chunk_counter = 0
|
310 |
self.current_transcription = ""
|
311 |
+
if self.audio_buffer:
|
312 |
+
await self.audio_buffer.clear()
|
313 |
+
if self.silence_detector:
|
314 |
+
self.silence_detector.reset()
|
315 |
+
self.last_stt_stop_time = datetime.now()
|
316 |
|
317 |
async def restart_stt_if_needed(self):
|
318 |
"""Restart STT if it's not active"""
|
319 |
try:
|
320 |
# Sadece LISTENING state'inde ve WebSocket aktifse restart yap
|
321 |
if not self.is_streaming and self.is_websocket_active and self.state == ConversationState.LISTENING:
|
322 |
+
log_info(f"🔄 Restarting STT stream (session #{self.stt_session_count} -> #{self.stt_session_count + 1})",
|
323 |
+
session_id=self.session.session_id)
|
324 |
|
325 |
+
# Yeni session başlat (initialize_stt zaten stop_stt_streaming'i çağırıyor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
stt_initialized = await self.initialize_stt()
|
327 |
if stt_initialized:
|
328 |
log_info(f"✅ STT stream restarted successfully", session_id=self.session.session_id)
|
|
|
332 |
return False
|
333 |
return True
|
334 |
except Exception as e:
|
335 |
+
log_error(f"❌ Error restarting STT", error=str(e), traceback=traceback.format_exc(),
|
336 |
+
session_id=self.session.session_id)
|
337 |
return False
|
338 |
|
339 |
async def change_state(self, new_state: ConversationState):
|
|
|
354 |
|
355 |
async def reset_for_new_utterance(self):
|
356 |
"""Reset for new user utterance"""
|
357 |
+
log_info(f"🔄 Resetting for new utterance", session_id=self.session.session_id)
|
358 |
+
|
359 |
+
# Buffer ve detector'ı temizle
|
360 |
await self.audio_buffer.clear()
|
361 |
self.silence_detector.reset()
|
362 |
+
|
363 |
+
# Transcription ve counter'ı sıfırla
|
364 |
self.current_transcription = ""
|
365 |
+
self.chunk_counter = 0
|
366 |
+
|
367 |
+
# Speech started flag'ini temizle
|
368 |
if hasattr(self, 'speech_started'):
|
369 |
delattr(self, 'speech_started')
|
370 |
+
|
371 |
+
log_info(f"✅ Reset for new utterance complete", session_id=self.session.session_id)
|
372 |
|
373 |
async def cleanup(self):
|
374 |
"""Clean up resources"""
|