File size: 2,662 Bytes
fd64912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import streamlit as st
from transformers import pipeline
from gtts import gTTS
import os
from youtubesearchpython import VideosSearch

# Page configuration
st.set_page_config(page_title="Grief Support Bot", layout="wide")
st.markdown("<style>.stApp {background-color: #f0f5f9;}</style>", unsafe_allow_html=True)

# Load the pre-trained chatbot model
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")

# Function for voice output
def speak_text(text):
    tts = gTTS(text, lang='en')
    tts.save("response.mp3")
    os.system("mpg321 response.mp3")  # This plays the audio
    st.audio("response.mp3")

# Function to search for YouTube videos
def get_video_links(query, max_results=3):
    search = VideosSearch(query, max_results=max_results)
    results = search.result()['result']
    return [(video['title'], video['link']) for video in results]

# Function to detect crisis keywords
crisis_keywords = ["suicide", "hopeless", "alone", "depressed"]

def detect_crisis(input_text):
    return any(word in input_text.lower() for word in crisis_keywords)

# Function to suggest hobbies
def suggest_hobbies():
    return ["Painting", "Gardening", "Writing", "Yoga", "Learning an instrument"]

# App header
st.title("Grief and Loss Support Bot")

# Maintain conversation history using session state
if 'history' not in st.session_state:
    st.session_state['history'] = []

# User input
user_input = st.text_input("You:")

# If there is input, process the conversation
if user_input:
    # Crisis detection
    if detect_crisis(user_input):
        st.warning("We care about you. If you're in crisis, please reach out to a trusted person or call emergency services.")
        st.markdown("Hotline resources: [Link to emergency contacts]")
    else:
        # Generate chatbot response
        response = chatbot(user_input)
        generated_text = response[0]['generated_text']
        
        # Append to history and display response
        st.session_state['history'].append(("You", user_input))
        st.session_state['history'].append(("Bot", generated_text))
        
        # Display conversation history
        for speaker, text in st.session_state['history']:
            st.write(f"**{speaker}:** {text}")
        
        # Voice output
        speak_text(generated_text)

# Display hobby suggestions
st.markdown("### Hobby Suggestions")
st.markdown(", ".join(suggest_hobbies()))

# Provide interactive YouTube video links
st.markdown("### Videos to Help You Cope with Grief")
video_links = get_video_links("coping with grief and loss", max_results=3)
for title, link in video_links:
    st.markdown(f"- [{title}]({link})")