File size: 3,598 Bytes
fd64912
8f34be7
b4bd0de
9216a0a
 
 
f1985ef
8f34be7
f1985ef
 
8f34be7
162b1ba
9dfe35d
8f34be7
9216a0a
 
8f34be7
 
9dfe35d
 
8f34be7
 
9216a0a
 
 
9dfe35d
8f34be7
9216a0a
 
9dfe35d
7645fdc
9216a0a
2951ecb
 
 
 
9dfe35d
d7596e9
f1985ef
7645fdc
f1985ef
5e480b6
 
f1985ef
 
 
 
2951ecb
 
 
5e480b6
2951ecb
 
 
f1985ef
9dfe35d
5e480b6
503bff0
f1985ef
 
9216a0a
80d269f
f1985ef
9216a0a
7645fdc
4d516ad
 
 
 
 
9dfe35d
7645fdc
2951ecb
9dfe35d
 
8f34be7
7645fdc
9216a0a
 
9dfe35d
8f34be7
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
79
80
81
82
import streamlit as st
from transformers import pipeline
from gtts import gTTS
from pytube import Search
import os

# Initialize conversational model for empathetic dialogue
conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")

# Initialize sentiment analysis
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

# Set up Streamlit page
st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
st.markdown("""
    <style>
    .css-1d391kg { background-color: #F3F7F6; }
    .css-ffhzg2 { font-size: 1.5em; font-weight: 500; color: #4C6D7D; }
    .stTextInput>div>div>input { background-color: #D8E3E2; }
    .stButton>button { background-color: #A9D0B6; color: white; border-radius: 5px; }
    .stButton>button:hover { background-color: #8FB79A; }
    .stTextInput>div>label { color: #4C6D7D; }
    </style>
""", unsafe_allow_html=True)

# Title
st.title("Grief and Loss Support Bot 🌿")
st.subheader("Your compassionate companion in tough times πŸ’š")

# Get user input
user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)

# Store previous response to check for repetition
if 'previous_response' not in st.session_state:
    st.session_state.previous_response = ""

# Check if user has entered text
if user_input:
    # Run sentiment analysis to check for distress
    sentiment = sentiment_analysis(user_input)[0]

    # Generate empathetic response with increased length and more context
    response = conversational_bot(user_input, max_length=300, num_return_sequences=1, temperature=0.9, top_k=50)[0]['generated_text']

    # Ensure response does not repeat what the user said, and is supportive
    if user_input.lower() in response.lower():
        response = "I understand how you're feeling. You're not alone in this. I'm here to listen and help."
    
    # Check if the response is too similar to the previous one, if so, generate a new one
    if response == st.session_state.previous_response:
        response = conversational_bot(user_input, max_length=300, num_return_sequences=1, temperature=0.9, top_k=50)[0]['generated_text']
    
    # Store the response for future comparison
    st.session_state.previous_response = response

    # Display response
    st.text_area("Bot's Response:", response, height=250)

    # Text-to-speech output
    tts = gTTS(response, lang='en')
    audio_file = "response.mp3"
    tts.save(audio_file)
    st.audio(audio_file, format="audio/mp3")

    # Suggest a productive activity based on detected keywords
    if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad"]):
        st.info("Here's a suggestion to help you cope:")
        hobbies = ["journaling", "yoga", "painting"]
        activity = st.selectbox("Choose an activity you'd like to try:", hobbies)
        
        # Search YouTube for videos related to the selected activity
        search = Search(activity)
        search_results = search.results[:3]  # limit results to 3 videos
        for video in search_results:
            st.write(f"[{video.title}]({video.watch_url})")

    # Crisis resources
    crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
    if any(keyword in user_input.lower() for keyword in crisis_keywords):
        st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.")
        st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")