Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
from gtts import gTTS | |
import os | |
from youtubesearchpython import VideosSearch | |
# Page configuration | |
st.set_page_config(page_title="Grief Support Bot", layout="wide") | |
st.markdown("<style>.stApp {background-color: #f0f5f9;}</style>", unsafe_allow_html=True) | |
# Load the pre-trained chatbot model | |
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium") | |
# Function for voice output | |
def speak_text(text): | |
tts = gTTS(text, lang='en') | |
tts.save("response.mp3") | |
os.system("mpg321 response.mp3") # This plays the audio | |
st.audio("response.mp3") | |
# Function to search for YouTube videos | |
def get_video_links(query, max_results=3): | |
search = VideosSearch(query, max_results=max_results) | |
results = search.result()['result'] | |
return [(video['title'], video['link']) for video in results] | |
# Function to detect crisis keywords | |
crisis_keywords = ["suicide", "hopeless", "alone", "depressed"] | |
def detect_crisis(input_text): | |
return any(word in input_text.lower() for word in crisis_keywords) | |
# Function to suggest hobbies | |
def suggest_hobbies(): | |
return ["Painting", "Gardening", "Writing", "Yoga", "Learning an instrument"] | |
# App header | |
st.title("Grief and Loss Support Bot") | |
# Maintain conversation history using session state | |
if 'history' not in st.session_state: | |
st.session_state['history'] = [] | |
# User input | |
user_input = st.text_input("You:") | |
# If there is input, process the conversation | |
if user_input: | |
# Crisis detection | |
if detect_crisis(user_input): | |
st.warning("We care about you. If you're in crisis, please reach out to a trusted person or call emergency services.") | |
st.markdown("Hotline resources: [Link to emergency contacts]") | |
else: | |
# Generate chatbot response | |
response = chatbot(user_input) | |
generated_text = response[0]['generated_text'] | |
# Append to history and display response | |
st.session_state['history'].append(("You", user_input)) | |
st.session_state['history'].append(("Bot", generated_text)) | |
# Display conversation history | |
for speaker, text in st.session_state['history']: | |
st.write(f"**{speaker}:** {text}") | |
# Voice output | |
speak_text(generated_text) | |
# Display hobby suggestions | |
st.markdown("### Hobby Suggestions") | |
st.markdown(", ".join(suggest_hobbies())) | |
# Provide interactive YouTube video links | |
st.markdown("### Videos to Help You Cope with Grief") | |
video_links = get_video_links("coping with grief and loss", max_results=3) | |
for title, link in video_links: | |
st.markdown(f"- [{title}]({link})") | |