DasariHarshitha's picture
Update app.py
d672b27 verified
import streamlit as st
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
# Load Pre-trained Emotion Detection Model
MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
# Emotion Mapping (Emojis & Colors)
emotion_styles = {
"joy": {"emoji": "πŸ˜ƒ", "color": "#E6E6FA"},
"sadness": {"emoji": "😒", "color": "#3498DB"},
"anger": {"emoji": "😑", "color": "#FFDAB9"},
"fear": {"emoji": "😨", "color": "#FFFACD"},
"surprise": {"emoji": "😲", "color": "#98FB98"},
"disgust": {"emoji": "🀒", "color": "#FFB6C1"},
"neutral": {"emoji": "😐", "color": "#D3D3D3"}
}
# Configure Streamlit Page
st.set_page_config(page_title="Emotion Detection", layout="centered")
# Custom CSS for Background and Styling
st.markdown(
"""
<style>
body { background-color: black; color: white; }
.result-box {
text-align: center;
padding: 15px;
border-radius: 10px;
font-size: 22px;
font-weight: bold;
}
</style>
""",
unsafe_allow_html=True
)
# Header Section
st.image("innomatics-footer-logo.webp", use_container_width=True) # Replace with your image file
st.markdown("<h1 style='text-align: center; color: blue;'>πŸ” Emotion Detection 😊</h1>", unsafe_allow_html=True)
# Business Context
st.markdown("<h2 style='color: orange;'> πŸ“Š Business Context</h2>",unsafe_allow_html=True)
st.markdown("<h3 style='color: red;'>πŸ“Œ Business Problem</h3>", unsafe_allow_html=True)
st.markdown("""
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:
- **Missed opportunities** for improving customer experience.
- **Delayed insights** into customer satisfaction.
- **Inability to personalize interactions** based on real emotions.
An effective emotion detection system can help businesses analyze customer sentiments instantly, enabling proactive engagement and improved decision-making.
""", unsafe_allow_html=True)
st.markdown("<h3 style='color: green;'>🎯 Business Objective</h3>", unsafe_allow_html=True)
st.markdown("""
The primary goal of this Emotion Detection System is to enhance customer experience by identifying emotions in real-time from text-based interactions.
##### **Key Objectives:**
- βœ… **Real-time Emotion Analysis** – Detect emotions from customer messages, emails, and social media interactions.
- βœ… **Improved Customer Satisfaction** – Address negative sentiments promptly to enhance brand loyalty.
- βœ… **Personalized Engagement** – Tailor responses based on detected emotions for a better user experience.
- βœ… **Data-Driven Decisions** – Provide insights for optimizing services, marketing strategies, and customer interactions.
- βœ… **Operational Efficiency** – Automate sentiment analysis, reducing manual effort and response time.
""", unsafe_allow_html=True)
st.markdown("<h3 style='color: blue;'>βš–οΈ Business Constraints</h3>", unsafe_allow_html=True)
st.markdown("""
The system must meet the following business and technical constraints:
- 1️⃣ **Data Privacy & Compliance** – Must adhere to regulations like **GDPR** and **CCPA** to ensure user data protection.
- 2️⃣ **Real-time Processing** – The model should analyze and respond to emotions instantly, without significant delays.
- 3️⃣ **System Integration** – Should seamlessly integrate with **chatbots, CRMs, call centers, and social media platforms**.
- 4️⃣ **Accuracy & Reliability** – High **precision with minimal false positives** to avoid misinterpretations.
- 5️⃣ **Scalability** – Should efficiently handle **large-scale interactions** across multiple customer touchpoints.
- 6️⃣ **Cost-effectiveness** – Must be financially viable while delivering measurable ROI.
- 7️⃣ **Multi-language & Multi-model Support** – Capable of detecting emotions across **various languages** and communication channels **(text, voice, images)**.
""", unsafe_allow_html=True)
# User Input Section
st.markdown("<h2 style='color: purple;'> πŸ“ Enter Your Text Below</h2>",unsafe_allow_html=True)
user_text = st.text_input("", placeholder="Type your text here...")
# Emotion Prediction
if st.button("Predict Emotion"):
if user_text:
inputs = tokenizer(user_text, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
# Get predicted emotion
scores = outputs.logits[0]
predicted_label_id = torch.argmax(scores).item()
predicted_emotion = model.config.id2label[predicted_label_id].lower()
# Display Results
emotion_data = emotion_styles.get(predicted_emotion, {"emoji": "😐", "color": "#95A5A6"})
st.markdown(
f"""
<div class="result-box" style="background-color: {emotion_data['color']}; color: black;">
Detected Emotion: <b>{predicted_emotion.capitalize()} {emotion_data['emoji']}</b>
</div>
""",
unsafe_allow_html=True
)
else:
st.warning("Please enter some text!")