Spaces:
Building
Building
Update websocket_handler.py
Browse files- websocket_handler.py +22 -47
websocket_handler.py
CHANGED
@@ -12,6 +12,7 @@ from enum import Enum
|
|
12 |
import numpy as np
|
13 |
import traceback
|
14 |
|
|
|
15 |
from session import Session, session_store
|
16 |
from config_provider import ConfigProvider
|
17 |
from chat_handler import handle_new_message, handle_parameter_followup
|
@@ -272,55 +273,29 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
|
|
272 |
if not stt_initialized:
|
273 |
await websocket.send_json({
|
274 |
"type": "error",
|
275 |
-
"message": "
|
276 |
-
"error_type": "stt_init_failed"
|
277 |
})
|
278 |
-
await realtime_session.cleanup()
|
279 |
-
await websocket.close()
|
280 |
-
return
|
281 |
|
282 |
-
#
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
total_chunks = (len(audio_data) + chunk_size - 1) // chunk_size
|
303 |
-
|
304 |
-
for i in range(0, len(audio_data), chunk_size):
|
305 |
-
chunk = audio_data[i:i + chunk_size]
|
306 |
-
chunk_index = i // chunk_size
|
307 |
-
|
308 |
-
await websocket.send_json({
|
309 |
-
"type": "tts_audio",
|
310 |
-
"data": base64.b64encode(chunk).decode('utf-8'),
|
311 |
-
"chunk_index": chunk_index,
|
312 |
-
"total_chunks": total_chunks,
|
313 |
-
"is_last": chunk_index == total_chunks - 1,
|
314 |
-
"is_welcome": True
|
315 |
-
})
|
316 |
-
|
317 |
-
await asyncio.sleep(0.01)
|
318 |
-
|
319 |
-
log_info(f"✅ Welcome message TTS sent", session_id=session_id)
|
320 |
-
except Exception as e:
|
321 |
-
log_error(f"Failed to generate welcome TTS", error=str(e))
|
322 |
-
|
323 |
-
# STT başarılı, devam et
|
324 |
try:
|
325 |
while True:
|
326 |
# Receive message
|
|
|
12 |
import numpy as np
|
13 |
import traceback
|
14 |
|
15 |
+
from realtime_session_manager import send_tts_welcome_message
|
16 |
from session import Session, session_store
|
17 |
from config_provider import ConfigProvider
|
18 |
from chat_handler import handle_new_message, handle_parameter_followup
|
|
|
273 |
if not stt_initialized:
|
274 |
await websocket.send_json({
|
275 |
"type": "error",
|
276 |
+
"message": "STT initialization failed"
|
|
|
277 |
})
|
|
|
|
|
|
|
278 |
|
279 |
+
# Generate and send welcome message TTS
|
280 |
+
tts_provider = TTSFactory.create_provider()
|
281 |
+
if tts_provider:
|
282 |
+
# Get welcome text from session history
|
283 |
+
welcome_text = None
|
284 |
+
if session.chat_history and len(session.chat_history) > 0:
|
285 |
+
# Find the first assistant message (welcome message)
|
286 |
+
for msg in session.chat_history:
|
287 |
+
if msg.get('role') == 'assistant':
|
288 |
+
welcome_text = msg.get('content', '')
|
289 |
+
break
|
290 |
+
|
291 |
+
if welcome_text:
|
292 |
+
await send_tts_welcome_message(
|
293 |
+
websocket,
|
294 |
+
session_id,
|
295 |
+
tts_provider,
|
296 |
+
welcome_text
|
297 |
+
)
|
298 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
299 |
try:
|
300 |
while True:
|
301 |
# Receive message
|