Spaces:
Running
Running
| import streamlit as st | |
| from transformers import pipeline | |
| import pandas as pd | |
| # Page setup | |
| st.set_page_config(page_title="π Emotion Guessing Game", layout="centered") | |
| # CSS for emoji bounce | |
| st.markdown(""" | |
| <style> | |
| .emoji-bounce { | |
| font-size: 60px; | |
| animation: bounce 1s infinite; | |
| text-align: center; | |
| } | |
| @keyframes bounce { | |
| 0%, 100% { transform: translateY(0px); } | |
| 50% { transform: translateY(-10px); } | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Emoji for each emotion | |
| emoji_dict = { | |
| "joy": "π", "sadness": "π’", "anger": "π ", | |
| "fear": "π±", "love": "β€οΈ", "surprise": "π²" | |
| } | |
| # Online sound for each emotion | |
| sound_urls = { | |
| "joy": "https://www.soundjay.com/human/sounds/applause-8.mp3", | |
| "sadness": "https://www.soundjay.com/human/sounds/sigh-01.mp3", | |
| "anger": "https://www.soundjay.com/human/sounds/angry-shout-1.mp3", | |
| "fear": "https://www.soundjay.com/human/sounds/scream-02.mp3", | |
| "love": "https://www.soundjay.com/button/beep-07.mp3", | |
| "surprise": "https://www.soundjay.com/button/button-2.mp3" | |
| } | |
| # Load emotion detection model | |
| def load_model(): | |
| return pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion") | |
| classifier = load_model() | |
| # Title | |
| st.title("π Emotion Guessing Game") | |
| st.markdown("Type a sentence and let AI guess your emotion!") | |
| # Input | |
| text = st.text_input("π¬ How are you feeling today?") | |
| if text: | |
| result = classifier(text)[0] | |
| emotion = result["label"] | |
| confidence = round(result["score"] * 100, 2) | |
| emoji = emoji_dict.get(emotion, "π€") | |
| # Show animated emoji | |
| st.markdown(f"<div class='emoji-bounce'>{emoji}</div>", unsafe_allow_html=True) | |
| st.markdown(f"### Emotion Detected: **{emotion}** {emoji}") | |
| st.markdown(f"Confidence: {confidence}%") | |
| # π Confetti for positive emotions | |
| if emotion in ["joy", "love", "surprise"]: | |
| st.balloons() | |
| # π Auto play sound | |
| if emotion in sound_urls: | |
| st.audio(sound_urls[emotion], format="audio/mp3", start_time=0) | |
| # Show full emotion chart | |
| all_results = classifier(text) | |
| df = pd.DataFrame({ | |
| "Emotion": [r["label"] for r in all_results], | |
| "Score": [round(r["score"] * 100, 2) for r in all_results] | |
| }) | |
| st.subheader("π Emotion Confidence Scores") | |
| st.bar_chart(df.set_index("Emotion")) | |