Views
No views yet
1from transformers import BertTokenizer, BertForSequenceClassification
2import torch
3
4# Load the tokenizer and model from Hugging Face
5tokenizer = BertTokenizer.from_pretrained("Avvirup/emotion-bert-multiclass")
6model = BertForSequenceClassification.from_pretrained("Avvirup/emotion-bert-multiclass")
7
8# Example text
9text = "I am feeling really happy today!"
10
11# Tokenize the text
12inputs = tokenizer(
13 text,
14 return_tensors="pt",
15 truncation=True,
16 padding="max_length",
17 max_length=128
18)
19
20# Make prediction
21with torch.no_grad():
22 outputs = model(**inputs)
23 prediction = torch.argmax(outputs.logits, dim=1).item()
24
25# Map prediction to emotion label
26label_names = ["sadness", "joy", "anger"]
27print(f"Prediction: {label_names[prediction]}")