Update ui/tabs.py
Browse files- ui/tabs.py +45 -52
ui/tabs.py
CHANGED
|
@@ -416,67 +416,60 @@ def create_tts_tab(tts_service: EnhancedTTSService):
|
|
| 416 |
outputs=[tts_output_audio]
|
| 417 |
)
|
| 418 |
def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
|
| 419 |
-
"""Tạo tab streaming voice
|
| 420 |
-
|
| 421 |
-
# Create components
|
| 422 |
-
|
| 423 |
-
transcription, ai_response, tts_output, streaming_state,
|
| 424 |
-
conversation_history, vad_visualizer) = create_streaming_voice_components()
|
| 425 |
-
|
| 426 |
-
def start_streaming():
|
| 427 |
-
"""Bắt đầu streaming với VAD"""
|
| 428 |
-
def callback_handler(result):
|
| 429 |
-
"""Xử lý kết quả real-time"""
|
| 430 |
-
# Cập nhật UI với kết quả mới
|
| 431 |
-
# Note: Trong thực tế, cần sử dụng gr.update() và queue
|
| 432 |
-
print(f"🎯 Kết quả: {result['transcription']}")
|
| 433 |
-
print(f"🤖 Phản hồi: {result['response']}")
|
| 434 |
-
|
| 435 |
-
success = streaming_service.start_listening(callback_handler)
|
| 436 |
-
status = "✅ Đang lắng nghe..." if success else "❌ Lỗi khởi động"
|
| 437 |
-
|
| 438 |
-
# Start background thread for UI updates
|
| 439 |
-
if success:
|
| 440 |
-
threading.Thread(target=update_ui_loop, daemon=True).start()
|
| 441 |
-
|
| 442 |
-
return status, streaming_service.get_conversation_state()
|
| 443 |
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
return "🛑 Đã dừng lắng nghe", streaming_service.get_conversation_state()
|
| 448 |
|
| 449 |
-
def
|
| 450 |
-
"""
|
| 451 |
-
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
state = streaming_service.get_conversation_state()
|
|
|
|
| 454 |
|
| 455 |
-
|
| 456 |
-
# Đây là phiên bản đơn giản, trong thực tế cần tích hợp với Gradio Queue
|
| 457 |
|
| 458 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
|
| 460 |
# Event handlers
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
|
|
|
| 464 |
)
|
| 465 |
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
outputs=[status_display, state_display]
|
| 469 |
)
|
| 470 |
|
| 471 |
-
#
|
| 472 |
-
def
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
"Phản hồi sẽ xuất hiện ở đây...",
|
| 479 |
-
state
|
| 480 |
-
)
|
| 481 |
-
return "Chưa lắng nghe", "Chưa có phản hồi", {}
|
| 482 |
|
|
|
|
| 416 |
outputs=[tts_output_audio]
|
| 417 |
)
|
| 418 |
def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
|
| 419 |
+
"""Tạo tab streaming voice sử dụng Gradio microphone"""
|
| 420 |
+
|
| 421 |
+
# Create components - SỬA TÊN BIẾN CHO ĐÚNG
|
| 422 |
+
components = create_streaming_voice_components()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
|
| 424 |
+
# Unpack components - CHỈ CÓ 7 GIÁ TRỊ
|
| 425 |
+
(microphone, clear_btn, status_display, state_display,
|
| 426 |
+
transcription, ai_response, tts_output) = components
|
|
|
|
| 427 |
|
| 428 |
+
def process_audio_stream(audio_data):
|
| 429 |
+
"""Xử lý audio stream từ microphone"""
|
| 430 |
+
if audio_data is None:
|
| 431 |
+
return "❌ Không có âm thanh", "Vui lòng nói lại", None, "Đang chờ...", {}
|
| 432 |
+
|
| 433 |
+
try:
|
| 434 |
+
print("🎯 Đang xử lý audio stream...")
|
| 435 |
+
|
| 436 |
+
# Xử lý audio
|
| 437 |
+
result = streaming_service.process_streaming_audio(audio_data)
|
| 438 |
+
|
| 439 |
+
# Cập nhật state
|
| 440 |
state = streaming_service.get_conversation_state()
|
| 441 |
+
status = f"✅ Đã xử lý - {len(result['transcription'])} ký tự"
|
| 442 |
|
| 443 |
+
return result['transcription'], result['response'], result['tts_audio'], status, state
|
|
|
|
| 444 |
|
| 445 |
+
except Exception as e:
|
| 446 |
+
error_msg = f"❌ Lỗi xử lý: {str(e)}"
|
| 447 |
+
print(f"Lỗi: {traceback.format_exc()}")
|
| 448 |
+
return error_msg, "Xin lỗi, có lỗi xảy ra", None, "❌ Lỗi", {}
|
| 449 |
+
|
| 450 |
+
def clear_conversation():
|
| 451 |
+
"""Xóa hội thoại"""
|
| 452 |
+
streaming_service.clear_conversation()
|
| 453 |
+
state = streaming_service.get_conversation_state()
|
| 454 |
+
return "", "", None, "🗑️ Đã xóa hội thoại", state
|
| 455 |
|
| 456 |
# Event handlers
|
| 457 |
+
microphone.stream(
|
| 458 |
+
process_audio_stream,
|
| 459 |
+
inputs=[microphone],
|
| 460 |
+
outputs=[transcription, ai_response, tts_output, status_display, state_display]
|
| 461 |
)
|
| 462 |
|
| 463 |
+
clear_btn.click(
|
| 464 |
+
clear_conversation,
|
| 465 |
+
outputs=[transcription, ai_response, tts_output, status_display, state_display]
|
| 466 |
)
|
| 467 |
|
| 468 |
+
# Khởi tạo giá trị ban đầu
|
| 469 |
+
def initialize_tab():
|
| 470 |
+
state = streaming_service.get_conversation_state()
|
| 471 |
+
return "Sẵn sàng - nhấn nút microphone để nói", state
|
| 472 |
+
|
| 473 |
+
# Gọi hàm khởi tạo
|
| 474 |
+
initialize_tab()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|