Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,8 @@ import torch
|
|
6 |
from huggingface_hub import login
|
7 |
from fastapi import FastAPI, Request
|
8 |
from fastapi.responses import JSONResponse
|
|
|
|
|
9 |
|
10 |
# Cấu hình logging
|
11 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -56,7 +58,7 @@ async def api_answer(request: Request):
|
|
56 |
logging.error(f"API error: {e}")
|
57 |
return JSONResponse({"error": str(e)}, status_code=500)
|
58 |
|
59 |
-
# Tạo Gradio Blocks
|
60 |
with gr.Blocks() as demo:
|
61 |
gr.Markdown("# AgriBot: Hỏi đáp nông nghiệp")
|
62 |
gr.Markdown("Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp.")
|
@@ -68,12 +70,17 @@ with gr.Blocks() as demo:
|
|
68 |
submit_btn = gr.Button("Gửi")
|
69 |
submit_btn.click(fn=gradio_answer, inputs=[question_input, context_input], outputs=output)
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
|
|
74 |
|
75 |
# Chạy ứng dụng
|
76 |
if __name__ == "__main__":
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
79 |
uvicorn.run(app, host="0.0.0.0", port=80)
|
|
|
6 |
from huggingface_hub import login
|
7 |
from fastapi import FastAPI, Request
|
8 |
from fastapi.responses import JSONResponse
|
9 |
+
import uvicorn
|
10 |
+
import threading
|
11 |
|
12 |
# Cấu hình logging
|
13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
58 |
logging.error(f"API error: {e}")
|
59 |
return JSONResponse({"error": str(e)}, status_code=500)
|
60 |
|
61 |
+
# Tạo Gradio Blocks
|
62 |
with gr.Blocks() as demo:
|
63 |
gr.Markdown("# AgriBot: Hỏi đáp nông nghiệp")
|
64 |
gr.Markdown("Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp.")
|
|
|
70 |
submit_btn = gr.Button("Gửi")
|
71 |
submit_btn.click(fn=gradio_answer, inputs=[question_input, context_input], outputs=output)
|
72 |
|
73 |
+
# Hàm chạy Gradio
|
74 |
+
def run_gradio():
|
75 |
+
logging.info("Starting Gradio on port 7860...")
|
76 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, show_error=True)
|
77 |
|
78 |
# Chạy ứng dụng
|
79 |
if __name__ == "__main__":
|
80 |
+
# Chạy Gradio trong một thread riêng
|
81 |
+
gradio_thread = threading.Thread(target=run_gradio)
|
82 |
+
gradio_thread.start()
|
83 |
+
|
84 |
+
# Chạy FastAPI trên port chính (80 cho Hugging Face Spaces)
|
85 |
+
logging.info("Starting FastAPI on port 80...")
|
86 |
uvicorn.run(app, host="0.0.0.0", port=80)
|