Spaces:
Building
Building
Update realtime_session_manager.py
Browse files- realtime_session_manager.py +23 -4
realtime_session_manager.py
CHANGED
@@ -5,8 +5,9 @@ import base64
|
|
5 |
from typing import Optional
|
6 |
from datetime import datetime
|
7 |
import sys
|
|
|
8 |
|
9 |
-
from logger import log_info, log_error, log_debug
|
10 |
|
11 |
def log(message: str):
|
12 |
"""Log helper with timestamp"""
|
@@ -17,31 +18,49 @@ def log(message: str):
|
|
17 |
async def send_tts_welcome_message(websocket, session_id: str, tts_provider, text: str):
|
18 |
"""Send welcome message TTS audio"""
|
19 |
try:
|
|
|
|
|
20 |
# Generate TTS
|
21 |
audio_data = await tts_provider.synthesize(text)
|
|
|
22 |
|
23 |
# Convert to base64
|
|
|
24 |
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Send in chunks
|
27 |
chunk_size = 16384
|
28 |
total_length = len(audio_base64)
|
29 |
total_chunks = (total_length + chunk_size - 1) // chunk_size
|
30 |
|
|
|
|
|
31 |
for i in range(0, total_length, chunk_size):
|
32 |
chunk = audio_base64[i:i + chunk_size]
|
33 |
chunk_index = i // chunk_size
|
|
|
|
|
|
|
34 |
|
35 |
await websocket.send_json({
|
36 |
"type": "tts_audio",
|
37 |
"data": chunk,
|
38 |
"chunk_index": chunk_index,
|
39 |
"total_chunks": total_chunks,
|
40 |
-
"is_last":
|
41 |
"mime_type": "audio/mpeg"
|
42 |
})
|
43 |
|
44 |
-
log_info(f"β
Welcome message TTS sent", session_id=session_id)
|
45 |
|
46 |
except Exception as e:
|
47 |
-
log_error(
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from typing import Optional
|
6 |
from datetime import datetime
|
7 |
import sys
|
8 |
+
import traceback
|
9 |
|
10 |
+
from logger import log_info, log_error, log_debug, log_warning
|
11 |
|
12 |
def log(message: str):
|
13 |
"""Log helper with timestamp"""
|
|
|
18 |
async def send_tts_welcome_message(websocket, session_id: str, tts_provider, text: str):
|
19 |
"""Send welcome message TTS audio"""
|
20 |
try:
|
21 |
+
log_info(f"ποΈ Generating welcome TTS for: '{text[:50]}...'", session_id=session_id)
|
22 |
+
|
23 |
# Generate TTS
|
24 |
audio_data = await tts_provider.synthesize(text)
|
25 |
+
log_info(f"β
Welcome TTS generated: {len(audio_data)} bytes", session_id=session_id)
|
26 |
|
27 |
# Convert to base64
|
28 |
+
log_debug(f"π¦ Converting welcome audio to base64...")
|
29 |
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
30 |
+
log_info(f"π Welcome base64 ready: {len(audio_base64)} chars", session_id=session_id)
|
31 |
+
|
32 |
+
# Log preview
|
33 |
+
log_debug(f"π Welcome base64 preview: {audio_base64[:100]}...")
|
34 |
|
35 |
# Send in chunks
|
36 |
chunk_size = 16384
|
37 |
total_length = len(audio_base64)
|
38 |
total_chunks = (total_length + chunk_size - 1) // chunk_size
|
39 |
|
40 |
+
log_info(f"π€ Sending welcome TTS in {total_chunks} chunks", session_id=session_id)
|
41 |
+
|
42 |
for i in range(0, total_length, chunk_size):
|
43 |
chunk = audio_base64[i:i + chunk_size]
|
44 |
chunk_index = i // chunk_size
|
45 |
+
is_last = chunk_index == total_chunks - 1
|
46 |
+
|
47 |
+
log_debug(f"π¨ Welcome chunk {chunk_index}/{total_chunks}, size: {len(chunk)}")
|
48 |
|
49 |
await websocket.send_json({
|
50 |
"type": "tts_audio",
|
51 |
"data": chunk,
|
52 |
"chunk_index": chunk_index,
|
53 |
"total_chunks": total_chunks,
|
54 |
+
"is_last": is_last,
|
55 |
"mime_type": "audio/mpeg"
|
56 |
})
|
57 |
|
58 |
+
log_info(f"β
Welcome message TTS sent successfully", session_id=session_id, chunks=total_chunks)
|
59 |
|
60 |
except Exception as e:
|
61 |
+
log_error(
|
62 |
+
f"β Failed to send welcome TTS",
|
63 |
+
error=str(e),
|
64 |
+
traceback=traceback.format_exc(),
|
65 |
+
session_id=session_id
|
66 |
+
)
|