Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline, GPT2LMHeadModel, GPT2Tokenizer | |
from gtts import gTTS | |
from pytube import Search | |
import os | |
import random | |
# Initialize GPT-2 model and tokenizer from Hugging Face | |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") | |
model = GPT2LMHeadModel.from_pretrained("gpt2") | |
# Create a text generation pipeline using GPT-2 | |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer) | |
# Set up Streamlit page | |
st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered") | |
st.markdown(""" | |
<style> | |
.css-1d391kg { background-color: #F3F7F6; } | |
.css-ffhzg2 { font-size: 1.5em; font-weight: 500; color: #4C6D7D; } | |
.stTextInput>div>div>input { background-color: #D8E3E2; } | |
.stButton>button { background-color: #A9D0B6; color: white; border-radius: 5px; } | |
.stButton>button:hover { background-color: #8FB79A; } | |
.stTextInput>div>label { color: #4C6D7D; } | |
</style> | |
""", 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 responses to check for repetition | |
if 'previous_responses' not in st.session_state: | |
st.session_state.previous_responses = [] | |
# Check if user has entered text | |
if user_input: | |
# Refined prompt for better empathetic responses | |
prompt = f"The user is expressing their feelings of grief, loss, or overwhelm. Respond with empathy and support. User's input: {user_input}" | |
# Run the text generation model to generate a response based on user input | |
generated_responses = generator(prompt, max_length=250, num_return_sequences=3, temperature=0.7) | |
# Filter out any responses that are too similar to previous responses or user input | |
new_responses = [response['generated_text'] for response in generated_responses] | |
new_responses = [resp for resp in new_responses if resp.lower() not in [prev.lower() for prev in st.session_state.previous_responses] and resp.lower() != user_input.lower()] | |
# If there are valid new responses, pick one, otherwise fallback | |
if new_responses: | |
selected_response = random.choice(new_responses) | |
else: | |
# If no new response, fallback to a more generic empathetic message | |
fallback_responses = [ | |
"I understand how you're feeling. You're not alone in this. I'm here to listen and help.", | |
"I'm really sorry you're going through this. Let's take one step at a time. I'm here for you.", | |
"It sounds really tough right now. It's okay to feel overwhelmed. You're doing your best, and that's enough." | |
] | |
selected_response = random.choice(fallback_responses) | |
# Add extra empathetic phrases to the response | |
extra_empathy = [ | |
"It’s completely normal to feel this way when things get tough. You're doing great by reaching out.", | |
"I know it can feel like a lot right now, but one step at a time. You're not alone in this.", | |
"Even in the toughest times, remember that there’s always support around you." | |
] | |
selected_response += " " + random.choice(extra_empathy) | |
# Store the new response for future checks | |
st.session_state.previous_responses.append(selected_response) | |
# Display response | |
st.text_area("Bot's Response:", selected_response, height=250) | |
# Text-to-speech output | |
tts = gTTS(selected_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", "overwhelmed", "academic", "exam"]): | |
st.info("Here's a suggestion to help you cope:") | |
# Providing a variety of activities based on user mood and needs | |
activities = { | |
"journaling": "Express your feelings in writing. Journaling is a great way to process emotions.", | |
"yoga": "Yoga helps you relax and refocus. Try some deep breathing exercises or light stretching.", | |
"painting": "Creative expression through painting or drawing can be soothing and help you release pent-up emotions.", | |
"meditation": "Take a moment to calm your mind. Guided meditation can help reduce stress and anxiety.", | |
"exercise": "Physical activity can lift your mood. Even a short walk in nature can make a big difference." | |
} | |
# Randomly select an activity category to suggest | |
activity = random.choice(list(activities.keys())) | |
st.write(f"How about {activity}? {activities[activity]}") | |
# 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)") | |