Spaces:
Building
Building
Update websocket_handler.py
Browse files- websocket_handler.py +51 -20
websocket_handler.py
CHANGED
@@ -190,12 +190,21 @@ class RealtimeSession:
|
|
190 |
self.chunk_counter = 0
|
191 |
|
192 |
async def initialize_stt(self):
|
193 |
-
"""Initialize STT provider"""
|
194 |
try:
|
195 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
self.chunk_counter = 0
|
|
|
|
|
|
|
197 |
|
198 |
-
# Yeni STT instance oluştur
|
199 |
self.stt_manager = STTFactory.create_provider()
|
200 |
if not self.stt_manager:
|
201 |
log_error("❌ STT manager is None - STTFactory.create_provider() returned None", session_id=self.session.session_id)
|
@@ -234,14 +243,13 @@ class RealtimeSession:
|
|
234 |
await self.stt_manager.start_streaming(stt_config)
|
235 |
self.is_streaming = True
|
236 |
|
237 |
-
log_info("✅ STT streaming started successfully", session_id=self.session.session_id)
|
238 |
return True
|
239 |
|
240 |
except Exception as e:
|
241 |
log_error(f"❌ Failed to initialize STT", error=str(e), traceback=traceback.format_exc(), session_id=self.session.session_id)
|
242 |
-
|
243 |
-
self.
|
244 |
-
self.chunk_counter = 0
|
245 |
return False
|
246 |
|
247 |
async def restart_stt_if_needed(self):
|
@@ -268,21 +276,44 @@ class RealtimeSession:
|
|
268 |
return False
|
269 |
|
270 |
async def stop_stt_streaming(self):
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
|
|
|
|
|
|
275 |
await self.stt_manager.stop_streaming()
|
276 |
-
|
277 |
-
|
278 |
-
# STT manager'ı sıfırla - yeni instance oluşturulması için
|
279 |
-
self.stt_manager = None
|
280 |
-
log_info(f"✅ STT stream stopped and manager reset", session_id=self.session.session_id)
|
281 |
-
except Exception as e:
|
282 |
-
log_warning(f"⚠️ Error stopping STT stream: {e}", session_id=self.session.session_id)
|
283 |
-
self.is_streaming = False
|
284 |
-
self.chunk_counter = 0
|
285 |
self.stt_manager = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
287 |
async def restart_stt_if_needed(self):
|
288 |
"""Restart STT if it's not active"""
|
|
|
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 |
+
# Biraz bekle - temizlik işleminin tamamlanması için
|
199 |
+
await asyncio.sleep(0.1)
|
200 |
+
|
201 |
+
# Tüm değişkenleri yeniden başlat
|
202 |
self.chunk_counter = 0
|
203 |
+
self.current_transcription = ""
|
204 |
+
await self.audio_buffer.clear()
|
205 |
+
self.silence_detector.reset()
|
206 |
|
207 |
+
# Yeni STT instance oluştur
|
208 |
self.stt_manager = STTFactory.create_provider()
|
209 |
if not self.stt_manager:
|
210 |
log_error("❌ STT manager is None - STTFactory.create_provider() returned None", session_id=self.session.session_id)
|
|
|
243 |
await self.stt_manager.start_streaming(stt_config)
|
244 |
self.is_streaming = True
|
245 |
|
246 |
+
log_info("✅ STT streaming started successfully with clean state", session_id=self.session.session_id)
|
247 |
return True
|
248 |
|
249 |
except Exception as e:
|
250 |
log_error(f"❌ Failed to initialize STT", error=str(e), traceback=traceback.format_exc(), session_id=self.session.session_id)
|
251 |
+
# Hata durumunda da temizlik yap
|
252 |
+
await self.stop_stt_streaming()
|
|
|
253 |
return False
|
254 |
|
255 |
async def restart_stt_if_needed(self):
|
|
|
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 |
await self.stt_manager.stop_streaming()
|
287 |
+
|
288 |
+
# STT manager'ı tamamen sil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
289 |
self.stt_manager = None
|
290 |
+
|
291 |
+
# Tüm STT ile ilgili değişkenleri resetle
|
292 |
+
self.is_streaming = False
|
293 |
+
self.chunk_counter = 0
|
294 |
+
self.current_transcription = ""
|
295 |
+
|
296 |
+
# Audio buffer'ı temizle
|
297 |
+
await self.audio_buffer.clear()
|
298 |
+
|
299 |
+
# Silence detector'ı resetle
|
300 |
+
self.silence_detector.reset()
|
301 |
+
|
302 |
+
# Speech started flag'ini temizle
|
303 |
+
if hasattr(self, 'speech_started'):
|
304 |
+
delattr(self, 'speech_started')
|
305 |
+
|
306 |
+
log_info(f"✅ STT stream stopped and all STT data reset", session_id=self.session.session_id)
|
307 |
+
|
308 |
+
except Exception as e:
|
309 |
+
log_warning(f"⚠️ Error stopping STT stream: {e}", session_id=self.session.session_id)
|
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 |
+
await self.audio_buffer.clear()
|
316 |
+
self.silence_detector.reset()
|
317 |
|
318 |
async def restart_stt_if_needed(self):
|
319 |
"""Restart STT if it's not active"""
|