Spaces:
Build error
Build error
| 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!") | |