Spaces:
Sleeping
Sleeping
File size: 3,190 Bytes
fd64912 8f34be7 b4bd0de 9216a0a 80d269f 9216a0a 8f34be7 9216a0a 8f34be7 9216a0a 8f34be7 9216a0a 8f34be7 9216a0a 8f34be7 9216a0a 8f34be7 d7596e9 8f34be7 503bff0 8f34be7 503bff0 9216a0a 80d269f 9216a0a 8f34be7 9216a0a 503bff0 8f34be7 9216a0a 8f34be7 9216a0a 503bff0 8f34be7 9216a0a 8f34be7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import streamlit as st
from transformers import pipeline
from gtts import gTTS
from pytube import Search
import random
import os
# Initialize conversational and sentiment analysis models
conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
# Streamlit page configuration
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: #E8F1F2; border-radius: 5px; }
.stButton>button { background-color: #A9D0B6; color: white; }
.stButton>button:hover { background-color: #8FB79A; }
.stTextInput>div>label { color: #4C6D7D; }
</style>
""", unsafe_allow_html=True)
# Title and description
st.title("Grief and 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)
# Generate response
if user_input:
# Analyze sentiment
sentiment = sentiment_analysis(user_input)[0]
# Use conversational bot for response
response = conversational_bot(user_input, max_length=150)[0]['generated_text']
st.text_area("Bot's Response:", response, height=150)
# Text-to-speech conversion
tts = gTTS(response, lang='en')
audio_file = "response.mp3"
tts.save(audio_file)
st.audio(audio_file, format="audio/mp3")
# Suggest activity based on mood keywords
mood_keywords = {
"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"]
}
mood_type = "neutral" # Default category
if any(word in user_input.lower() for word in ["lonely", "lost", "sad", "stressed"]):
mood_type = "negative"
elif sentiment["label"] == "POSITIVE":
mood_type = "positive"
suggested_activity = random.choice(mood_keywords[mood_type])
st.info("Here's a suggestion to help lift your spirits:")
st.write(f"**Activity Suggestion:** {suggested_activity}")
# YouTube search for video resources
search = Search(suggested_activity)
st.write("Recommended Videos:")
for video in search.results[:2]: # Display top 2 videos
st.write(f"[{video.title}]({video.watch_url})")
# Crisis intervention resources
crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
if any(keyword in user_input.lower() for keyword in crisis_keywords):
st.warning("It sounds like you're going through a very tough time. Please reach out to someone you trust or a professional for support.")
st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")
|