Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import AutoModelForSequenceClassification, AutoTokenizer | |
| import torch | |
| # Streamlit UI | |
| st.set_page_config(page_title="Emotion Detection", layout="centered") | |
| # π― Add Image at the Top | |
| st.image("inno.jpg", use_container_width=True,width=300) | |
| #1. Business and Data Understanding | |
| st.header(" 1. Business and Data Understanding") | |
| st.write(""" | |
| Understanding human emotions is critical for businesses that interact with customers, employees, or users through text, speech, or images. | |
| Subjectivity of Emotions: Emotions can be complex, overlapping, and vary based on individual perceptions. | |
| * Data Limitations: Labeled emotional datasets are often limited or imbalanced. | |
| * Real-time Processing Needs: Businesses require fast, scalable solutions for handling large volumes of data. | |
| * Multimodal Complexity: Emotions can be expressed through text, speech, or facial expressions, requiring different ML models. | |
| * Privacy & Ethical Concerns: Emotion analysis can raise ethical and legal issues related to data privacy.""") | |
| # b) Business Objective | |
| st.subheader(" b) Business Objective") | |
| st.write(""" | |
| The primary business objective of emotion detection is to understand, analyze, and | |
| respond to human emotions in real time to improve customer experience, | |
| employee engagement, marketing strategies, and decision-making. | |
| Key Business Objectives: | |
| 1 Improve Customer Experience & Satisfaction: | |
| - Detect customer emotions in support chats, emails, and voice calls. | |
| - Provide personalized responses and proactive service to reduce customer frustration. | |
| - Optimize chatbots and virtual assistants to improve engagement. | |
| 2 Enhance Brand Reputation & Market Intelligence: | |
| - Analyze social media sentiments and product reviews to measure public perception. | |
| - Identify negative feedback early to prevent PR crises. | |
| - Improve product and service offerings based on emotional insights. | |
| 3 Increase Employee Engagement & Workplace Well-being: | |
| - Analyze employee sentiment in feedback surveys, emails, and communication tools. | |
| - Detect burnout, dissatisfaction, or stress levels in employees. | |
| - Improve HR policies and work culture based on emotion analytics. | |
| 4 Optimize Marketing & Advertising Strategies: | |
| - Measure emotional reactions to ads, videos, and brand campaigns. | |
| - Personalize marketing content based on user emotions. | |
| - Improve product recommendations and customer targeting. | |
| 5 Support Mental Health & Well-being: | |
| - Detect signs of stress, anxiety, or depression in conversations. | |
| - Provide AI-driven emotional support through chatbots and virtual therapy. | |
| - Assist psychologists and therapists in tracking patient emotions. | |
| 6 Enable Real-Time Decision Making: | |
| - Use emotion-based insights to make faster, data-driven business decisions. | |
| - Improve customer retention by addressing negative sentiments proactively. | |
| - Automate sentiment monitoring for large-scale data analysis.""") | |
| # c) Business Constraints | |
| st.subheader(" c) Business Constraints") | |
| st.write(""" | |
| - *Data Privacy & Security* | |
| - *Language & Emoji Variations* | |
| - *Accuracy & Context Awareness* | |
| - *scalability* | |
| - *Real-Time Processing* | |
| """) | |
| # d) Data Understanding | |
| st.subheader(" d) Data Understanding") | |
| st.write(""" | |
| - *Dataset Size:* 422,746 text samples. | |
| - *Data Types:* Text data, labeled emotions, unstructured text. | |
| - *Data Features:* | |
| *Raw Text:* Actual emotional expressions. | |
| *Emojis & Symbols:* Indicators of sentiment. | |
| """) | |
| # 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) | |
| # Get the actual emotion labels from the model | |
| emotion_labels = model.config.id2label | |
| # Emotion styles (emoji + colors) | |
| emotion_styles = { | |
| "joy": {"emoji": "π", "color": "#D3D3D3"}, | |
| "sadness": {"emoji": "π’", "color": "#3498BB"}, | |
| "anger": {"emoji": "π‘", "color": "#FFDAA9"}, | |
| "fear": {"emoji": "π¨", "color": "#FFFAAD"}, | |
| "surprise": {"emoji": "π²", "color": "#98CB98"}, | |
| "disgust": {"emoji": "π€’", "color": "#FFBFC1"}, | |
| "neutral": {"emoji": "π", "color": "#E6E6FA"} | |
| } | |
| # π¨ Change Background Color | |
| st.markdown( | |
| """ | |
| <style> | |
| body { | |
| background-color:#AFEEEE; /* Light Grayish Blue */ | |
| } | |
| </style> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| st.markdown("<h1 style='text-align: center; color: #7B3B98;'>Emotion Detection using ML</h1>", unsafe_allow_html=True) | |
| st.markdown("<h3 style='text-align: left; color: #3498BB;'>π Enter the text:</h3>", unsafe_allow_html=True) | |
| # User Input | |
| user_text = st.text_input("", placeholder="Type your text here...") | |
| if st.button("Submit"): | |
| if user_text: | |
| # Tokenize input text | |
| inputs = tokenizer(user_text, return_tensors="pt") | |
| # Get model predictions | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| # Get the predicted emotion | |
| scores = outputs.logits[0] | |
| predicted_label_id = torch.argmax(scores).item() | |
| predicted_emotion = emotion_labels[predicted_label_id].strip().lower() | |
| # Get emoji & color | |
| emotion_data = emotion_styles.get(predicted_emotion, {"emoji": "π", "color": "#D3D3D3"}) | |
| emoji_display = emotion_data["emoji"] | |
| text_color = emotion_data["color"] | |
| # Display Results with Color | |
| st.markdown( | |
| f""" | |
| <div style="text-align: center; padding: 10px; border-radius: 10px; background-color: {text_color}; color: black; font-size: 24px;"> | |
| <b>Detected Emotion:</b> {predicted_emotion.capitalize()} {emoji_display} | |
| </div> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| else: | |
| st.warning("Please enter some text!") |