Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1330,10 +1330,9 @@ print(' - Response Diversity: Tránh lặp lại câu trả lời')
|
|
| 1330 |
print(' - Advanced RAG: Tìm kiếm chính xác hơn')
|
| 1331 |
print()
|
| 1332 |
print('='*70)
|
| 1333 |
-
|
| 1334 |
# ======================================================================
|
| 1335 |
# ============================================================
|
| 1336 |
-
# PHẦN 12.5: GRADIO INTERFACE (BẢN FIXED
|
| 1337 |
# ============================================================
|
| 1338 |
|
| 1339 |
import gradio as gr
|
|
@@ -1412,36 +1411,34 @@ def process_chat_pro(audio, text, history, persona="thuong"):
|
|
| 1412 |
return
|
| 1413 |
|
| 1414 |
if 'ai_process_pro' not in globals():
|
| 1415 |
-
yield history + [
|
| 1416 |
return
|
| 1417 |
|
| 1418 |
-
#
|
| 1419 |
result = ai_process_pro(query, use_memory=True)
|
| 1420 |
|
| 1421 |
-
#
|
| 1422 |
-
|
| 1423 |
-
|
|
|
|
|
|
|
|
|
|
| 1424 |
if result is None:
|
| 1425 |
-
|
| 1426 |
-
elif isinstance(result,
|
| 1427 |
-
|
| 1428 |
-
|
| 1429 |
-
|
| 1430 |
-
|
| 1431 |
-
|
|
|
|
|
|
|
|
|
|
| 1432 |
|
| 1433 |
-
|
| 1434 |
-
raw_response = result.get('response', result.get('answer', ''))
|
| 1435 |
-
process_time = result.get('process_time', 0)
|
| 1436 |
-
docs_count = result.get('docs_count', 0)
|
| 1437 |
-
temperature = result.get('temperature', 0.7)
|
| 1438 |
-
|
| 1439 |
-
# Xử lý trường hợp raw_response bị Tuple lồng nhau hoặc None
|
| 1440 |
-
if isinstance(raw_response, tuple):
|
| 1441 |
raw_response = raw_response[0] if len(raw_response) > 0 else ""
|
| 1442 |
-
|
| 1443 |
-
|
| 1444 |
-
raw_response = ""
|
| 1445 |
|
| 1446 |
raw_response = str(raw_response).strip()
|
| 1447 |
|
|
@@ -1450,25 +1447,18 @@ def process_chat_pro(audio, text, history, persona="thuong"):
|
|
| 1450 |
|
| 1451 |
response_meta = f"\n\n---\n_⏱️ Xử lý: {process_time}s | 📄 Tài liệu: {docs_count} | 🌡️ Temp: {temperature}_"
|
| 1452 |
|
| 1453 |
-
#
|
| 1454 |
full_text = ""
|
| 1455 |
words = raw_response.split(" ")
|
| 1456 |
for word in words:
|
| 1457 |
full_text += word + " "
|
| 1458 |
-
current_history = history + [
|
| 1459 |
-
{"role": "user", "content": query},
|
| 1460 |
-
{"role": "assistant", "content": full_text}
|
| 1461 |
-
]
|
| 1462 |
yield current_history, None, ''
|
| 1463 |
|
| 1464 |
-
|
| 1465 |
-
final_history = history + [
|
| 1466 |
-
{"role": "user", "content": query},
|
| 1467 |
-
{"role": "assistant", "content": full_text + response_meta}
|
| 1468 |
-
]
|
| 1469 |
yield final_history, None, ''
|
| 1470 |
|
| 1471 |
-
#
|
| 1472 |
audio_out = None
|
| 1473 |
if 'text_to_speech' in globals():
|
| 1474 |
try:
|
|
@@ -1486,7 +1476,7 @@ def process_chat_pro(audio, text, history, persona="thuong"):
|
|
| 1486 |
except Exception as e:
|
| 1487 |
traceback.print_exc()
|
| 1488 |
error_msg = f"Lỗi hệ thống: {str(e)}"
|
| 1489 |
-
yield history, None,
|
| 1490 |
|
| 1491 |
|
| 1492 |
# ============================================================
|
|
@@ -1567,7 +1557,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", neutral_hue="slate"), cs
|
|
| 1567 |
with gr.Tab("💬 Trợ lý AI"):
|
| 1568 |
with gr.Row():
|
| 1569 |
with gr.Column(scale=7):
|
| 1570 |
-
|
|
|
|
| 1571 |
audio_out = gr.Audio(label='🔊 Nghe phản hồi', autoplay=True)
|
| 1572 |
|
| 1573 |
with gr.Column(scale=3):
|
|
@@ -1715,14 +1706,14 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", neutral_hue="slate"), cs
|
|
| 1715 |
def refresh_memory():
|
| 1716 |
if 'conversation_memory' in globals():
|
| 1717 |
ctx = conversation_memory.get_context_summary()
|
| 1718 |
-
|
|
|
|
| 1719 |
for m in conversation_memory.history[-5:]])
|
| 1720 |
return ctx or "Không có ngữ cảnh", hist or "Không có lịch sử"
|
| 1721 |
return "Chưa khởi tạo bộ nhớ", "Chưa có dữ liệu"
|
| 1722 |
|
| 1723 |
btn_refresh.click(refresh_memory, [], [memory_info, memory_history])
|
| 1724 |
btn_clear_mem.click(clear_chat_pro, [], [chat, audio_out, msg_in])
|
| 1725 |
-
|
| 1726 |
# ======================================================================
|
| 1727 |
# ============================================================
|
| 1728 |
# PHẦN 12.7: LAUNCH APP PRO
|
|
|
|
| 1330 |
print(' - Advanced RAG: Tìm kiếm chính xác hơn')
|
| 1331 |
print()
|
| 1332 |
print('='*70)
|
|
|
|
| 1333 |
# ======================================================================
|
| 1334 |
# ============================================================
|
| 1335 |
+
# PHẦN 12.5: GRADIO INTERFACE (BẢN FIXED TƯƠNG THÍCH MỌI PHIÊN BẢN)
|
| 1336 |
# ============================================================
|
| 1337 |
|
| 1338 |
import gradio as gr
|
|
|
|
| 1411 |
return
|
| 1412 |
|
| 1413 |
if 'ai_process_pro' not in globals():
|
| 1414 |
+
yield history + [[query, "Lỗi: Không tìm thấy nhân xử lý AI (ai_process_pro)."]], None, ''
|
| 1415 |
return
|
| 1416 |
|
| 1417 |
+
# 1. Gọi hàm AI
|
| 1418 |
result = ai_process_pro(query, use_memory=True)
|
| 1419 |
|
| 1420 |
+
# 2. Xử lý an toàn mọi kiểu dữ liệu trả về
|
| 1421 |
+
raw_response = ""
|
| 1422 |
+
process_time = 0
|
| 1423 |
+
docs_count = 0
|
| 1424 |
+
temperature = 0.7
|
| 1425 |
+
|
| 1426 |
if result is None:
|
| 1427 |
+
raw_response = ""
|
| 1428 |
+
elif isinstance(result, dict):
|
| 1429 |
+
raw_response = result.get('response', result.get('answer', result.get('text', '')))
|
| 1430 |
+
process_time = result.get('process_time', 0)
|
| 1431 |
+
docs_count = result.get('docs_count', 0)
|
| 1432 |
+
temperature = result.get('temperature', 0.7)
|
| 1433 |
+
elif isinstance(result, (tuple, list)):
|
| 1434 |
+
raw_response = result[0] if len(result) > 0 else ""
|
| 1435 |
+
else:
|
| 1436 |
+
raw_response = str(result)
|
| 1437 |
|
| 1438 |
+
if isinstance(raw_response, (tuple, list)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1439 |
raw_response = raw_response[0] if len(raw_response) > 0 else ""
|
| 1440 |
+
elif isinstance(raw_response, dict):
|
| 1441 |
+
raw_response = raw_response.get('response', str(raw_response))
|
|
|
|
| 1442 |
|
| 1443 |
raw_response = str(raw_response).strip()
|
| 1444 |
|
|
|
|
| 1447 |
|
| 1448 |
response_meta = f"\n\n---\n_⏱️ Xử lý: {process_time}s | 📄 Tài liệu: {docs_count} | 🌡️ Temp: {temperature}_"
|
| 1449 |
|
| 1450 |
+
# 3. Tương thích cấu trúc History (mảng 2 chiều [[user, bot]])
|
| 1451 |
full_text = ""
|
| 1452 |
words = raw_response.split(" ")
|
| 1453 |
for word in words:
|
| 1454 |
full_text += word + " "
|
| 1455 |
+
current_history = history + [[query, full_text]]
|
|
|
|
|
|
|
|
|
|
| 1456 |
yield current_history, None, ''
|
| 1457 |
|
| 1458 |
+
final_history = history + [[query, full_text + response_meta]]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1459 |
yield final_history, None, ''
|
| 1460 |
|
| 1461 |
+
# 4. Giọng nói (nếu có)
|
| 1462 |
audio_out = None
|
| 1463 |
if 'text_to_speech' in globals():
|
| 1464 |
try:
|
|
|
|
| 1476 |
except Exception as e:
|
| 1477 |
traceback.print_exc()
|
| 1478 |
error_msg = f"Lỗi hệ thống: {str(e)}"
|
| 1479 |
+
yield history + [[query if 'query' in locals() else "Unknown", error_msg]], None, ''
|
| 1480 |
|
| 1481 |
|
| 1482 |
# ============================================================
|
|
|
|
| 1557 |
with gr.Tab("💬 Trợ lý AI"):
|
| 1558 |
with gr.Row():
|
| 1559 |
with gr.Column(scale=7):
|
| 1560 |
+
# Đã loại bỏ type="messages" để chống lỗi TypeError trên các phiên bản Gradio cũ
|
| 1561 |
+
chat = gr.Chatbot(height=520, label='Hệ thống tư vấn tự động', elem_classes="main-chatbot")
|
| 1562 |
audio_out = gr.Audio(label='🔊 Nghe phản hồi', autoplay=True)
|
| 1563 |
|
| 1564 |
with gr.Column(scale=3):
|
|
|
|
| 1706 |
def refresh_memory():
|
| 1707 |
if 'conversation_memory' in globals():
|
| 1708 |
ctx = conversation_memory.get_context_summary()
|
| 1709 |
+
# Cập nhật format theo mảng mới
|
| 1710 |
+
hist = "\n".join([f"Khách/Bot: {str(m)[:100]}..."
|
| 1711 |
for m in conversation_memory.history[-5:]])
|
| 1712 |
return ctx or "Không có ngữ cảnh", hist or "Không có lịch sử"
|
| 1713 |
return "Chưa khởi tạo bộ nhớ", "Chưa có dữ liệu"
|
| 1714 |
|
| 1715 |
btn_refresh.click(refresh_memory, [], [memory_info, memory_history])
|
| 1716 |
btn_clear_mem.click(clear_chat_pro, [], [chat, audio_out, msg_in])
|
|
|
|
| 1717 |
# ======================================================================
|
| 1718 |
# ============================================================
|
| 1719 |
# PHẦN 12.7: LAUNCH APP PRO
|