Update ui/tabs.py
Browse files- ui/tabs.py +21 -14
ui/tabs.py
CHANGED
|
@@ -202,53 +202,60 @@ def create_audio_tab(audio_service: AudioService):
|
|
| 202 |
def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
|
| 203 |
"""Tạo tab streaming voice sử dụng Gradio microphone"""
|
| 204 |
|
| 205 |
-
# Create components
|
| 206 |
-
(microphone, clear_btn, state_display,
|
| 207 |
transcription, ai_response, tts_output, conversation_state) = create_streaming_voice_components()
|
| 208 |
|
| 209 |
def process_audio_stream(audio_data):
|
| 210 |
"""Xử lý audio stream từ microphone"""
|
| 211 |
if audio_data is None:
|
| 212 |
-
return {
|
| 213 |
-
'transcription': "❌ Không có âm thanh",
|
| 214 |
-
'response': "Vui lòng nói lại",
|
| 215 |
-
'tts_audio': None
|
| 216 |
-
}, {}, {}
|
| 217 |
|
| 218 |
try:
|
|
|
|
|
|
|
| 219 |
# Xử lý audio
|
| 220 |
result = streaming_service.process_streaming_audio(audio_data)
|
| 221 |
|
| 222 |
# Cập nhật state
|
| 223 |
state = streaming_service.get_conversation_state()
|
|
|
|
| 224 |
|
| 225 |
-
return result['transcription'], result['response'], result['tts_audio'], state
|
| 226 |
|
| 227 |
except Exception as e:
|
| 228 |
error_msg = f"❌ Lỗi xử lý: {str(e)}"
|
| 229 |
-
|
|
|
|
| 230 |
|
| 231 |
def clear_conversation():
|
| 232 |
"""Xóa hội thoại"""
|
| 233 |
streaming_service.clear_conversation()
|
| 234 |
state = streaming_service.get_conversation_state()
|
| 235 |
-
return "", "", None, state
|
| 236 |
|
| 237 |
-
# Event handlers
|
| 238 |
microphone.stream(
|
| 239 |
process_audio_stream,
|
| 240 |
inputs=[microphone],
|
| 241 |
-
outputs=[transcription, ai_response, tts_output, state_display]
|
| 242 |
)
|
| 243 |
|
| 244 |
clear_btn.click(
|
| 245 |
clear_conversation,
|
| 246 |
-
outputs=[transcription, ai_response, tts_output, state_display]
|
| 247 |
)
|
| 248 |
|
| 249 |
# Initial state
|
| 250 |
def get_initial_state():
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
state_display.change(
|
| 254 |
get_initial_state,
|
|
|
|
| 202 |
def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
|
| 203 |
"""Tạo tab streaming voice sử dụng Gradio microphone"""
|
| 204 |
|
| 205 |
+
# Create components - SỬA: sử dụng components mới
|
| 206 |
+
(microphone, clear_btn, status_display, state_display,
|
| 207 |
transcription, ai_response, tts_output, conversation_state) = create_streaming_voice_components()
|
| 208 |
|
| 209 |
def process_audio_stream(audio_data):
|
| 210 |
"""Xử lý audio stream từ microphone"""
|
| 211 |
if audio_data is None:
|
| 212 |
+
return "❌ Không có âm thanh", "Vui lòng nói lại", None, "Đang chờ...", {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
try:
|
| 215 |
+
print("🎯 Đang xử lý audio stream...")
|
| 216 |
+
|
| 217 |
# Xử lý audio
|
| 218 |
result = streaming_service.process_streaming_audio(audio_data)
|
| 219 |
|
| 220 |
# Cập nhật state
|
| 221 |
state = streaming_service.get_conversation_state()
|
| 222 |
+
status = f"✅ Đã xử lý - {len(result['transcription'])} ký tự"
|
| 223 |
|
| 224 |
+
return result['transcription'], result['response'], result['tts_audio'], status, state
|
| 225 |
|
| 226 |
except Exception as e:
|
| 227 |
error_msg = f"❌ Lỗi xử lý: {str(e)}"
|
| 228 |
+
print(f"Lỗi: {traceback.format_exc()}")
|
| 229 |
+
return error_msg, "Xin lỗi, có lỗi xảy ra", None, "❌ Lỗi", {}
|
| 230 |
|
| 231 |
def clear_conversation():
|
| 232 |
"""Xóa hội thoại"""
|
| 233 |
streaming_service.clear_conversation()
|
| 234 |
state = streaming_service.get_conversation_state()
|
| 235 |
+
return "", "", None, "🗑️ Đã xóa hội thoại", state
|
| 236 |
|
| 237 |
+
# Event handlers - SỬA: số lượng outputs phải khớp
|
| 238 |
microphone.stream(
|
| 239 |
process_audio_stream,
|
| 240 |
inputs=[microphone],
|
| 241 |
+
outputs=[transcription, ai_response, tts_output, status_display, state_display]
|
| 242 |
)
|
| 243 |
|
| 244 |
clear_btn.click(
|
| 245 |
clear_conversation,
|
| 246 |
+
outputs=[transcription, ai_response, tts_output, status_display, state_display]
|
| 247 |
)
|
| 248 |
|
| 249 |
# Initial state
|
| 250 |
def get_initial_state():
|
| 251 |
+
state = streaming_service.get_conversation_state()
|
| 252 |
+
return "Sẵn sàng - nhấn nút microphone để nói", state
|
| 253 |
+
|
| 254 |
+
# Load initial state khi tab được mở
|
| 255 |
+
status_display.change(
|
| 256 |
+
lambda: "Sẵn sàng - nhấn nút microphone để nói",
|
| 257 |
+
outputs=[status_display]
|
| 258 |
+
)
|
| 259 |
|
| 260 |
state_display.change(
|
| 261 |
get_initial_state,
|