Spaces:
Running
Running
Commit
·
a536271
1
Parent(s):
9f7308f
Refactor websocket_conversation function to simplify access to app state: remove request parameter and directly use websocket.app for model availability checks and audio processing tasks.
Browse files- app/api/realtime.py +7 -4
app/api/realtime.py
CHANGED
@@ -281,12 +281,15 @@ def is_silence(audio_data: bytes, threshold=SILENCE_THRESHOLD) -> bool:
|
|
281 |
return False
|
282 |
|
283 |
@router.websocket("/conversation/{client_id}")
|
284 |
-
async def websocket_conversation(websocket: WebSocket, client_id: str
|
285 |
"""WebSocket endpoint for real-time audio conversation"""
|
286 |
await manager.connect(websocket, client_id)
|
287 |
|
|
|
|
|
|
|
288 |
# Validate model availability
|
289 |
-
if not hasattr(
|
290 |
await manager.send_message(client_id, MessageType.ERROR,
|
291 |
{"message": "TTS model not available"})
|
292 |
manager.disconnect(client_id)
|
@@ -330,7 +333,7 @@ async def websocket_conversation(websocket: WebSocket, client_id: str, request:
|
|
330 |
|
331 |
# Process the complete audio asynchronously
|
332 |
asyncio.create_task(process_complete_audio(
|
333 |
-
|
334 |
))
|
335 |
|
336 |
# Notify client of end of speech
|
@@ -361,7 +364,7 @@ async def websocket_conversation(websocket: WebSocket, client_id: str, request:
|
|
361 |
|
362 |
# Process the complete audio asynchronously
|
363 |
asyncio.create_task(process_complete_audio(
|
364 |
-
|
365 |
))
|
366 |
|
367 |
elif message_type == "set_voice":
|
|
|
281 |
return False
|
282 |
|
283 |
@router.websocket("/conversation/{client_id}")
|
284 |
+
async def websocket_conversation(websocket: WebSocket, client_id: str):
|
285 |
"""WebSocket endpoint for real-time audio conversation"""
|
286 |
await manager.connect(websocket, client_id)
|
287 |
|
288 |
+
# Get access to app state through the websocket
|
289 |
+
app = websocket.app
|
290 |
+
|
291 |
# Validate model availability
|
292 |
+
if not hasattr(app.state, "generator") or app.state.generator is None:
|
293 |
await manager.send_message(client_id, MessageType.ERROR,
|
294 |
{"message": "TTS model not available"})
|
295 |
manager.disconnect(client_id)
|
|
|
333 |
|
334 |
# Process the complete audio asynchronously
|
335 |
asyncio.create_task(process_complete_audio(
|
336 |
+
app, client_id, full_audio
|
337 |
))
|
338 |
|
339 |
# Notify client of end of speech
|
|
|
364 |
|
365 |
# Process the complete audio asynchronously
|
366 |
asyncio.create_task(process_complete_audio(
|
367 |
+
app, client_id, full_audio
|
368 |
))
|
369 |
|
370 |
elif message_type == "set_voice":
|