Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,8 @@ import gradio as gr
|
|
5 |
import os
|
6 |
import torch
|
7 |
from huggingface_hub import login
|
|
|
|
|
8 |
|
9 |
# Cấu hình logging
|
10 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -35,7 +37,7 @@ def gradio_answer(question, context):
|
|
35 |
result = qa_pipeline(question=question, context=context)
|
36 |
return result["answer"]
|
37 |
|
38 |
-
# Tạo Flask app
|
39 |
app = Flask(__name__)
|
40 |
|
41 |
@app.route("/api/answer", methods=["POST"])
|
@@ -55,14 +57,19 @@ def answer():
|
|
55 |
logging.error(f"API error: {e}")
|
56 |
return jsonify({"error": str(e)}), 500
|
57 |
|
58 |
-
# Tạo Gradio
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
)
|
|
|
66 |
|
67 |
-
#
|
68 |
-
|
|
|
|
|
|
|
|
|
|
5 |
import os
|
6 |
import torch
|
7 |
from huggingface_hub import login
|
8 |
+
from werkzeug.middleware.dispatcher import DispatcherMiddleware
|
9 |
+
from werkzeug.serving import run_simple
|
10 |
|
11 |
# Cấu hình logging
|
12 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
37 |
result = qa_pipeline(question=question, context=context)
|
38 |
return result["answer"]
|
39 |
|
40 |
+
# Tạo Flask app cho API
|
41 |
app = Flask(__name__)
|
42 |
|
43 |
@app.route("/api/answer", methods=["POST"])
|
|
|
57 |
logging.error(f"API error: {e}")
|
58 |
return jsonify({"error": str(e)}), 500
|
59 |
|
60 |
+
# Tạo Gradio Blocks
|
61 |
+
with gr.Blocks() as demo:
|
62 |
+
gr.Markdown("# AgriBot: Hỏi đáp nông nghiệp")
|
63 |
+
gr.Markdown("Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp.")
|
64 |
+
question_input = gr.Textbox(label="Câu hỏi", placeholder="Nhập câu hỏi của bạn...")
|
65 |
+
context_input = gr.Textbox(label="Ngữ cảnh", placeholder="Nhập ngữ cảnh liên quan...")
|
66 |
+
output = gr.Textbox(label="Câu trả lời")
|
67 |
+
submit_btn = gr.Button("Gửi")
|
68 |
+
submit_btn.click(fn=gradio_answer, inputs=[question_input, context_input], outputs=output)
|
69 |
|
70 |
+
# Tích hợp Flask và Gradio trên cùng port
|
71 |
+
application = DispatcherMiddleware(demo.app, {"/api": app})
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
logging.info("Starting Gradio and Flask on port 7860...")
|
75 |
+
run_simple("0.0.0.0", 7860, application)
|