Update ui/tabs.py
Browse files- ui/tabs.py +205 -2
ui/tabs.py
CHANGED
|
@@ -13,6 +13,8 @@ from services.stream_object_detection_service import StreamObjectDetection
|
|
| 13 |
from services.voice_coding_service import VoiceCodingService
|
| 14 |
from services.sambanova_voice_service import SambanovaVoiceService
|
| 15 |
from services.gemini_realtime_service import GeminiRealtimeService
|
|
|
|
|
|
|
| 16 |
from core.rag_system import EnhancedRAGSystem
|
| 17 |
from core.tts_service import EnhancedTTSService
|
| 18 |
from core.wikipedia_processor import WikipediaProcessor
|
|
@@ -26,7 +28,7 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 26 |
voice_coding_service: VoiceCodingService,
|
| 27 |
sambanova_voice_service: SambanovaVoiceService
|
| 28 |
):
|
| 29 |
-
|
| 30 |
with gr.Tab("🎙️ Streaming Voice "):
|
| 31 |
create_streaming_voice_tab(streaming_voice_service)
|
| 32 |
with gr.Tab("OpenAI Realtime"):
|
|
@@ -48,7 +50,11 @@ def create_all_tabs(audio_service: AudioService, chat_service: ChatService,
|
|
| 48 |
|
| 49 |
with gr.Tab("📚 RAG Wikipedia"):
|
| 50 |
create_rag_tab(rag_system, wikipedia_processor)
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
with gr.Tab("🔊 Text-to-Speech"):
|
| 53 |
create_tts_tab(tts_service)
|
| 54 |
|
|
@@ -276,6 +282,203 @@ def create_gemini_realtime_tab():
|
|
| 276 |
return gemini_tab
|
| 277 |
|
| 278 |
# FastAPI routes cho Gemini (tùy chọn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
| 13 |
from services.voice_coding_service import VoiceCodingService
|
| 14 |
from services.sambanova_voice_service import SambanovaVoiceService
|
| 15 |
from services.gemini_realtime_service import GeminiRealtimeService
|
| 16 |
+
from core.conversational_agent import ConversationalAgent
|
| 17 |
+
|
| 18 |
from core.rag_system import EnhancedRAGSystem
|
| 19 |
from core.tts_service import EnhancedTTSService
|
| 20 |
from core.wikipedia_processor import WikipediaProcessor
|
|
|
|
| 28 |
voice_coding_service: VoiceCodingService,
|
| 29 |
sambanova_voice_service: SambanovaVoiceService
|
| 30 |
):
|
| 31 |
+
multilingual_manager = rag_system.multilingual_manager if hasattr(rag_system, 'multilingual_manager') else None
|
| 32 |
with gr.Tab("🎙️ Streaming Voice "):
|
| 33 |
create_streaming_voice_tab(streaming_voice_service)
|
| 34 |
with gr.Tab("OpenAI Realtime"):
|
|
|
|
| 50 |
|
| 51 |
with gr.Tab("📚 RAG Wikipedia"):
|
| 52 |
create_rag_tab(rag_system, wikipedia_processor)
|
| 53 |
+
with gr.Tab("🌐 Language Info"):
|
| 54 |
+
if multilingual_manager:
|
| 55 |
+
create_language_info_tab(multilingual_manager)
|
| 56 |
+
else:
|
| 57 |
+
gr.Markdown("⚠️ Không có multilingual manager")
|
| 58 |
with gr.Tab("🔊 Text-to-Speech"):
|
| 59 |
create_tts_tab(tts_service)
|
| 60 |
|
|
|
|
| 282 |
return gemini_tab
|
| 283 |
|
| 284 |
# FastAPI routes cho Gemini (tùy chọn)
|
| 285 |
+
def create_cag_tab(rag_system: EnhancedRAGSystem, multilingual_manager: MultilingualManager):
|
| 286 |
+
"""Tạo tab CAG (Conversational Agent Grounding)"""
|
| 287 |
+
|
| 288 |
+
# Khởi tạo conversational agent
|
| 289 |
+
conversational_agent = ConversationalAgent(rag_system, multilingual_manager)
|
| 290 |
+
|
| 291 |
+
with gr.Blocks() as cag_tab:
|
| 292 |
+
gr.Markdown("## 🤖 Chatbot với Tri thức (CAG)")
|
| 293 |
+
gr.Markdown("Chatbot thông minh sử dụng thông tin từ cơ sở tri thức RAG để trả lời chính xác")
|
| 294 |
+
|
| 295 |
+
with gr.Row():
|
| 296 |
+
with gr.Column(scale=2):
|
| 297 |
+
# Chat interface
|
| 298 |
+
chatbot = gr.Chatbot(
|
| 299 |
+
label="🤖 CAG Chatbot",
|
| 300 |
+
height=400,
|
| 301 |
+
show_copy_button=True,
|
| 302 |
+
bubble_full_width=False,
|
| 303 |
+
avatar_images=(
|
| 304 |
+
"https://cdn-icons-png.flaticon.com/512/1077/1077114.png", # User
|
| 305 |
+
"https://cdn-icons-png.flaticon.com/512/4712/4712027.png" # Assistant
|
| 306 |
+
)
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
with gr.Row():
|
| 310 |
+
msg = gr.Textbox(
|
| 311 |
+
label="Nhập câu hỏi",
|
| 312 |
+
placeholder="Hỏi tôi về bất kỳ điều gì trong cơ sở tri thức...",
|
| 313 |
+
scale=4,
|
| 314 |
+
container=False
|
| 315 |
+
)
|
| 316 |
+
submit_btn = gr.Button("Gửi", variant="primary", scale=1)
|
| 317 |
+
clear_btn = gr.Button("Xóa", variant="secondary", scale=1)
|
| 318 |
+
|
| 319 |
+
# Additional info
|
| 320 |
+
with gr.Row():
|
| 321 |
+
lang_display = gr.Textbox(
|
| 322 |
+
label="🌐 Ngôn ngữ phát hiện",
|
| 323 |
+
interactive=False,
|
| 324 |
+
scale=2
|
| 325 |
+
)
|
| 326 |
+
conv_stats_btn = gr.Button("📊 Thống kê", variant="secondary", scale=1)
|
| 327 |
+
|
| 328 |
+
# Knowledge base stats
|
| 329 |
+
with gr.Accordion("📊 Cơ sở tri thức", open=False):
|
| 330 |
+
kb_stats = gr.Textbox(
|
| 331 |
+
label="Thống kê",
|
| 332 |
+
interactive=False,
|
| 333 |
+
lines=4
|
| 334 |
+
)
|
| 335 |
+
refresh_kb_btn = gr.Button("🔄 Cập nhật", variant="secondary", size="sm")
|
| 336 |
+
|
| 337 |
+
with gr.Column(scale=1):
|
| 338 |
+
gr.Markdown("### 📚 Nguồn thông tin")
|
| 339 |
+
|
| 340 |
+
# Knowledge sources from RAG
|
| 341 |
+
sources_display = gr.JSON(
|
| 342 |
+
label="Kết quả tìm kiếm từ RAG",
|
| 343 |
+
show_label=True,
|
| 344 |
+
container=True
|
| 345 |
+
)
|
| 346 |
+
|
| 347 |
+
# Conversation statistics
|
| 348 |
+
conv_stats_display = gr.JSON(
|
| 349 |
+
label="📈 Thống kê hội thoại",
|
| 350 |
+
show_label=True,
|
| 351 |
+
container=True
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
# Language info
|
| 355 |
+
with gr.Accordion("🌐 Thông tin ngôn ngữ", open=False):
|
| 356 |
+
lang_info_btn = gr.Button("ℹ️ Xem ngôn ngữ", variant="secondary", size="sm")
|
| 357 |
+
lang_info_display = gr.JSON(
|
| 358 |
+
label="Ngôn ngữ hỗ trợ",
|
| 359 |
+
container=True
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
# Instructions
|
| 363 |
+
with gr.Accordion("ℹ️ Hướng dẫn", open=False):
|
| 364 |
+
gr.Markdown("""
|
| 365 |
+
**Cách sử dụng CAG:**
|
| 366 |
+
1. Nhập câu hỏi vào ô chat
|
| 367 |
+
2. Bot tự động tìm trong cơ sở tri thức
|
| 368 |
+
3. Trả lời dựa trên thông tin tìm thấy
|
| 369 |
+
4. Xem nguồn thông tin bên phải
|
| 370 |
+
|
| 371 |
+
**Đặc điểm:**
|
| 372 |
+
- 🤖 Grounding từ RAG
|
| 373 |
+
- 🔍 Tự động tìm kiếm
|
| 374 |
+
- 🌐 Đa ngôn ngữ
|
| 375 |
+
- 📊 Theo dõi nguồn
|
| 376 |
+
""")
|
| 377 |
+
|
| 378 |
+
def respond(message: str, chat_history: List[Tuple[str, str]]) -> Tuple[str, List, List, str]:
|
| 379 |
+
"""Xử lý phản hồi chatbot"""
|
| 380 |
+
if not message.strip():
|
| 381 |
+
return "", chat_history, [], "⏳ Chờ câu hỏi..."
|
| 382 |
+
|
| 383 |
+
try:
|
| 384 |
+
# Gọi conversational agent
|
| 385 |
+
response, search_results = conversational_agent.process_query(message, chat_history)
|
| 386 |
+
|
| 387 |
+
# Cập nhật chat history
|
| 388 |
+
chat_history.append((message, response))
|
| 389 |
+
|
| 390 |
+
# Phát hiện ngôn ngữ
|
| 391 |
+
language = multilingual_manager.detect_language(message)
|
| 392 |
+
lang_info = multilingual_manager.get_language_info(language)
|
| 393 |
+
lang_name = lang_info.get('name', language)
|
| 394 |
+
|
| 395 |
+
return "", chat_history, search_results, f"🌐 {lang_name}"
|
| 396 |
+
|
| 397 |
+
except Exception as e:
|
| 398 |
+
error_response = f"Xin lỗi, đã xảy ra lỗi: {str(e)}"
|
| 399 |
+
chat_history.append((message, error_response))
|
| 400 |
+
return "", chat_history, [{"error": str(e)}], "❌ Lỗi"
|
| 401 |
+
|
| 402 |
+
def get_conversation_stats():
|
| 403 |
+
"""Lấy thống kê hội thoại"""
|
| 404 |
+
return conversational_agent.get_conversation_stats()
|
| 405 |
+
|
| 406 |
+
def get_kb_stats():
|
| 407 |
+
"""Lấy thống kê knowledge base"""
|
| 408 |
+
stats = rag_system.get_collection_stats()
|
| 409 |
+
lang_dist = stats['language_distribution']
|
| 410 |
+
lang_dist_str = ", ".join([f"{k}: {v}" for k, v in lang_dist.items()])
|
| 411 |
+
|
| 412 |
+
return f"""
|
| 413 |
+
📊 THỐNG KÊ CƠ SỞ TRI THỨC:
|
| 414 |
+
• 📄 Tổng documents: {stats['total_documents']}
|
| 415 |
+
• 🔤 Embeddings: {stats['embedding_count']}
|
| 416 |
+
• 🌐 Ngôn ngữ: {lang_dist_str}
|
| 417 |
+
• 📐 Dimension: {stats['embedding_dimension']}
|
| 418 |
+
• 🏷️ Trạng thái: {stats['status']}
|
| 419 |
+
"""
|
| 420 |
+
|
| 421 |
+
def get_language_info():
|
| 422 |
+
"""Lấy thông tin ngôn ngữ hỗ trợ"""
|
| 423 |
+
return multilingual_manager.get_supported_languages()
|
| 424 |
+
|
| 425 |
+
def clear_chat():
|
| 426 |
+
"""Xóa chat"""
|
| 427 |
+
conversational_agent.clear_conversation_history()
|
| 428 |
+
return [], [], "🧹 Đã xóa"
|
| 429 |
+
|
| 430 |
+
def refresh_all():
|
| 431 |
+
"""Làm mới tất cả"""
|
| 432 |
+
kb_stats_text = get_kb_stats()
|
| 433 |
+
conv_stats = get_conversation_stats()
|
| 434 |
+
lang_info = get_language_info()
|
| 435 |
+
return kb_stats_text, conv_stats, lang_info
|
| 436 |
+
|
| 437 |
+
# Event handlers
|
| 438 |
+
submit_btn.click(
|
| 439 |
+
respond,
|
| 440 |
+
inputs=[msg, chatbot],
|
| 441 |
+
outputs=[msg, chatbot, sources_display, lang_display]
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
+
msg.submit(
|
| 445 |
+
respond,
|
| 446 |
+
inputs=[msg, chatbot],
|
| 447 |
+
outputs=[msg, chatbot, sources_display, lang_display]
|
| 448 |
+
)
|
| 449 |
+
|
| 450 |
+
clear_btn.click(
|
| 451 |
+
clear_chat,
|
| 452 |
+
inputs=[],
|
| 453 |
+
outputs=[chatbot, sources_display, lang_display]
|
| 454 |
+
)
|
| 455 |
+
|
| 456 |
+
conv_stats_btn.click(
|
| 457 |
+
get_conversation_stats,
|
| 458 |
+
inputs=[],
|
| 459 |
+
outputs=[conv_stats_display]
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
refresh_kb_btn.click(
|
| 463 |
+
get_kb_stats,
|
| 464 |
+
inputs=[],
|
| 465 |
+
outputs=[kb_stats]
|
| 466 |
+
)
|
| 467 |
+
|
| 468 |
+
lang_info_btn.click(
|
| 469 |
+
get_language_info,
|
| 470 |
+
inputs=[],
|
| 471 |
+
outputs=[lang_info_display]
|
| 472 |
+
)
|
| 473 |
+
|
| 474 |
+
# Initialize on load
|
| 475 |
+
cag_tab.load(
|
| 476 |
+
refresh_all,
|
| 477 |
+
inputs=[],
|
| 478 |
+
outputs=[kb_stats, conv_stats_display, lang_info_display]
|
| 479 |
+
)
|
| 480 |
+
|
| 481 |
+
return cag_tab
|
| 482 |
def setup_gemini_routes(app):
|
| 483 |
"""Thiết lập routes FastAPI cho Gemini"""
|
| 484 |
|