|
|
--- |
|
|
title: Emotion Classifier Deploy |
|
|
emoji: π |
|
|
colorFrom: red |
|
|
colorTo: red |
|
|
sdk: docker |
|
|
app_port: 8501 |
|
|
tags: |
|
|
- streamlit |
|
|
pinned: false |
|
|
short_description: Streamlit template space |
|
|
--- |
|
|
|
|
|
# Multi-Label Emotion Classifier |
|
|
|
|
|
A fine-tuned RoBERTa-based multi-label emotion classifier with a Streamlit web interface. The model predicts five emotions: **anger**, **fear**, **joy**, **sadness**, and **surprise**. |
|
|
|
|
|
[](https://huggingface.co/JashMevada/emotion-classifier) |
|
|
[](https://huggingface.co/spaces/JashMevada/emotion-classifier-deploy) |
|
|
|
|
|
## Features |
|
|
|
|
|
- **Multi-label Classification**: Detects multiple emotions in a single text |
|
|
- **Fine-tuned RoBERTa-Large**: Built on `FacebookAI/roberta-large` for robust performance |
|
|
- **Interactive Web UI**: Streamlit-based interface with adjustable decision threshold |
|
|
- **Batch Inference**: Process multiple texts at once |
|
|
- **Visualization**: Bar charts showing emotion probabilities |
|
|
|
|
|
|
|
|
## Quick Start |
|
|
|
|
|
### Option 1: Run with Docker |
|
|
|
|
|
```bash |
|
|
docker build -t emotion-classifier . |
|
|
|
|
|
docker run -p 8501:8501 emotion-classifier |
|
|
``` |
|
|
|
|
|
Open your browser at `http://localhost:8501` |
|
|
|
|
|
### Option 2: Run Locally |
|
|
|
|
|
```bash |
|
|
pip install -r requirements.txt |
|
|
|
|
|
streamlit run src/streamlit_app.py |
|
|
``` |
|
|
|
|
|
## Model Architecture |
|
|
|
|
|
- **Base Model**: `FacebookAI/roberta-large` |
|
|
- **Classification Head**: |
|
|
- Linear(1024 β 1024) β ReLU β Dropout(0.2) β Linear(1024 β 5) |
|
|
- **Pooling**: Mean pooling with attention mask |
|
|
- **Output**: Sigmoid activation for multi-label classification |
|
|
|
|
|
## Usage |
|
|
|
|
|
### Web Interface |
|
|
|
|
|
1. Enter one or more text lines in the text area |
|
|
2. Adjust the decision threshold using the sidebar slider |
|
|
3. Click "Run inference" to get predictions |
|
|
4. View results in the table and bar chart |
|
|
|
|
|
### Python API |
|
|
|
|
|
```python |
|
|
from transformers import AutoModel, AutoTokenizer |
|
|
from huggingface_hub import hf_hub_download |
|
|
import torch |
|
|
|
|
|
# Load model components |
|
|
repo_id = "JashMevada/emotion-classifier" |
|
|
tokenizer = AutoTokenizer.from_pretrained(repo_id) |
|
|
encoder = AutoModel.from_pretrained(repo_id, add_pooling_layer=False) |
|
|
|
|
|
# Load classifier head |
|
|
weights_path = hf_hub_download(repo_id, "classifier.pth") |
|
|
classifier = torch.nn.Sequential( |
|
|
torch.nn.Linear(1024, 1024), |
|
|
torch.nn.ReLU(), |
|
|
torch.nn.Dropout(0.2), |
|
|
torch.nn.Linear(1024, 5), |
|
|
) |
|
|
classifier.load_state_dict(torch.load(weights_path, map_location="cpu")) |
|
|
|
|
|
# Inference |
|
|
text = "I am so happy today!" |
|
|
encoded = tokenizer(text, return_tensors="pt", truncation=True, max_length=512) |
|
|
|
|
|
with torch.no_grad(): |
|
|
outputs = encoder(**encoded).last_hidden_state |
|
|
mask = encoded["attention_mask"].unsqueeze(-1).float() |
|
|
pooled = (outputs * mask).sum(dim=1) / mask.sum(dim=1).clamp(min=1e-9) |
|
|
logits = classifier(pooled) |
|
|
probs = torch.sigmoid(logits) |
|
|
|
|
|
emotions = ["anger", "fear", "joy", "sadness", "surprise"] |
|
|
for emo, prob in zip(emotions, probs[0]): |
|
|
print(f"{emo}: {prob:.3f}") |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
## Links |
|
|
|
|
|
- **Model**: [HuggingFace Hub](https://huggingface.co/JashMevada/emotion-classifier) |
|
|
- **Demo**: [Streamlit App](https://huggingface.co/spaces/JashMevada/emotion-classifier-deploy) |
|
|
|