Spaces:
Building
Building
Update websocket_handler.py
Browse files- websocket_handler.py +41 -3
websocket_handler.py
CHANGED
@@ -270,17 +270,55 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
|
|
270 |
# Initialize STT
|
271 |
stt_initialized = await realtime_session.initialize_stt()
|
272 |
if not stt_initialized:
|
273 |
-
# STT başarısız oldu, kullanıcıya bildir ve bağlantıyı kapat
|
274 |
await websocket.send_json({
|
275 |
"type": "error",
|
276 |
"message": "Speech-to-Text service initialization failed. Please check your configuration.",
|
277 |
"error_type": "stt_init_failed"
|
278 |
})
|
279 |
-
|
280 |
-
# Cleanup ve close
|
281 |
await realtime_session.cleanup()
|
282 |
await websocket.close()
|
283 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
|
285 |
# STT başarılı, devam et
|
286 |
try:
|
|
|
270 |
# Initialize STT
|
271 |
stt_initialized = await realtime_session.initialize_stt()
|
272 |
if not stt_initialized:
|
|
|
273 |
await websocket.send_json({
|
274 |
"type": "error",
|
275 |
"message": "Speech-to-Text service initialization failed. Please check your configuration.",
|
276 |
"error_type": "stt_init_failed"
|
277 |
})
|
|
|
|
|
278 |
await realtime_session.cleanup()
|
279 |
await websocket.close()
|
280 |
return
|
281 |
+
|
282 |
+
# Send welcome message if exists
|
283 |
+
if session.chat_history and len(session.chat_history) > 0:
|
284 |
+
first_message = session.chat_history[0]
|
285 |
+
if first_message.get('role') == 'assistant':
|
286 |
+
# Send welcome message
|
287 |
+
await websocket.send_json({
|
288 |
+
"type": "assistant_response",
|
289 |
+
"text": first_message.get('content', ''),
|
290 |
+
"is_welcome": True
|
291 |
+
})
|
292 |
+
|
293 |
+
# Generate TTS for welcome message
|
294 |
+
tts_provider = TTSFactory.create_provider()
|
295 |
+
if tts_provider:
|
296 |
+
try:
|
297 |
+
welcome_text = first_message.get('content', '')
|
298 |
+
audio_data = await tts_provider.synthesize(welcome_text)
|
299 |
+
|
300 |
+
# Send audio in chunks
|
301 |
+
chunk_size = realtime_session.audio_chunk_size
|
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:
|