File size: 3,388 Bytes
fd64912
a5fe917
b4bd0de
80d269f
 
 
 
fd64912
80d269f
 
b1dc82e
80d269f
 
 
 
 
fd64912
80d269f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1dc82e
80d269f
 
 
 
 
 
d7596e9
 
80d269f
a5fe917
80d269f
 
 
b1dc82e
80d269f
 
 
b1dc82e
80d269f
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline
from gtts import gTTS
from io import BytesIO
import random
import requests
from youtubesearchpython import VideosSearch

# Initialize the empathy bot pipeline
empathy_bot = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-empathy")

# Function to process the user's input and generate a dynamic response
def generate_dynamic_response(user_input):
    # Generate an empathetic response using the model
    response = empathy_bot(user_input)
    empathetic_response = response[0]['generated_text']

    # Possible activities to suggest
    activities = {
        "relaxation": [
            "Deep Breathing Exercises",
            "Guided Meditation",
            "Mindfulness Breathing"
        ],
        "creative": [
            "Journaling your thoughts",
            "Drawing or doodling",
            "Writing a poem"
        ],
        "exercise": [
            "Try some yoga",
            "Go for a walk outside",
            "Stretching exercises to release tension"
        ],
        "self-care": [
            "Take a warm bath",
            "Listen to calming music",
            "Treat yourself with some relaxation time"
        ]
    }

    # Logic to determine context of user input (e.g., stress, sadness, etc.)
    if "sad" in user_input or "overwhelmed" in user_input:
        suggested_activity = random.choice(activities["relaxation"])
    elif "bored" in user_input or "unmotivated" in user_input:
        suggested_activity = random.choice(activities["creative"])
    elif "stressed" in user_input or "tired" in user_input:
        suggested_activity = random.choice(activities["exercise"])
    else:
        suggested_activity = random.choice(activities["self-care"])

    # Search YouTube for relevant videos based on the activity suggestion
    video_keywords = suggested_activity
    video_results = search_youtube_videos(video_keywords)

    return empathetic_response, suggested_activity, video_results

# Function to search for YouTube videos
def search_youtube_videos(query):
    videos_search = VideosSearch(query, limit=3)
    results = videos_search.result()

    # Collect video details
    video_links = []
    for video in results['videos']:
        title = video['title']
        link = video['link']
        video_links.append((title, link))

    return video_links

# Streamlit UI
st.title("Grief and Loss Support Bot 🌿")
st.markdown("Your compassionate companion in tough times πŸ’š")
user_input = st.text_input("Share what's on your mind...")

if user_input:
    empathetic_response, suggested_activity, video_results = generate_dynamic_response(user_input)

    # Display the empathetic response
    st.write(f"**Bot's Response:**")
    st.write(empathetic_response)

    # Suggest an activity
    st.write(f"Here's a suggestion to help you cope:")
    st.write(f"**Activity to try:** {suggested_activity}")

    # Display YouTube video suggestions
    if video_results:
        st.write("Here are some YouTube videos that may help:")
        for title, link in video_results:
            st.write(f"[{title}]({link})")

    # Generate and play an audio response using gTTS
    tts = gTTS(empathetic_response, lang='en')
    audio_file = BytesIO()
    tts.save(audio_file)
    audio_file.seek(0)
    st.audio(audio_file, format="audio/mp3", use_container_width=True)