Update ui/tabs.py
Browse files- ui/tabs.py +184 -48
ui/tabs.py
CHANGED
|
@@ -53,36 +53,74 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 53 |
with gr.Tab("Stream Object Detection"):
|
| 54 |
create_streaming_object_detection()
|
| 55 |
def create_sambanova_voice_tab(sambanova_service):
|
| 56 |
-
"""Tạo tab Sambanova AI
|
| 57 |
|
| 58 |
with gr.Blocks() as sambanova_tab:
|
| 59 |
-
gr.Markdown("## 🤖 Sambanova AI -
|
| 60 |
-
gr.Markdown("Trò chuyện với
|
| 61 |
|
| 62 |
# State variables
|
| 63 |
chatbot = gr.Chatbot(
|
| 64 |
type="messages",
|
| 65 |
value=[],
|
| 66 |
label="💬 Hội thoại",
|
| 67 |
-
height=500
|
|
|
|
| 68 |
)
|
| 69 |
conversation_state = gr.State(value=[])
|
|
|
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column(scale=1):
|
| 73 |
# Header
|
| 74 |
-
gr.Markdown("### 💬 Chat với
|
| 75 |
|
| 76 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
text_input = gr.Textbox(
|
| 78 |
label="Tin nhắn của bạn",
|
| 79 |
placeholder="Xin chào! Hãy hỏi tôi bất cứ điều gì...",
|
| 80 |
lines=3
|
| 81 |
)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# Buttons
|
| 84 |
with gr.Row():
|
| 85 |
-
|
|
|
|
| 86 |
clear_btn = gr.Button("🗑️ Xóa", variant="secondary")
|
| 87 |
|
| 88 |
# Status
|
|
@@ -91,32 +129,44 @@ def create_sambanova_voice_tab(sambanova_service):
|
|
| 91 |
value="✅ Sẵn sàng",
|
| 92 |
interactive=False
|
| 93 |
)
|
| 94 |
-
|
| 95 |
-
# Voice notice
|
| 96 |
-
gr.Markdown("---")
|
| 97 |
-
gr.Markdown("**🎤 Voice features:** Đang được phát triển")
|
| 98 |
|
| 99 |
with gr.Column(scale=2):
|
| 100 |
-
#
|
| 101 |
-
gr.Markdown("###
|
| 102 |
-
gr.
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
|
|
|
|
|
|
|
|
|
| 109 |
**Tính năng:**
|
| 110 |
-
✅
|
| 111 |
-
✅
|
| 112 |
-
✅
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
""")
|
| 114 |
|
| 115 |
-
# Event handlers
|
| 116 |
-
def
|
| 117 |
-
"""Gửi tin nhắn
|
| 118 |
if not text or not text.strip():
|
| 119 |
-
return history, state, "❌ Vui lòng nhập tin nhắn"
|
| 120 |
|
| 121 |
try:
|
| 122 |
# Thêm user message vào history
|
|
@@ -124,42 +174,128 @@ def create_sambanova_voice_tab(sambanova_service):
|
|
| 124 |
new_history = history + [user_message]
|
| 125 |
new_state = state + [user_message]
|
| 126 |
|
| 127 |
-
|
| 128 |
-
yield new_history, new_state, "⏳ Đang xử lý..."
|
| 129 |
-
|
| 130 |
-
# Gọi Sambanova API
|
| 131 |
-
print("🤖 Calling Sambanova API...")
|
| 132 |
-
ai_response_text = sambanova_service.generate_response(new_state)
|
| 133 |
-
|
| 134 |
-
# Thêm AI response
|
| 135 |
-
ai_message = {"role": "assistant", "content": ai_response_text}
|
| 136 |
-
new_history = new_history + [ai_message]
|
| 137 |
-
new_state = new_state + [ai_message]
|
| 138 |
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
print(f"❌ Error: {e}")
|
| 143 |
error_msg = f"❌ Lỗi: {str(e)}"
|
| 144 |
-
yield history, state, error_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
def clear_chat():
|
| 147 |
"""Xóa toàn bộ hội thoại"""
|
| 148 |
-
return [], [], "🔄 Đã xóa hội thoại"
|
| 149 |
|
| 150 |
-
# Kết nối events
|
| 151 |
-
|
| 152 |
-
fn=
|
| 153 |
-
inputs=[
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
| 155 |
).then(
|
| 156 |
-
fn=lambda: "",
|
| 157 |
-
outputs=[text_input]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
)
|
| 159 |
|
| 160 |
clear_btn.click(
|
| 161 |
fn=clear_chat,
|
| 162 |
-
outputs=[chatbot, conversation_state, status]
|
| 163 |
)
|
| 164 |
|
| 165 |
return sambanova_tab
|
|
|
|
| 53 |
with gr.Tab("Stream Object Detection"):
|
| 54 |
create_streaming_object_detection()
|
| 55 |
def create_sambanova_voice_tab(sambanova_service):
|
| 56 |
+
"""Tạo tab Sambanova AI với Voice Input/Output hoàn chỉnh"""
|
| 57 |
|
| 58 |
with gr.Blocks() as sambanova_tab:
|
| 59 |
+
gr.Markdown("## 🤖 Sambanova AI - Voice & Text Complete")
|
| 60 |
+
gr.Markdown("Trò chuyện với AI - Hỗ trợ voice input/output hoàn chỉnh")
|
| 61 |
|
| 62 |
# State variables
|
| 63 |
chatbot = gr.Chatbot(
|
| 64 |
type="messages",
|
| 65 |
value=[],
|
| 66 |
label="💬 Hội thoại",
|
| 67 |
+
height=500,
|
| 68 |
+
render=False
|
| 69 |
)
|
| 70 |
conversation_state = gr.State(value=[])
|
| 71 |
+
audio_output_state = gr.State(value=None)
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
with gr.Column(scale=1):
|
| 75 |
# Header
|
| 76 |
+
gr.Markdown("### 💬 Chat với AI")
|
| 77 |
|
| 78 |
+
# Model selection
|
| 79 |
+
model_dropdown = gr.Dropdown(
|
| 80 |
+
choices=sambanova_service.get_available_models(),
|
| 81 |
+
value="Meta-Llama-3.1-8B-Instruct",
|
| 82 |
+
label="Chọn Model"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Language selection for TTS
|
| 86 |
+
language_dropdown = gr.Dropdown(
|
| 87 |
+
choices=['vi', 'en', 'ja', 'ko', 'zh', 'fr', 'es', 'de'],
|
| 88 |
+
value='vi',
|
| 89 |
+
label="Ngôn ngữ TTS"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# Text input
|
| 93 |
text_input = gr.Textbox(
|
| 94 |
label="Tin nhắn của bạn",
|
| 95 |
placeholder="Xin chào! Hãy hỏi tôi bất cứ điều gì...",
|
| 96 |
lines=3
|
| 97 |
)
|
| 98 |
|
| 99 |
+
# Voice input
|
| 100 |
+
with gr.Group():
|
| 101 |
+
gr.Markdown("### 🎤 Voice Input")
|
| 102 |
+
audio_input = gr.Audio(
|
| 103 |
+
sources=["microphone"],
|
| 104 |
+
type="numpy",
|
| 105 |
+
label="Nói tin nhắn của bạn",
|
| 106 |
+
show_download_button=False
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Parameters
|
| 110 |
+
with gr.Row():
|
| 111 |
+
temperature = gr.Slider(0, 1, value=0.1, label="Temperature")
|
| 112 |
+
top_p = gr.Slider(0, 1, value=0.1, label="Top-P")
|
| 113 |
+
|
| 114 |
+
# Voice output toggle
|
| 115 |
+
voice_output_toggle = gr.Checkbox(
|
| 116 |
+
label="🔊 Bật Voice Output (TTS)",
|
| 117 |
+
value=True
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
# Buttons
|
| 121 |
with gr.Row():
|
| 122 |
+
send_text_btn = gr.Button("🚀 Gửi Text", variant="primary")
|
| 123 |
+
send_voice_btn = gr.Button("🎤 Gửi Voice", variant="primary")
|
| 124 |
clear_btn = gr.Button("🗑️ Xóa", variant="secondary")
|
| 125 |
|
| 126 |
# Status
|
|
|
|
| 129 |
value="✅ Sẵn sàng",
|
| 130 |
interactive=False
|
| 131 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
with gr.Column(scale=2):
|
| 134 |
+
# Audio output
|
| 135 |
+
gr.Markdown("### 🔊 Voice Output")
|
| 136 |
+
audio_output = gr.Audio(
|
| 137 |
+
label="Giọng nói AI",
|
| 138 |
+
autoplay=True,
|
| 139 |
+
visible=False
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Streaming output
|
| 143 |
+
streaming_output = gr.Textbox(
|
| 144 |
+
label="Streaming Response",
|
| 145 |
+
lines=5,
|
| 146 |
+
max_lines=10
|
| 147 |
+
)
|
| 148 |
|
| 149 |
+
# Model information
|
| 150 |
+
gr.Markdown("### 📊 Thông tin")
|
| 151 |
+
gr.Markdown("""
|
| 152 |
**Tính năng:**
|
| 153 |
+
✅ Text chat
|
| 154 |
+
✅ Voice input (STT)
|
| 155 |
+
✅ Voice output (TTS)
|
| 156 |
+
✅ Streaming response
|
| 157 |
+
✅ Đa ngôn ngữ
|
| 158 |
+
|
| 159 |
+
**Công nghệ:**
|
| 160 |
+
- Sambanova API
|
| 161 |
+
- FastRTC STT
|
| 162 |
+
- Google/Microsoft TTS
|
| 163 |
""")
|
| 164 |
|
| 165 |
+
# Event handlers cho text với voice output
|
| 166 |
+
def send_text_message(text, history, state, audio_state, model, language, temp, top_p_val, voice_enabled):
|
| 167 |
+
"""Gửi tin nhắn text và nhận voice response"""
|
| 168 |
if not text or not text.strip():
|
| 169 |
+
return history, state, audio_state, "❌ Vui lòng nhập tin nhắn", gr.update(visible=False), ""
|
| 170 |
|
| 171 |
try:
|
| 172 |
# Thêm user message vào history
|
|
|
|
| 174 |
new_history = history + [user_message]
|
| 175 |
new_state = state + [user_message]
|
| 176 |
|
| 177 |
+
yield new_history, new_state, audio_state, "⏳ Đang xử lý...", gr.update(visible=False), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
+
# Lấy response từ AI
|
| 180 |
+
if voice_enabled:
|
| 181 |
+
# Sử dụng voice response
|
| 182 |
+
response_data = sambanova_service.generate_response_with_voice(
|
| 183 |
+
new_state, model, language
|
| 184 |
+
)
|
| 185 |
+
ai_text = response_data["text"]
|
| 186 |
+
audio_filepath = response_data["audio"]
|
| 187 |
+
|
| 188 |
+
# Thêm AI response
|
| 189 |
+
ai_message = {"role": "assistant", "content": ai_text}
|
| 190 |
+
final_history = new_history + [ai_message]
|
| 191 |
+
final_state = new_state + [ai_message]
|
| 192 |
+
|
| 193 |
+
# Cập nhật audio output
|
| 194 |
+
audio_update = gr.update(value=audio_filepath, visible=True) if audio_filepath else gr.update(visible=False)
|
| 195 |
+
new_audio_state = audio_filepath
|
| 196 |
+
|
| 197 |
+
yield final_history, final_state, new_audio_state, "✅ Hoàn thành với voice", audio_update, ai_text
|
| 198 |
+
else:
|
| 199 |
+
# Chỉ text response
|
| 200 |
+
ai_text = sambanova_service.generate_response(new_state, model, temp, top_p_val)
|
| 201 |
+
ai_message = {"role": "assistant", "content": ai_text}
|
| 202 |
+
final_history = new_history + [ai_message]
|
| 203 |
+
final_state = new_state + [ai_message]
|
| 204 |
+
|
| 205 |
+
yield final_history, final_state, audio_state, "✅ Hoàn thành", gr.update(visible=False), ai_text
|
| 206 |
|
| 207 |
except Exception as e:
|
| 208 |
print(f"❌ Error: {e}")
|
| 209 |
error_msg = f"❌ Lỗi: {str(e)}"
|
| 210 |
+
yield history, state, audio_state, error_msg, gr.update(visible=False), ""
|
| 211 |
+
|
| 212 |
+
# Event handlers cho voice với voice output
|
| 213 |
+
def send_voice_message(audio, history, state, audio_state, model, language, temp, top_p_val, voice_enabled):
|
| 214 |
+
"""Gửi tin nhắn voice và nhận voice response"""
|
| 215 |
+
if audio is None:
|
| 216 |
+
return history, state, audio_state, "❌ Vui lòng ghi âm tin nhắn", gr.update(visible=False), ""
|
| 217 |
+
|
| 218 |
+
try:
|
| 219 |
+
# Chuyển speech thành text
|
| 220 |
+
yield history, state, audio_state, "🎤 Đang chuyển speech thành text...", gr.update(visible=False), ""
|
| 221 |
+
|
| 222 |
+
text = sambanova_service.speech_to_text(audio)
|
| 223 |
+
if not text:
|
| 224 |
+
return history, state, audio_state, "❌ Không thể nhận dạng giọng nói", gr.update(visible=False), ""
|
| 225 |
+
|
| 226 |
+
# Thêm user message vào history (cả audio và text)
|
| 227 |
+
user_audio_message = {"role": "user", "content": gr.Audio(audio)}
|
| 228 |
+
user_text_message = {"role": "user", "content": text}
|
| 229 |
+
|
| 230 |
+
new_history = history + [user_audio_message]
|
| 231 |
+
new_state = state + [user_text_message]
|
| 232 |
+
|
| 233 |
+
yield new_history, new_state, audio_state, "⏳ Đang xử lý voice message...", gr.update(visible=False), ""
|
| 234 |
+
|
| 235 |
+
# Lấy response từ AI
|
| 236 |
+
if voice_enabled:
|
| 237 |
+
response_data = sambanova_service.generate_response_with_voice(
|
| 238 |
+
new_state, model, language
|
| 239 |
+
)
|
| 240 |
+
ai_text = response_data["text"]
|
| 241 |
+
audio_filepath = response_data["audio"]
|
| 242 |
+
|
| 243 |
+
# Thêm AI response
|
| 244 |
+
ai_message = {"role": "assistant", "content": ai_text}
|
| 245 |
+
final_history = new_history + [ai_message]
|
| 246 |
+
final_state = new_state + [ai_message]
|
| 247 |
+
|
| 248 |
+
# Cập nhật audio output
|
| 249 |
+
audio_update = gr.update(value=audio_filepath, visible=True) if audio_filepath else gr.update(visible=False)
|
| 250 |
+
new_audio_state = audio_filepath
|
| 251 |
+
|
| 252 |
+
yield final_history, final_state, new_audio_state, "✅ Voice message hoàn thành", audio_update, ai_text
|
| 253 |
+
else:
|
| 254 |
+
ai_text = sambanova_service.generate_response(new_state, model, temp, top_p_val)
|
| 255 |
+
ai_message = {"role": "assistant", "content": ai_text}
|
| 256 |
+
final_history = new_history + [ai_message]
|
| 257 |
+
final_state = new_state + [ai_message]
|
| 258 |
+
|
| 259 |
+
yield final_history, final_state, audio_state, "✅ Hoàn thành", gr.update(visible=False), ai_text
|
| 260 |
+
|
| 261 |
+
except Exception as e:
|
| 262 |
+
print(f"❌ Voice Error: {e}")
|
| 263 |
+
error_msg = f"❌ Lỗi voice: {str(e)}"
|
| 264 |
+
yield history, state, audio_state, error_msg, gr.update(visible=False), ""
|
| 265 |
|
| 266 |
def clear_chat():
|
| 267 |
"""Xóa toàn bộ hội thoại"""
|
| 268 |
+
return [], [], None, "🔄 Đã xóa hội thoại", gr.update(visible=False), ""
|
| 269 |
|
| 270 |
+
# Kết nối events cho text
|
| 271 |
+
send_text_btn.click(
|
| 272 |
+
fn=send_text_message,
|
| 273 |
+
inputs=[
|
| 274 |
+
text_input, chatbot, conversation_state, audio_output_state,
|
| 275 |
+
model_dropdown, language_dropdown, temperature, top_p, voice_output_toggle
|
| 276 |
+
],
|
| 277 |
+
outputs=[chatbot, conversation_state, audio_output_state, status, audio_output, streaming_output]
|
| 278 |
).then(
|
| 279 |
+
fn=lambda: ("", None),
|
| 280 |
+
outputs=[text_input, audio_input]
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
# Kết nối events cho voice
|
| 284 |
+
send_voice_btn.click(
|
| 285 |
+
fn=send_voice_message,
|
| 286 |
+
inputs=[
|
| 287 |
+
audio_input, chatbot, conversation_state, audio_output_state,
|
| 288 |
+
model_dropdown, language_dropdown, temperature, top_p, voice_output_toggle
|
| 289 |
+
],
|
| 290 |
+
outputs=[chatbot, conversation_state, audio_output_state, status, audio_output, streaming_output]
|
| 291 |
+
).then(
|
| 292 |
+
fn=lambda: ("", None),
|
| 293 |
+
outputs=[text_input, audio_input]
|
| 294 |
)
|
| 295 |
|
| 296 |
clear_btn.click(
|
| 297 |
fn=clear_chat,
|
| 298 |
+
outputs=[chatbot, conversation_state, audio_output_state, status, audio_output, streaming_output]
|
| 299 |
)
|
| 300 |
|
| 301 |
return sambanova_tab
|