VietCat commited on
Commit
4487291
·
1 Parent(s): 3317364
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -2,23 +2,24 @@ import logging
2
  import gradio as gr
3
  from fastapi import FastAPI, Request
4
  from rag_core import business
 
5
  import uvicorn
6
 
7
  logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
8
 
9
- app = FastAPI()
 
10
 
11
- # --- API Routes ---
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 logic ---
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."))
@@ -34,10 +35,11 @@ with gr.Blocks() as iface:
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 vào ROOT path "/"
38
- app = gr.mount_gradio_app(app, iface, path="/")
 
39
 
40
- # Chạy server khi standalone
41
- if __name__ == "__main__":
42
- logging.info("🚀 Khởi chạy FastAPI + Gradio tại cổng 7860")
43
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
2
  import gradio as gr
3
  from fastapi import FastAPI, Request
4
  from rag_core import business
5
+ import threading
6
  import uvicorn
7
 
8
  logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
9
 
10
+ # FastAPI dùng cho API nếu cần gọi từ backend hoặc automation
11
+ api_app = FastAPI()
12
 
13
+ @api_app.post("/ask")
 
14
  async def ask_api(req: Request):
15
  data = await req.json()
16
  return business.answer_query(data.get("query"))
17
 
18
+ @api_app.post("/rescan")
19
  async def rescan_api():
20
  return business.rescan_index()
21
 
22
+ # Gradio UI
23
  def answer_fn(query):
24
  result = business.answer_query(query)
25
  return result.get("answer", result.get("error", "Lỗi không xác định."))
 
35
  query_box.submit(fn=answer_fn, inputs=query_box, outputs=output_box)
36
  build_btn.click(fn=build_index_ui, outputs=[build_btn, query_box, output_box])
37
 
38
+ # Chạy FastAPI trong thread phụ (nếu cần gọi /ask /rescan qua HTTP)
39
+ def start_fastapi():
40
+ uvicorn.run(api_app, host="0.0.0.0", port=7861)
41
 
42
+ threading.Thread(target=start_fastapi).start()
43
+
44
+ # 🚀 Gradio chạy app chính (bắt buộc với Hugging Face)
45
+ iface.launch(server_name="0.0.0.0", server_port=7860)