|
|
import torch |
|
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
|
|
|
print("sentiment module loaded (English RoBERTa)") |
|
|
|
|
|
|
|
|
|
|
|
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment" |
|
|
|
|
|
try: |
|
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False) |
|
|
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) |
|
|
model.eval() |
|
|
except Exception as e: |
|
|
print(f"CRITICAL ERROR loading sentiment model: {e}") |
|
|
raise e |
|
|
|
|
|
|
|
|
def sentiment_scores(text: str): |
|
|
""" |
|
|
Returns sentiment probabilities as: |
|
|
[negative, neutral, positive] |
|
|
""" |
|
|
with torch.no_grad(): |
|
|
inputs = tokenizer( |
|
|
text, |
|
|
return_tensors="pt", |
|
|
truncation=True, |
|
|
padding=True, |
|
|
max_length=128 |
|
|
) |
|
|
outputs = model(**inputs) |
|
|
probs = torch.softmax(outputs.logits, dim=1) |
|
|
|
|
|
return probs[0].tolist() |
|
|
|