saramneena's picture
Upload app.py
5a8befc 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)
# 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(
"""
<style>
body {
background-color: #000000; /* Black Background */
}
</style>
""",
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("<h1 style='text-align: center; color: white;'>πŸ” Emotion Detection 😊</h1>", unsafe_allow_html=True)
# πŸ“Œ **Business Context**
st.markdown(
"""
<h3 style='color: #00FFFF;'> Business Problem</h3>
<p style='text-align: justify; font-size: 16px; color: ##1E90FF;'>
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.
</p>
<h3 style='color: #00FFFF;'> Business Objective</h3>
<p style='text-align: justify; font-size: 16px; color: #D3D3D3;'>
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.
</p>
<h3 style='color: #00FFFF;'> Business Constraints</h3>
<p style='text-align: justify; font-size: 16px; color: #D3D3D3;'>
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.
</p>
""",
unsafe_allow_html=True
)
st.markdown("<h3 style='text-align: left; color: #00FFFF;'>πŸ“ 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].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"""
<div style="text-align: center; padding: 10px; border-radius: 10px; background-color: {text_color}; color: white; font-size: 24px;">
<b>Detected Emotion:</b> {predicted_emotion.capitalize()} {emoji_display}
</div>
""",
unsafe_allow_html=True
)
else:
st.warning("Please enter some text!")