ChatBot / app.py
dat257's picture
Update app.py
8ba1afc verified
raw
history blame
2.72 kB
import logging
from flask import Flask, request, jsonify
import gradio as gr
from transformers import pipeline
import os
import torch
from huggingface_hub import login
# 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 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.")
# Giao diện Gradio
with gr.Row():
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)
# Endpoint API
@demo.route("/api/answer", methods=["POST"])
async def api_answer(request):
try:
data = await 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
# Chạy ứng dụng
if __name__ == "__main__":
logging.info("Starting Gradio on port 7860...")
demo.launch(server_name="0.0.0.0", server_port=7860, inline=False)