fix UI
Browse files- app.py +5 -21
- rag_core/business.py +1 -1
- ui.py +33 -0
app.py
CHANGED
|
@@ -1,10 +1,6 @@
|
|
| 1 |
-
import logging
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 7 |
-
logging.info("🚀 Khởi chạy FastAPI + Gradio tại cổng 7860")
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
@@ -18,18 +14,6 @@ async def ask_api(req: Request):
|
|
| 18 |
async def rescan_api():
|
| 19 |
return rescan_index()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
query_box = gr.Textbox(label="Nhập câu hỏi")
|
| 25 |
-
output_box = gr.Textbox(label="Trả lời")
|
| 26 |
-
|
| 27 |
-
build_btn.click(fn=lambda: build_index() or "✅ Đã xây xong Index.", outputs=None)
|
| 28 |
-
query_box.submit(fn=lambda q: answer_query(q)["answer"], inputs=query_box, outputs=output_box)
|
| 29 |
-
|
| 30 |
-
return ui
|
| 31 |
-
|
| 32 |
-
# Mount Gradio vào FastAPI
|
| 33 |
-
app = gr.mount_gradio_app(app, gradio_ui(), path="/")
|
| 34 |
-
|
| 35 |
-
# Run với: uvicorn app:app --host 0.0.0.0 --port 7860
|
|
|
|
|
|
|
| 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 |
async def rescan_api():
|
| 15 |
return rescan_index()
|
| 16 |
|
| 17 |
+
# Gắn Gradio UI vào FastAPI
|
| 18 |
+
import gradio as gr
|
| 19 |
+
app = gr.mount_gradio_app(app, app_ui, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rag_core/business.py
CHANGED
|
@@ -45,7 +45,7 @@ def rescan_index():
|
|
| 45 |
logging.info("🔄 Thực hiện rescan & bổ sung FAISS index.")
|
| 46 |
if not ready:
|
| 47 |
logging.warning("⚠️ Index chưa sẵn sàng.")
|
| 48 |
-
|
| 49 |
try:
|
| 50 |
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 51 |
text = f.read()
|
|
|
|
| 45 |
logging.info("🔄 Thực hiện rescan & bổ sung FAISS index.")
|
| 46 |
if not ready:
|
| 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()
|
ui.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import logging
|
| 3 |
+
from rag_core.business import build_index, rescan_index, answer_query, retriever, ready
|
| 4 |
+
|
| 5 |
+
logging.info("🎨 Khởi tạo Gradio UI...")
|
| 6 |
+
|
| 7 |
+
def handle_rebuild():
|
| 8 |
+
if retriever.index is None:
|
| 9 |
+
logging.info("⚙️ Chưa có index, gọi build_index()")
|
| 10 |
+
build_index()
|
| 11 |
+
return "✅ Đã tạo mới FAISS index."
|
| 12 |
+
else:
|
| 13 |
+
logging.info("♻️ Đã có index, gọi rescan_index()")
|
| 14 |
+
result = rescan_index()
|
| 15 |
+
return result["status"]
|
| 16 |
+
|
| 17 |
+
def handle_query(query):
|
| 18 |
+
result = answer_query(query)
|
| 19 |
+
return result.get("answer", result.get("error", "❌ Lỗi không xác định."))
|
| 20 |
+
|
| 21 |
+
with gr.Blocks(title="Luật Giao Thông RAG") as iface:
|
| 22 |
+
gr.Markdown("## 📚 Trợ lý hỏi đáp văn bản luật")
|
| 23 |
+
|
| 24 |
+
query_box = gr.Textbox(label="❓ Nhập câu hỏi", lines=2)
|
| 25 |
+
answer_box = gr.Textbox(label="💬 Trả lời", lines=6)
|
| 26 |
+
|
| 27 |
+
submit_btn = gr.Button("📤 Gửi câu hỏi")
|
| 28 |
+
rebuild_btn = gr.Button("🔄 Rebuild Index")
|
| 29 |
+
|
| 30 |
+
submit_btn.click(fn=handle_query, inputs=query_box, outputs=answer_box)
|
| 31 |
+
rebuild_btn.click(fn=handle_rebuild, outputs=answer_box)
|
| 32 |
+
|
| 33 |
+
app_ui = iface
|