Spaces:
Running
Running
Upload 3 files
Browse files- README.md +37 -6
- app.py +55 -0
- requirements.txt +7 -0
README.md
CHANGED
|
@@ -1,12 +1,43 @@
|
|
| 1 |
---
|
| 2 |
-
title: Emotion Classifier
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Emotion Text Classifier
|
| 3 |
+
emoji: 😊
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "4.19.0"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Emotion Text Classifier
|
| 14 |
+
|
| 15 |
+
Fine-tuned `roberta-base` on the [dair-ai/emotion](https://huggingface.co/datasets/dair-ai/emotion) dataset.
|
| 16 |
+
|
| 17 |
+
Classifies text into 6 emotions: sadness, joy, love, anger, fear, surprise.
|
| 18 |
+
|
| 19 |
+
## Files
|
| 20 |
+
|
| 21 |
+
| File | Purpose |
|
| 22 |
+
|------|---------|
|
| 23 |
+
| `train.py` | Fine-tune roberta-base on dair-ai/emotion (run on Colab) |
|
| 24 |
+
| `push_to_hub.py` | Push trained model to HuggingFace Hub with model card |
|
| 25 |
+
| `app.py` | Gradio demo app |
|
| 26 |
+
| `requirements.txt` | Python dependencies |
|
| 27 |
+
|
| 28 |
+
## Quick Start (Colab)
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
# 1. Install dependencies
|
| 32 |
+
pip install transformers datasets scikit-learn accelerate
|
| 33 |
+
|
| 34 |
+
# 2. Train
|
| 35 |
+
python train.py
|
| 36 |
+
|
| 37 |
+
# 3. Push to Hub (log in first: huggingface-cli login)
|
| 38 |
+
python push_to_hub.py --repo_id your-username/emotion-roberta
|
| 39 |
+
|
| 40 |
+
# 4. Test the demo locally
|
| 41 |
+
pip install gradio
|
| 42 |
+
python app.py
|
| 43 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# ---------------------------------------------------------------------------
|
| 6 |
+
# Load the model — update this to your Hub repo ID after pushing
|
| 7 |
+
# ---------------------------------------------------------------------------
|
| 8 |
+
MODEL_ID = "dk409/emotion-roberta" for Hub
|
| 9 |
+
classifier = pipeline("text-classification", model=MODEL_ID, top_k=None)
|
| 10 |
+
|
| 11 |
+
# ---------------------------------------------------------------------------
|
| 12 |
+
# Prediction function
|
| 13 |
+
# ---------------------------------------------------------------------------
|
| 14 |
+
def classify_emotion(text):
|
| 15 |
+
"""
|
| 16 |
+
Run the classifier and return a dict of {label: score} for Gradio's Label component.
|
| 17 |
+
The Label component automatically sorts and displays as a bar chart.
|
| 18 |
+
"""
|
| 19 |
+
if not text or not text.strip():
|
| 20 |
+
return {}
|
| 21 |
+
|
| 22 |
+
results = classifier(text)[0] # list of {"label": ..., "score": ...}
|
| 23 |
+
return {r["label"]: r["score"] for r in results}
|
| 24 |
+
|
| 25 |
+
# ---------------------------------------------------------------------------
|
| 26 |
+
# Gradio interface
|
| 27 |
+
# ---------------------------------------------------------------------------
|
| 28 |
+
examples = [
|
| 29 |
+
"I'm so happy to see you after all these years!",
|
| 30 |
+
"This is absolutely terrifying, I can't watch.",
|
| 31 |
+
"I can't believe they cancelled the show. So angry right now.",
|
| 32 |
+
"She looked at him with so much love in her eyes.",
|
| 33 |
+
"I feel so alone and empty inside.",
|
| 34 |
+
"Wait, you got promoted? I had no idea! That's amazing!",
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
demo = gr.Interface(
|
| 38 |
+
fn=classify_emotion,
|
| 39 |
+
inputs=gr.Textbox(
|
| 40 |
+
label="Enter text",
|
| 41 |
+
placeholder="Type a sentence and I'll detect the emotion...",
|
| 42 |
+
lines=3,
|
| 43 |
+
),
|
| 44 |
+
outputs=gr.Label(label="Emotion Probabilities", num_top_classes=6),
|
| 45 |
+
title="Emotion Text Classifier",
|
| 46 |
+
description=(
|
| 47 |
+
"Detects 6 emotions in text: **sadness, joy, love, anger, fear, surprise**. "
|
| 48 |
+
"Fine-tuned RoBERTa model trained on the dair-ai/emotion dataset."
|
| 49 |
+
),
|
| 50 |
+
examples=examples,
|
| 51 |
+
allow_flagging="never",
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.38.0
|
| 2 |
+
torch>=2.0.0
|
| 3 |
+
datasets>=2.18.0
|
| 4 |
+
scikit-learn>=1.4.0
|
| 5 |
+
accelerate>=0.27.0
|
| 6 |
+
gradio>=4.19.0
|
| 7 |
+
huggingface_hub>=0.21.0
|