Downloads ยท 30 days
16
20% of all-time downloads
punithreddy-ai/bert-emotion-classifier
bert-emotion-classifier is a text classification model from punithreddy-ai. Use it when you need a label for a piece of text. It is set up for transformers. The card lists the license as mit.
Fine-tuned bert-base-uncased on the dair-ai/emotion dataset for multi-class emotion detection in English text.
Downloads ยท 30 days
16
20% of all-time downloads
All-time downloads
79
Public
Parameters
109M
438 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors438 MB ยท 100%
From the Hugging Face model README
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.