Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
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,36 +36,37 @@ def gradio_answer(question, context):
|
|
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__":
|
|
|
1 |
import logging
|
|
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
import os
|
5 |
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')
|
|
|
36 |
result = qa_pipeline(question=question, context=context)
|
37 |
return result["answer"]
|
38 |
|
39 |
+
# Tạo FastAPI app để thêm endpoint API
|
40 |
+
app = FastAPI()
|
41 |
+
|
42 |
+
@app.post("/api/answer")
|
43 |
+
async def api_answer(request: Request):
|
44 |
+
try:
|
45 |
+
data = await request.json()
|
46 |
+
question = data.get("question")
|
47 |
+
context = data.get("context")
|
48 |
+
logging.info(f"Received request - Question: {question}, Context: {context[:200]}...")
|
49 |
+
if not question or not context:
|
50 |
+
logging.error("Missing question or context")
|
51 |
+
return JSONResponse({"error": "Missing question or context"}, status_code=400)
|
52 |
+
result = qa_pipeline(question=question, context=context)
|
53 |
+
logging.info(f"Response - Answer: {result['answer']}")
|
54 |
+
return JSONResponse({"answer": result["answer"]})
|
55 |
+
except Exception as e:
|
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(app=app) 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.")
|
63 |
|
|
|
64 |
with gr.Row():
|
65 |
question_input = gr.Textbox(label="Câu hỏi", placeholder="Nhập câu hỏi của bạn...")
|
66 |
context_input = gr.Textbox(label="Ngữ cảnh", placeholder="Nhập ngữ cảnh liên quan...")
|
67 |
output = gr.Textbox(label="Câu trả lời")
|
68 |
submit_btn = gr.Button("Gửi")
|
69 |
submit_btn.click(fn=gradio_answer, inputs=[question_input, context_input], outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
# Chạy ứng dụng
|
72 |
if __name__ == "__main__":
|