init project
Browse files
app.py
CHANGED
|
@@ -1,82 +1,42 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
import logging
|
|
|
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
-
|
| 6 |
-
from rag_core
|
| 7 |
-
from rag_core.retriever import Retriever
|
| 8 |
-
from rag_core.llm import generate_answer
|
| 9 |
|
| 10 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 11 |
|
| 12 |
-
logging.info("🔄 Khởi tạo hệ thống...")
|
| 13 |
-
|
| 14 |
-
retriever = Retriever()
|
| 15 |
app = FastAPI()
|
| 16 |
-
ready = retriever.index is not None
|
| 17 |
-
|
| 18 |
-
if ready:
|
| 19 |
-
logging.info("✅ FAISS index đã tồn tại. Sẵn sàng xử lý truy vấn.")
|
| 20 |
-
else:
|
| 21 |
-
logging.info("⚠️ FAISS index chưa tồn tại. Yêu cầu người dùng bấm nút 'Xây Index' để khởi tạo.")
|
| 22 |
|
|
|
|
| 23 |
@app.post("/ask")
|
| 24 |
async def ask_api(req: Request):
|
| 25 |
-
if not ready:
|
| 26 |
-
return {"error": "Index chưa sẵn sàng. Vui lòng thử lại sau."}
|
| 27 |
data = await req.json()
|
| 28 |
-
|
| 29 |
-
logging.info(f"📥 Nhận câu hỏi: {query}")
|
| 30 |
-
docs = retriever.query(query, get_embedding)
|
| 31 |
-
prompt = "\n\n".join(docs) + f"\n\nCâu hỏi: {query}\nTrả lời:"
|
| 32 |
-
answer = generate_answer(prompt)
|
| 33 |
-
return {"answer": answer}
|
| 34 |
|
| 35 |
@app.post("/rescan")
|
| 36 |
async def rescan_api():
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
retriever.rescan_and_append(chunks, get_embedding)
|
| 44 |
-
logging.info("✅ Rescan hoàn tất.")
|
| 45 |
-
return {"status": "Rescan & update thành công."}
|
| 46 |
|
| 47 |
def build_index_ui():
|
| 48 |
-
|
| 49 |
-
logging.info("⚙️ Người dùng yêu cầu xây FAISS index từ UI...")
|
| 50 |
-
with gr.Textbox(visible=False):
|
| 51 |
-
pass # trigger
|
| 52 |
-
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 53 |
-
text = f.read()
|
| 54 |
-
chunks = chunk_legal_text(text)
|
| 55 |
-
retriever.build(chunks, get_embedding)
|
| 56 |
-
ready = True
|
| 57 |
-
logging.info("✅ Đã xây dựng FAISS index từ UI.")
|
| 58 |
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
| 59 |
|
| 60 |
-
def answer_fn(query):
|
| 61 |
-
if not ready:
|
| 62 |
-
return "Index chưa sẵn sàng. Vui lòng chờ hoàn tất xử lý."
|
| 63 |
-
logging.info(f"📨 Truy vấn từ UI: {query}")
|
| 64 |
-
docs = retriever.query(query, get_embedding)
|
| 65 |
-
prompt = "\n\n".join(docs) + f"\n\nCâu hỏi: {query}\nTrả lời:"
|
| 66 |
-
return generate_answer(prompt)
|
| 67 |
-
|
| 68 |
with gr.Blocks() as iface:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
output_box = gr.Textbox(label="Trả lời", visible=ready)
|
| 73 |
query_box.submit(fn=answer_fn, inputs=query_box, outputs=output_box)
|
| 74 |
build_btn.click(fn=build_index_ui, outputs=[build_btn, query_box, output_box])
|
| 75 |
|
| 76 |
-
# Mount Gradio lên FastAPI
|
| 77 |
app = gr.mount_gradio_app(app, iface, path="/")
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
logging.info("🚀 Khởi chạy FastAPI + Gradio tại cổng 7860")
|
| 81 |
-
import uvicorn
|
| 82 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
| 1 |
import logging
|
| 2 |
+
import gradio as gr
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
+
import uvicorn
|
| 5 |
+
from rag_core import business
|
|
|
|
|
|
|
| 6 |
|
| 7 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# === API ===
|
| 12 |
@app.post("/ask")
|
| 13 |
async def ask_api(req: Request):
|
|
|
|
|
|
|
| 14 |
data = await req.json()
|
| 15 |
+
return business.answer_query(data.get("query"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
@app.post("/rescan")
|
| 18 |
async def rescan_api():
|
| 19 |
+
return business.rescan_index()
|
| 20 |
+
|
| 21 |
+
# === Gradio UI ===
|
| 22 |
+
def answer_fn(query):
|
| 23 |
+
result = business.answer_query(query)
|
| 24 |
+
return result.get("answer", result.get("error", "Lỗi không xác định."))
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def build_index_ui():
|
| 27 |
+
business.build_index()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
with gr.Blocks() as iface:
|
| 31 |
+
build_btn = gr.Button("🔄 Xây Index", visible=not business.ready)
|
| 32 |
+
query_box = gr.Textbox(label="Nhập câu hỏi", visible=business.ready)
|
| 33 |
+
output_box = gr.Textbox(label="Trả lời", visible=business.ready)
|
|
|
|
| 34 |
query_box.submit(fn=answer_fn, inputs=query_box, outputs=output_box)
|
| 35 |
build_btn.click(fn=build_index_ui, outputs=[build_btn, query_box, output_box])
|
| 36 |
|
| 37 |
+
# Mount Gradio lên FastAPI
|
| 38 |
app = gr.mount_gradio_app(app, iface, path="/")
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
logging.info("🚀 Khởi chạy FastAPI + Gradio tại cổng 7860")
|
|
|
|
| 42 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|