Spaces:
No application file
No application file
| 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 | |
| def root(): | |
| return {"message": "Sentiment API is running"} | |
| def health(): | |
| return {"status": "ok"} | |
| 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 | |
| } |