Spaces:
Sleeping
Sleeping
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) | |