|
|
from fastapi import FastAPI, Request |
|
|
from rag_core.business import answer_query, rescan_index |
|
|
from ui import app_ui |
|
|
import gradio as gr |
|
|
import logging |
|
|
from fastapi.responses import JSONResponse |
|
|
import json |
|
|
import os |
|
|
|
|
|
logging.info("🚀 Khởi động ứng dụng FastAPI...") |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.post("/ask") |
|
|
async def ask_api(req: Request): |
|
|
data = await req.json() |
|
|
query = data.get("query", "") |
|
|
logging.info(f"📥 API /ask nhận query: {query}") |
|
|
return answer_query(query) |
|
|
|
|
|
@app.post("/rescan") |
|
|
async def rescan_api(): |
|
|
logging.info("♻️ API /rescan được gọi") |
|
|
return rescan_index() |
|
|
|
|
|
@app.get("/get_structure_file") |
|
|
def get_structure_file(): |
|
|
path = "faiss_index/chunk_structure.json" |
|
|
if os.path.exists(path): |
|
|
try: |
|
|
with open(path, "r", encoding="utf-8") as f: |
|
|
data = json.load(f) |
|
|
return JSONResponse(content=data) |
|
|
except Exception as e: |
|
|
logging.error(f"❌ Lỗi đọc file JSON: {e}") |
|
|
return {"error": f"Lỗi đọc file: {str(e)}"} |
|
|
else: |
|
|
logging.warning("⚠️ File chunk_structure.json không tồn tại.") |
|
|
return {"error": "File không tồn tại."} |
|
|
|
|
|
|
|
|
app = gr.mount_gradio_app(app, app_ui, path="") |
|
|
|
|
|
logging.info("✅ Gradio UI đã mount vào root /") |
|
|
|