import streamlit as st from transformers import pipeline from gtts import gTTS from pytube import Search import os # Initialize conversational model for empathetic dialogue conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium") # Initialize sentiment analysis sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") # Set up Streamlit page st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered") st.markdown(""" """, unsafe_allow_html=True) # Title st.title("Grief and Loss Support Bot 🌿") st.subheader("Your compassionate companion in tough times 💚") # Get user input user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500) # Store previous response to check for repetition if 'previous_response' not in st.session_state: st.session_state.previous_response = "" # Check if user has entered text if user_input: # Run sentiment analysis to check for distress sentiment = sentiment_analysis(user_input)[0] # Generate empathetic response with increased length and more context response = conversational_bot(user_input, max_length=300, temperature=0.9, top_k=50, num_return_sequences=1) # Choose the response that does not repeat what the user said best_response = response[0]['generated_text'] # Ensure the response is supportive and does not repeat the user's input if user_input.lower() in best_response.lower(): best_response = "I understand how you're feeling. You're not alone in this. I'm here to listen and help." # Store the response for future comparison st.session_state.previous_response = best_response # Display response st.text_area("Bot's Response:", best_response, height=250) # Text-to-speech output tts = gTTS(best_response, lang='en') audio_file = "response.mp3" tts.save(audio_file) st.audio(audio_file, format="audio/mp3") # Suggest a productive activity based on detected keywords if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad"]): st.info("Here's a suggestion to help you cope:") hobbies = ["journaling", "yoga", "painting"] activity = st.selectbox("Choose an activity you'd like to try:", hobbies) # Search YouTube for videos related to the selected activity search = Search(activity) search_results = search.results[:3] # limit results to 3 videos for video in search_results: st.write(f"[{video.title}]({video.watch_url})") # Crisis resources crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"] if any(keyword in user_input.lower() for keyword in crisis_keywords): st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.") st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")