Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,73 +2,79 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
from pytube import Search
|
5 |
-
import random
|
6 |
import os
|
7 |
|
8 |
-
# Initialize
|
9 |
conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
10 |
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
11 |
|
12 |
-
# Streamlit page
|
13 |
st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="πΏ", layout="centered")
|
14 |
st.markdown("""
|
15 |
<style>
|
16 |
.css-1d391kg { background-color: #F3F7F6; }
|
17 |
.css-ffhzg2 { font-size: 1.5em; font-weight: 500; color: #4C6D7D; }
|
18 |
-
.stTextInput>div>div>input { background-color: #
|
19 |
-
.stButton>button { background-color: #A9D0B6; color: white; }
|
20 |
.stButton>button:hover { background-color: #8FB79A; }
|
21 |
.stTextInput>div>label { color: #4C6D7D; }
|
22 |
</style>
|
23 |
""", unsafe_allow_html=True)
|
24 |
|
25 |
-
# Title
|
26 |
st.title("Grief and Loss Support Bot πΏ")
|
27 |
st.subheader("Your compassionate companion in tough times π")
|
28 |
|
29 |
-
#
|
30 |
-
user_input = st.text_input("How are you feeling today?", placeholder="Share your
|
31 |
|
32 |
-
#
|
33 |
if user_input:
|
34 |
-
#
|
35 |
sentiment = sentiment_analysis(user_input)[0]
|
|
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
|
|
|
|
40 |
|
41 |
-
# Text-to-speech
|
42 |
-
tts = gTTS(
|
43 |
audio_file = "response.mp3"
|
44 |
tts.save(audio_file)
|
45 |
st.audio(audio_file, format="audio/mp3")
|
46 |
|
47 |
-
# Suggest activity
|
48 |
-
|
49 |
-
"
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
|
60 |
-
|
61 |
-
st.info("Here's a suggestion to help lift your spirits:")
|
62 |
-
st.write(f"**Activity Suggestion:** {suggested_activity}")
|
63 |
-
|
64 |
-
# YouTube search for video resources
|
65 |
-
search = Search(suggested_activity)
|
66 |
-
st.write("Recommended Videos:")
|
67 |
-
for video in search.results[:2]: # Display top 2 videos
|
68 |
-
st.write(f"[{video.title}]({video.watch_url})")
|
69 |
-
|
70 |
-
# Crisis intervention resources
|
71 |
crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
|
72 |
if any(keyword in user_input.lower() for keyword in crisis_keywords):
|
73 |
-
st.warning("It
|
74 |
st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")
|
|
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
from pytube import Search
|
|
|
5 |
import os
|
6 |
|
7 |
+
# Initialize models
|
8 |
conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
9 |
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
10 |
|
11 |
+
# Set up Streamlit page
|
12 |
st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="πΏ", layout="centered")
|
13 |
st.markdown("""
|
14 |
<style>
|
15 |
.css-1d391kg { background-color: #F3F7F6; }
|
16 |
.css-ffhzg2 { font-size: 1.5em; font-weight: 500; color: #4C6D7D; }
|
17 |
+
.stTextInput>div>div>input { background-color: #D8E3E2; }
|
18 |
+
.stButton>button { background-color: #A9D0B6; color: white; border-radius: 5px; }
|
19 |
.stButton>button:hover { background-color: #8FB79A; }
|
20 |
.stTextInput>div>label { color: #4C6D7D; }
|
21 |
</style>
|
22 |
""", unsafe_allow_html=True)
|
23 |
|
24 |
+
# Title
|
25 |
st.title("Grief and Loss Support Bot πΏ")
|
26 |
st.subheader("Your compassionate companion in tough times π")
|
27 |
|
28 |
+
# Get user input
|
29 |
+
user_input = st.text_input("How are you feeling today?", placeholder="Share what's on your mind...", max_chars=500)
|
30 |
|
31 |
+
# Check if user has entered text
|
32 |
if user_input:
|
33 |
+
# Run sentiment analysis to assess mood
|
34 |
sentiment = sentiment_analysis(user_input)[0]
|
35 |
+
sentiment_label = sentiment['label'].lower()
|
36 |
|
37 |
+
# Generate empathetic response
|
38 |
+
bot_response = conversational_bot(user_input, max_length=150)[0]['generated_text']
|
39 |
+
|
40 |
+
# Display response
|
41 |
+
st.text_area("Bot's Response:", bot_response, height=150)
|
42 |
|
43 |
+
# Text-to-speech output
|
44 |
+
tts = gTTS(bot_response, lang='en')
|
45 |
audio_file = "response.mp3"
|
46 |
tts.save(audio_file)
|
47 |
st.audio(audio_file, format="audio/mp3")
|
48 |
|
49 |
+
# Suggest an activity
|
50 |
+
if sentiment_label == "negative":
|
51 |
+
st.info("Here's a suggestion to help lift your spirits:")
|
52 |
+
activities = {
|
53 |
+
"relaxation": ["mindful breathing", "yoga"],
|
54 |
+
"creative": ["painting", "writing in a journal"],
|
55 |
+
"exercise": ["a short walk", "stretching"],
|
56 |
+
"self-care": ["listening to calming music", "meditation"]
|
57 |
+
}
|
58 |
+
|
59 |
+
# Choose activity based on keyword in input
|
60 |
+
if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad", "down"]):
|
61 |
+
activity_category = "self-care"
|
62 |
+
elif any(keyword in user_input.lower() for keyword in ["stress", "pressure", "overwhelmed"]):
|
63 |
+
activity_category = "relaxation"
|
64 |
+
else:
|
65 |
+
activity_category = "creative"
|
66 |
+
|
67 |
+
suggested_activity = activities[activity_category]
|
68 |
+
st.write(f"Activity Suggestion: {suggested_activity}")
|
69 |
|
70 |
+
# Search YouTube for videos related to the selected activity
|
71 |
+
search = Search(suggested_activity[0])
|
72 |
+
search_results = search.results[:2] # limit results to 2 videos
|
73 |
+
for video in search_results:
|
74 |
+
st.write(f"[{video.title}]({video.watch_url})")
|
75 |
|
76 |
+
# Crisis resources for critical keywords
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
|
78 |
if any(keyword in user_input.lower() for keyword in crisis_keywords):
|
79 |
+
st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.")
|
80 |
st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")
|