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(
"""
""",
unsafe_allow_html=True
)
# Header Section
st.image("innomatics-footer-logo.webp", use_container_width=True) # Replace with your image file
st.markdown("
🔍 Emotion Detection 😊
", unsafe_allow_html=True)
# Business Context
st.markdown(" 📊 Business Context
",unsafe_allow_html=True)
st.markdown("📌 Business Problem
", 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("🎯 Business Objective
", 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("⚖️ Business Constraints
", 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(" 📝 Enter Your Text Below
",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"""
Detected Emotion: {predicted_emotion.capitalize()} {emotion_data['emoji']}
""",
unsafe_allow_html=True
)
else:
st.warning("Please enter some text!")