First commit
Browse files- app.py +106 -0
- readme.md +40 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
|
| 6 |
+
# Load the emotion classification model
|
| 7 |
+
model_id = "S-4-G-4-R/distilbert-base-uncased-finetuned-emotion"
|
| 8 |
+
classifier = pipeline("text-classification", model=model_id)
|
| 9 |
+
|
| 10 |
+
def classify_emotion(text):
|
| 11 |
+
"""
|
| 12 |
+
Classify the emotion in the given text and return results with visualization
|
| 13 |
+
"""
|
| 14 |
+
if not text.strip():
|
| 15 |
+
return None, "Please enter some text to analyze."
|
| 16 |
+
|
| 17 |
+
# Get predictions
|
| 18 |
+
preds = classifier(text, return_all_scores=True)[0]
|
| 19 |
+
|
| 20 |
+
# Create DataFrame
|
| 21 |
+
df = pd.DataFrame(preds)
|
| 22 |
+
df['score'] = df['score'] * 100 # Convert to percentage
|
| 23 |
+
df = df.sort_values('score', ascending=True)
|
| 24 |
+
|
| 25 |
+
# Create horizontal bar chart using Plotly
|
| 26 |
+
fig = go.Figure(go.Bar(
|
| 27 |
+
x=df['score'],
|
| 28 |
+
y=df['label'],
|
| 29 |
+
orientation='h',
|
| 30 |
+
marker=dict(color='steelblue')
|
| 31 |
+
))
|
| 32 |
+
|
| 33 |
+
fig.update_layout(
|
| 34 |
+
title=f'Emotion Classification Results',
|
| 35 |
+
xaxis_title='Probability (%)',
|
| 36 |
+
yaxis_title='Emotion',
|
| 37 |
+
height=400,
|
| 38 |
+
margin=dict(l=100, r=20, t=60, b=40)
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Format results as text
|
| 42 |
+
results_text = "**Prediction Results:**\n\n"
|
| 43 |
+
sorted_df = df.sort_values('score', ascending=False)
|
| 44 |
+
for _, row in sorted_df.iterrows():
|
| 45 |
+
results_text += f"- **{row['label']}**: {row['score']:.2f}%\n"
|
| 46 |
+
|
| 47 |
+
return fig, results_text
|
| 48 |
+
|
| 49 |
+
# Example texts
|
| 50 |
+
examples = [
|
| 51 |
+
"I was feeling very alone today walking down on road",
|
| 52 |
+
"I am so happy and excited about this new opportunity!",
|
| 53 |
+
"This makes me really angry and frustrated.",
|
| 54 |
+
"I'm scared about what might happen next.",
|
| 55 |
+
"What a beautiful day, I love this!",
|
| 56 |
+
"I feel so sad and disappointed about the news."
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
# Create Gradio interface
|
| 60 |
+
with gr.Blocks(title="Emotion Classifier") as demo:
|
| 61 |
+
gr.Markdown(
|
| 62 |
+
"""
|
| 63 |
+
# π Emotion Classification
|
| 64 |
+
Enter any text to analyze its emotional content. The model will classify it into different emotion categories.
|
| 65 |
+
|
| 66 |
+
**Model:** S-4-G-4-R/distilbert-base-uncased-finetuned-emotion
|
| 67 |
+
"""
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
with gr.Column():
|
| 72 |
+
text_input = gr.Textbox(
|
| 73 |
+
label="Enter text to analyze",
|
| 74 |
+
placeholder="Type your text here...",
|
| 75 |
+
lines=3
|
| 76 |
+
)
|
| 77 |
+
classify_btn = gr.Button("Classify Emotion", variant="primary")
|
| 78 |
+
|
| 79 |
+
with gr.Column():
|
| 80 |
+
results_text = gr.Markdown(label="Results")
|
| 81 |
+
|
| 82 |
+
plot_output = gr.Plot(label="Emotion Probabilities")
|
| 83 |
+
|
| 84 |
+
# Examples section
|
| 85 |
+
gr.Examples(
|
| 86 |
+
examples=examples,
|
| 87 |
+
inputs=text_input,
|
| 88 |
+
label="Try these examples:"
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Connect the button
|
| 92 |
+
classify_btn.click(
|
| 93 |
+
fn=classify_emotion,
|
| 94 |
+
inputs=text_input,
|
| 95 |
+
outputs=[plot_output, results_text]
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Also trigger on Enter key
|
| 99 |
+
text_input.submit(
|
| 100 |
+
fn=classify_emotion,
|
| 101 |
+
inputs=text_input,
|
| 102 |
+
outputs=[plot_output, results_text]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Launch the app
|
| 106 |
+
demo.launch()
|
readme.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Emotion Classifier
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Emotion Classification App
|
| 13 |
+
|
| 14 |
+
This is a Gradio web app for classifying emotions in text using the `S-4-G-4-R/distilbert-base-uncased-finetuned-emotion` model.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
|
| 18 |
+
- π Analyze text for emotional content
|
| 19 |
+
- π Interactive visualization of emotion probabilities
|
| 20 |
+
- π‘ Example texts to try
|
| 21 |
+
- π¨ User-friendly interface
|
| 22 |
+
|
| 23 |
+
## Model
|
| 24 |
+
|
| 25 |
+
The app uses a fine-tuned DistilBERT model specifically trained for emotion classification.
|
| 26 |
+
|
| 27 |
+
## Usage
|
| 28 |
+
|
| 29 |
+
Simply enter any text and click "Classify Emotion" to see the emotional analysis with probability scores for different emotions.
|
| 30 |
+
|
| 31 |
+
## Examples
|
| 32 |
+
|
| 33 |
+
Try these sample texts:
|
| 34 |
+
- "I was feeling very alone today walking down on road"
|
| 35 |
+
- "I am so happy and excited about this new opportunity!"
|
| 36 |
+
- "This makes me really angry and frustrated."
|
| 37 |
+
|
| 38 |
+
---
|
| 39 |
+
|
| 40 |
+
Built with Gradio and Transformers π€
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
gradio
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
plotly
|
| 6 |
+
pandas
|