File size: 1,982 Bytes
091451d 8ba1afc 091451d 3114f31 eea187e 091451d cfba9db 091451d a7aa06f 091451d eea187e 091451d 2cfcd90 deb315d 091451d deb315d 091451d deb315d 091451d 5bba1c1 8f95ed0 eea187e |
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 |
import logging
from transformers import pipeline
import os
from huggingface_hub import login
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import uvicorn
import torch
# 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
# Tạo FastAPI app
app = FastAPI()
@app.post("/api/answer")
async def api_answer(request: Request):
try:
data = await request.json()
question = data.get("question")
context = data.get("context", "Cây lúa là một loại cây trồng phổ biến ở Việt Nam, cần điều kiện đất và nước phù hợp.")
logging.info(f"Received request - Question: {question}, Context: {context[:200]}...")
if not question:
logging.error("Missing question")
return JSONResponse({"error": "Missing question"}, status_code=400)
result = qa_pipeline(question=question, context=context)
logging.info(f"Response - Answer: {result['answer']}")
return JSONResponse({"answer": result["answer"]})
except Exception as e:
logging.error(f"API error: {e}")
return JSONResponse({"error": str(e)}, status_code=500)
if __name__ == "__main__":
logging.info("Starting FastAPI...")
uvicorn.run(app, host="0.0.0.0") # Bỏ tham số port để Spaces tự xử lý |