Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
+
|
| 7 |
+
# Define emotion colors for visualization
|
| 8 |
+
EMOTION_COLORS = {
|
| 9 |
+
'anger': '#E74C3C', # Red
|
| 10 |
+
'joy': '#F1C40F', # Yellow
|
| 11 |
+
'love': '#E91E63', # Pink
|
| 12 |
+
'sadness': '#3498DB', # Blue
|
| 13 |
+
'fear': '#7D3C98', # Purple
|
| 14 |
+
'surprise': '#2ECC71' # Green
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# Load model and tokenizer
|
| 18 |
+
print("Loading model and tokenizer...")
|
| 19 |
+
model_name = "bhadresh-savani/bert-base-uncased-emotion"
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 21 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 22 |
+
|
| 23 |
+
# Get emotion labels
|
| 24 |
+
id2label = model.config.id2label
|
| 25 |
+
emotions_list = list(id2label.values())
|
| 26 |
+
print(f"Model loaded. Emotions: {emotions_list}")
|
| 27 |
+
|
| 28 |
+
def analyze_emotion(text):
|
| 29 |
+
"""Analyze emotion in text and return visualization and results"""
|
| 30 |
+
if not text or not text.strip():
|
| 31 |
+
return None, {"error": "Please enter some text to analyze"}
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# Tokenize and get prediction
|
| 35 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 36 |
+
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
outputs = model(**inputs)
|
| 39 |
+
scores = torch.nn.functional.softmax(outputs.logits, dim=1).squeeze().numpy()
|
| 40 |
+
|
| 41 |
+
# Get emotion labels and their scores
|
| 42 |
+
emotion_scores = [(id2label[i], float(scores[i])) for i in range(len(scores))]
|
| 43 |
+
|
| 44 |
+
# Sort by scores
|
| 45 |
+
emotion_scores.sort(key=lambda x: x[1], reverse=True)
|
| 46 |
+
emotions, scores = zip(*emotion_scores)
|
| 47 |
+
|
| 48 |
+
# Create visualization
|
| 49 |
+
fig = create_visualization(emotions, scores, text)
|
| 50 |
+
|
| 51 |
+
# Format output
|
| 52 |
+
output = {
|
| 53 |
+
"dominant_emotion": emotions[0],
|
| 54 |
+
"confidence": f"{scores[0]*100:.1f}%",
|
| 55 |
+
"detailed_scores": {emotion: f"{score*100:.1f}%" for emotion, score in zip(emotions, scores)}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
return fig, output
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"Error in analyze_emotion: {str(e)}")
|
| 62 |
+
return None, {"error": f"Analysis failed: {str(e)}"}
|
| 63 |
+
|
| 64 |
+
def create_visualization(emotions, scores, text=None):
|
| 65 |
+
"""Create a bar chart visualization of emotion scores"""
|
| 66 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 67 |
+
|
| 68 |
+
# Use custom colors for the bars
|
| 69 |
+
colors = [EMOTION_COLORS.get(emotion, '#1f77b4') for emotion in emotions]
|
| 70 |
+
|
| 71 |
+
# Create horizontal bar chart
|
| 72 |
+
y_pos = np.arange(len(emotions))
|
| 73 |
+
ax.barh(y_pos, [score * 100 for score in scores], color=colors)
|
| 74 |
+
ax.set_yticks(y_pos)
|
| 75 |
+
ax.set_yticklabels(emotions)
|
| 76 |
+
ax.invert_yaxis() # Labels read top-to-bottom
|
| 77 |
+
ax.set_xlabel('Confidence (%)')
|
| 78 |
+
|
| 79 |
+
# Add value labels to the bars
|
| 80 |
+
for i, v in enumerate(scores):
|
| 81 |
+
ax.text(v * 100 + 1, i, f"{v*100:.1f}%", va='center')
|
| 82 |
+
|
| 83 |
+
# Set title with truncated text if provided
|
| 84 |
+
if text:
|
| 85 |
+
display_text = text if len(text) < 50 else text[:47] + "..."
|
| 86 |
+
ax.set_title(f'Emotion Analysis: "{display_text}"', pad=20)
|
| 87 |
+
else:
|
| 88 |
+
ax.set_title('Emotion Analysis', pad=20)
|
| 89 |
+
|
| 90 |
+
plt.tight_layout()
|
| 91 |
+
return fig
|
| 92 |
+
|
| 93 |
+
# Create Gradio interface
|
| 94 |
+
demo = gr.Interface(
|
| 95 |
+
fn=analyze_emotion,
|
| 96 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter text to analyze emotions...", label="Input Text"),
|
| 97 |
+
outputs=[gr.Plot(label="Emotion Distribution"), gr.JSON(label="Analysis Results")],
|
| 98 |
+
title="🧠 Emotion Analysis App",
|
| 99 |
+
description="This app analyzes emotions in text using BERT. It can detect six emotions: anger, joy, love, sadness, fear, and surprise.",
|
| 100 |
+
examples=[
|
| 101 |
+
["I'm so excited about this new opportunity!"],
|
| 102 |
+
["The news made me very sad and disappointed."],
|
| 103 |
+
["I can't believe what just happened! This is totally unexpected!"],
|
| 104 |
+
["I'm really angry about how they treated me."],
|
| 105 |
+
["I love spending time with my family and friends."],
|
| 106 |
+
["I'm terrified of what might happen next."]
|
| 107 |
+
]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Launch the app
|
| 111 |
+
demo.launch()
|