init project
Browse files
app.py
CHANGED
|
@@ -7,16 +7,26 @@ from rag_core.embedder import get_embedding
|
|
| 7 |
from rag_core.retriever import Retriever
|
| 8 |
from rag_core.llm import generate_answer
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
retriever = Retriever()
|
| 11 |
app = FastAPI()
|
| 12 |
ready = retriever.index is not None
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.post("/ask")
|
| 15 |
async def ask_api(req: Request):
|
| 16 |
if not ready:
|
| 17 |
return {"error": "Index chưa sẵn sàng. Vui lòng thử lại sau."}
|
| 18 |
data = await req.json()
|
| 19 |
query = data.get("query")
|
|
|
|
| 20 |
docs = retriever.query(query, get_embedding)
|
| 21 |
prompt = "\n\n".join(docs) + f"\n\nCâu hỏi: {query}\nTrả lời:"
|
| 22 |
answer = generate_answer(prompt)
|
|
@@ -26,14 +36,17 @@ async def ask_api(req: Request):
|
|
| 26 |
async def rescan_api():
|
| 27 |
if not ready:
|
| 28 |
return {"error": "Index chưa sẵn sàng."}
|
|
|
|
| 29 |
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 30 |
text = f.read()
|
| 31 |
chunks = chunk_legal_text(text)
|
| 32 |
retriever.rescan_and_append(chunks, get_embedding)
|
|
|
|
| 33 |
return {"status": "Rescan & update thành công."}
|
| 34 |
|
| 35 |
def build_index_ui():
|
| 36 |
global ready
|
|
|
|
| 37 |
with gr.Textbox(visible=False):
|
| 38 |
pass # trigger
|
| 39 |
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
|
@@ -41,16 +54,19 @@ def build_index_ui():
|
|
| 41 |
chunks = chunk_legal_text(text)
|
| 42 |
retriever.build(chunks, get_embedding)
|
| 43 |
ready = True
|
|
|
|
| 44 |
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
| 45 |
|
| 46 |
def answer_fn(query):
|
| 47 |
if not ready:
|
| 48 |
return "Index chưa sẵn sàng. Vui lòng chờ hoàn tất xử lý."
|
|
|
|
| 49 |
docs = retriever.query(query, get_embedding)
|
| 50 |
prompt = "\n\n".join(docs) + f"\n\nCâu hỏi: {query}\nTrả lời:"
|
| 51 |
return generate_answer(prompt)
|
| 52 |
|
| 53 |
with gr.Blocks() as iface:
|
|
|
|
| 54 |
build_btn = gr.Button("🔄 Xây Index", visible=not ready)
|
| 55 |
query_box = gr.Textbox(label="Nhập câu hỏi", visible=ready)
|
| 56 |
output_box = gr.Textbox(label="Trả lời", visible=ready)
|
|
@@ -61,5 +77,6 @@ with gr.Blocks() as iface:
|
|
| 61 |
app = gr.mount_gradio_app(app, iface, path="/")
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
|
|
|
| 64 |
import uvicorn
|
| 65 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 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 |
query = data.get("query")
|
| 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)
|
|
|
|
| 36 |
async def rescan_api():
|
| 37 |
if not ready:
|
| 38 |
return {"error": "Index chưa sẵn sàng."}
|
| 39 |
+
logging.info("🔎 Bắt đầu rescan và bổ sung embedding còn thiếu...")
|
| 40 |
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 41 |
text = f.read()
|
| 42 |
chunks = chunk_legal_text(text)
|
| 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 |
global ready
|
| 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:
|
|
|
|
| 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 |
+
logging.info("🎨 Đang khởi tạo giao diện Gradio...")
|
| 70 |
build_btn = gr.Button("🔄 Xây Index", visible=not ready)
|
| 71 |
query_box = gr.Textbox(label="Nhập câu hỏi", visible=ready)
|
| 72 |
output_box = gr.Textbox(label="Trả lời", visible=ready)
|
|
|
|
| 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)
|