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) # Get the actual emotion labels from the model emotion_labels = model.config.id2label # Emotion styles (emoji + colors) emotion_styles = { "joy": {"emoji": "😃", "color": "#FFD700"}, # Gold "sadness": {"emoji": "😢", "color": "#3498DB"}, # Blue "anger": {"emoji": "😡", "color": "#8E44AD"}, # Red "fear": {"emoji": "😨", "color": "#E74C3C"}, # Purple "surprise": {"emoji": "😲", "color": "#F1C40F"}, # Yellow "disgust": {"emoji": "🤢", "color": "#2ECC71"}, # Green "neutral": {"emoji": "😐", "color": "#95A5A6"} # Gray } # Streamlit UI st.set_page_config(page_title="Emotion Detection", layout="centered") # 🎨 Change Background Color to Black st.markdown( """ """, unsafe_allow_html=True ) # 🎯 Add Image at the Top st.image("innomatics_logo.png", use_container_width=True, width=300) # Replace with your image filename # 🏷️ Title st.markdown("

🔍 Emotion Detection 😊

", unsafe_allow_html=True) # 📌 **Business Context** st.markdown( """

Business Problem

Organizations aim to enhance customer experiences, mental health support, and marketing strategies by analyzing human emotions from text, speech, and facial expressions. However, accurately detecting emotions remains a challenge due to their subjective nature.

Business Objective

The project aims to create a Machine Learning-based Emotion Detection System that can accurately identify human emotions. This will aid businesses in analyzing customer sentiment, enhancing user engagement, and improving real-time decision-making.

Business Constraints

The model should provide high accuracy in detecting emotions, but complex models may be difficult for business users to interpret. Real-time processing and latency are crucial for applications like chatbots and customer support. Data quality and availability are essential, with a balanced dataset and handling of missing or noisy data. The model should be scalable, ethical, and privacy-conscious, integrating seamlessly with existing business tools. Cost constraints include compute costs for training and cloud deployment. The model should be robust and context-aware, working across multiple platforms.

""", unsafe_allow_html=True ) st.markdown("

📝 Enter the text:

", 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].lower() # Get emoji & color emotion_data = emotion_styles.get(predicted_emotion, {"emoji": "😐", "color": "#95A5A6"}) emoji_display = emotion_data["emoji"] text_color = emotion_data["color"] # Display Results with Color st.markdown( f"""
Detected Emotion: {predicted_emotion.capitalize()} {emoji_display}
""", unsafe_allow_html=True ) else: st.warning("Please enter some text!")