Spaces:
Build error
Build error
Upload 3 files
Browse files- app.py +77 -0
- innomatics-footer-logo.webp +0 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load pre-trained emotion detection model
|
| 6 |
+
model_name = "j-hartmann/emotion-english-distilroberta-base"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Get the actual emotion labels from the model
|
| 11 |
+
emotion_labels = model.config.id2label
|
| 12 |
+
|
| 13 |
+
# Emotion styles (emoji + colors)
|
| 14 |
+
emotion_styles = {
|
| 15 |
+
"joy": {"emoji": "π", "color": "#E6E6FA"},
|
| 16 |
+
"sadness": {"emoji": "π’", "color": "#3498DB"},
|
| 17 |
+
"anger": {"emoji": "π‘", "color": "#FFDAB9"},
|
| 18 |
+
"fear": {"emoji": "π¨", "color": "#FFFACD"},
|
| 19 |
+
"surprise": {"emoji": "π²", "color": "#98FB98"},
|
| 20 |
+
"disgust": {"emoji": "π€’", "color": "#FFB6C1"},
|
| 21 |
+
"neutral": {"emoji": "π", "color": "#D3D3D3"}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# Streamlit UI
|
| 25 |
+
st.set_page_config(page_title="Emotion Detection", layout="centered")
|
| 26 |
+
|
| 27 |
+
# π¨ Change Background Color
|
| 28 |
+
st.markdown(
|
| 29 |
+
"""
|
| 30 |
+
<style>
|
| 31 |
+
body {
|
| 32 |
+
background-color:#AFEEEE; /* Light Grayish Blue */
|
| 33 |
+
}
|
| 34 |
+
</style>
|
| 35 |
+
""",
|
| 36 |
+
unsafe_allow_html=True
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# π― Add Image at the Top
|
| 40 |
+
st.image("innomatics-footer-logo.webp", use_container_width=True,width=300)
|
| 41 |
+
|
| 42 |
+
st.markdown("<h1 style='text-align: center; color: #7D3C98;'>Emotion Detection using ML</h1>", unsafe_allow_html=True)
|
| 43 |
+
st.markdown("<h3 style='text-align: left; color: #3498DB;'>π Enter the text:</h3>", unsafe_allow_html=True)
|
| 44 |
+
|
| 45 |
+
# User Input
|
| 46 |
+
user_text = st.text_input("", placeholder="Type your text here...")
|
| 47 |
+
|
| 48 |
+
if st.button("Submit"):
|
| 49 |
+
if user_text:
|
| 50 |
+
# Tokenize input text
|
| 51 |
+
inputs = tokenizer(user_text, return_tensors="pt")
|
| 52 |
+
|
| 53 |
+
# Get model predictions
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
outputs = model(**inputs)
|
| 56 |
+
|
| 57 |
+
# Get the predicted emotion
|
| 58 |
+
scores = outputs.logits[0]
|
| 59 |
+
predicted_label_id = torch.argmax(scores).item()
|
| 60 |
+
predicted_emotion = emotion_labels[predicted_label_id].strip().lower()
|
| 61 |
+
|
| 62 |
+
# Get emoji & color
|
| 63 |
+
emotion_data = emotion_styles.get(predicted_emotion, {"emoji": "π", "color": "#D3D3D3"})
|
| 64 |
+
emoji_display = emotion_data["emoji"]
|
| 65 |
+
text_color = emotion_data["color"]
|
| 66 |
+
|
| 67 |
+
# Display Results with Color
|
| 68 |
+
st.markdown(
|
| 69 |
+
f"""
|
| 70 |
+
<div style="text-align: center; padding: 10px; border-radius: 10px; background-color: {text_color}; color: black; font-size: 24px;">
|
| 71 |
+
<b>Detected Emotion:</b> {predicted_emotion.capitalize()} {emoji_display}
|
| 72 |
+
</div>
|
| 73 |
+
""",
|
| 74 |
+
unsafe_allow_html=True
|
| 75 |
+
)
|
| 76 |
+
else:
|
| 77 |
+
st.warning("Please enter some text!")
|
innomatics-footer-logo.webp
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformer
|
| 3 |
+
torch
|