Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,68 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
-
import
|
4 |
from youtubesearchpython import VideosSearch
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
#
|
|
|
10 |
st.markdown("""
|
11 |
<style>
|
12 |
-
.
|
13 |
-
|
14 |
-
}
|
15 |
-
.
|
16 |
-
|
17 |
-
}
|
18 |
</style>
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
st.
|
|
|
23 |
|
24 |
-
#
|
25 |
-
try:
|
26 |
-
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
27 |
-
except Exception as e:
|
28 |
-
st.error(f"Error loading the model: {e}")
|
29 |
-
|
30 |
-
# Set up text-to-speech engine
|
31 |
-
engine = pyttsx3.init()
|
32 |
-
engine.setProperty('rate', 150) # Speed of speech
|
33 |
-
engine.setProperty('volume', 0.9) # Volume level
|
34 |
-
voices = engine.getProperty('voices')
|
35 |
-
engine.setProperty('voice', voices[0].id) # Choose the desired voice
|
36 |
-
|
37 |
-
# User input for conversation
|
38 |
user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
|
39 |
|
|
|
40 |
if user_input:
|
41 |
-
#
|
42 |
-
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
st.
|
52 |
-
st.write("Here are some hobbies that might help you feel better:")
|
53 |
-
for hobby in hobbies:
|
54 |
-
st.write(f"- {hobby}")
|
55 |
|
56 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
+
from gtts import gTTS
|
4 |
from youtubesearchpython import VideosSearch
|
5 |
+
import os
|
6 |
|
7 |
+
# Initialize conversational models
|
8 |
+
empathy_bot = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-empathy")
|
9 |
+
general_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
10 |
+
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
11 |
|
12 |
+
# Set up 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: #D8E3E2; }
|
19 |
+
.stButton>button { background-color: #A9D0B6; color: white; border-radius: 5px; }
|
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 |
+
# Get user input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
|
31 |
|
32 |
+
# Check if user has entered text
|
33 |
if user_input:
|
34 |
+
# Run sentiment analysis for crisis detection
|
35 |
+
sentiment = sentiment_analysis(user_input)[0]
|
36 |
|
37 |
+
# Use empathy model for more specific support if the user is distressed
|
38 |
+
if sentiment['label'] == 'NEGATIVE' and sentiment['score'] > 0.8:
|
39 |
+
response = empathy_bot(user_input, max_length=150)[0]['generated_text']
|
40 |
+
else:
|
41 |
+
response = general_bot(user_input, max_length=150)[0]['generated_text']
|
42 |
+
|
43 |
+
# Display response
|
44 |
+
st.text_area("Bot's Response:", response, height=150)
|
|
|
|
|
|
|
45 |
|
46 |
+
# Text-to-speech output
|
47 |
+
tts = gTTS(response, lang='en')
|
48 |
+
audio_file = "response.mp3"
|
49 |
+
tts.save(audio_file)
|
50 |
+
st.audio(audio_file, format="audio/mp3", use_container_width=True)
|
51 |
|
52 |
+
# Suggest a productive activity based on detected keywords
|
53 |
+
if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad"]):
|
54 |
+
st.info("Here's a suggestion to help you cope:")
|
55 |
+
hobbies = ["journaling", "yoga", "painting"]
|
56 |
+
activity = st.selectbox("Choose an activity you'd like to try:", hobbies)
|
57 |
+
|
58 |
+
# Search YouTube for videos related to the selected activity
|
59 |
+
videosSearch = VideosSearch(activity, limit=2)
|
60 |
+
video_links = [result['link'] for result in videosSearch.result()['result']]
|
61 |
+
for link in video_links:
|
62 |
+
st.write(link)
|
63 |
|
64 |
+
# Crisis resources
|
65 |
+
crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
|
66 |
+
if any(keyword in user_input.lower() for keyword in crisis_keywords):
|
67 |
+
st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.")
|
68 |
+
st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")
|