File size: 13,470 Bytes
dbf2148 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
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"): # NEW TAB
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()
# NEW: Language display
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] # UPDATED
)
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()
# NEW: Language display
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] # UPDATED
)
clear_button.click(
chat_service.clear_chat_history,
inputs=[state],
outputs=[chatbot, state]
)
def create_language_info_tab(multilingual_manager): # NEW FUNCTION
"""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"""
# Create components
(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"""
# Cập nhật UI với kết quả mới
# Note: Trong thực tế, cần sử dụng gr.update() và queue
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"
# Start background thread for UI updates
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:
# Cập nhật trạng thái
state = streaming_service.get_conversation_state()
# Ở đây cần sử dụng gr.update() và queue để cập nhật UI
# Đây là phiên bản đơn giản, trong thực tế cần tích hợp với Gradio Queue
time.sleep(0.1)
# Event handlers
start_btn.click(
start_streaming,
outputs=[status_display, state_display]
)
stop_btn.click(
stop_streaming,
outputs=[status_display, state_display]
)
# Demo real-time updates (simplified)
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", {}
|