from fastapi import FastAPI from pydantic import BaseModel from fastapi.middleware.cors import CORSMiddleware from transformers import AutoTokenizer, AutoModelForSequenceClassification import torch from utils import clean_text MODEL_ID = "hiuS04/phobert-sentiment-api" LABELS = ["Negative", "Neutral", "Positive"] # Load model tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=False) model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID) model.eval() app = FastAPI(title="PhoBERT Sentiment API") app.add_middleware( CORSMiddleware, allow_origins=["*"], # cho phép extension gọi allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) class TextInput(BaseModel): text: str @app.get("/") def root(): return {"message": "PhoBERT Sentiment API is live on Hugging Face"} @app.post("/predict") def predict(inp: TextInput): raw_text = inp.text.strip() if not raw_text: return {"error": "Text cannot be empty"} cleaned = clean_text(raw_text) inputs = tokenizer(cleaned, return_tensors="pt", truncation=True, max_length=128) with torch.no_grad(): outputs = model(**inputs) probs = torch.nn.functional.softmax(outputs.logits, dim=-1) pred = torch.argmax(probs, dim=-1).item() confidence = probs[0, pred].item() return { "sentiment": LABELS[pred], "confidence": round(confidence, 4), "original_text": raw_text }