Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
# Streamlit UI
|
| 5 |
+
st.set_page_config(page_title="Emotion Detection", layout="centered")
|
| 6 |
+
# π― Add Image at the Top
|
| 7 |
+
st.image("inno.jpg", use_container_width=True,width=300)
|
| 8 |
+
#1. Business and Data Understanding
|
| 9 |
+
st.header(" 1. Business and Data Understanding")
|
| 10 |
+
st.write("""
|
| 11 |
+
Understanding human emotions is critical for businesses that interact with customers, employees, or users through text, speech, or images.
|
| 12 |
+
Subjectivity of Emotions: Emotions can be complex, overlapping, and vary based on individual perceptions.
|
| 13 |
+
* Data Limitations: Labeled emotional datasets are often limited or imbalanced.
|
| 14 |
+
* Real-time Processing Needs: Businesses require fast, scalable solutions for handling large volumes of data.
|
| 15 |
+
* Multimodal Complexity: Emotions can be expressed through text, speech, or facial expressions, requiring different ML models.
|
| 16 |
+
* Privacy & Ethical Concerns: Emotion analysis can raise ethical and legal issues related to data privacy.""")
|
| 17 |
+
# b) Business Objective
|
| 18 |
+
st.subheader(" b) Business Objective")
|
| 19 |
+
st.write("""
|
| 20 |
+
The primary business objective of emotion detection is to understand, analyze, and
|
| 21 |
+
respond to human emotions in real time to improve customer experience,
|
| 22 |
+
employee engagement, marketing strategies, and decision-making.
|
| 23 |
+
Key Business Objectives:
|
| 24 |
+
1 Improve Customer Experience & Satisfaction:
|
| 25 |
+
- Detect customer emotions in support chats, emails, and voice calls.
|
| 26 |
+
- Provide personalized responses and proactive service to reduce customer frustration.
|
| 27 |
+
- Optimize chatbots and virtual assistants to improve engagement.
|
| 28 |
+
|
| 29 |
+
2 Enhance Brand Reputation & Market Intelligence:
|
| 30 |
+
- Analyze social media sentiments and product reviews to measure public perception.
|
| 31 |
+
- Identify negative feedback early to prevent PR crises.
|
| 32 |
+
- Improve product and service offerings based on emotional insights.
|
| 33 |
+
|
| 34 |
+
3 Increase Employee Engagement & Workplace Well-being:
|
| 35 |
+
- Analyze employee sentiment in feedback surveys, emails, and communication tools.
|
| 36 |
+
- Detect burnout, dissatisfaction, or stress levels in employees.
|
| 37 |
+
- Improve HR policies and work culture based on emotion analytics.
|
| 38 |
+
|
| 39 |
+
4 Optimize Marketing & Advertising Strategies:
|
| 40 |
+
- Measure emotional reactions to ads, videos, and brand campaigns.
|
| 41 |
+
- Personalize marketing content based on user emotions.
|
| 42 |
+
- Improve product recommendations and customer targeting.
|
| 43 |
+
|
| 44 |
+
5 Support Mental Health & Well-being:
|
| 45 |
+
- Detect signs of stress, anxiety, or depression in conversations.
|
| 46 |
+
- Provide AI-driven emotional support through chatbots and virtual therapy.
|
| 47 |
+
- Assist psychologists and therapists in tracking patient emotions.
|
| 48 |
+
|
| 49 |
+
6 Enable Real-Time Decision Making:
|
| 50 |
+
- Use emotion-based insights to make faster, data-driven business decisions.
|
| 51 |
+
- Improve customer retention by addressing negative sentiments proactively.
|
| 52 |
+
- Automate sentiment monitoring for large-scale data analysis.""")
|
| 53 |
+
# c) Business Constraints
|
| 54 |
+
st.subheader(" c) Business Constraints")
|
| 55 |
+
st.write("""
|
| 56 |
+
- *Data Privacy & Security*
|
| 57 |
+
- *Language & Emoji Variations*
|
| 58 |
+
- *Accuracy & Context Awareness*
|
| 59 |
+
- *scalability*
|
| 60 |
+
- *Real-Time Processing*
|
| 61 |
+
""")
|
| 62 |
+
|
| 63 |
+
# d) Data Understanding
|
| 64 |
+
st.subheader(" d) Data Understanding")
|
| 65 |
+
st.write("""
|
| 66 |
+
- *Dataset Size:* 422,746 text samples.
|
| 67 |
+
- *Data Types:* Text data, labeled emotions, unstructured text.
|
| 68 |
+
- *Data Features:*
|
| 69 |
+
*Raw Text:* Actual emotional expressions.
|
| 70 |
+
*Emojis & Symbols:* Indicators of sentiment.
|
| 71 |
+
""")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Load pre-trained emotion detection model
|
| 75 |
+
model_name = "j-hartmann/emotion-english-distilroberta-base"
|
| 76 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 77 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 78 |
+
|
| 79 |
+
# Get the actual emotion labels from the model
|
| 80 |
+
emotion_labels = model.config.id2label
|
| 81 |
+
|
| 82 |
+
# Emotion styles (emoji + colors)
|
| 83 |
+
emotion_styles = {
|
| 84 |
+
"joy": {"emoji": "π", "color": "#D3D3D3"},
|
| 85 |
+
"sadness": {"emoji": "π’", "color": "#3498BB"},
|
| 86 |
+
"anger": {"emoji": "π‘", "color": "#FFDAA9"},
|
| 87 |
+
"fear": {"emoji": "π¨", "color": "#FFFAAD"},
|
| 88 |
+
"surprise": {"emoji": "π²", "color": "#98CB98"},
|
| 89 |
+
"disgust": {"emoji": "π€’", "color": "#FFBFC1"},
|
| 90 |
+
"neutral": {"emoji": "π", "color": "#E6E6FA"}
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# π¨ Change Background Color
|
| 95 |
+
st.markdown(
|
| 96 |
+
"""
|
| 97 |
+
<style>
|
| 98 |
+
body {
|
| 99 |
+
background-color:#AFEEEE; /* Light Grayish Blue */
|
| 100 |
+
}
|
| 101 |
+
</style>
|
| 102 |
+
""",
|
| 103 |
+
unsafe_allow_html=True
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
st.markdown("<h1 style='text-align: center; color: #7B3B98;'>Emotion Detection using ML</h1>", unsafe_allow_html=True)
|
| 109 |
+
st.markdown("<h3 style='text-align: left; color: #3498BB;'>π Enter the text:</h3>", unsafe_allow_html=True)
|
| 110 |
+
|
| 111 |
+
# User Input
|
| 112 |
+
user_text = st.text_input("", placeholder="Type your text here...")
|
| 113 |
+
|
| 114 |
+
if st.button("Submit"):
|
| 115 |
+
if user_text:
|
| 116 |
+
# Tokenize input text
|
| 117 |
+
inputs = tokenizer(user_text, return_tensors="pt")
|
| 118 |
+
|
| 119 |
+
# Get model predictions
|
| 120 |
+
with torch.no_grad():
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
outputs = model(**inputs)
|
| 125 |
+
|
| 126 |
+
# Get the predicted emotion
|
| 127 |
+
scores = outputs.logits[0]
|
| 128 |
+
predicted_label_id = torch.argmax(scores).item()
|
| 129 |
+
predicted_emotion = emotion_labels[predicted_label_id].strip().lower()
|
| 130 |
+
|
| 131 |
+
# Get emoji & color
|
| 132 |
+
emotion_data = emotion_styles.get(predicted_emotion, {"emoji": "π", "color": "#D3D3D3"})
|
| 133 |
+
emoji_display = emotion_data["emoji"]
|
| 134 |
+
text_color = emotion_data["color"]
|
| 135 |
+
|
| 136 |
+
# Display Results with Color
|
| 137 |
+
st.markdown(
|
| 138 |
+
f"""
|
| 139 |
+
<div style="text-align: center; padding: 10px; border-radius: 10px; background-color: {text_color}; color: black; font-size: 24px;">
|
| 140 |
+
<b>Detected Emotion:</b> {predicted_emotion.capitalize()} {emoji_display}
|
| 141 |
+
</div>
|
| 142 |
+
""",
|
| 143 |
+
unsafe_allow_html=True
|
| 144 |
+
)
|
| 145 |
+
else:
|
| 146 |
+
st.warning("Please enter some text!")
|