fix UI
Browse files- app.py +6 -3
- rag_core/business.py +26 -25
- ui.py +1 -1
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from rag_core.business import answer_query, rescan_index
|
| 3 |
from ui import app_ui
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
@@ -14,6 +16,7 @@ async def ask_api(req: Request):
|
|
| 14 |
async def rescan_api():
|
| 15 |
return rescan_index()
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from rag_core.business import answer_query, rescan_index
|
| 3 |
from ui import app_ui
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import logging
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 16 |
async def rescan_api():
|
| 17 |
return rescan_index()
|
| 18 |
|
| 19 |
+
# Mount Gradio UI vào FastAPI tại root
|
| 20 |
+
app = gr.mount_gradio_app(app, app_ui, path="")
|
| 21 |
+
|
| 22 |
+
logging.info("✅ Gradio UI đã mount vào root /")
|
rag_core/business.py
CHANGED
|
@@ -6,55 +6,56 @@ from rag_core.retriever import Retriever
|
|
| 6 |
from rag_core.llm import generate_answer
|
| 7 |
|
| 8 |
retriever = Retriever()
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def build_index():
|
| 12 |
-
global ready
|
| 13 |
logging.info("🔄 Bắt đầu xây FAISS index từ đầu...")
|
| 14 |
try:
|
| 15 |
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 16 |
text = f.read()
|
| 17 |
-
logging.info("📄 Đọc
|
| 18 |
chunks = chunk_legal_text(text)
|
| 19 |
-
logging.info(f"
|
| 20 |
retriever.build(chunks, get_embedding)
|
| 21 |
-
|
| 22 |
-
logging.info("✅ Đã xây xong FAISS index.")
|
| 23 |
except Exception as e:
|
| 24 |
logging.exception(f"❌ Lỗi khi xây index: {e}")
|
| 25 |
-
raise
|
| 26 |
|
| 27 |
def answer_query(query):
|
| 28 |
-
logging.info("
|
| 29 |
-
if not
|
| 30 |
-
logging.warning("⚠️ Index chưa sẵn sàng.")
|
| 31 |
-
return {"error": "Index chưa sẵn sàng. Vui lòng bấm '
|
| 32 |
try:
|
| 33 |
-
logging.info(f"🔍 Truy vấn: {query}")
|
| 34 |
docs = retriever.query(query, get_embedding)
|
| 35 |
-
logging.info(f"📚
|
| 36 |
prompt = "\n\n".join(docs) + f"\n\nCâu hỏi: {query}\nTrả lời:"
|
| 37 |
answer = generate_answer(prompt)
|
| 38 |
-
logging.info("✅ Đã
|
| 39 |
return {"answer": answer}
|
| 40 |
except Exception as e:
|
| 41 |
-
logging.exception(f"❌ Lỗi khi
|
| 42 |
-
return {"error":
|
| 43 |
|
| 44 |
def rescan_index():
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
logging.warning("⚠️ Index chưa sẵn sàng.")
|
| 48 |
build_index()
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 51 |
text = f.read()
|
| 52 |
-
logging.info("📄 Đọc
|
| 53 |
chunks = chunk_legal_text(text)
|
| 54 |
-
logging.info(f"
|
| 55 |
retriever.rescan_and_append(chunks, get_embedding)
|
| 56 |
-
logging.info("✅
|
| 57 |
-
return {"status": "Rescan & update thành công."}
|
| 58 |
except Exception as e:
|
| 59 |
-
logging.exception(f"❌ Lỗi
|
| 60 |
-
return {"
|
|
|
|
| 6 |
from rag_core.llm import generate_answer
|
| 7 |
|
| 8 |
retriever = Retriever()
|
| 9 |
+
|
| 10 |
+
def is_ready():
|
| 11 |
+
ready = retriever.index is not None
|
| 12 |
+
logging.info(f"✅ Kiểm tra index sẵn sàng: {ready}")
|
| 13 |
+
return ready
|
| 14 |
|
| 15 |
def build_index():
|
|
|
|
| 16 |
logging.info("🔄 Bắt đầu xây FAISS index từ đầu...")
|
| 17 |
try:
|
| 18 |
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 19 |
text = f.read()
|
| 20 |
+
logging.info(f"📄 Đọc văn bản thành công, độ dài: {len(text)} ký tự")
|
| 21 |
chunks = chunk_legal_text(text)
|
| 22 |
+
logging.info(f"✂️ Đã chunk thành {len(chunks)} đoạn")
|
| 23 |
retriever.build(chunks, get_embedding)
|
| 24 |
+
logging.info("✅ Xây FAISS index thành công.")
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
logging.exception(f"❌ Lỗi khi xây index: {e}")
|
|
|
|
| 27 |
|
| 28 |
def answer_query(query):
|
| 29 |
+
logging.info(f"❓ Nhận câu hỏi: {query}")
|
| 30 |
+
if not is_ready():
|
| 31 |
+
logging.warning("⚠️ Index chưa sẵn sàng khi truy vấn.")
|
| 32 |
+
return {"error": "Index chưa sẵn sàng. Vui lòng bấm 'Rebuild Index'."}
|
| 33 |
try:
|
|
|
|
| 34 |
docs = retriever.query(query, get_embedding)
|
| 35 |
+
logging.info(f"📚 Truy xuất được {len(docs)} đoạn liên quan")
|
| 36 |
prompt = "\n\n".join(docs) + f"\n\nCâu hỏi: {query}\nTrả lời:"
|
| 37 |
answer = generate_answer(prompt)
|
| 38 |
+
logging.info("✅ Đã tạo câu trả lời thành công.")
|
| 39 |
return {"answer": answer}
|
| 40 |
except Exception as e:
|
| 41 |
+
logging.exception(f"❌ Lỗi khi trả lời câu hỏi: {e}")
|
| 42 |
+
return {"error": str(e)}
|
| 43 |
|
| 44 |
def rescan_index():
|
| 45 |
+
if not is_ready():
|
| 46 |
+
logging.info("⚠️ Chưa có index, gọi build_index() thay vì rescan.")
|
|
|
|
| 47 |
build_index()
|
| 48 |
+
return {"status": "✅ Đã tạo mới FAISS index."}
|
| 49 |
+
logging.info("♻️ Tiến hành rescan và cập nhật các chunk mới...")
|
| 50 |
try:
|
| 51 |
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 52 |
text = f.read()
|
| 53 |
+
logging.info(f"📄 Đọc văn bản thành công, độ dài: {len(text)} ký tự")
|
| 54 |
chunks = chunk_legal_text(text)
|
| 55 |
+
logging.info(f"✂️ Đã chunk thành {len(chunks)} đoạn")
|
| 56 |
retriever.rescan_and_append(chunks, get_embedding)
|
| 57 |
+
logging.info("✅ Rescan hoàn tất.")
|
| 58 |
+
return {"status": "✅ Rescan & update thành công."}
|
| 59 |
except Exception as e:
|
| 60 |
+
logging.exception(f"❌ Lỗi trong quá trình rescan: {e}")
|
| 61 |
+
return {"status": f"Lỗi: {str(e)}"}
|
ui.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import logging
|
| 3 |
-
from rag_core.business import build_index, rescan_index, answer_query, retriever,
|
| 4 |
|
| 5 |
logging.info("🎨 Khởi tạo Gradio UI...")
|
| 6 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import logging
|
| 3 |
+
from rag_core.business import build_index, rescan_index, answer_query, retriever, is_ready
|
| 4 |
|
| 5 |
logging.info("🎨 Khởi tạo Gradio UI...")
|
| 6 |
|