Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
from gtts import gTTS | |
from pytube import Search | |
import random | |
import os | |
# Initialize pretrained Hugging Face models | |
empathetic_response = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-empathy") | |
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") | |
# Configure Streamlit page | |
st.set_page_config(page_title="Grief & Loss Support Bot", page_icon="๐ฑ", layout="centered") | |
st.markdown(""" | |
<style> | |
.main { background-color: #F4F9F9; } | |
h1, h2, h3, h4 { color: #4C6D7D; } | |
.stTextInput>div>div>input { background-color: #E8F1F2; border-radius: 5px; } | |
.stButton>button { background-color: #A4D4AE; color: white; } | |
.stButton>button:hover { background-color: #92C8A6; } | |
</style> | |
""", unsafe_allow_html=True) | |
# Title and description | |
st.title("Grief & Loss Support Bot ๐ฑ") | |
st.subheader("Your compassionate companion in tough times ๐") | |
# User input | |
user_input = st.text_input("How are you feeling today?", placeholder="Share your thoughts here...", max_chars=500) | |
# Detect user sentiment and respond | |
if user_input: | |
sentiment = sentiment_analysis(user_input)[0] | |
empathy_response = empathetic_response(user_input, max_length=100)[0]['generated_text'] | |
# Display empathetic response | |
st.text_area("Bot's Response:", empathy_response, height=150) | |
# Convert text to speech for added comfort | |
tts = gTTS(empathy_response, lang='en') | |
audio_file = "response.mp3" | |
tts.save(audio_file) | |
st.audio(audio_file, format="audio/mp3") | |
# Activity Suggestion based on emotions detected | |
mood = sentiment['label'].lower() | |
activity_suggestions = { | |
"positive": ["try journaling your feelings", "practice yoga", "learn a new recipe"], | |
"neutral": ["take a short walk", "listen to calming music", "try some mindful breathing"], | |
"negative": ["explore creative activities like painting", "watch a motivational video", "write down small goals to feel organized"] | |
} | |
activities = activity_suggestions.get(mood, ["try relaxation exercises", "reach out to someone you trust"]) | |
# Show dynamic activity suggestion | |
st.info("Hereโs something you could try to lift your spirits:") | |
selected_activity = random.choice(activities) | |
st.write(f"**Activity Suggestion:** {selected_activity}") | |
# YouTube search for video resources | |
search = Search(selected_activity) | |
st.write("Recommended Videos:") | |
for video in search.results[:2]: # Display top 2 videos | |
st.write(f"[{video.title}]({video.watch_url})") | |
# Crisis response if critical keywords are detected | |
crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"] | |
if any(keyword in user_input.lower() for keyword in crisis_keywords): | |
st.error("It sounds like you're going through a very tough time. Please don't hesitate to reach out for help.") | |
st.write("[Click here for emergency resources](https://www.helpguide.org/find-help.htm)") | |