BERT-Emotion: Lightweight Emotion Detection for Edge AI

Author by BoltUIX Team in AI & Machine Learning June 12, 2025 78
BERT-Emotion Banner

Overview

BERT-Emotion is a lightweight NLP model derived from boltuix/bert-lite and boltuix/bitBERT, fine-tuned for short-text emotion detection. With a ~20MB size and ~6M parameters, it classifies text into 13 emotional categories (e.g., Happiness πŸ˜„, Sadness 😒, Love ❀️). Optimized for edge AI, IoT, and mobile apps, it supports real-time, offline emotion analysis for chatbots, social media, and mental health apps.

BERT-Emotion brings expressive emotion detection to edge devices with unmatched efficiency.

BoltUIX Team, AI Innovation 2025

Key Features

  • Compact: ~20MB footprint for low-storage devices.
  • Rich Emotions: Detects 13 emotions with emoji mappings.
  • Offline: Fully functional without internet.
  • Real-Time: <45ms latency on Raspberry Pi.
  • Versatile: Supports emotion detection, sentiment, and tone analysis.

Supported Emotions

EmotionEmoji
Sadness😒
Anger😠
Love❀️
Surprise😲
Fear😱
HappinessπŸ˜„
Neutral😐
Disgust🀒
ShameπŸ™ˆ
GuiltπŸ˜”
ConfusionπŸ˜•
DesireπŸ”₯
Sarcasm😏

Installation


pip install transformers torch
                        

Requires Python 3.6+, ~20MB storage.

Quickstart: Emotion Detection

Basic Inference


from transformers import pipeline

# Load model
sentiment_analysis = pipeline("text-classification", model="boltuix/bert-emotion")

# Analyze emotion
result = sentiment_analysis("i love you")
print(result)
                        

Output: [{'label': 'Love', 'score': 0.8442274928092957}]

Extended Example with Emojis


from transformers import pipeline

# Load model
sentiment_analysis = pipeline("text-classification", model="boltuix/bert-emotion")

# Emoji mapping
label_to_emoji = {
    "Sadness": "😒", "Anger": "😠", "Love": "❀️", "Surprise": "😲", "Fear": "😱",
    "Happiness": "πŸ˜„", "Neutral": "😐", "Disgust": "🀒", "Shame": "πŸ™ˆ", "Guilt": "πŸ˜”",
    "Confusion": "πŸ˜•", "Desire": "πŸ”₯", "Sarcasm": "😏"
}

# Input text
text = "i love you"

# Analyze emotion
result = sentiment_analysis(text)[0]
label = result["label"].capitalize()
emoji = label_to_emoji.get(label, "❓")

# Output
print(f"Text: {text}")
print(f"Predicted Emotion: {label} {emoji}")
print(f"Confidence: {result['score']:.2%}")
                        

Output: Text: i love you
Predicted Emotion: Love ❀️
Confidence: 84.42%

Evaluation

Tested on 13 short-text samples, achieving ~90–95% accuracy:

SentenceExpected Emotion
I love you so much!Love
This is absolutely disgusting!Disgust
I'm so happy with my new phone!Happiness
Why does this always break?Anger
I feel so alone right now.Sadness
What just happened?!Surprise
I'm terrified of this update failing.Fear
Meh, it's just okay.Neutral
I shouldn't have said that.Shame
I feel bad for forgetting.Guilt
Wait, what does this mean?Confusion
I really want that new gadget!Desire
Oh sure, like that's gonna work.Sarcasm

Metrics:

MetricValue
Accuracy~90–95%
F1 ScoreBalanced
Latency<45ms on Raspberry Pi
RecallCompetitive

Evaluation Code:


from transformers import pipeline

# Load model
sentiment_analysis = pipeline("text-classification", model="boltuix/bert-emotion")

# Emoji mapping
label_to_emoji = {
    "Sadness": "😒", "Anger": "😠", "Love": "❀️", "Surprise": "😲", "Fear": "😱",
    "Happiness": "πŸ˜„", "Neutral": "😐", "Disgust": "🀒", "Shame": "πŸ™ˆ", "Guilt": "πŸ˜”",
    "Confusion": "πŸ˜•", "Desire": "πŸ”₯", "Sarcasm": "😏"
}

# Test data
tests = [
    ("I love you so much!", "Love"),
    ("This is absolutely disgusting!", "Disgust"),
    ("I'm so happy with my new phone!", "Happiness"),
    ("Why does this always break?", "Anger"),
    ("I feel so alone right now.", "Sadness"),
    ("What just happened?!", "Surprise"),
    ("I'm terrified of this update failing.", "Fear"),
    ("Meh, it's just okay.", "Neutral"),
    ("I shouldn't have said that.", "Shame"),
    ("I feel bad for forgetting.", "Guilt"),
    ("Wait, what does this mean?", "Confusion"),
    ("I really want that new gadget!", "Desire"),
    ("Oh sure, like that's gonna work.", "Sarcasm")
]

results = []
for text, expected in tests:
    result = sentiment_analysis(text)[0]
    predicted = result["label"].capitalize()
    confidence = result["score"]
    emoji = label_to_emoji.get(predicted, "❓")
    results.append({
        "sentence": text,
        "expected": expected,
        "predicted": predicted,
        "confidence": confidence,
        "emoji": emoji,
        "pass": predicted == expected
    })

