Jash Mevada
commited on
Commit
Β·
0f5600d
1
Parent(s):
1f06aea
updated README.
Browse files- README.md +97 -19
- src/DL-23f2003807-notebook-t32025.ipynb +0 -0
- src/streamlit_app.py +2 -2
README.md
CHANGED
|
@@ -1,19 +1,97 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Multi-Label Emotion Classifier
|
| 2 |
+
|
| 3 |
+
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**.
|
| 4 |
+
|
| 5 |
+
[](https://huggingface.co/JashMevada/emotion-classifier)
|
| 6 |
+
[](https://huggingface.co/spaces/JashMevada/emotion-classifier-deploy)
|
| 7 |
+
|
| 8 |
+
## Features
|
| 9 |
+
|
| 10 |
+
- **Multi-label Classification**: Detects multiple emotions in a single text
|
| 11 |
+
- **Fine-tuned RoBERTa-Large**: Built on `FacebookAI/roberta-large` for robust performance
|
| 12 |
+
- **Interactive Web UI**: Streamlit-based interface with adjustable decision threshold
|
| 13 |
+
- **Batch Inference**: Process multiple texts at once
|
| 14 |
+
- **Visualization**: Bar charts showing emotion probabilities
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
## Quick Start
|
| 18 |
+
|
| 19 |
+
### Option 1: Run with Docker
|
| 20 |
+
|
| 21 |
+
```bash
|
| 22 |
+
docker build -t emotion-classifier .
|
| 23 |
+
|
| 24 |
+
docker run -p 8501:8501 emotion-classifier
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
Open your browser at `http://localhost:8501`
|
| 28 |
+
|
| 29 |
+
### Option 2: Run Locally
|
| 30 |
+
|
| 31 |
+
```bash
|
| 32 |
+
pip install -r requirements.txt
|
| 33 |
+
|
| 34 |
+
streamlit run src/streamlit_app.py
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
## Model Architecture
|
| 38 |
+
|
| 39 |
+
- **Base Model**: `FacebookAI/roberta-large`
|
| 40 |
+
- **Classification Head**:
|
| 41 |
+
- Linear(1024 β 1024) β ReLU β Dropout(0.2) β Linear(1024 β 5)
|
| 42 |
+
- **Pooling**: Mean pooling with attention mask
|
| 43 |
+
- **Output**: Sigmoid activation for multi-label classification
|
| 44 |
+
|
| 45 |
+
## Usage
|
| 46 |
+
|
| 47 |
+
### Web Interface
|
| 48 |
+
|
| 49 |
+
1. Enter one or more text lines in the text area
|
| 50 |
+
2. Adjust the decision threshold using the sidebar slider
|
| 51 |
+
3. Click "Run inference" to get predictions
|
| 52 |
+
4. View results in the table and bar chart
|
| 53 |
+
|
| 54 |
+
### Python API
|
| 55 |
+
|
| 56 |
+
```python
|
| 57 |
+
from transformers import AutoModel, AutoTokenizer
|
| 58 |
+
from huggingface_hub import hf_hub_download
|
| 59 |
+
import torch
|
| 60 |
+
|
| 61 |
+
# Load model components
|
| 62 |
+
repo_id = "JashMevada/emotion-classifier"
|
| 63 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 64 |
+
encoder = AutoModel.from_pretrained(repo_id, add_pooling_layer=False)
|
| 65 |
+
|
| 66 |
+
# Load classifier head
|
| 67 |
+
weights_path = hf_hub_download(repo_id, "classifier.pth")
|
| 68 |
+
classifier = torch.nn.Sequential(
|
| 69 |
+
torch.nn.Linear(1024, 1024),
|
| 70 |
+
torch.nn.ReLU(),
|
| 71 |
+
torch.nn.Dropout(0.2),
|
| 72 |
+
torch.nn.Linear(1024, 5),
|
| 73 |
+
)
|
| 74 |
+
classifier.load_state_dict(torch.load(weights_path, map_location="cpu"))
|
| 75 |
+
|
| 76 |
+
# Inference
|
| 77 |
+
text = "I am so happy today!"
|
| 78 |
+
encoded = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 79 |
+
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
outputs = encoder(**encoded).last_hidden_state
|
| 82 |
+
mask = encoded["attention_mask"].unsqueeze(-1).float()
|
| 83 |
+
pooled = (outputs * mask).sum(dim=1) / mask.sum(dim=1).clamp(min=1e-9)
|
| 84 |
+
logits = classifier(pooled)
|
| 85 |
+
probs = torch.sigmoid(logits)
|
| 86 |
+
|
| 87 |
+
emotions = ["anger", "fear", "joy", "sadness", "surprise"]
|
| 88 |
+
for emo, prob in zip(emotions, probs[0]):
|
| 89 |
+
print(f"{emo}: {prob:.3f}")
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
## Links
|
| 95 |
+
|
| 96 |
+
- **Model**: [HuggingFace Hub](https://huggingface.co/JashMevada/emotion-classifier)
|
| 97 |
+
- **Demo**: [Streamlit App](https://huggingface.co/spaces/JashMevada/emotion-classifier-deploy)
|
src/DL-23f2003807-notebook-t32025.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
src/streamlit_app.py
CHANGED
|
@@ -8,7 +8,7 @@ import torch
|
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
from transformers import AutoModel, AutoTokenizer
|
| 10 |
|
| 11 |
-
st.set_page_config(page_title="Emotion Classifier",
|
| 12 |
|
| 13 |
EMOTIONS = ["anger", "fear", "joy", "sadness", "surprise"]
|
| 14 |
BASE_MODEL_ID = "FacebookAI/roberta-large"
|
|
@@ -88,7 +88,7 @@ st.write(
|
|
| 88 |
|
| 89 |
with st.sidebar:
|
| 90 |
st.subheader("Inference Settings")
|
| 91 |
-
decision_threshold = st.slider("Decision threshold", 0.1, 0.9, 0.
|
| 92 |
st.caption("Threshold was tuned on validation data; tweak it to prioritize precision or recall.")
|
| 93 |
|
| 94 |
sample_texts = {
|
|
|
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
from transformers import AutoModel, AutoTokenizer
|
| 10 |
|
| 11 |
+
st.set_page_config(page_title="Emotion Classifier", layout="wide")
|
| 12 |
|
| 13 |
EMOTIONS = ["anger", "fear", "joy", "sadness", "surprise"]
|
| 14 |
BASE_MODEL_ID = "FacebookAI/roberta-large"
|
|
|
|
| 88 |
|
| 89 |
with st.sidebar:
|
| 90 |
st.subheader("Inference Settings")
|
| 91 |
+
decision_threshold = st.slider("Decision threshold", 0.1, 0.9, 0.86, 0.01)
|
| 92 |
st.caption("Threshold was tuned on validation data; tweak it to prioritize precision or recall.")
|
| 93 |
|
| 94 |
sample_texts = {
|