| import streamlit as st |
| from transformers import AutoModelForSequenceClassification, AutoTokenizer |
| import torch |
|
|
| |
| model_name = "j-hartmann/emotion-english-distilroberta-base" |
| tokenizer = AutoTokenizer.from_pretrained(model_name) |
| model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
|
| |
| emotion_labels = model.config.id2label |
|
|
| |
| emotion_styles = { |
| "joy": {"emoji": "π", "color": "#FFD700"}, |
| "sadness": {"emoji": "π’", "color": "#3498DB"}, |
| "anger": {"emoji": "π‘", "color": "#8E44AD"}, |
| "fear": {"emoji": "π¨", "color": "#E74C3C"}, |
| "surprise": {"emoji": "π²", "color": "#F1C40F"}, |
| "disgust": {"emoji": "π€’", "color": "#2ECC71"}, |
| "neutral": {"emoji": "π", "color": "#95A5A6"} |
| } |
|
|
| |
| st.set_page_config(page_title="Emotion Detection", layout="centered") |
|
|
| |
| st.markdown( |
| """ |
| <style> |
| body { |
| background-color: #000000; /* Black Background */ |
| } |
| </style> |
| """, |
| unsafe_allow_html=True |
| ) |
|
|
| |
| st.image("innomatics_logo.png", use_container_width=True, width=300) |
|
|
| |
| st.markdown("<h1 style='text-align: center; color: white;'>π Emotion Detection π</h1>", unsafe_allow_html=True) |
|
|
| |
| st.markdown( |
| """ |
| <h3 style='color: #00FFFF;'> Business Problem</h3> |
| <p style='text-align: justify; font-size: 16px; color: BLACK;'> |
| 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: BLACK;'> |
| 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: BLACK;'> |
| 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_text = st.text_input("", placeholder="Type your text here...") |
|
|
| if st.button("Submit"): |
| if user_text: |
| |
| inputs = tokenizer(user_text, return_tensors="pt") |
|
|
| |
| with torch.no_grad(): |
| outputs = model(**inputs) |
|
|
| |
| scores = outputs.logits[0] |
| predicted_label_id = torch.argmax(scores).item() |
| predicted_emotion = emotion_labels[predicted_label_id].lower() |
|
|
| |
| emotion_data = emotion_styles.get(predicted_emotion, {"emoji": "π", "color": "#95A5A6"}) |
| emoji_display = emotion_data["emoji"] |
| text_color = emotion_data["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!") |
|
|