Spaces:
No application file
No application file
initial deployment
Browse files- app.py +47 -0
- dockerfile +10 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="RoBERTa LoRA Sentiment API")
|
| 8 |
+
|
| 9 |
+
model_name = "Burhan21/roberta-lora-sentiment"
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
class TextRequest(BaseModel):
|
| 15 |
+
text: str
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def root():
|
| 19 |
+
return {"message": "Sentiment API is running"}
|
| 20 |
+
|
| 21 |
+
@app.get("/health")
|
| 22 |
+
def health():
|
| 23 |
+
return {"status": "ok"}
|
| 24 |
+
|
| 25 |
+
@app.post("/predict")
|
| 26 |
+
def predict(request: TextRequest):
|
| 27 |
+
inputs = tokenizer(
|
| 28 |
+
request.text,
|
| 29 |
+
return_tensors="pt",
|
| 30 |
+
truncation=True,
|
| 31 |
+
padding=True
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model(**inputs)
|
| 36 |
+
probs = F.softmax(outputs.logits, dim=-1)
|
| 37 |
+
confidence = probs.max().item()
|
| 38 |
+
pred_class = probs.argmax(-1).item()
|
| 39 |
+
|
| 40 |
+
label = "Positive" if pred_class == 1 else "Negative"
|
| 41 |
+
|
| 42 |
+
return {
|
| 43 |
+
"text": request.text,
|
| 44 |
+
"prediction": label,
|
| 45 |
+
"confidence": round(confidence, 4),
|
| 46 |
+
"model": model_name
|
| 47 |
+
}
|
dockerfile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|