Spaces:
No application file
No application file
File size: 1,180 Bytes
684e468 | 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 | from fastapi import FastAPI
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F
app = FastAPI(title="RoBERTa LoRA Sentiment API")
model_name = "Burhan21/roberta-lora-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
class TextRequest(BaseModel):
text: str
@app.get("/")
def root():
return {"message": "Sentiment API is running"}
@app.get("/health")
def health():
return {"status": "ok"}
@app.post("/predict")
def predict(request: TextRequest):
inputs = tokenizer(
request.text,
return_tensors="pt",
truncation=True,
padding=True
)
with torch.no_grad():
outputs = model(**inputs)
probs = F.softmax(outputs.logits, dim=-1)
confidence = probs.max().item()
pred_class = probs.argmax(-1).item()
label = "Positive" if pred_class == 1 else "Negative"
return {
"text": request.text,
"prediction": label,
"confidence": round(confidence, 4),
"model": model_name
} |