Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,112 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
# Load
|
| 6 |
-
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
st.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
st.
|
| 43 |
-
st.markdown("<
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load Pre-trained Emotion Detection Model
|
| 6 |
+
MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 9 |
+
|
| 10 |
+
# Emotion Mapping (Emojis & Colors)
|
| 11 |
+
emotion_styles = {
|
| 12 |
+
"joy": {"emoji": "π", "color": "#E6E6FA"},
|
| 13 |
+
"sadness": {"emoji": "π’", "color": "#3498DB"},
|
| 14 |
+
"anger": {"emoji": "π‘", "color": "#FFDAB9"},
|
| 15 |
+
"fear": {"emoji": "π¨", "color": "#FFFACD"},
|
| 16 |
+
"surprise": {"emoji": "π²", "color": "#98FB98"},
|
| 17 |
+
"disgust": {"emoji": "π€’", "color": "#FFB6C1"},
|
| 18 |
+
"neutral": {"emoji": "π", "color": "#D3D3D3"}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Configure Streamlit Page
|
| 22 |
+
st.set_page_config(page_title="Emotion Detection", layout="centered")
|
| 23 |
+
|
| 24 |
+
# Custom CSS for Background and Styling
|
| 25 |
+
st.markdown(
|
| 26 |
+
"""
|
| 27 |
+
<style>
|
| 28 |
+
body { background-color: black; color: white; }
|
| 29 |
+
.result-box {
|
| 30 |
+
text-align: center;
|
| 31 |
+
padding: 15px;
|
| 32 |
+
border-radius: 10px;
|
| 33 |
+
font-size: 22px;
|
| 34 |
+
font-weight: bold;
|
| 35 |
+
}
|
| 36 |
+
</style>
|
| 37 |
+
""",
|
| 38 |
+
unsafe_allow_html=True
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Header Section
|
| 42 |
+
st.image("innomatics-footer-logo.webp", use_container_width=True) # Replace with your image file
|
| 43 |
+
st.markdown("<h1 style='text-align: center; color: blue;'>π Emotion Detection π</h1>", unsafe_allow_html=True)
|
| 44 |
+
|
| 45 |
+
# Business Context
|
| 46 |
+
st.markdown("<h2 style='color: orange;'> π― Business Context</h2>",unsafe_allow_html=True)
|
| 47 |
+
|
| 48 |
+
st.markdown("<h3 style='color: red;'>π Business Problem</h3>", unsafe_allow_html=True)
|
| 49 |
+
st.markdown("""
|
| 50 |
+
Businesses struggle to understand customer emotions in real-time. Traditional feedback methods, such as surveys and reviews, fail to capture spontaneous emotional responses, leading to:
|
| 51 |
+
|
| 52 |
+
- **Missed opportunities** for improving customer experience.
|
| 53 |
+
- **Delayed insights** into customer satisfaction.
|
| 54 |
+
- **Inability to personalize interactions** based on real emotions.
|
| 55 |
+
|
| 56 |
+
An effective emotion detection system can help businesses analyze customer sentiments instantly, enabling proactive engagement and improved decision-making.
|
| 57 |
+
""", unsafe_allow_html=True)
|
| 58 |
+
|
| 59 |
+
st.markdown("<h3 style='color: green;'>π― Business Objective</h3>", unsafe_allow_html=True)
|
| 60 |
+
st.markdown("""
|
| 61 |
+
The primary goal of this Emotion Detection System is to enhance customer experience by identifying emotions in real-time from text-based interactions.
|
| 62 |
+
|
| 63 |
+
##### **Key Objectives:**
|
| 64 |
+
- β
**Real-time Emotion Analysis** β Detect emotions from customer messages, emails, and social media interactions.
|
| 65 |
+
- β
**Improved Customer Satisfaction** β Address negative sentiments promptly to enhance brand loyalty.
|
| 66 |
+
- β
**Personalized Engagement** β Tailor responses based on detected emotions for a better user experience.
|
| 67 |
+
- β
**Data-Driven Decisions** β Provide insights for optimizing services, marketing strategies, and customer interactions.
|
| 68 |
+
- β
**Operational Efficiency** β Automate sentiment analysis, reducing manual effort and response time.
|
| 69 |
+
""", unsafe_allow_html=True)
|
| 70 |
+
|
| 71 |
+
st.markdown("<h3 style='color: blue;'>βοΈ Business Constraints</h3>", unsafe_allow_html=True)
|
| 72 |
+
st.markdown("""
|
| 73 |
+
The system must meet the following business and technical constraints:
|
| 74 |
+
|
| 75 |
+
- 1οΈβ£ **Data Privacy & Compliance** β Must adhere to regulations like **GDPR** and **CCPA** to ensure user data protection.
|
| 76 |
+
- 2οΈβ£ **Real-time Processing** β The model should analyze and respond to emotions instantly, without significant delays.
|
| 77 |
+
- 3οΈβ£ **System Integration** β Should seamlessly integrate with **chatbots, CRMs, call centers, and social media platforms**.
|
| 78 |
+
- 4οΈβ£ **Accuracy & Reliability** β High **precision with minimal false positives** to avoid misinterpretations.
|
| 79 |
+
- 5οΈβ£ **Scalability** β Should efficiently handle **large-scale interactions** across multiple customer touchpoints.
|
| 80 |
+
- 6οΈβ£ **Cost-effectiveness** β Must be financially viable while delivering measurable ROI.
|
| 81 |
+
- 7οΈβ£ **Multi-language & Multi-model Support** β Capable of detecting emotions across **various languages** and communication channels **(text, voice, images)**.
|
| 82 |
+
""", unsafe_allow_html=True)
|
| 83 |
+
|
| 84 |
+
# User Input Section
|
| 85 |
+
st.markdown("<h2 style='color: purple;'> π Enter Your Text Below</h2>",unsafe_allow_html=True)
|
| 86 |
+
user_text = st.text_input("", placeholder="Type your text here...")
|
| 87 |
+
|
| 88 |
+
# Emotion Prediction
|
| 89 |
+
if st.button("Predict Emotion"):
|
| 90 |
+
if user_text:
|
| 91 |
+
inputs = tokenizer(user_text, return_tensors="pt")
|
| 92 |
+
|
| 93 |
+
with torch.no_grad():
|
| 94 |
+
outputs = model(**inputs)
|
| 95 |
+
|
| 96 |
+
# Get predicted emotion
|
| 97 |
+
scores = outputs.logits[0]
|
| 98 |
+
predicted_label_id = torch.argmax(scores).item()
|
| 99 |
+
predicted_emotion = model.config.id2label[predicted_label_id].lower()
|
| 100 |
+
|
| 101 |
+
# Display Results
|
| 102 |
+
emotion_data = emotion_styles.get(predicted_emotion, {"emoji": "π", "color": "#95A5A6"})
|
| 103 |
+
st.markdown(
|
| 104 |
+
f"""
|
| 105 |
+
<div class="result-box" style="background-color: {emotion_data['color']}; color: black;">
|
| 106 |
+
Detected Emotion: <b>{predicted_emotion.capitalize()} {emotion_data['emoji']}</b>
|
| 107 |
+
</div>
|
| 108 |
+
""",
|
| 109 |
+
unsafe_allow_html=True
|
| 110 |
+
)
|
| 111 |
+
else:
|
| 112 |
+
st.warning("Please enter some text!")
|