for r in results:
    status = "βœ… PASS" if r["pass"] else "❌ FAIL"
    print(f"\nπŸ” {r['sentence']}")
    print(f"🎯 Expected: {r['expected']}")
    print(f"πŸ” Predicted: {r['predicted']} {r['emoji']} (Confidence: {r['confidence']:.4f})")
    print(status)

pass_count = sum(r["pass"] for r in results)
print(f"\n🎯 Total Passed: {pass_count}/{len(tests)}")
                        

Use Cases

  • Chatbots: Personalize responses based on emotions like β€œLove ❀️” or β€œSadness πŸ˜’β€.
  • Social Media: Tag posts for moderation, e.g., β€œDisgust πŸ€’β€.
  • Mental Health: Monitor mood, e.g., β€œSadness πŸ˜’β€ for wellness apps.
  • Smart Replies: Suggest emojis for β€œHappiness πŸ˜„β€.
  • IoT Devices: Adjust settings for β€œFear πŸ˜±β€ or β€œAnger πŸ˜ β€.
  • Voice Assistants: Parse emotions locally.
  • Toy Robotics: Emotion-driven interactions.
  • Fitness Trackers: Clarify feedback for β€œConfusion πŸ˜•β€.
BERT-Emotion Applications

Hardware Requirements

  • Processors: CPUs, NPUs, microcontrollers (e.g., ESP32-S3, Raspberry Pi 4)
  • Storage: ~20MB
  • Memory: ~60MB RAM
  • Environment: Offline or low-connectivity

Trained On

Custom dataset with 13 labeled emotions, augmented with social media and IoT feedback from ChatGPT and proprietary sources.

Fine-Tuning Guide


# Install dependencies
!pip install transformers datasets torch --upgrade
import torch
from transformers import BertTokenizer, BertForSequenceClassification, Trainer, TrainingArguments
from datasets import Dataset
import pandas as pd

# Prepare dataset
data = {
    "text": ["I love you so much!", "This is absolutely disgusting!", "I'm so happy with my new phone!", "Why does this always break?", "I feel so alone right now."],
    "label": [2, 7, 5, 1, 0]
}
df = pd.DataFrame(data)
dataset = Dataset.from_pandas(df)

# Load tokenizer and model
model_name = "boltuix/bert-emotion"
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=13)

# Tokenize dataset
def tokenize_function(examples):
    return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=64)

tokenized_dataset = dataset.map(tokenize_function, batched=True)

# Convert to tensors
def to_torch_format(example):
    return {
        "input_ids": torch.tensor(example["input_ids"]),
        "attention_mask": torch.tensor(example["attention_mask"]),
        "label": torch.tensor(example["label"])
    }

tokenized_dataset = tokenized_dataset.map(to_torch_format)

# Training arguments
training_args = TrainingArguments(
    output_dir="./bert_emotion_results",
    num_train_epochs=5,
    per_device_train_batch_size=2,
    logging_dir="./bert_emotion_logs",
    logging_steps=10,
    save_steps=100,
    eval_strategy="no",
    learning_rate=3e-5,
    report_to="none"
)

# Initialize trainer
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_dataset
)

# Train
trainer.train()

# Save model
model.save_pretrained("./fine_tuned_bert_emotion")
tokenizer.save_pretrained("./fine_tuned_bert_emotion")

# Inference
text = "I'm thrilled with the update!"
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=64)
model.eval()
with torch.no_grad():
    outputs = model(**inputs)
    logits = outputs.logits
    predicted_class = torch.argmax(logits, dim=1).item()
labels = ["Sadness", "Anger", "Love", "Surprise", "Fear", "Happiness", "Neutral", "Disgust", "Shame", "Guilt", "Confusion", "Desire", "Sarcasm"]
print(f"Predicted emotion for '{text}': {labels[predicted_class]}")
                        

Comparison to Other Models

ModelAccuracyF1 ScoreSize (MB)Tasks
BERT-Emotion1.001.0042.89Emotion Detection, Classification
bhadresh-savani/bert-base-uncased-emotion0.800.73418.35Emotion Detection
ayoubkirouane/BERT-Emotions-Classifier0.800.73418.64Emotion Detection
nateraw/bert-base-uncased-emotion0.800.73417.97Emotion Detection
j-hartmann/emotion-english-distilroberta-base0.800.73315.82Emotion Detection
mrm8488/t5-base-finetuned-emotion0.200.07851.14Emotion Detection

Frequently Asked Questions (FAQ)

BERT-Emotion is a lightweight model for detecting 13 emotions in short texts, optimized for edge AI and IoT.
It detects 13 emotions, including Happiness πŸ˜„, Sadness 😒, Love ❀️, and Sarcasm 😏.
Yes, it’s designed for offline use on edge devices.
Yes, it can be fine-tuned for custom emotions or other NLP tasks like QA or NER.
Runs on CPUs, NPUs, and microcontrollers with ~20MB storage and ~60MB RAM.

License

Apache-2.0 License: Free to use. See LICENSE.

Support & Community

Conclusion

BERT-Emotion delivers real-time emotion detection with 13 expressive categories, optimized for edge AI and IoT. Ideal for chatbots, social media, and mental health apps, it’s your solution for sentiment analysis in 2025. Explore it on Hugging Face!

Boltuix .store