Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,74 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
from gtts import gTTS
|
4 |
from pytube import Search
|
5 |
import random
|
6 |
import os
|
7 |
|
8 |
-
# Initialize
|
9 |
-
|
|
|
10 |
|
11 |
-
#
|
12 |
-
st.set_page_config(page_title="Grief
|
13 |
st.markdown("""
|
14 |
<style>
|
15 |
-
.
|
16 |
-
|
17 |
.stTextInput>div>div>input { background-color: #E8F1F2; border-radius: 5px; }
|
18 |
-
.stButton>button { background-color: #
|
19 |
-
.stButton>button:hover { background-color: #
|
|
|
20 |
</style>
|
21 |
""", unsafe_allow_html=True)
|
22 |
|
23 |
# Title and description
|
24 |
-
st.title("Grief
|
25 |
st.subheader("Your compassionate companion in tough times π")
|
26 |
|
27 |
# User input
|
28 |
user_input = st.text_input("How are you feeling today?", placeholder="Share your thoughts here...", max_chars=500)
|
29 |
|
30 |
-
# Generate
|
31 |
if user_input:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
)[
|
37 |
-
|
38 |
-
# Display empathetic response
|
39 |
st.text_area("Bot's Response:", response, height=150)
|
40 |
|
41 |
-
#
|
42 |
tts = gTTS(response, lang='en')
|
43 |
audio_file = "response.mp3"
|
44 |
tts.save(audio_file)
|
45 |
st.audio(audio_file, format="audio/mp3")
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
"positive": ["try journaling your feelings", "practice yoga", "learn a new recipe"],
|
50 |
"neutral": ["take a short walk", "listen to calming music", "try some mindful breathing"],
|
51 |
"negative": ["explore creative activities like painting", "watch a motivational video", "write down small goals to feel organized"]
|
52 |
}
|
53 |
-
activities = activity_suggestions["neutral"] # Default to "neutral" for simplicity
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# YouTube search for video resources
|
61 |
-
search = Search(
|
62 |
st.write("Recommended Videos:")
|
63 |
for video in search.results[:2]: # Display top 2 videos
|
64 |
st.write(f"[{video.title}]({video.watch_url})")
|
65 |
|
66 |
-
# Crisis
|
67 |
crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
|
68 |
if any(keyword in user_input.lower() for keyword in crisis_keywords):
|
69 |
-
st.
|
70 |
-
st.write("[
|
|
|
1 |
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 conversational and sentiment analysis models
|
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 configuration
|
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: #E8F1F2; border-radius: 5px; }
|
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 and description
|
26 |
+
st.title("Grief and Loss Support Bot πΏ")
|
27 |
st.subheader("Your compassionate companion in tough times π")
|
28 |
|
29 |
# User input
|
30 |
user_input = st.text_input("How are you feeling today?", placeholder="Share your thoughts here...", max_chars=500)
|
31 |
|
32 |
+
# Generate response
|
33 |
if user_input:
|
34 |
+
# Analyze sentiment
|
35 |
+
sentiment = sentiment_analysis(user_input)[0]
|
36 |
+
|
37 |
+
# Use conversational bot for response
|
38 |
+
response = conversational_bot(user_input, max_length=150)[0]['generated_text']
|
|
|
|
|
39 |
st.text_area("Bot's Response:", response, height=150)
|
40 |
|
41 |
+
# Text-to-speech conversion
|
42 |
tts = gTTS(response, lang='en')
|
43 |
audio_file = "response.mp3"
|
44 |
tts.save(audio_file)
|
45 |
st.audio(audio_file, format="audio/mp3")
|
46 |
|
47 |
+
# Suggest activity based on mood keywords
|
48 |
+
mood_keywords = {
|
49 |
"positive": ["try journaling your feelings", "practice yoga", "learn a new recipe"],
|
50 |
"neutral": ["take a short walk", "listen to calming music", "try some mindful breathing"],
|
51 |
"negative": ["explore creative activities like painting", "watch a motivational video", "write down small goals to feel organized"]
|
52 |
}
|
|
|
53 |
|
54 |
+
mood_type = "neutral" # Default category
|
55 |
+
if any(word in user_input.lower() for word in ["lonely", "lost", "sad", "stressed"]):
|
56 |
+
mood_type = "negative"
|
57 |
+
elif sentiment["label"] == "POSITIVE":
|
58 |
+
mood_type = "positive"
|
59 |
+
|
60 |
+
suggested_activity = random.choice(mood_keywords[mood_type])
|
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 sounds like you're going through a very tough time. Please reach out to someone you trust or a professional for support.")
|
74 |
+
st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")
|