Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,8 @@ import sys
|
|
| 3 |
sys.modules["sqlite3"] = sys.modules.pop("pysqlite3")
|
| 4 |
|
| 5 |
import os
|
| 6 |
-
import json
|
| 7 |
import logging
|
| 8 |
-
import
|
| 9 |
import gradio as gr
|
| 10 |
import pandas as pd
|
| 11 |
import docx2txt
|
|
@@ -14,332 +13,251 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
| 14 |
from langchain_chroma import Chroma
|
| 15 |
from langchain_community.document_loaders import PyPDFLoader
|
| 16 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 18 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 19 |
from langchain_core.documents import Document
|
| 20 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 21 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 22 |
-
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
DATA_PATH = "medical_data"
|
| 28 |
DB_PATH = "chroma_db"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 32 |
-
|
| 33 |
-
# --- HÀM XỬ LÝ DỮ LIỆU (INCREMENTAL) ---
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
hash_md5 = hashlib.md5()
|
| 38 |
-
try:
|
| 39 |
-
with open(file_path, "rb") as f:
|
| 40 |
-
for chunk in iter(lambda: f.read(4096), b""):
|
| 41 |
-
hash_md5.update(chunk)
|
| 42 |
-
return hash_md5.hexdigest()
|
| 43 |
-
except Exception:
|
| 44 |
-
return None
|
| 45 |
|
| 46 |
-
|
| 47 |
-
"""Đọc Excel giữ ngữ cảnh từng dòng"""
|
| 48 |
-
try:
|
| 49 |
-
df = pd.read_excel(file_path)
|
| 50 |
-
text_parts = []
|
| 51 |
-
columns = df.columns.tolist()
|
| 52 |
-
for index, row in df.iterrows():
|
| 53 |
-
row_str = f"Nguồn: {filename}, Dòng {index+2}: "
|
| 54 |
-
items = []
|
| 55 |
-
for col in columns:
|
| 56 |
-
val = row[col]
|
| 57 |
-
if pd.notna(val):
|
| 58 |
-
items.append(f"{col}: {val}")
|
| 59 |
-
if items:
|
| 60 |
-
text_parts.append(row_str + " | ".join(items))
|
| 61 |
-
return "\n".join(text_parts)
|
| 62 |
-
except Exception as e:
|
| 63 |
-
logging.error(f"Lỗi đọc Excel {filename}: {e}")
|
| 64 |
-
return ""
|
| 65 |
-
|
| 66 |
-
def process_single_file(file_path, filename):
|
| 67 |
-
"""Chọn loader phù hợp cho từng loại file"""
|
| 68 |
-
docs = []
|
| 69 |
-
filename_lower = filename.lower()
|
| 70 |
-
try:
|
| 71 |
-
if filename_lower.endswith(".pdf"):
|
| 72 |
-
loader = PyPDFLoader(file_path)
|
| 73 |
-
loaded_docs = loader.load()
|
| 74 |
-
for d in loaded_docs:
|
| 75 |
-
d.metadata["source"] = filename
|
| 76 |
-
docs.extend(loaded_docs)
|
| 77 |
-
|
| 78 |
-
elif filename_lower.endswith((".xlsx", ".xls")):
|
| 79 |
-
text = load_excel_enhanced(file_path, filename)
|
| 80 |
-
if text:
|
| 81 |
-
docs.append(Document(page_content=text, metadata={"source": filename}))
|
| 82 |
-
|
| 83 |
-
elif filename_lower.endswith(".docx"):
|
| 84 |
-
text = docx2txt.process(file_path)
|
| 85 |
-
if text.strip():
|
| 86 |
-
docs.append(Document(page_content=text, metadata={"source": filename}))
|
| 87 |
-
|
| 88 |
-
elif filename_lower.endswith(".txt"):
|
| 89 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
| 90 |
-
text = f.read()
|
| 91 |
-
if text.strip():
|
| 92 |
-
docs.append(Document(page_content=text, metadata={"source": filename}))
|
| 93 |
-
|
| 94 |
-
except Exception as e:
|
| 95 |
-
logging.error(f"Không thể đọc file {filename}: {e}")
|
| 96 |
-
|
| 97 |
-
return docs
|
| 98 |
|
| 99 |
-
def
|
| 100 |
-
""
|
| 101 |
-
|
| 102 |
-
"""
|
| 103 |
if not os.path.exists(folder_path):
|
| 104 |
os.makedirs(folder_path, exist_ok=True)
|
| 105 |
-
return
|
| 106 |
-
|
| 107 |
-
# 1. Load file theo dõi cũ (nếu có)
|
| 108 |
-
processed_files = {}
|
| 109 |
-
if os.path.exists(TRACKING_FILE):
|
| 110 |
-
try:
|
| 111 |
-
with open(TRACKING_FILE, "r") as f:
|
| 112 |
-
processed_files = json.load(f)
|
| 113 |
-
except Exception:
|
| 114 |
-
processed_files = {} # Nếu file lỗi json thì coi như chưa có
|
| 115 |
-
|
| 116 |
-
# 2. --- KẾT NỐI & KIỂM TRA THỰC TẾ DATABASE (Đoạn Code Mới) ---
|
| 117 |
-
vectorstore = Chroma(persist_directory=DB_PATH, embedding_function=embedding_model)
|
| 118 |
-
|
| 119 |
-
db_is_empty = False
|
| 120 |
-
try:
|
| 121 |
-
# Đếm số lượng dữ liệu thực tế trong Chroma
|
| 122 |
-
count = vectorstore._collection.count()
|
| 123 |
-
if count == 0:
|
| 124 |
-
db_is_empty = True
|
| 125 |
-
except Exception:
|
| 126 |
-
db_is_empty = True
|
| 127 |
-
|
| 128 |
-
# QUAN TRỌNG: Nếu DB rỗng nhưng file theo dõi lại bảo "đã học" -> LỖI CACHE
|
| 129 |
-
if db_is_empty and processed_files:
|
| 130 |
-
logging.warning("⚠️ PHÁT HIỆN LỖI CACHE: Database rỗng nhưng file theo dõi báo đã học.")
|
| 131 |
-
logging.warning("🔄 Hệ thống sẽ tự động RESET và học lại toàn bộ dữ liệu ngay bây giờ...")
|
| 132 |
-
processed_files = {} # Xóa sạch ký ức giả để ép hệ thống học lại
|
| 133 |
-
|
| 134 |
-
# 3. Quét file và học như bình thường
|
| 135 |
-
new_docs = []
|
| 136 |
-
current_files_status = {}
|
| 137 |
-
files_changed = False
|
| 138 |
-
|
| 139 |
-
logging.info("--- Bắt đầu quét dữ liệu ---")
|
| 140 |
for root, _, files in os.walk(folder_path):
|
| 141 |
for filename in files:
|
| 142 |
-
if filename.startswith("~$"): continue
|
| 143 |
-
|
| 144 |
file_path = os.path.join(root, filename)
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
splits = text_splitter.split_documents(new_docs)
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
for i in range(0, len(splits), batch_size):
|
| 166 |
-
batch = splits[i:i + batch_size]
|
| 167 |
-
vectorstore.add_documents(batch)
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
logging.warning("⚠️ Thư mục dữ liệu trống! Vui lòng copy file vào folder 'medical_data'.")
|
| 173 |
-
else:
|
| 174 |
-
logging.info("✅ Dữ liệu đã cập nhật đầy đủ.")
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
class DeepMedBot:
|
| 187 |
def __init__(self):
|
| 188 |
self.retriever = None
|
| 189 |
-
self.
|
| 190 |
-
self.history_chain = None
|
| 191 |
self.ready = False
|
| 192 |
|
| 193 |
if not GOOGLE_API_KEY:
|
| 194 |
-
logging.error("
|
| 195 |
return
|
| 196 |
-
|
| 197 |
try:
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
# Load DB (Thông minh)
|
| 203 |
-
self.vectorstore = load_and_process_new_files(DATA_PATH, embedding_model)
|
| 204 |
-
|
| 205 |
-
# Thiết lập Retriever
|
| 206 |
-
self.retriever = self.vectorstore.as_retriever(search_kwargs={"k": 5})
|
| 207 |
-
|
| 208 |
-
logging.info("--- Khởi tạo LLM ---")
|
| 209 |
self.llm = ChatGoogleGenerativeAI(
|
| 210 |
model="gemini-2.5-flash",
|
| 211 |
temperature=0.2,
|
| 212 |
-
google_api_key=GOOGLE_API_KEY
|
| 213 |
-
streaming=True # Quan trọng cho hiệu ứng gõ chữ
|
| 214 |
)
|
| 215 |
-
|
| 216 |
self._build_chains()
|
| 217 |
self.ready = True
|
| 218 |
logging.info("Bot đã sẵn sàng!")
|
| 219 |
-
|
| 220 |
except Exception as e:
|
| 221 |
-
logging.error(f"Lỗi
|
|
|
|
| 222 |
|
| 223 |
def _build_chains(self):
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
"
|
| 227 |
-
"
|
| 228 |
-
"KHÔNG trả lời câu hỏi, chỉ viết lại câu hỏi thôi."
|
| 229 |
)
|
| 230 |
-
|
| 231 |
-
("system",
|
| 232 |
MessagesPlaceholder("chat_history"),
|
| 233 |
("human", "{input}"),
|
| 234 |
])
|
| 235 |
-
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
# 2. Chain trả lời câu hỏi (RAG)
|
| 238 |
qa_system_prompt = (
|
| 239 |
-
"Bạn là trợ lý y tế DeepMed
|
| 240 |
"Context:\n{context}\n\n"
|
| 241 |
-
"
|
| 242 |
-
"-
|
| 243 |
-
"-
|
| 244 |
-
"- Nếu không có thông tin trong Context, hãy nói 'Tôi chưa có dữ liệu về vấn đề này'."
|
| 245 |
)
|
| 246 |
qa_prompt = ChatPromptTemplate.from_messages([
|
| 247 |
("system", qa_system_prompt),
|
| 248 |
-
("
|
|
|
|
| 249 |
])
|
| 250 |
|
| 251 |
-
|
| 252 |
-
self.
|
| 253 |
|
| 254 |
def chat_stream(self, message: str, history: list):
|
|
|
|
|
|
|
|
|
|
| 255 |
if not self.ready:
|
| 256 |
-
yield "Hệ thống đang khởi động hoặc lỗi
|
| 257 |
return
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
chat_history_objs = []
|
| 264 |
-
# Lấy 4 turn gần nhất để làm context
|
| 265 |
-
for u, a in history[-4:]:
|
| 266 |
-
chat_history_objs.append(HumanMessage(content=u))
|
| 267 |
-
chat_history_objs.append(AIMessage(content=a))
|
| 268 |
-
|
| 269 |
-
try:
|
| 270 |
-
actual_question = self.history_chain.invoke({
|
| 271 |
-
"chat_history": chat_history_objs,
|
| 272 |
-
"input": message
|
| 273 |
-
})
|
| 274 |
-
except Exception:
|
| 275 |
-
actual_question = message # Fallback nếu lỗi
|
| 276 |
|
| 277 |
-
# Bước 2: Retrieval - Tìm kiếm
|
| 278 |
-
yield "📚 Đang tra cứu tài liệu y khoa..."
|
| 279 |
-
docs = self.retriever.invoke(actual_question)
|
| 280 |
-
|
| 281 |
-
if not docs:
|
| 282 |
-
yield "Không tìm thấy thông tin phù hợp trong cơ sở dữ liệu nội bộ."
|
| 283 |
-
return
|
| 284 |
-
|
| 285 |
-
# Bước 3: Generation - Trả lời Streaming
|
| 286 |
full_response = ""
|
| 287 |
-
|
| 288 |
|
| 289 |
try:
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
| 302 |
|
| 303 |
-
# Bước 4: Thêm nguồn tham khảo
|
| 304 |
-
refs = self._build_references_text(docs)
|
| 305 |
-
if refs:
|
| 306 |
-
full_response += f"\n\n---\n📚 **Nguồn tham khảo:**\n{refs}"
|
| 307 |
-
yield full_response
|
| 308 |
-
|
| 309 |
except Exception as e:
|
| 310 |
-
logging.error(f"Lỗi
|
| 311 |
-
yield "
|
| 312 |
|
| 313 |
@staticmethod
|
| 314 |
-
def _build_references_text(docs):
|
| 315 |
-
|
|
|
|
| 316 |
for doc in docs:
|
| 317 |
src = os.path.basename(doc.metadata.get("source", "Tài liệu"))
|
| 318 |
-
# Nếu có số trang (PDF)
|
| 319 |
if "page" in doc.metadata:
|
| 320 |
-
src
|
| 321 |
-
|
|
|
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
bot = DeepMedBot()
|
| 328 |
|
| 329 |
-
def
|
| 330 |
-
# Hàm này đóng vai trò cầu nối, gọi generator của bot
|
| 331 |
for partial_response in bot.chat_stream(message, history):
|
| 332 |
yield partial_response
|
| 333 |
|
| 334 |
-
custom_css = """
|
| 335 |
-
#component-0 {height: 90vh !important;}
|
| 336 |
-
footer {visibility: hidden}
|
| 337 |
-
"""
|
| 338 |
-
|
| 339 |
demo = gr.ChatInterface(
|
| 340 |
-
fn=
|
| 341 |
-
title="🏥 DeepMed AI - Trợ lý Y tế Trung Tâm Y Tế Khu Vực Thanh Ba",
|
| 342 |
)
|
| 343 |
|
| 344 |
if __name__ == "__main__":
|
| 345 |
-
demo.launch(
|
|
|
|
| 3 |
sys.modules["sqlite3"] = sys.modules.pop("pysqlite3")
|
| 4 |
|
| 5 |
import os
|
|
|
|
| 6 |
import logging
|
| 7 |
+
import traceback
|
| 8 |
import gradio as gr
|
| 9 |
import pandas as pd
|
| 10 |
import docx2txt
|
|
|
|
| 13 |
from langchain_chroma import Chroma
|
| 14 |
from langchain_community.document_loaders import PyPDFLoader
|
| 15 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 16 |
+
from langchain_community.retrievers import BM25Retriever
|
| 17 |
+
from langchain.retrievers.ensemble import EnsembleRetriever
|
| 18 |
+
from langchain.chains import create_retrieval_chain, create_history_aware_retriever
|
| 19 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 20 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 21 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 22 |
from langchain_core.documents import Document
|
|
|
|
| 23 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
|
| 24 |
|
| 25 |
+
from langchain.retrievers import ContextualCompressionRetriever
|
| 26 |
+
from langchain.retrievers.document_compressors import CrossEncoderReranker
|
| 27 |
+
from langchain_community.cross_encoders import HuggingFaceCrossEncoder
|
| 28 |
+
|
| 29 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 30 |
DATA_PATH = "medical_data"
|
| 31 |
DB_PATH = "chroma_db"
|
| 32 |
+
MAX_HISTORY_TURNS = 6
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
USE_BM25 = True
|
| 35 |
+
USE_MMR = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def load_documents_from_folder(folder_path: str) -> list[Document]:
|
| 40 |
+
logging.info(f"--- Bắt đầu quét thư mục: {folder_path} ---")
|
| 41 |
+
documents: list[Document] = []
|
|
|
|
| 42 |
if not os.path.exists(folder_path):
|
| 43 |
os.makedirs(folder_path, exist_ok=True)
|
| 44 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
for root, _, files in os.walk(folder_path):
|
| 46 |
for filename in files:
|
|
|
|
|
|
|
| 47 |
file_path = os.path.join(root, filename)
|
| 48 |
+
filename_lower = filename.lower()
|
| 49 |
+
try:
|
| 50 |
+
if filename_lower.endswith(".pdf"):
|
| 51 |
+
loader = PyPDFLoader(file_path)
|
| 52 |
+
docs = loader.load()
|
| 53 |
+
for d in docs: d.metadata["source"] = filename
|
| 54 |
+
documents.extend(docs)
|
| 55 |
+
elif filename_lower.endswith(".docx"):
|
| 56 |
+
text = docx2txt.process(file_path)
|
| 57 |
+
if text.strip(): documents.append(Document(page_content=text, metadata={"source": filename}))
|
| 58 |
+
elif filename_lower.endswith((".xlsx", ".xls", ".csv")):
|
| 59 |
+
# Xử lý đơn giản cho excel/csv
|
| 60 |
+
if filename_lower.endswith(".csv"): df = pd.read_csv(file_path)
|
| 61 |
+
else: df = pd.read_excel(file_path)
|
| 62 |
+
text_data = "\n".join([" | ".join(f"{c}:{v}" for c,v in r.items() if pd.notna(v)) for _, r in df.iterrows()])
|
| 63 |
+
if text_data.strip(): documents.append(Document(page_content=text_data, metadata={"source": filename}))
|
| 64 |
+
elif filename_lower.endswith((".txt", ".md")):
|
| 65 |
+
with open(file_path, "r", encoding="utf-8") as f: text = f.read()
|
| 66 |
+
if text.strip(): documents.append(Document(page_content=text, metadata={"source": filename}))
|
| 67 |
+
except Exception as e:
|
| 68 |
+
logging.error(f"Lỗi file {filename}: {e}")
|
| 69 |
+
return documents
|
| 70 |
+
|
| 71 |
+
def build_vectorstore_and_corpus(embedding_model):
|
| 72 |
+
from shutil import rmtree
|
| 73 |
+
splits: list[Document] = []
|
| 74 |
+
vectorstore = None
|
| 75 |
+
|
| 76 |
+
if os.path.exists(DB_PATH) and os.listdir(DB_PATH):
|
| 77 |
+
try:
|
| 78 |
+
vectorstore = Chroma(persist_directory=DB_PATH, embedding_function=embedding_model)
|
| 79 |
+
existing = vectorstore.get()
|
| 80 |
+
if existing.get("documents"):
|
| 81 |
+
for text, meta in zip(existing["documents"], existing["metadatas"]):
|
| 82 |
+
splits.append(Document(page_content=text, metadata=meta))
|
| 83 |
+
logging.info(f"Đã load {len(splits)} chunks từ DB cũ.")
|
| 84 |
+
else:
|
| 85 |
+
splits = []
|
| 86 |
+
except Exception:
|
| 87 |
+
rmtree(DB_PATH, ignore_errors=True)
|
| 88 |
+
splits = []
|
| 89 |
|
| 90 |
+
if not splits:
|
| 91 |
+
logging.info("--- Tạo index mới ---")
|
| 92 |
+
documents = load_documents_from_folder(DATA_PATH)
|
| 93 |
+
if not documents: return None, []
|
|
|
|
| 94 |
|
| 95 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=150)
|
| 96 |
+
splits = text_splitter.split_documents(documents)
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
vectorstore = Chroma.from_documents(documents=splits, embedding=embedding_model, persist_directory=DB_PATH)
|
| 99 |
+
|
| 100 |
+
return vectorstore, splits
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
def get_retriever():
|
| 103 |
+
logging.info("--- Tải Embedding Model ---")
|
| 104 |
+
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
| 105 |
+
|
| 106 |
+
vectorstore, splits = build_vectorstore_and_corpus(embedding_model)
|
| 107 |
+
if not vectorstore: return None
|
| 108 |
|
| 109 |
+
if USE_MMR:
|
| 110 |
+
base_retriever = vectorstore.as_retriever(search_type="mmr", search_kwargs={"k": 10})
|
| 111 |
+
else:
|
| 112 |
+
base_retriever = vectorstore.as_retriever(search_kwargs={"k": 10})
|
| 113 |
+
|
| 114 |
+
if USE_BM25:
|
| 115 |
+
bm25_retriever = BM25Retriever.from_documents(splits)
|
| 116 |
+
bm25_retriever.k = 10
|
| 117 |
+
ensemble_retriever = EnsembleRetriever(
|
| 118 |
+
retrievers=[bm25_retriever, base_retriever],
|
| 119 |
+
weights=[0.4, 0.6]
|
| 120 |
+
)
|
| 121 |
+
first_stage_retriever = ensemble_retriever
|
| 122 |
+
else:
|
| 123 |
+
first_stage_retriever = base_retriever
|
| 124 |
|
| 125 |
+
logging.info("--- Tải Reranker Model (BGE-M3) ---")
|
| 126 |
+
reranker_model = HuggingFaceCrossEncoder(model_name="BAAI/bge-reranker-v2-m3")
|
| 127 |
+
|
| 128 |
+
compressor = CrossEncoderReranker(model=reranker_model, top_n=5)
|
| 129 |
+
|
| 130 |
+
compression_retriever = ContextualCompressionRetriever(
|
| 131 |
+
base_compressor=compressor,
|
| 132 |
+
base_retriever=first_stage_retriever
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
logging.info("Đã tích hợp Reranker thành công.")
|
| 136 |
+
return compression_retriever
|
| 137 |
|
| 138 |
class DeepMedBot:
|
| 139 |
def __init__(self):
|
| 140 |
self.retriever = None
|
| 141 |
+
self.rag_chain = None
|
|
|
|
| 142 |
self.ready = False
|
| 143 |
|
| 144 |
if not GOOGLE_API_KEY:
|
| 145 |
+
logging.error("Thiếu GOOGLE_API_KEY")
|
| 146 |
return
|
| 147 |
+
|
| 148 |
try:
|
| 149 |
+
self.retriever = get_retriever()
|
| 150 |
+
if not self.retriever: return
|
| 151 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
self.llm = ChatGoogleGenerativeAI(
|
| 153 |
model="gemini-2.5-flash",
|
| 154 |
temperature=0.2,
|
| 155 |
+
google_api_key=GOOGLE_API_KEY
|
|
|
|
| 156 |
)
|
|
|
|
| 157 |
self._build_chains()
|
| 158 |
self.ready = True
|
| 159 |
logging.info("Bot đã sẵn sàng!")
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
+
logging.error(f"Lỗi init bot: {e}")
|
| 162 |
+
logging.debug(traceback.format_exc())
|
| 163 |
|
| 164 |
def _build_chains(self):
|
| 165 |
+
contextualize_q_system_prompt = (
|
| 166 |
+
"Dựa trên lịch sử trò chuyện và câu hỏi mới nhất, "
|
| 167 |
+
"hãy viết lại câu hỏi thành một câu hoàn chỉnh nếu cần. "
|
| 168 |
+
"Nếu không liên quan, giữ nguyên. KHÔNG trả lời, chỉ viết lại."
|
|
|
|
| 169 |
)
|
| 170 |
+
contextualize_q_prompt = ChatPromptTemplate.from_messages([
|
| 171 |
+
("system", contextualize_q_system_prompt),
|
| 172 |
MessagesPlaceholder("chat_history"),
|
| 173 |
("human", "{input}"),
|
| 174 |
])
|
| 175 |
+
history_aware_retriever = create_history_aware_retriever(
|
| 176 |
+
self.llm, self.retriever, contextualize_q_prompt
|
| 177 |
+
)
|
| 178 |
|
|
|
|
| 179 |
qa_system_prompt = (
|
| 180 |
+
"Bạn là trợ lý y tế DeepMed. Dựa vào Context sau để trả lời.\n"
|
| 181 |
"Context:\n{context}\n\n"
|
| 182 |
+
"Yêu cầu:\n"
|
| 183 |
+
"- Chỉ trả lời dựa trên Context. Nếu không có thông tin, nói 'Tôi không tìm thấy thông tin trong tài liệu'.\n"
|
| 184 |
+
"- Trả lời ngắn gọn, có cấu trúc.\n"
|
|
|
|
| 185 |
)
|
| 186 |
qa_prompt = ChatPromptTemplate.from_messages([
|
| 187 |
("system", qa_system_prompt),
|
| 188 |
+
MessagesPlaceholder("chat_history"),
|
| 189 |
+
("human", "{input}"),
|
| 190 |
])
|
| 191 |
|
| 192 |
+
question_answer_chain = create_stuff_documents_chain(self.llm, qa_prompt)
|
| 193 |
+
self.rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
|
| 194 |
|
| 195 |
def chat_stream(self, message: str, history: list):
|
| 196 |
+
"""
|
| 197 |
+
Hàm Generator dùng cho Streaming
|
| 198 |
+
"""
|
| 199 |
if not self.ready:
|
| 200 |
+
yield "Hệ thống đang khởi động hoặc lỗi dữ liệu."
|
| 201 |
return
|
| 202 |
|
| 203 |
+
chat_history = []
|
| 204 |
+
for u, b in history[-MAX_HISTORY_TURNS:]:
|
| 205 |
+
chat_history.append(HumanMessage(content=u))
|
| 206 |
+
chat_history.append(AIMessage(content=b))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
full_response = ""
|
| 209 |
+
retrieved_docs = []
|
| 210 |
|
| 211 |
try:
|
| 212 |
+
for chunk in self.rag_chain.stream({"input": message, "chat_history": chat_history}):
|
| 213 |
+
if "answer" in chunk:
|
| 214 |
+
content = chunk["answer"]
|
| 215 |
+
full_response += content
|
| 216 |
+
yield full_response
|
| 217 |
+
|
| 218 |
+
if "context" in chunk:
|
| 219 |
+
retrieved_docs = chunk["context"]
|
| 220 |
+
|
| 221 |
+
if retrieved_docs:
|
| 222 |
+
refs = self._build_references_text(retrieved_docs)
|
| 223 |
+
if refs:
|
| 224 |
+
full_response += f"\n\n---\n📚 **Nguồn:**\n{refs}"
|
| 225 |
+
yield full_response
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
except Exception as e:
|
| 228 |
+
logging.error(f"Lỗi stream: {e}")
|
| 229 |
+
yield "Gặp lỗi trong quá trình xử lý."
|
| 230 |
|
| 231 |
@staticmethod
|
| 232 |
+
def _build_references_text(docs) -> str:
|
| 233 |
+
from collections import defaultdict
|
| 234 |
+
source_pages = defaultdict(set)
|
| 235 |
for doc in docs:
|
| 236 |
src = os.path.basename(doc.metadata.get("source", "Tài liệu"))
|
|
|
|
| 237 |
if "page" in doc.metadata:
|
| 238 |
+
source_pages[src].add(doc.metadata["page"] + 1)
|
| 239 |
+
else:
|
| 240 |
+
source_pages[src]
|
| 241 |
|
| 242 |
+
lines = []
|
| 243 |
+
for src, pages in source_pages.items():
|
| 244 |
+
if pages:
|
| 245 |
+
p_str = ", ".join(str(p) for p in sorted(pages))
|
| 246 |
+
lines.append(f"- {src} (Trang {p_str})")
|
| 247 |
+
else:
|
| 248 |
+
lines.append(f"- {src}")
|
| 249 |
+
return "\n".join(lines)
|
| 250 |
|
| 251 |
bot = DeepMedBot()
|
| 252 |
|
| 253 |
+
def gradio_chat_stream(message, history):
|
|
|
|
| 254 |
for partial_response in bot.chat_stream(message, history):
|
| 255 |
yield partial_response
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
demo = gr.ChatInterface(
|
| 258 |
+
fn=gradio_chat_stream,
|
| 259 |
+
title="🏥 DeepMed AI - Trợ lý Y tế tại Trung Tâm Y Tế Khu Vực Thanh Ba",
|
| 260 |
)
|
| 261 |
|
| 262 |
if __name__ == "__main__":
|
| 263 |
+
demo.launch()
|