Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- backend.py +30 -0
- main.py +19 -0
- requirements.txt +0 -0
backend.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
from peft import PeftModel, PeftConfig
|
4 |
+
|
5 |
+
# Load model and tokenizer only once at startup
|
6 |
+
config = PeftConfig.from_pretrained("rabindra-sss/sentiment-distilbert")
|
7 |
+
base_model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")
|
8 |
+
model = PeftModel.from_pretrained(base_model, "rabindra-sss/sentiment-distilbert", config=config)
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("rabindra-sss/sentiment-distilbert")
|
10 |
+
|
11 |
+
# Ensure model is in evaluation mode for inference
|
12 |
+
model.eval()
|
13 |
+
|
14 |
+
# Define id2label mappings
|
15 |
+
id2label = {0: "Negative", 1: "Positive"}
|
16 |
+
|
17 |
+
def predict(text: str) -> str:
|
18 |
+
# Tokenize the input text
|
19 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
20 |
+
|
21 |
+
# Run the model to get logits
|
22 |
+
with torch.no_grad():
|
23 |
+
outputs = model(**inputs)
|
24 |
+
logits = outputs.logits
|
25 |
+
|
26 |
+
# Convert logits to predicted class
|
27 |
+
predictions = torch.argmax(logits, dim=-1)
|
28 |
+
predicted_label = id2label[predictions.item()]
|
29 |
+
|
30 |
+
return predicted_label
|
main.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from backend import predict
|
4 |
+
from mangum import Mangum
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
handler = Mangum(app)
|
9 |
+
class Input(BaseModel):
|
10 |
+
text: str
|
11 |
+
|
12 |
+
@app.get("/")
|
13 |
+
async def root():
|
14 |
+
return {"message": "Sentiment Analysis API is running!"}
|
15 |
+
|
16 |
+
@app.post("/predict/")
|
17 |
+
async def predict_sentiment(input: Input):
|
18 |
+
prediction = predict(input.text)
|
19 |
+
return {"text": input.text, "sentiment": prediction}
|
requirements.txt
ADDED
Binary file (126 Bytes). View file
|
|