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 light purple theme st.markdown(""" """, unsafe_allow_html=True) # Emotion databases POSITIVE_WORDS = {"happy", "awesome", "great", "joy", "excited", "good", "wonderful", "fantastic", "amazing"} NEGATIVE_WORDS = {"sad", "depressed", "angry", "cry", "lonely", "bad", "terrible", "awful", "miserable"} HELP_RESPONSES = [ "Would you like to talk about it? šŸ’¬", "I'm here to listen šŸ’™", "Want some uplifting quotes? šŸ“œ", "Would a virtual hug help? šŸ¤—", "Let's focus on something positive 🌈" ] # ASCII Art Library FACES = { "happy": r""" ╔════════╗ šŸ˜„ AWESOME! ā•šā•ā•ā•ā•ā•ā•ā•ā•ā• """, "sad": r""" ╔════════╗ 😢 SAD DAY? ā•šā•ā•ā•ā•ā•ā•ā•ā•ā• """, "neutral": r""" ╔════════╗ 😐 HELLO ā•šā•ā•ā•ā•ā•ā•ā•ā•ā• """, "love": r""" ╔════════╗ šŸ˜ LOVELY! ā•šā•ā•ā•ā•ā•ā•ā•ā•ā• """ } # 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 "love" in text or "heart" in text: return "love" return "neutral" # Initialize chat history if "messages" not in st.session_state: st.session_state.messages = [] st.session_state.current_emotion = "neutral" # Title and description st.title("✨ Emotion Mirror Chatbot") st.markdown("I'm a reactive AI agent that mirrors your emotions! Try words like *happy*, *sad*, or *awesome*") # 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!" confetti_effect() elif emotion == "sad": response = FACES["sad"] + "\n\n" + random.choice(HELP_RESPONSES) elif emotion == "love": response = FACES["love"] + "\n\nšŸ’– Love is in the air!" 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.experimental_rerun() # Add reset button if st.button("Reset Conversation"): st.session_state.messages = [] st.session_state.current_emotion = "neutral" st.experimental_rerun()