Spaces:
Building
Building
Update stt/stt_lifecycle_manager.py
Browse files- stt/stt_lifecycle_manager.py +110 -1
stt/stt_lifecycle_manager.py
CHANGED
@@ -176,6 +176,11 @@ class STTLifecycleManager:
|
|
176 |
error=str(e)
|
177 |
)
|
178 |
|
|
|
|
|
|
|
|
|
|
|
179 |
async def _handle_stt_stop(self, event: Event):
|
180 |
"""Handle STT stop request and perform transcription"""
|
181 |
session_id = event.session_id
|
@@ -232,4 +237,108 @@ class STTLifecycleManager:
|
|
232 |
f"β Error stopping STT",
|
233 |
session_id=session_id,
|
234 |
error=str(e)
|
235 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
error=str(e)
|
177 |
)
|
178 |
|
179 |
+
async def _handle_session_ended(self, event: Event):
|
180 |
+
"""Clean up STT resources when session ends"""
|
181 |
+
session_id = event.session_id
|
182 |
+
await self._cleanup_session(session_id)
|
183 |
+
|
184 |
async def _handle_stt_stop(self, event: Event):
|
185 |
"""Handle STT stop request and perform transcription"""
|
186 |
session_id = event.session_id
|
|
|
237 |
f"β Error stopping STT",
|
238 |
session_id=session_id,
|
239 |
error=str(e)
|
240 |
+
)
|
241 |
+
|
242 |
+
async def _cleanup_session(self, session_id: str):
|
243 |
+
"""Clean up STT session"""
|
244 |
+
stt_session = self.stt_sessions.pop(session_id, None)
|
245 |
+
if not stt_session:
|
246 |
+
return
|
247 |
+
|
248 |
+
try:
|
249 |
+
# Mark as inactive
|
250 |
+
stt_session.is_active = False
|
251 |
+
|
252 |
+
# Release resource
|
253 |
+
resource_id = f"stt_{session_id}"
|
254 |
+
await self.resource_manager.release(resource_id, delay_seconds=60)
|
255 |
+
|
256 |
+
log_info(
|
257 |
+
f"π§Ή STT session cleaned up",
|
258 |
+
session_id=session_id,
|
259 |
+
total_chunks=stt_session.total_chunks,
|
260 |
+
total_bytes=stt_session.total_bytes
|
261 |
+
)
|
262 |
+
|
263 |
+
except Exception as e:
|
264 |
+
log_error(
|
265 |
+
f"β Error cleaning up STT session",
|
266 |
+
session_id=session_id,
|
267 |
+
error=str(e)
|
268 |
+
)
|
269 |
+
|
270 |
+
async def _cleanup_stt_instance(self, stt_instance: STTInterface):
|
271 |
+
"""Cleanup callback for STT instance"""
|
272 |
+
try:
|
273 |
+
log_debug("π§Ή STT instance cleaned up")
|
274 |
+
except Exception as e:
|
275 |
+
log_error(f"β Error cleaning up STT instance", error=str(e))
|
276 |
+
|
277 |
+
def _setup_resource_pool(self):
|
278 |
+
"""Setup STT instance pool"""
|
279 |
+
self.resource_manager.register_pool(
|
280 |
+
resource_type=ResourceType.STT_INSTANCE,
|
281 |
+
factory=self._create_stt_instance,
|
282 |
+
max_idle=5,
|
283 |
+
max_age_seconds=300 # 5 minutes
|
284 |
+
)
|
285 |
+
|
286 |
+
async def _create_stt_instance(self) -> STTInterface:
|
287 |
+
"""Factory for creating STT instances"""
|
288 |
+
try:
|
289 |
+
stt_instance = STTFactory.create_provider()
|
290 |
+
if not stt_instance:
|
291 |
+
raise ValueError("Failed to create STT instance")
|
292 |
+
|
293 |
+
log_debug("π€ Created new STT instance")
|
294 |
+
return stt_instance
|
295 |
+
|
296 |
+
except Exception as e:
|
297 |
+
log_error(f"β Failed to create STT instance", error=str(e))
|
298 |
+
raise
|
299 |
+
|
300 |
+
def _get_language_code(self, locale: str) -> str:
|
301 |
+
"""Convert locale to STT language code"""
|
302 |
+
# Map common locales to STT language codes
|
303 |
+
locale_map = {
|
304 |
+
"tr": "tr-TR",
|
305 |
+
"en": "en-US",
|
306 |
+
"de": "de-DE",
|
307 |
+
"fr": "fr-FR",
|
308 |
+
"es": "es-ES",
|
309 |
+
"it": "it-IT",
|
310 |
+
"pt": "pt-BR",
|
311 |
+
"ru": "ru-RU",
|
312 |
+
"ja": "ja-JP",
|
313 |
+
"ko": "ko-KR",
|
314 |
+
"zh": "zh-CN",
|
315 |
+
"ar": "ar-SA"
|
316 |
+
}
|
317 |
+
|
318 |
+
# Check direct match
|
319 |
+
if locale in locale_map:
|
320 |
+
return locale_map[locale]
|
321 |
+
|
322 |
+
# Check if it's already a full code
|
323 |
+
if "-" in locale and len(locale) == 5:
|
324 |
+
return locale
|
325 |
+
|
326 |
+
# Default to locale-LOCALE format
|
327 |
+
return f"{locale}-{locale.upper()}"
|
328 |
+
|
329 |
+
def get_stats(self) -> Dict[str, Any]:
|
330 |
+
"""Get STT manager statistics"""
|
331 |
+
session_stats = {}
|
332 |
+
for session_id, stt_session in self.stt_sessions.items():
|
333 |
+
session_stats[session_id] = {
|
334 |
+
"is_active": stt_session.is_active,
|
335 |
+
"total_chunks": stt_session.total_chunks,
|
336 |
+
"total_bytes": stt_session.total_bytes,
|
337 |
+
"vad_stats": stt_session.vad.get_stats() if stt_session.vad else {}
|
338 |
+
}
|
339 |
+
|
340 |
+
return {
|
341 |
+
"active_sessions": len(self.stt_sessions),
|
342 |
+
"active_streaming": sum(1 for s in self.stt_sessions.values() if s.is_active),
|
343 |
+
"sessions": session_stats
|
344 |
+
}
|