import streamlit as st import random import time from streamlit.components.v1 import html # Set page config with light purple theme st.set_page_config( page_title="Emotion Mirror Chatbot", page_icon="😊", layout="centered", initial_sidebar_state="collapsed" ) # Custom CSS for enhanced light purple theme st.markdown(""" """, unsafe_allow_html=True) # Emotion databases POSITIVE_WORDS = {"happy", "awesome", "great", "joy", "excited", "good", "wonderful", "fantastic", "amazing", "yay", "ecstatic"} NEGATIVE_WORDS = {"sad", "depressed", "angry", "cry", "lonely", "bad", "terrible", "awful", "miserable", "upset", "grief"} LOVE_WORDS = {"love", "heart", "adore", "crush", "romance", "affection", "passion"} HELP_RESPONSES = [ "Would you like to talk about it? šŸ’¬", "I'm here to listen whenever you need šŸ’™", "Want some uplifting quotes? šŸ“œ", "Would a virtual hug help? šŸ¤—", "Let's focus on something positive 🌈", "Remember: this too shall pass šŸŒ¤ļø" ] # ASCII Art Library FACES = { "happy": r""" ╔════════════╗ šŸ˜„ AWESOME DAY! ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā• """, "sad": r""" ╔════════════╗ 😢 TOUGH TIMES? ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā• """, "neutral": r""" ╔════════════╗ 😐 HELLO THERE ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā• """, "love": r""" ╔════════════╗ šŸ˜ LOVELY FEELING! ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā• """, "angry": r""" ╔════════════╗ 😠 TAKE A DEEP BREATH ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā• """ } # Confetti effect using JavaScript def confetti_effect(): confetti_js = """ """ html(confetti_js) # Emotion detection function def detect_emotion(text): text = text.lower() if any(word in text for word in POSITIVE_WORDS): return "happy" elif any(word in text for word in NEGATIVE_WORDS): return "sad" elif any(word in text for word in LOVE_WORDS): return "love" elif "angry" in text or "mad" in text or "furious" in text: return "angry" return "neutral" # Initialize chat history if "messages" not in st.session_state: st.session_state.messages = [] st.session_state.current_emotion = "neutral" # Header with title and description st.markdown('
✨ Emotion Mirror Chatbot
I\'m a reactive AI agent that mirrors your emotions! Try words like happy, sad, love, or awesome
', unsafe_allow_html=True) # Display current face with st.container(): st.markdown(f"
\n{FACES[st.session_state.current_emotion]}\n
", unsafe_allow_html=True) # Display chat messages for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(f"
{message['content']}
", unsafe_allow_html=True) # User input if prompt := st.chat_input("How are you feeling today?"): # Add user message to chat history st.session_state.messages.append({"role": "user", "content": prompt}) # Detect emotion emotion = detect_emotion(prompt) st.session_state.current_emotion = emotion # Generate bot response if emotion == "happy": response = FACES["happy"] + "\n\n🌟 That's wonderful to hear! Keep spreading positivity!" confetti_effect() elif emotion == "sad": response = FACES["sad"] + "\n\n" + random.choice(HELP_RESPONSES) elif emotion == "love": response = FACES["love"] + "\n\nšŸ’– Love is the most beautiful feeling! Treasure it." elif emotion == "angry": response = FACES["angry"] + "\n\nā˜ļø Take a deep breath. Count to ten. You've got this." else: response = FACES["neutral"] + "\n\nTell me more about your feelings..." # Add bot response to chat history st.session_state.messages.append({"role": "bot", "content": response}) # Rerun to update the display st.rerun() # Add reset button if st.button("Reset Conversation"): st.session_state.messages = [] st.session_state.current_emotion = "neutral" st.rerun() # Footer st.markdown('', unsafe_allow_html=True)