Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
from gtts import gTTS | |
from pytube import Search | |
import os | |
# Initialize models | |
conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium") | |
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(""" | |
<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("How are you feeling today?", placeholder="Share what's on your mind...", max_chars=500) | |
# Check if user has entered text | |
if user_input: | |
# Run sentiment analysis to assess mood | |
try: | |
sentiment = sentiment_analysis(user_input)[0] | |
sentiment_label = sentiment['label'].lower() | |
st.write(f"Sentiment detected: {sentiment_label}") # Debugging line | |
except Exception as e: | |
st.write(f"Error in sentiment analysis: {e}") # Error handling | |
# Generate empathetic response from the conversational bot | |
try: | |
bot_response = conversational_bot(user_input, max_length=150)[0]['generated_text'] | |
st.write(f"Bot Response: {bot_response}") # Debugging line | |
except Exception as e: | |
st.write(f"Error generating bot response: {e}") # Error handling | |
# Display response | |
st.text_area("Bot's Response:", bot_response, height=150) | |
# Text-to-speech output | |
tts = gTTS(bot_response, lang='en') | |
audio_file = "response.mp3" | |
tts.save(audio_file) | |
st.audio(audio_file, format="audio/mp3") | |
# Suggest an activity | |
if sentiment_label == "negative": | |
st.info("Here's a suggestion to help lift your spirits:") | |
activities = { | |
"relaxation": ["mindful breathing", "yoga"], | |
"creative": ["painting", "writing in a journal"], | |
"exercise": ["a short walk", "stretching"], | |
"self-care": ["listening to calming music", "meditation"] | |
} | |
# Choose activity based on keyword in input | |
if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad", "down"]): | |
activity_category = "self-care" | |
elif any(keyword in user_input.lower() for keyword in ["stress", "pressure", "overwhelmed"]): | |
activity_category = "relaxation" | |
else: | |
activity_category = "creative" | |
suggested_activity = activities[activity_category] | |
st.write(f"Activity Suggestion: {suggested_activity}") | |
# Search YouTube for videos related to the selected activity | |
search = Search(suggested_activity[0]) | |
search_results = search.results[:2] # limit results to 2 videos | |
for video in search_results: | |
st.write(f"[{video.title}]({video.watch_url})") | |
# Crisis resources for critical keywords | |
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)") | |