Spaces:
Running
on
Zero
Running
on
Zero
Y Phung Nguyen
commited on
Commit
Β·
ef40fb8
1
Parent(s):
d99bfd8
Fix TTS player
Browse files
ui.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
"""Gradio UI setup"""
|
|
|
|
| 2 |
import time
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
|
@@ -112,24 +113,39 @@ def create_demo():
|
|
| 112 |
outputs=[recording_timer, message_input]
|
| 113 |
)
|
| 114 |
|
| 115 |
-
with gr.Row(
|
| 116 |
-
|
| 117 |
-
tts_audio = gr.Audio(label="
|
| 118 |
|
| 119 |
def generate_speech_from_chat(history):
|
| 120 |
"""Extract last assistant message and generate speech"""
|
| 121 |
if not history or len(history) == 0:
|
|
|
|
| 122 |
return None
|
| 123 |
last_msg = history[-1]
|
| 124 |
if last_msg.get("role") == "assistant":
|
| 125 |
text = last_msg.get("content", "").replace(" π", "").strip()
|
| 126 |
if text:
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
return None
|
| 130 |
|
| 131 |
-
tts_button = gr.Button("π Play Response", visible=False, size="sm")
|
| 132 |
-
|
| 133 |
def update_tts_button(history):
|
| 134 |
if history and len(history) > 0 and history[-1].get("role") == "assistant":
|
| 135 |
return gr.update(visible=True)
|
|
|
|
| 1 |
"""Gradio UI setup"""
|
| 2 |
+
import os
|
| 3 |
import time
|
| 4 |
import gradio as gr
|
| 5 |
import spaces
|
|
|
|
| 113 |
outputs=[recording_timer, message_input]
|
| 114 |
)
|
| 115 |
|
| 116 |
+
with gr.Row():
|
| 117 |
+
tts_button = gr.Button("π Play Response", visible=False, size="sm")
|
| 118 |
+
tts_audio = gr.Audio(label="", visible=True, autoplay=True, show_label=False, container=False)
|
| 119 |
|
| 120 |
def generate_speech_from_chat(history):
|
| 121 |
"""Extract last assistant message and generate speech"""
|
| 122 |
if not history or len(history) == 0:
|
| 123 |
+
logger.warning("[TTS] No history available")
|
| 124 |
return None
|
| 125 |
last_msg = history[-1]
|
| 126 |
if last_msg.get("role") == "assistant":
|
| 127 |
text = last_msg.get("content", "").replace(" π", "").strip()
|
| 128 |
if text:
|
| 129 |
+
logger.info(f"[TTS] Generating speech for text: {text[:100]}...")
|
| 130 |
+
try:
|
| 131 |
+
audio_path = generate_speech(text)
|
| 132 |
+
if audio_path and os.path.exists(audio_path):
|
| 133 |
+
logger.info(f"[TTS] β
Generated audio successfully: {audio_path}")
|
| 134 |
+
return audio_path
|
| 135 |
+
else:
|
| 136 |
+
logger.warning(f"[TTS] β Failed to generate audio or file doesn't exist: {audio_path}")
|
| 137 |
+
return None
|
| 138 |
+
except Exception as e:
|
| 139 |
+
logger.error(f"[TTS] Error generating speech: {e}")
|
| 140 |
+
import traceback
|
| 141 |
+
logger.debug(f"[TTS] Traceback: {traceback.format_exc()}")
|
| 142 |
+
return None
|
| 143 |
+
else:
|
| 144 |
+
logger.warning("[TTS] Empty text extracted from assistant message")
|
| 145 |
+
else:
|
| 146 |
+
logger.warning(f"[TTS] Last message is not from assistant: {last_msg.get('role')}")
|
| 147 |
return None
|
| 148 |
|
|
|
|
|
|
|
| 149 |
def update_tts_button(history):
|
| 150 |
if history and len(history) > 0 and history[-1].get("role") == "assistant":
|
| 151 |
return gr.update(visible=True)
|