Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
tags:
|
| 6 |
+
- emotion-classification
|
| 7 |
+
- emotion
|
| 8 |
+
- mental-health
|
| 9 |
+
- bert
|
| 10 |
+
- text-classification
|
| 11 |
+
pipeline_tag: text-classification
|
| 12 |
+
base_model:
|
| 13 |
+
- bert-base-uncased
|
| 14 |
+
datasets:
|
| 15 |
+
- google-research-datasets/go_emotions
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# 😄 Emotion Classification with BERT
|
| 19 |
+
|
| 20 |
+
This model is a fine-tuned version of `bert-base-uncased` for **multi-label emotion classification**.
|
| 21 |
+
It predicts **eight basic emotions** from a given piece of text using sigmoid-based multi-label classification.
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## 🧠 Model Details
|
| 26 |
+
|
| 27 |
+
- **Base model**: `bert-base-uncased`
|
| 28 |
+
- **Fine-tuned for**: Multi-label emotion classification
|
| 29 |
+
- **Emotion labels**:
|
| 30 |
+
- `anger`
|
| 31 |
+
- `fear`
|
| 32 |
+
- `disgust`
|
| 33 |
+
- `sadness`
|
| 34 |
+
- `surprise`
|
| 35 |
+
- `joy`
|
| 36 |
+
- `anticipation`
|
| 37 |
+
- `trust`
|
| 38 |
+
- **Intended use**: Emotion detection in messages, sentiment analysis, chatbot tuning, mental health signal recognition, etc.
|
| 39 |
+
|
| 40 |
+
---
|
| 41 |
+
|
| 42 |
+
## 📦 Usage
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
import torch
|
| 46 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 47 |
+
|
| 48 |
+
# Set device
|
| 49 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 50 |
+
|
| 51 |
+
# Load model and tokenizer
|
| 52 |
+
model_path = "sajeewa/emotion-classification-bert"
|
| 53 |
+
emotion_labels = ["anger", "fear", "disgust", "sadness", "surprise", "joy", "anticipation", "trust"]
|
| 54 |
+
|
| 55 |
+
tokenizer = BertTokenizer.from_pretrained(model_path)
|
| 56 |
+
model = BertForSequenceClassification.from_pretrained(model_path, num_labels=len(emotion_labels)).to(device)
|
| 57 |
+
|
| 58 |
+
# Emotion prediction function
|
| 59 |
+
def predict_emotions(text: str):
|
| 60 |
+
model.eval()
|
| 61 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=50).to(device)
|
| 62 |
+
inputs.pop("token_type_ids", None)
|
| 63 |
+
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
logits = model(**inputs).logits
|
| 66 |
+
|
| 67 |
+
probs = torch.sigmoid(logits).cpu().numpy()[0]
|
| 68 |
+
return {label: round(float(score), 4) for label, score in zip(emotion_labels, probs)}
|
| 69 |
+
|
| 70 |
+
# Example usage
|
| 71 |
+
example_text = "I'm feeling lonely today."
|
| 72 |
+
predictions = predict_emotions(example_text)
|
| 73 |
+
dominant_emotion = max(predictions, key=predictions.get)
|
| 74 |
+
print({dominant_emotion: predictions[dominant_emotion]})
|