Update ui/components.py
Browse files- ui/components.py +45 -14
ui/components.py
CHANGED
|
@@ -105,9 +105,10 @@ def create_chat_components() -> tuple:
|
|
| 105 |
)
|
| 106 |
|
| 107 |
return chatbot, state, user_input, send_button, clear_button, chat_tts_output
|
| 108 |
-
def
|
| 109 |
-
"""Tạo
|
| 110 |
-
|
|
|
|
| 111 |
gr.Markdown("## 🎤 Trò chuyện giọng nói thời gian thực")
|
| 112 |
gr.Markdown("Sử dụng microphone của thiết bị để trò chuyện với AI - hoạt động trên cả điện thoại và máy tính")
|
| 113 |
|
|
@@ -160,16 +161,46 @@ def create_streaming_voice_components() -> tuple:
|
|
| 160 |
interactive=False,
|
| 161 |
autoplay=True
|
| 162 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
return (
|
| 166 |
-
microphone,
|
| 167 |
-
clear_btn,
|
| 168 |
-
status_display,
|
| 169 |
-
state_display,
|
| 170 |
-
realtime_transcription,
|
| 171 |
-
ai_response,
|
| 172 |
-
tts_output
|
| 173 |
-
# Đã bỏ 2 giá trị cuối: streaming_state, conversation_history
|
| 174 |
-
)
|
| 175 |
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
return chatbot, state, user_input, send_button, clear_button, chat_tts_output
|
| 108 |
+
def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
|
| 109 |
+
"""Tạo tab streaming voice sử dụng Gradio microphone"""
|
| 110 |
+
|
| 111 |
+
with gr.Blocks() as streaming_tab:
|
| 112 |
gr.Markdown("## 🎤 Trò chuyện giọng nói thời gian thực")
|
| 113 |
gr.Markdown("Sử dụng microphone của thiết bị để trò chuyện với AI - hoạt động trên cả điện thoại và máy tính")
|
| 114 |
|
|
|
|
| 161 |
interactive=False,
|
| 162 |
autoplay=True
|
| 163 |
)
|
| 164 |
+
|
| 165 |
+
def process_audio_stream(audio_data):
|
| 166 |
+
"""Xử lý audio stream từ microphone"""
|
| 167 |
+
if audio_data is None:
|
| 168 |
+
return "❌ Không có âm thanh", "Vui lòng nói lại", None, "Đang chờ...", {}
|
| 169 |
+
|
| 170 |
+
try:
|
| 171 |
+
print("🎯 Đang xử lý audio stream...")
|
| 172 |
+
|
| 173 |
+
# Xử lý audio
|
| 174 |
+
result = streaming_service.process_streaming_audio(audio_data)
|
| 175 |
+
|
| 176 |
+
# Cập nhật state
|
| 177 |
+
state = streaming_service.get_conversation_state()
|
| 178 |
+
status = f"✅ Đã xử lý - {len(result['transcription'])} ký tự"
|
| 179 |
+
|
| 180 |
+
return result['transcription'], result['response'], result['tts_audio'], status, state
|
| 181 |
+
|
| 182 |
+
except Exception as e:
|
| 183 |
+
error_msg = f"❌ Lỗi xử lý: {str(e)}"
|
| 184 |
+
print(f"Lỗi: {traceback.format_exc()}")
|
| 185 |
+
return error_msg, "Xin lỗi, có lỗi xảy ra", None, "❌ Lỗi", {}
|
| 186 |
+
|
| 187 |
+
def clear_conversation():
|
| 188 |
+
"""Xóa hội thoại"""
|
| 189 |
+
streaming_service.clear_conversation()
|
| 190 |
+
state = streaming_service.get_conversation_state()
|
| 191 |
+
return "", "", None, "🗑️ Đã xóa hội thoại", state
|
| 192 |
+
|
| 193 |
+
# Event handlers
|
| 194 |
+
microphone.stream(
|
| 195 |
+
process_audio_stream,
|
| 196 |
+
inputs=[microphone],
|
| 197 |
+
outputs=[realtime_transcription, ai_response, tts_output, status_display, state_display]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
clear_btn.click(
|
| 201 |
+
clear_conversation,
|
| 202 |
+
outputs=[realtime_transcription, ai_response, tts_output, status_display, state_display]
|
| 203 |
+
)
|
| 204 |
|
| 205 |
+
return streaming_tab
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|