File size: 1,350 Bytes
ec666c4
5723ef4
 
af30c7d
 
7bc48c0
 
 
1106120
c6d8421
 
25d4844
1106120
25d4844
ec666c4
 
25d4844
c6d8421
25d4844
ec666c4
25d4844
d01f520
c6d8421
25d4844
d01f520
7bc48c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af30c7d
 
 
7bc48c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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."}

# Mount Gradio UI vào FastAPI tại root
app = gr.mount_gradio_app(app, app_ui, path="")

logging.info("✅ Gradio UI đã mount vào root /")