import streamlit as st from transformers import pipeline import random from streamlit.components.v1 import html # ----------------- Page Config ----------------- st.set_page_config( page_title="SentiPal - Emotional AI Companion", layout="centered", initial_sidebar_state="collapsed" ) # ----------------- Enhanced CSS ----------------- st.markdown(""" """, unsafe_allow_html=True) # ----------------- Celebration Effects ----------------- def show_confetti(): # Simple confetti that works in Hugging Face Spaces confetti_js = """ """ html(confetti_js, height=0, width=0) def show_balloons(): # Use Streamlit's built-in balloons which work everywhere st.balloons() def show_snow(): # Use Streamlit's built-in snow which work everywhere st.snow() # ----------------- Content Collections ----------------- JOKES = [ "Why don't skeletons fight each other? They don't have the guts!", "What do you call a fake noodle? An impasta!", "Why did the scarecrow win an award? Because he was outstanding in his field!", "How do you organize a space party? You planet!", "Why did the math book look sad? Because it had too many problems.", "What do you call a bear with no teeth? A gummy bear!", "Why can't you trust an atom? Because they make up everything!", "What did one wall say to the other wall? I'll meet you at the corner!", "Why did the golfer bring two pairs of pants? In case he got a hole in one!", "What's orange and sounds like a parrot? A carrot!" ] GAMES = { "Would You Rather": [ "Have hands for feet or feet for hands?", "Live without internet or without air conditioning?", "Be able to fly or be invisible?", "Have unlimited money or unlimited time?", "Live in a castle or on a tropical island?" ], "Truth or Dare": [ "Tell me about your happiest memory", "Dance to a song for 10 seconds!", "What's your most embarrassing moment?", "Sing the chorus of your favorite song", "What's something you've never told anyone?" ], "This or That": [ "Pizza or burgers?", "Summer or winter?", "Movies or TV shows?", "Books or video games?", "City life or country life?" ] } ADVICE = [ "Try the 5-4-3-2-1 grounding technique: Name 5 things you see, 4 you can touch, 3 you can hear, 2 you can smell, and 1 you can taste", "Write down 3 things you're grateful for today", "Take 3 deep breaths - inhale for 4 counts, hold for 7, exhale for 8", "When feeling overwhelmed, break tasks into smaller 'bite-sized' pieces", "Practice positive self-talk - say kind things to yourself in the mirror", "Set small achievable goals to build confidence and momentum", "Take regular screen breaks - try the 20-20-20 rule (every 20 minutes, look at something 20 feet away for 20 seconds)", "Keep a worry journal - write down concerns and possible solutions", "Practice mindfulness by focusing on your current activity without judgment", "Connect with nature - even a short walk outside can improve mood" ] # ----------------- Session State ----------------- if "history" not in st.session_state: st.session_state.history = [] if "show_chat" not in st.session_state: st.session_state.show_chat = False if "models_loaded" not in st.session_state: st.session_state.models_loaded = False if "chat_memory" not in st.session_state: st.session_state.chat_memory = None # ----------------- Helpers ----------------- def add_to_chat(bot_message: str): st.session_state.history.append({"user": "", "bot": bot_message}) st.session_state.show_chat = True # show_balloons() # Show balloons on any interaction def load_models(): if not st.session_state.models_loaded: with st.spinner("Loading AI models..."): st.session_state.emotion_pipe = pipeline( "text-classification", model="j-hartmann/emotion-english-distilroberta-base" ) st.session_state.chatbot_pipe = pipeline( "text-generation", model="microsoft/DialoGPT-medium" ) st.session_state.models_loaded = True def detect_emotion(text): result = st.session_state.emotion_pipe(text)[0] return result['label'], result['score'] def get_bot_reply(user_input): response = st.session_state.chatbot_pipe( user_input, max_length=1000, do_sample=True, top_k=50, top_p=0.95, temperature=0.7, ) return response[0]['generated_text'] # ----------------- UI ----------------- st.markdown('
Your emotional AI companion 🤖💬
', unsafe_allow_html=True) # Front-page quick-help buttons st.markdown("### Need some help?") col1, col2, col3 = st.columns(3) if col1.button("😂 Tell me a joke", key="main_joke"): add_to_chat(random.choice(JOKES)) if col2.button("🎮 Play a game", key="main_game"): game_type = random.choice(list(GAMES.keys())) question = random.choice(GAMES[game_type]) add_to_chat(f"Let's play **{game_type}**! {question}") if col3.button("💡 Get advice", key="main_advice"): add_to_chat(f"Here's something to try: {random.choice(ADVICE)}") # Enhanced Celebration buttons st.markdown("### Need some cheer?") celeb_col1, celeb_col2, celeb_col3 = st.columns(3) if celeb_col1.button("🎉 Confetti", key="confetti"): show_confetti() add_to_chat("Enjoy the confetti! ✨") if celeb_col2.button("🎈 Balloons", key="balloons"): show_balloons() add_to_chat("Here come the balloons! 🎈") if celeb_col3.button("❄️ Snow", key="snow"): show_snow() add_to_chat("Snow is falling! ❄️") # Chat input user_input = st.text_input("💬 Say something to SentiPal...", key="chat_input") if st.button("Send", key="send_button") and user_input.strip(): load_models() with st.spinner("SentiPal is thinking..."): emotion, confidence = detect_emotion(user_input) reply = get_bot_reply(user_input) st.session_state.history.append({ "user": user_input, "bot": reply + f"\n\n_(detected emotion: **{emotion}**, {confidence*100:.1f}%)_" }) st.session_state.show_chat = True show_balloons() # Show balloons on message send # ----------------- Chat Display ----------------- if st.session_state.history: chat_visibility = "chat-visible" if st.session_state.show_chat else "" st.markdown(f"""