Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,10 @@
|
|
1 |
import logging
|
2 |
from flask import Flask, request, jsonify
|
3 |
-
from transformers import pipeline
|
4 |
import gradio as gr
|
|
|
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,39 +35,38 @@ def gradio_answer(question, context):
|
|
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"])
|
44 |
-
def answer():
|
45 |
-
try:
|
46 |
-
data = request.json
|
47 |
-
question = data.get("question")
|
48 |
-
context = data.get("context")
|
49 |
-
logging.info(f"Received request - Question: {question}, Context: {context[:200]}...")
|
50 |
-
if not question or not context:
|
51 |
-
logging.error("Missing question or context")
|
52 |
-
return jsonify({"error": "Missing question or context"}), 400
|
53 |
-
result = qa_pipeline(question=question, context=context)
|
54 |
-
logging.info(f"Response - Answer: {result['answer']}")
|
55 |
-
return jsonify({"answer": result["answer"]})
|
56 |
-
except Exception as e:
|
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 |
-
|
65 |
-
|
|
|
|
|
|
|
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 |
-
#
|
71 |
-
application = DispatcherMiddleware(demo.app, {"/api": app})
|
72 |
-
|
73 |
if __name__ == "__main__":
|
74 |
-
logging.info("Starting Gradio
|
75 |
-
|
|
|
1 |
import logging
|
2 |
from flask import Flask, request, jsonify
|
|
|
3 |
import gradio as gr
|
4 |
+
from transformers import pipeline
|
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 |
result = qa_pipeline(question=question, context=context)
|
36 |
return result["answer"]
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Tạo Gradio Blocks
|
39 |
with gr.Blocks() as demo:
|
40 |
gr.Markdown("# AgriBot: Hỏi đáp nông nghiệp")
|
41 |
gr.Markdown("Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp.")
|
42 |
+
|
43 |
+
# Giao diện Gradio
|
44 |
+
with gr.Row():
|
45 |
+
question_input = gr.Textbox(label="Câu hỏi", placeholder="Nhập câu hỏi của bạn...")
|
46 |
+
context_input = gr.Textbox(label="Ngữ cảnh", placeholder="Nhập ngữ cảnh liên quan...")
|
47 |
output = gr.Textbox(label="Câu trả lời")
|
48 |
submit_btn = gr.Button("Gửi")
|
49 |
submit_btn.click(fn=gradio_answer, inputs=[question_input, context_input], outputs=output)
|
50 |
+
|
51 |
+
# Endpoint API
|
52 |
+
@demo.route("/api/answer", methods=["POST"])
|
53 |
+
async def api_answer(request):
|
54 |
+
try:
|
55 |
+
data = await request.json()
|
56 |
+
question = data.get("question")
|
57 |
+
context = data.get("context")
|
58 |
+
logging.info(f"Received request - Question: {question}, Context: {context[:200]}...")
|
59 |
+
if not question or not context:
|
60 |
+
logging.error("Missing question or context")
|
61 |
+
return jsonify({"error": "Missing question or context"}), 400
|
62 |
+
result = qa_pipeline(question=question, context=context)
|
63 |
+
logging.info(f"Response - Answer: {result['answer']}")
|
64 |
+
return jsonify({"answer": result["answer"]})
|
65 |
+
except Exception as e:
|
66 |
+
logging.error(f"API error: {e}")
|
67 |
+
return jsonify({"error": str(e)}), 500
|
68 |
|
69 |
+
# Chạy ứng dụng
|
|
|
|
|
70 |
if __name__ == "__main__":
|
71 |
+
logging.info("Starting Gradio on port 7860...")
|
72 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, inline=False)
|