dair-ai/emotion
Viewer β’ Updated β’ 437k β’ 34.7k β’ 446
How to use punithreddy-ai/bert-emotion-classifier with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-classification", model="punithreddy-ai/bert-emotion-classifier") # Load model directly
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("punithreddy-ai/bert-emotion-classifier")
model = AutoModelForSequenceClassification.from_pretrained("punithreddy-ai/bert-emotion-classifier")Fine-tuned bert-base-uncased on the dair-ai/emotion dataset for multi-class emotion detection in English text.
This model classifies English text into one of 6 emotional categories:
| Label | Emotion | Description |
|---|---|---|
| 0 | π’ sadness | Grief, melancholy, sorrow |
| 1 | π joy | Happiness, excitement, delight |
| 2 | β€οΈ love | Affection, warmth, attachment |
| 3 | π anger | Frustration, rage, indignation |
| 4 | π¨ fear | Anxiety, dread, apprehension |
| 5 | π² surprise | Astonishment, unexpectedness |
| Metric | Score |
|---|---|
| Test Accuracy | ~93% |
| Macro F1 | ~92% |
| Weighted F1 | ~93% |
from transformers import BertTokenizerFast, BertForSequenceClassification
import torch
import torch.nn.functional as F
# Load model
tokenizer = BertTokenizerFast.from_pretrained("punithreddy-ai/bert-emotion-classifier")
model = BertForSequenceClassification.from_pretrained("punithreddy-ai/bert-emotion-classifier")
model.eval()
LABELS = ["sadness", "joy", "love", "anger", "fear", "surprise"]
EMOJIS = ["π’", "π", "β€οΈ", "π ", "π¨", "π²"]
def predict(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
with torch.no_grad():
logits = model(**inputs).logits
probs = F.softmax(logits, dim=-1)[0]
idx = probs.argmax().item()
return {
"label": LABELS[idx],
"emoji": EMOJIS[idx],
"confidence": f"{probs[idx].item():.1%}",
"all_scores": {LABELS[i]: f"{probs[i].item():.1%}" for i in range(6)}
}
# Examples
print(predict("I just got promoted β I'm over the moon!"))
# {'label': 'joy', 'emoji': 'π', 'confidence': '96.3%', ...}
print(predict("I can't stop crying. I miss him so much."))
# {'label': 'sadness', 'emoji': 'π’', 'confidence': '94.1%', ...}
print(predict("How DARE they treat people like that!"))
# {'label': 'anger', 'emoji': 'π ', 'confidence': '91.7%', ...}
from transformers import pipeline
classifier = pipeline(
"text-classification",
model="punithreddy-ai/bert-emotion-classifier",
return_all_scores=True,
)
results = classifier("I can't believe we actually won!")
# [{'label': 'joy', 'score': 0.89}, {'label': 'surprise', 'score': 0.07}, ...]
| Hyperparameter | Value |
|---|---|
| Base model | bert-base-uncased |
| Max sequence length | 128 tokens |
| Batch size | 32 |
| Learning rate | 2e-5 |
| LR schedule | Linear warmup (10%) + linear decay |
| Epochs | 4 |
| Weight decay | 0.01 |
| Dropout | 0.1 |
| Optimizer | AdamW (Ξ΅=1e-8) |
| Gradient clipping | 1.0 |
| Seed | 42 |
dair-ai/emotion β English Twitter messages labelled with one of six emotions.
| Split | Samples |
|---|---|
| Train | 16,000 |
| Validation | 2,000 |
| Test | 2,000 |
Input Text
β
[BertTokenizerFast] β input_ids + attention_mask
β
[bert-base-uncased]
β’ 12 transformer encoder layers
β’ 12 attention heads
β’ 768 hidden dimensions
β’ 110M total parameters
β
[CLS] token β [Dropout 0.1] β [Linear 768β6] β [Softmax]
β
Probability distribution over 6 emotions
If you use this model, please cite the original dataset:
@inproceedings{saravia-etal-2018-carer,
title = "{CARER}: Contextualized Affect Representations for Emotion Recognition",
author = "Saravia, Elvis and Liu, Hsien-Chi Toby and Huang, Yi-Hsin and Wu, Jing and Chen, Yi-Shin",
booktitle = "Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing",
year = "2018",
publisher = "Association for Computational Linguistics",
}
Built by Punithreddy as a portfolio project demonstrating BERT fine-tuning for NLP classification.