Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from gtts import gTTS
|
4 |
+
import os
|
5 |
+
from youtubesearchpython import VideosSearch
|
6 |
+
|
7 |
+
# Page configuration
|
8 |
+
st.set_page_config(page_title="Grief Support Bot", layout="wide")
|
9 |
+
st.markdown("<style>.stApp {background-color: #f0f5f9;}</style>", unsafe_allow_html=True)
|
10 |
+
|
11 |
+
# Load the pre-trained chatbot model
|
12 |
+
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
13 |
+
|
14 |
+
# Function for voice output
|
15 |
+
def speak_text(text):
|
16 |
+
tts = gTTS(text, lang='en')
|
17 |
+
tts.save("response.mp3")
|
18 |
+
os.system("mpg321 response.mp3") # This plays the audio
|
19 |
+
st.audio("response.mp3")
|
20 |
+
|
21 |
+
# Function to search for YouTube videos
|
22 |
+
def get_video_links(query, max_results=3):
|
23 |
+
search = VideosSearch(query, max_results=max_results)
|
24 |
+
results = search.result()['result']
|
25 |
+
return [(video['title'], video['link']) for video in results]
|
26 |
+
|
27 |
+
# Function to detect crisis keywords
|
28 |
+
crisis_keywords = ["suicide", "hopeless", "alone", "depressed"]
|
29 |
+
|
30 |
+
def detect_crisis(input_text):
|
31 |
+
return any(word in input_text.lower() for word in crisis_keywords)
|
32 |
+
|
33 |
+
# Function to suggest hobbies
|
34 |
+
def suggest_hobbies():
|
35 |
+
return ["Painting", "Gardening", "Writing", "Yoga", "Learning an instrument"]
|
36 |
+
|
37 |
+
# App header
|
38 |
+
st.title("Grief and Loss Support Bot")
|
39 |
+
|
40 |
+
# Maintain conversation history using session state
|
41 |
+
if 'history' not in st.session_state:
|
42 |
+
st.session_state['history'] = []
|
43 |
+
|
44 |
+
# User input
|
45 |
+
user_input = st.text_input("You:")
|
46 |
+
|
47 |
+
# If there is input, process the conversation
|
48 |
+
if user_input:
|
49 |
+
# Crisis detection
|
50 |
+
if detect_crisis(user_input):
|
51 |
+
st.warning("We care about you. If you're in crisis, please reach out to a trusted person or call emergency services.")
|
52 |
+
st.markdown("Hotline resources: [Link to emergency contacts]")
|
53 |
+
else:
|
54 |
+
# Generate chatbot response
|
55 |
+
response = chatbot(user_input)
|
56 |
+
generated_text = response[0]['generated_text']
|
57 |
+
|
58 |
+
# Append to history and display response
|
59 |
+
st.session_state['history'].append(("You", user_input))
|
60 |
+
st.session_state['history'].append(("Bot", generated_text))
|
61 |
+
|
62 |
+
# Display conversation history
|
63 |
+
for speaker, text in st.session_state['history']:
|
64 |
+
st.write(f"**{speaker}:** {text}")
|
65 |
+
|
66 |
+
# Voice output
|
67 |
+
speak_text(generated_text)
|
68 |
+
|
69 |
+
# Display hobby suggestions
|
70 |
+
st.markdown("### Hobby Suggestions")
|
71 |
+
st.markdown(", ".join(suggest_hobbies()))
|
72 |
+
|
73 |
+
# Provide interactive YouTube video links
|
74 |
+
st.markdown("### Videos to Help You Cope with Grief")
|
75 |
+
video_links = get_video_links("coping with grief and loss", max_results=3)
|
76 |
+
for title, link in video_links:
|
77 |
+
st.markdown(f"- [{title}]({link})")
|