File size: 2,770 Bytes
cfba9db
 
 
 
 
 
a7aa06f
d2d0847
 
cfba9db
 
c654a7e
cfba9db
a7aa06f
2cfcd90
 
 
 
 
 
a7aa06f
cfba9db
 
2cfcd90
 
 
 
 
 
 
 
 
 
 
26c5e44
2cfcd90
 
 
 
d2d0847
26c5e44
cfba9db
 
 
 
 
 
 
c654a7e
cfba9db
c654a7e
cfba9db
 
c654a7e
cfba9db
 
 
 
 
d2d0847
 
 
 
 
 
 
 
 
cfba9db
d2d0847
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import logging
from flask import Flask, request, jsonify
from transformers import pipeline
import gradio as gr
import os
import torch
from huggingface_hub import login
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple

# Cấu hình logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Đăng nhập Hugging Face
try:
    login(token=os.getenv("HF_TOKEN"))
    logging.info("Logged in to Hugging Face Hub successfully")
except Exception as e:
    logging.error(f"Failed to login to Hugging Face Hub: {e}")
    raise

# Load mô hình
logging.info("Loading nguyenvulebinh/vi-mrc-base...")
try:
    qa_pipeline = pipeline(
        "question-answering",
        model="nguyenvulebinh/vi-mrc-base",
        device=0 if torch.cuda.is_available() else -1
    )
    logging.info("Model loaded successfully")
except Exception as e:
    logging.error(f"Failed to load model: {e}")
    raise

# Hàm xử lý cho Gradio và API
def gradio_answer(question, context):
    result = qa_pipeline(question=question, context=context)
    return result["answer"]

# Tạo Flask app cho API
app = Flask(__name__)

@app.route("/api/answer", methods=["POST"])
def answer():
    try:
        data = request.json
        question = data.get("question")
        context = data.get("context")
        logging.info(f"Received request - Question: {question}, Context: {context[:200]}...")
        if not question or not context:
            logging.error("Missing question or context")
            return jsonify({"error": "Missing question or context"}), 400
        result = qa_pipeline(question=question, context=context)
        logging.info(f"Response - Answer: {result['answer']}")
        return jsonify({"answer": result["answer"]})
    except Exception as e:
        logging.error(f"API error: {e}")
        return jsonify({"error": str(e)}), 500

# Tạo Gradio Blocks
with gr.Blocks() as demo:
    gr.Markdown("# AgriBot: Hỏi đáp nông nghiệp")
    gr.Markdown("Nhập câu hỏi và ngữ cảnh để nhận câu trả lời về nông nghiệp.")
    question_input = gr.Textbox(label="Câu hỏi", placeholder="Nhập câu hỏi của bạn...")
    context_input = gr.Textbox(label="Ngữ cảnh", placeholder="Nhập ngữ cảnh liên quan...")
    output = gr.Textbox(label="Câu trả lời")
    submit_btn = gr.Button("Gửi")
    submit_btn.click(fn=gradio_answer, inputs=[question_input, context_input], outputs=output)

# Tích hợp Flask và Gradio trên cùng port
application = DispatcherMiddleware(demo.app, {"/api": app})

if __name__ == "__main__":
    logging.info("Starting Gradio and Flask on port 7860...")
    run_simple("0.0.0.0", 7860, application)