Spaces:
Building
Building
Update websocket_handler.py
Browse files- websocket_handler.py +39 -8
websocket_handler.py
CHANGED
@@ -258,17 +258,48 @@ class RealtimeSession:
|
|
258 |
return False
|
259 |
|
260 |
async def stop_stt_streaming(self):
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
self.is_streaming = False
|
267 |
self.chunk_counter = 0
|
268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
except Exception as e:
|
270 |
-
|
271 |
-
|
272 |
|
273 |
async def change_state(self, new_state: ConversationState):
|
274 |
"""Change conversation state"""
|
|
|
258 |
return False
|
259 |
|
260 |
async def stop_stt_streaming(self):
|
261 |
+
"""Stop STT streaming completely"""
|
262 |
+
try:
|
263 |
+
if self.stt_manager and self.is_streaming:
|
264 |
+
log_info(f"🛑 Stopping STT stream", session_id=self.session.session_id)
|
265 |
+
await self.stt_manager.stop_streaming()
|
266 |
+
self.is_streaming = False
|
267 |
+
self.chunk_counter = 0
|
268 |
+
# STT manager'ı sıfırla - yeni instance oluşturulması için
|
269 |
+
self.stt_manager = None
|
270 |
+
log_info(f"✅ STT stream stopped and manager reset", session_id=self.session.session_id)
|
271 |
+
except Exception as e:
|
272 |
+
log_warning(f"⚠️ Error stopping STT stream: {e}", session_id=self.session.session_id)
|
273 |
self.is_streaming = False
|
274 |
self.chunk_counter = 0
|
275 |
+
self.stt_manager = None
|
276 |
+
|
277 |
+
async def restart_stt_if_needed(self):
|
278 |
+
"""Restart STT if it's not active"""
|
279 |
+
try:
|
280 |
+
# Sadece LISTENING state'inde ve WebSocket aktifse restart yap
|
281 |
+
if not self.is_streaming and self.is_websocket_active and self.state == ConversationState.LISTENING:
|
282 |
+
log_info(f"🔄 Restarting STT stream...", session_id=self.session.session_id)
|
283 |
+
|
284 |
+
# Önce mevcut stream'i temizle (eğer varsa)
|
285 |
+
if self.stt_manager:
|
286 |
+
await self.stop_stt_streaming()
|
287 |
+
|
288 |
+
# Biraz bekle - Google API'nin toparlanması için
|
289 |
+
await asyncio.sleep(0.5)
|
290 |
+
|
291 |
+
# Sonra yeniden başlat
|
292 |
+
stt_initialized = await self.initialize_stt()
|
293 |
+
if stt_initialized:
|
294 |
+
log_info(f"✅ STT stream restarted successfully", session_id=self.session.session_id)
|
295 |
+
return True
|
296 |
+
else:
|
297 |
+
log_error(f"❌ Failed to restart STT stream", session_id=self.session.session_id)
|
298 |
+
return False
|
299 |
+
return True
|
300 |
except Exception as e:
|
301 |
+
log_error(f"❌ Error restarting STT", error=str(e), traceback=traceback.format_exc(), session_id=self.session.session_id)
|
302 |
+
return False
|
303 |
|
304 |
async def change_state(self, new_state: ConversationState):
|
305 |
"""Change conversation state"""
|