Spaces:
Build error
Build error
File size: 5,336 Bytes
e7155e9 d672b27 e7155e9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
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!")
|