Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3import numpy as np
4
5# Load model and tokenizer
6model = AutoModelForSequenceClassification.from_pretrained("hrshlgunjal/emotion-classifier-roberta-base")
7tokenizer = AutoTokenizer.from_pretrained("hrshlgunjal/emotion-classifier-roberta-base")
8
9# Optimized thresholds (use these for best results)
10thresholds = np.array([0.5, 0.5, 0.5, 0.5, 0.5])
11labels = ['anger', 'fear', 'joy', 'sadness', 'surprise']
12
13# Predict emotions
14def predict_emotions(text):
15 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
16 with torch.no_grad():
17 outputs = model(**inputs)
18 probs = torch.sigmoid(outputs.logits).cpu().numpy()[0]
19 predictions = (probs >= thresholds).astype(int)
20 return {label: (pred, prob) for label, pred, prob in zip(labels, predictions, probs)}
21
22# Example
23text = "I am so excited about this amazing opportunity!"
24result = predict_emotions(text)
25print(result)