|
|
import gradio as gr |
|
|
import threading |
|
|
import time |
|
|
from services.audio_service import AudioService |
|
|
from services.chat_service import ChatService |
|
|
from services.image_service import ImageService |
|
|
from services.streaming_voice_service import StreamingVoiceService |
|
|
from core.rag_system import EnhancedRAGSystem |
|
|
from core.tts_service import EnhancedTTSService |
|
|
from core.wikipedia_processor import WikipediaProcessor |
|
|
from ui.components import create_audio_components, create_chat_components, create_streaming_voice_components |
|
|
|
|
|
def create_all_tabs(audio_service: AudioService, chat_service: ChatService, |
|
|
image_service: ImageService, rag_system: EnhancedRAGSystem, |
|
|
tts_service: EnhancedTTSService, wikipedia_processor: WikipediaProcessor, |
|
|
streaming_voice_service: StreamingVoiceService): |
|
|
|
|
|
with gr.Tab("🎙️ Streaming Voice (VAD)"): |
|
|
create_streaming_voice_tab(streaming_voice_service) |
|
|
|
|
|
with gr.Tab("🎙️ Audio"): |
|
|
create_audio_tab(audio_service) |
|
|
|
|
|
with gr.Tab("💬 Chat"): |
|
|
create_chat_tab(chat_service) |
|
|
|
|
|
with gr.Tab("🖼️ Image"): |
|
|
create_image_tab(image_service) |
|
|
|
|
|
with gr.Tab("📚 RAG Wikipedia"): |
|
|
create_rag_tab(rag_system, wikipedia_processor) |
|
|
|
|
|
with gr.Tab("🔊 Text-to-Speech"): |
|
|
create_tts_tab(tts_service) |
|
|
|
|
|
with gr.Tab("🌐 Language Info"): |
|
|
create_language_info_tab(rag_system.multilingual_manager) |
|
|
def create_rag_tab(rag_system: EnhancedRAGSystem, wikipedia_processor: WikipediaProcessor): |
|
|
gr.Markdown("## Upload data có sẵn") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
gr.Markdown("### 📤 Upload dữ liệu") |
|
|
file_upload = gr.File( |
|
|
label="Tải lên file", |
|
|
file_types=['.txt', '.csv', '.json'], |
|
|
file_count="single" |
|
|
) |
|
|
upload_btn = gr.Button("📤 Upload Data", variant="primary") |
|
|
upload_status = gr.Textbox(label="Trạng thái Upload", interactive=False) |
|
|
|
|
|
gr.Markdown("### 📊 Thống kê Database") |
|
|
stats_btn = gr.Button("📊 Database Stats", variant="secondary") |
|
|
stats_display = gr.Textbox(label="Thống kê", interactive=False) |
|
|
|
|
|
gr.Markdown("### 🔍 Tìm kiếm Database") |
|
|
search_query = gr.Textbox( |
|
|
label="Tìm kiếm trong database", |
|
|
placeholder="Nhập từ khóa để tìm kiếm..." |
|
|
) |
|
|
search_btn = gr.Button("🔍 Tìm kiếm", variant="secondary") |
|
|
|
|
|
with gr.Column(scale=2): |
|
|
gr.Markdown("### 📋 Kết quả tìm kiếm RAG") |
|
|
rag_results = gr.JSON(label="Tài liệu tham khảo tìm được") |
|
|
|
|
|
def upload_wikipedia_file(file): |
|
|
if file is None: |
|
|
return "Vui lòng chọn file để upload" |
|
|
|
|
|
try: |
|
|
documents = wikipedia_processor.process_uploaded_file(file.name) |
|
|
|
|
|
if not documents: |
|
|
return "Không tìm thấy dữ liệu nào trong file." |
|
|
|
|
|
metadatas = [{"source": "wikipedia", "type": "knowledge", "file": file.name, "language": "vi"} for _ in documents] |
|
|
rag_system.add_documents(documents, metadatas) |
|
|
|
|
|
stats = rag_system.get_collection_stats() |
|
|
return f"✅ Đã thêm {len(documents)} documents Wikipedia vào RAG database. Tổng số documents: {stats['count']}" |
|
|
|
|
|
except Exception as e: |
|
|
return f"❌ Lỗi xử lý file Wikipedia: {str(e)}" |
|
|
|
|
|
upload_btn.click(upload_wikipedia_file, inputs=[file_upload], outputs=[upload_status]) |
|
|
stats_btn.click(rag_system.get_collection_stats, inputs=[], outputs=[stats_display]) |
|
|
search_btn.click(rag_system.semantic_search, inputs=[search_query], outputs=[rag_results]) |
|
|
def create_audio_tab(audio_service: AudioService): |
|
|
gr.Markdown("## Nói chuyện với AI (Đa ngôn ngữ)") |
|
|
audio_input, transcription_output, response_output, tts_audio_output, process_button = create_audio_components() |
|
|
|
|
|
|
|
|
language_display = gr.Textbox( |
|
|
label="🌐 Ngôn ngữ phát hiện", |
|
|
interactive=False, |
|
|
placeholder="Ngôn ngữ sẽ hiển thị ở đây..." |
|
|
) |
|
|
|
|
|
process_button.click( |
|
|
audio_service.transcribe_audio, |
|
|
inputs=audio_input, |
|
|
outputs=[transcription_output, response_output, tts_audio_output, language_display] |
|
|
) |
|
|
def create_image_tab(image_service: ImageService): |
|
|
gr.Markdown("## Phân tích hình ảnh") |
|
|
with gr.Row(): |
|
|
image_input = gr.Image(type="numpy", label="Tải lên hình ảnh") |
|
|
with gr.Row(): |
|
|
image_description = gr.Textbox( |
|
|
label="Mô tả hình ảnh của bạn (tùy chọn)", |
|
|
placeholder="Mô tả ngắn về hình ảnh để AI phân tích chính xác hơn..." |
|
|
) |
|
|
with gr.Row(): |
|
|
image_output = gr.Textbox(label="Kết quả phân tích") |
|
|
analyze_button = gr.Button("Phân tích hình ảnh", variant="primary") |
|
|
analyze_button.click( |
|
|
image_service.analyze_image_with_description, |
|
|
inputs=[image_input, image_description], |
|
|
outputs=[image_output] |
|
|
) |
|
|
def create_chat_tab(chat_service: ChatService): |
|
|
gr.Markdown("## Trò chuyện với AI Assistant (Đa ngôn ngữ)") |
|
|
chatbot, state, user_input, send_button, clear_button, chat_tts_output = create_chat_components() |
|
|
|
|
|
|
|
|
chat_language_display = gr.Textbox( |
|
|
label="🌐 Ngôn ngữ phát hiện", |
|
|
interactive=False, |
|
|
placeholder="Ngôn ngữ sẽ hiển thị ở đây..." |
|
|
) |
|
|
|
|
|
send_button.click( |
|
|
chat_service.respond, |
|
|
inputs=[user_input, state], |
|
|
outputs=[user_input, chatbot, state, chat_tts_output, chat_language_display] |
|
|
) |
|
|
clear_button.click( |
|
|
chat_service.clear_chat_history, |
|
|
inputs=[state], |
|
|
outputs=[chatbot, state] |
|
|
) |
|
|
|
|
|
def create_language_info_tab(multilingual_manager): |
|
|
"""Tab hiển thị thông tin về hệ thống đa ngôn ngữ""" |
|
|
gr.Markdown("## 🌐 Thông tin Hệ thống Đa ngôn ngữ") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
gr.Markdown("### 🔧 Cấu hình Model") |
|
|
|
|
|
vietnamese_info = multilingual_manager.get_language_info('vi') |
|
|
multilingual_info = multilingual_manager.get_language_info('en') |
|
|
|
|
|
gr.Markdown(f""" |
|
|
**Tiếng Việt:** |
|
|
- Embedding Model: `{vietnamese_info['embedding_model']}` |
|
|
- LLM Model: `{vietnamese_info['llm_model']}` |
|
|
- Trạng thái: {vietnamese_info['status']} |
|
|
|
|
|
**Đa ngôn ngữ:** |
|
|
- Embedding Model: `{multilingual_info['embedding_model']}` |
|
|
- LLM Model: `{multilingual_info['llm_model']}` |
|
|
- Trạng thái: {multilingual_info['status']} |
|
|
""") |
|
|
|
|
|
with gr.Column(): |
|
|
gr.Markdown("### 🎯 Ngôn ngữ được hỗ trợ") |
|
|
|
|
|
supported_languages = """ |
|
|
- 🇻🇳 **Tiếng Việt**: Sử dụng model chuyên biệt |
|
|
- 🇺🇸 **English**: Sử dụng model đa ngôn ngữ |
|
|
- 🇫🇷 **French**: Sử dụng model đa ngôn ngữ |
|
|
- 🇪🇸 **Spanish**: Sử dụng model đa ngôn ngữ |
|
|
- 🇩🇪 **German**: Sử dụng model đa ngôn ngữ |
|
|
- 🇯🇵 **Japanese**: Sử dụng model đa ngôn ngữ |
|
|
- 🇰🇷 **Korean**: Sử dụng model đa ngôn ngữ |
|
|
- 🇨🇳 **Chinese**: Sử dụng model đa ngôn ngữ |
|
|
""" |
|
|
gr.Markdown(supported_languages) |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
gr.Markdown("### 🔍 Kiểm tra Ngôn ngữ") |
|
|
test_text = gr.Textbox( |
|
|
label="Nhập văn bản để kiểm tra ngôn ngữ", |
|
|
placeholder="Nhập văn bản bằng bất kỳ ngôn ngữ nào..." |
|
|
) |
|
|
test_button = gr.Button("🔍 Kiểm tra", variant="primary") |
|
|
|
|
|
test_result = gr.JSON(label="Kết quả phát hiện ngôn ngữ") |
|
|
|
|
|
test_button.click( |
|
|
lambda text: { |
|
|
'detected_language': multilingual_manager.detect_language(text), |
|
|
'language_info': multilingual_manager.get_language_info(multilingual_manager.detect_language(text)), |
|
|
'embedding_model': multilingual_manager.get_embedding_model(multilingual_manager.detect_language(text)) is not None, |
|
|
'llm_model': multilingual_manager.get_llm_model(multilingual_manager.detect_language(text)) |
|
|
}, |
|
|
inputs=[test_text], |
|
|
outputs=[test_result] |
|
|
) |
|
|
def create_tts_tab(tts_service: EnhancedTTSService): |
|
|
gr.Markdown("## 🎵 Chuyển văn bản thành giọng nói nâng cao") |
|
|
gr.Markdown("Nhập văn bản và chọn ngôn ngữ để chuyển thành giọng nói") |
|
|
|
|
|
with gr.Group(): |
|
|
with gr.Row(): |
|
|
tts_text_input = gr.Textbox( |
|
|
label="Văn bản cần chuyển thành giọng nói", |
|
|
lines=4, |
|
|
placeholder="Nhập văn bản tại đây..." |
|
|
) |
|
|
with gr.Row(): |
|
|
tts_language = gr.Dropdown( |
|
|
choices=["vi", "en", "fr", "es", "de", "ja", "ko", "zh"], |
|
|
value="vi", |
|
|
label="Ngôn ngữ" |
|
|
) |
|
|
tts_provider = gr.Dropdown( |
|
|
choices=["auto", "gtts", "edgetts"], |
|
|
value="auto", |
|
|
label="Nhà cung cấp TTS" |
|
|
) |
|
|
with gr.Row(): |
|
|
tts_output_audio = gr.Audio( |
|
|
label="Kết quả giọng nói", |
|
|
interactive=False |
|
|
) |
|
|
tts_button = gr.Button("🔊 Chuyển thành giọng nói", variant="primary") |
|
|
|
|
|
def text_to_speech_standalone(text, language, tts_provider): |
|
|
if not text: |
|
|
return None |
|
|
|
|
|
try: |
|
|
tts_audio_bytes = tts_service.text_to_speech(text, language, tts_provider) |
|
|
if tts_audio_bytes: |
|
|
temp_audio_file = tts_service.save_audio_to_file(tts_audio_bytes) |
|
|
return temp_audio_file |
|
|
except Exception as e: |
|
|
print(f"❌ Lỗi TTS: {e}") |
|
|
|
|
|
return None |
|
|
|
|
|
tts_button.click( |
|
|
text_to_speech_standalone, |
|
|
inputs=[tts_text_input, tts_language, tts_provider], |
|
|
outputs=[tts_output_audio] |
|
|
) |
|
|
def create_streaming_voice_tab(streaming_service: StreamingVoiceService): |
|
|
"""Tạo tab streaming voice với VAD""" |
|
|
|
|
|
|
|
|
(start_btn, stop_btn, status_display, state_display, |
|
|
transcription, ai_response, tts_output, streaming_state, |
|
|
conversation_history, vad_visualizer) = create_streaming_voice_components() |
|
|
|
|
|
def start_streaming(): |
|
|
"""Bắt đầu streaming với VAD""" |
|
|
def callback_handler(result): |
|
|
"""Xử lý kết quả real-time""" |
|
|
|
|
|
|
|
|
print(f"🎯 Kết quả: {result['transcription']}") |
|
|
print(f"🤖 Phản hồi: {result['response']}") |
|
|
|
|
|
success = streaming_service.start_listening(callback_handler) |
|
|
status = "✅ Đang lắng nghe..." if success else "❌ Lỗi khởi động" |
|
|
|
|
|
|
|
|
if success: |
|
|
threading.Thread(target=update_ui_loop, daemon=True).start() |
|
|
|
|
|
return status, streaming_service.get_conversation_state() |
|
|
|
|
|
def stop_streaming(): |
|
|
"""Dừng streaming""" |
|
|
streaming_service.stop_listening() |
|
|
return "🛑 Đã dừng lắng nghe", streaming_service.get_conversation_state() |
|
|
|
|
|
def update_ui_loop(): |
|
|
"""Vòng lặp cập nhật UI real-time""" |
|
|
while streaming_service.is_listening: |
|
|
|
|
|
state = streaming_service.get_conversation_state() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
time.sleep(0.1) |
|
|
|
|
|
|
|
|
start_btn.click( |
|
|
start_streaming, |
|
|
outputs=[status_display, state_display] |
|
|
) |
|
|
|
|
|
stop_btn.click( |
|
|
stop_streaming, |
|
|
outputs=[status_display, state_display] |
|
|
) |
|
|
|
|
|
|
|
|
def demo_update(): |
|
|
"""Demo cập nhật real-time""" |
|
|
if streaming_service.is_listening: |
|
|
state = streaming_service.get_conversation_state() |
|
|
return ( |
|
|
state['current_transcription'] or "Đang lắng nghe...", |
|
|
"Phản hồi sẽ xuất hiện ở đây...", |
|
|
state |
|
|
) |
|
|
return "Chưa lắng nghe", "Chưa có phản hồi", {} |
|
|
|