Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,13 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline, GPT2LMHeadModel, GPT2Tokenizer
|
3 |
from gtts import gTTS
|
4 |
-
from pytube import Search
|
5 |
-
import os
|
6 |
import random
|
7 |
|
8 |
-
#
|
9 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
10 |
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
11 |
|
12 |
-
#
|
13 |
-
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
14 |
-
|
15 |
-
# Set up Streamlit page
|
16 |
st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
|
17 |
st.markdown("""
|
18 |
<style>
|
@@ -25,83 +20,61 @@ st.markdown("""
|
|
25 |
</style>
|
26 |
""", unsafe_allow_html=True)
|
27 |
|
28 |
-
# Title
|
29 |
st.title("Grief and Loss Support Bot 🌿")
|
30 |
st.subheader("Your compassionate companion in tough times 💚")
|
31 |
|
32 |
-
#
|
33 |
user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
|
34 |
|
35 |
# Store previous responses to check for repetition
|
36 |
if 'previous_responses' not in st.session_state:
|
37 |
st.session_state.previous_responses = []
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
new_responses = [resp for resp in new_responses if resp.lower() not in [prev.lower() for prev in st.session_state.previous_responses] and resp.lower() != user_input.lower()]
|
47 |
-
|
48 |
-
# If there are valid new responses, pick one, otherwise fallback
|
49 |
-
if new_responses:
|
50 |
-
selected_response = random.choice(new_responses)
|
51 |
-
else:
|
52 |
-
# If no new response, fallback to a more generic empathetic message
|
53 |
-
fallback_responses = [
|
54 |
-
"I understand how you're feeling. You're not alone in this. I'm here to listen and help.",
|
55 |
-
"I'm really sorry you're going through this. Let's take one step at a time. I'm here for you.",
|
56 |
-
"It sounds really tough right now. It's okay to feel overwhelmed. You're doing your best, and that's enough."
|
57 |
-
]
|
58 |
-
selected_response = random.choice(fallback_responses)
|
59 |
-
|
60 |
-
# Add extra empathetic phrases to the response
|
61 |
-
extra_empathy = [
|
62 |
-
"It’s completely normal to feel this way when things get tough. You're doing great by reaching out.",
|
63 |
-
"I know it can feel like a lot right now, but one step at a time. You're not alone in this.",
|
64 |
-
"Even in the toughest times, remember that there’s always support around you."
|
65 |
]
|
66 |
-
selected_response += " " + random.choice(extra_empathy)
|
67 |
|
68 |
-
#
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
#
|
72 |
-
|
|
|
|
|
73 |
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
audio_file = "response.mp3"
|
77 |
tts.save(audio_file)
|
78 |
st.audio(audio_file, format="audio/mp3")
|
79 |
-
|
80 |
-
# Suggest a productive activity based on detected keywords
|
81 |
-
if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad", "overwhelmed", "academic", "exam"]):
|
82 |
-
st.info("Here's a suggestion to help you cope:")
|
83 |
-
|
84 |
-
# Providing a variety of activities based on user mood and needs
|
85 |
-
activities = {
|
86 |
-
"journaling": "Express your feelings in writing. Journaling is a great way to process emotions.",
|
87 |
-
"yoga": "Yoga helps you relax and refocus. Try some deep breathing exercises or light stretching.",
|
88 |
-
"painting": "Creative expression through painting or drawing can be soothing and help you release pent-up emotions.",
|
89 |
-
"meditation": "Take a moment to calm your mind. Guided meditation can help reduce stress and anxiety.",
|
90 |
-
"exercise": "Physical activity can lift your mood. Even a short walk in nature can make a big difference."
|
91 |
-
}
|
92 |
-
|
93 |
-
# Randomly select an activity category to suggest
|
94 |
-
activity = random.choice(list(activities.keys()))
|
95 |
-
st.write(f"How about {activity}? {activities[activity]}")
|
96 |
-
|
97 |
-
# Search YouTube for videos related to the selected activity
|
98 |
-
search = Search(activity)
|
99 |
-
search_results = search.results[:3] # limit results to 3 videos
|
100 |
-
for video in search_results:
|
101 |
-
st.write(f"[{video.title}]({video.watch_url})")
|
102 |
-
|
103 |
-
# Crisis resources
|
104 |
-
crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
|
105 |
-
if any(keyword in user_input.lower() for keyword in crisis_keywords):
|
106 |
-
st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.")
|
107 |
-
st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline, GPT2LMHeadModel, GPT2Tokenizer
|
3 |
from gtts import gTTS
|
|
|
|
|
4 |
import random
|
5 |
|
6 |
+
# Load GPT-2 model and tokenizer from Hugging Face
|
7 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
8 |
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
9 |
|
10 |
+
# Set up Streamlit page configuration
|
|
|
|
|
|
|
11 |
st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
|
12 |
st.markdown("""
|
13 |
<style>
|
|
|
20 |
</style>
|
21 |
""", unsafe_allow_html=True)
|
22 |
|
23 |
+
# Title and introduction to the bot
|
24 |
st.title("Grief and Loss Support Bot 🌿")
|
25 |
st.subheader("Your compassionate companion in tough times 💚")
|
26 |
|
27 |
+
# User input
|
28 |
user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
|
29 |
|
30 |
# Store previous responses to check for repetition
|
31 |
if 'previous_responses' not in st.session_state:
|
32 |
st.session_state.previous_responses = []
|
33 |
|
34 |
+
# Function to generate a more empathetic and focused response
|
35 |
+
def generate_response(user_input):
|
36 |
+
# Predefined empathetic responses for cases of sadness and overwhelming stress
|
37 |
+
empathy_responses = [
|
38 |
+
"I'm really sorry you're going through this. It’s okay to feel this way, and I’m here to help you process it.",
|
39 |
+
"I understand how overwhelming things can feel right now. You're not alone. It’s important to take things one step at a time.",
|
40 |
+
"It sounds really tough, but reaching out is a big first step. You’re doing great. Take a deep breath. You're not alone in this."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
]
|
|
|
42 |
|
43 |
+
# Tailored coping suggestions based on the user's input
|
44 |
+
activity_suggestions = {
|
45 |
+
"journaling": "Journaling is a great way to process your emotions. Write down whatever comes to mind to help release the feelings you're carrying.",
|
46 |
+
"yoga": "Yoga can help you relax and find calm. Simple breathing exercises or gentle stretches might ease the tension you're feeling.",
|
47 |
+
"meditation": "Mindful meditation can help you center yourself and reduce stress. Even a few minutes can make a big difference.",
|
48 |
+
"exercise": "Physical activity can lift your mood and clear your mind. A short walk or some light exercise could help you feel better."
|
49 |
+
}
|
50 |
+
|
51 |
+
# Pick a relevant empathetic response
|
52 |
+
response = random.choice(empathy_responses)
|
53 |
+
|
54 |
+
# Based on keywords in the input, provide a relevant activity suggestion
|
55 |
+
if "exam" in user_input.lower() or "study" in user_input.lower():
|
56 |
+
activity = "journaling"
|
57 |
+
elif "stress" in user_input.lower() or "overwhelmed" in user_input.lower():
|
58 |
+
activity = "yoga"
|
59 |
+
else:
|
60 |
+
activity = random.choice(list(activity_suggestions.keys()))
|
61 |
|
62 |
+
# Add a coping activity suggestion to the response
|
63 |
+
response += f"\n\nHere's something you could try to help cope with how you're feeling:\n{activity_suggestions[activity]}"
|
64 |
+
|
65 |
+
return response
|
66 |
|
67 |
+
# Check if the user has typed something
|
68 |
+
if user_input:
|
69 |
+
# Generate the empathetic response
|
70 |
+
response = generate_response(user_input)
|
71 |
+
|
72 |
+
# Store and show the new response
|
73 |
+
st.session_state.previous_responses.append(response)
|
74 |
+
st.text_area("Bot's Response:", response, height=250)
|
75 |
+
|
76 |
+
# Text-to-speech output (optional)
|
77 |
+
tts = gTTS(response, lang='en')
|
78 |
audio_file = "response.mp3"
|
79 |
tts.save(audio_file)
|
80 |
st.audio(audio_file, format="audio/mp3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|