File size: 1,336 Bytes
e3e0ddf
 
 
 
 
 
 
 
 
 
4d632b4
 
 
 
 
e3e0ddf
4d632b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

# Force cache inside local folder
os.environ["TRANSFORMERS_CACHE"] = "./hf_cache"
os.environ["HF_HOME"] = "./hf_cache"
os.environ["XDG_CACHE_HOME"] = "./hf_cache"
os.environ["TORCH_HOME"] = "./hf_cache"
os.environ["HF_DATASETS_CACHE"] = "./hf_cache"
os.environ["SAFE_TENSORS_CACHE"] = "./hf_cache"

from fastapi import FastAPI, Request
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

# Load model
model_name = "tabularisai/multilingual-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

app = FastAPI()

sentiment_map = {
    0: "Very Negative",
    1: "Negative",
    2: "Neutral",
    3: "Positive",
    4: "Very Positive"
}

class ReviewRequest(BaseModel):
    text: str

@app.post("/predict-sentiment")
def predict_sentiment(review: ReviewRequest):
    inputs = tokenizer(review.text, return_tensors="pt", truncation=True, padding=True, max_length=512)
    with torch.no_grad():
        outputs = model(**inputs)
    probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
    predicted_label = torch.argmax(probabilities, dim=-1).item()
    sentiment = sentiment_map[predicted_label]
    return {"text": review.text, "sentiment": sentiment}