File size: 5,259 Bytes
fd64912
56b5bd3
b4bd0de
9216a0a
 
7f87605
9216a0a
56b5bd3
 
 
f1985ef
56b5bd3
 
162b1ba
9dfe35d
8f34be7
9216a0a
 
8f34be7
 
9dfe35d
 
8f34be7
 
9216a0a
 
 
9dfe35d
8f34be7
9216a0a
 
9dfe35d
7645fdc
9216a0a
7f87605
 
 
2951ecb
9dfe35d
d7596e9
56b5bd3
3db2ee2
f1985ef
9812384
56b5bd3
9812384
62d4ef4
7f87605
 
 
 
 
 
 
 
 
 
 
 
30d702b
 
 
 
 
 
 
 
7f87605
 
f1985ef
9dfe35d
7f87605
503bff0
f1985ef
7f87605
9216a0a
80d269f
f1985ef
9216a0a
7645fdc
56b5bd3
4d516ad
56b5bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import streamlit as st
from transformers import pipeline, GPT2LMHeadModel, GPT2Tokenizer
from gtts import gTTS
from pytube import Search
import os
import random

# Initialize GPT-2 model and tokenizer from Hugging Face
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")

# Create a text generation pipeline using GPT-2
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)

# 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 responses to check for repetition
if 'previous_responses' not in st.session_state:
    st.session_state.previous_responses = []

# Check if user has entered text
if user_input:
    # Run the text generation model to generate a response based on user input
    generated_responses = generator(user_input, max_length=250, num_return_sequences=3, temperature=0.7)

    # Filter out any responses that are too similar to previous responses or user input
    new_responses = [response['generated_text'] for response in generated_responses]
    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()]

    # If there are valid new responses, pick one, otherwise fallback
    if new_responses:
        selected_response = random.choice(new_responses)
    else:
        # If no new response, fallback to a more generic empathetic message
        fallback_responses = [
            "I understand how you're feeling. You're not alone in this. I'm here to listen and help.",
            "I'm really sorry you're going through this. Let's take one step at a time. I'm here for you.",
            "It sounds really tough right now. It's okay to feel overwhelmed. You're doing your best, and that's enough."
        ]
        selected_response = random.choice(fallback_responses)

    # Add extra empathetic phrases to the response
    extra_empathy = [
        "It’s completely normal to feel this way when things get tough. You're doing great by reaching out.",
        "I know it can feel like a lot right now, but one step at a time. You're not alone in this.",
        "Even in the toughest times, remember that there’s always support around you."
    ]
    selected_response += " " + random.choice(extra_empathy)

    # Store the new response for future checks
    st.session_state.previous_responses.append(selected_response)

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

    # Text-to-speech output
    tts = gTTS(selected_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", "overwhelmed", "academic", "exam"]):
        st.info("Here's a suggestion to help you cope:")

        # Providing a variety of activities based on user mood and needs
        activities = {
            "journaling": "Express your feelings in writing. Journaling is a great way to process emotions.",
            "yoga": "Yoga helps you relax and refocus. Try some deep breathing exercises or light stretching.",
            "painting": "Creative expression through painting or drawing can be soothing and help you release pent-up emotions.",
            "meditation": "Take a moment to calm your mind. Guided meditation can help reduce stress and anxiety.",
            "exercise": "Physical activity can lift your mood. Even a short walk in nature can make a big difference."
        }

        # Randomly select an activity category to suggest
        activity = random.choice(list(activities.keys()))
        st.write(f"How about {activity}? {activities[activity]}")

        # 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)")