File size: 3,086 Bytes
fd64912
9216a0a
b4bd0de
9216a0a
80d269f
9216a0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7596e9
9216a0a
 
 
 
 
 
 
 
 
80d269f
9216a0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline
from gtts import gTTS
from pytube import Search
import random
import os

# Initialize pretrained Hugging Face models
empathetic_response = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-empathy")
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

# Configure Streamlit page
st.set_page_config(page_title="Grief & Loss Support Bot", page_icon="🌱", layout="centered")
st.markdown("""
    <style>
    .main { background-color: #F4F9F9; }
    h1, h2, h3, h4 { color: #4C6D7D; }
    .stTextInput>div>div>input { background-color: #E8F1F2; border-radius: 5px; }
    .stButton>button { background-color: #A4D4AE; color: white; }
    .stButton>button:hover { background-color: #92C8A6; }
    </style>
""", unsafe_allow_html=True)

# Title and description
st.title("Grief & Loss Support Bot 🌱")
st.subheader("Your compassionate companion in tough times 💚")

# User input
user_input = st.text_input("How are you feeling today?", placeholder="Share your thoughts here...", max_chars=500)

# Detect user sentiment and respond
if user_input:
    sentiment = sentiment_analysis(user_input)[0]
    empathy_response = empathetic_response(user_input, max_length=100)[0]['generated_text']
    
    # Display empathetic response
    st.text_area("Bot's Response:", empathy_response, height=150)
    
    # Convert text to speech for added comfort
    tts = gTTS(empathy_response, lang='en')
    audio_file = "response.mp3"
    tts.save(audio_file)
    st.audio(audio_file, format="audio/mp3")

    # Activity Suggestion based on emotions detected
    mood = sentiment['label'].lower()
    activity_suggestions = {
        "positive": ["try journaling your feelings", "practice yoga", "learn a new recipe"],
        "neutral": ["take a short walk", "listen to calming music", "try some mindful breathing"],
        "negative": ["explore creative activities like painting", "watch a motivational video", "write down small goals to feel organized"]
    }
    activities = activity_suggestions.get(mood, ["try relaxation exercises", "reach out to someone you trust"])
    
    # Show dynamic activity suggestion
    st.info("Here’s something you could try to lift your spirits:")
    selected_activity = random.choice(activities)
    st.write(f"**Activity Suggestion:** {selected_activity}")

    # YouTube search for video resources
    search = Search(selected_activity)
    st.write("Recommended Videos:")
    for video in search.results[:2]:  # Display top 2 videos
        st.write(f"[{video.title}]({video.watch_url})")
    
    # Crisis response if critical keywords are detected
    crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
    if any(keyword in user_input.lower() for keyword in crisis_keywords):
        st.error("It sounds like you're going through a very tough time. Please don't hesitate to reach out for help.")
        st.write("[Click here for emergency resources](https://www.helpguide.org/find-help.htm)")