Spaces:
Sleeping
Sleeping
File size: 3,925 Bytes
fd64912 5721af8 b4bd0de b9dc3fd d67bb32 5721af8 9216a0a 5721af8 d6c911d 5721af8 f1985ef d6c911d 8f34be7 5721af8 9216a0a 8f34be7 9dfe35d 8f34be7 9216a0a 5721af8 8f34be7 9216a0a 5721af8 d67bb32 5721af8 d67bb32 5721af8 1f6d39f 5721af8 d67bb32 5721af8 d67bb32 5721af8 d67bb32 5721af8 d67bb32 5721af8 d67bb32 5721af8 |
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 |
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from gtts import gTTS
from pytube import Search
import random
import os
import datetime
import torch
# Load models
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
emotion_analyzer = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
# Set up Streamlit page configuration
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 and introduction
st.title("Grief and Loss Support Bot πΏ")
st.subheader("Your compassionate companion in tough times π")
# Function to generate a response
def generate_response(user_input):
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
chat_history_ids = model.generate(input_ids, max_length=200, pad_token_id=tokenizer.eos_token_id, temperature=0.7, top_k=50, repetition_penalty=1.2)
bot_output = tokenizer.decode(chat_history_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
return bot_output
# Function to generate a comforting story based on a theme
def generate_story(theme):
prompt = f"Tell me a short, comforting story about {theme}."
input_ids = tokenizer.encode(prompt + tokenizer.eos_token, return_tensors='pt')
# Generate story
story_ids = model.generate(input_ids, max_length=150, temperature=0.7, top_p=0.9, repetition_penalty=1.2, do_sample=True, num_return_sequences=1)
story = tokenizer.decode(story_ids[0], skip_special_tokens=True)
return story
# Mindfulness Meditation
st.write("### π§ Guided Meditation")
if st.button("Play a 5-minute Guided Meditation"):
meditation_text = "Focus on your breath. Inhale deeply, hold for a moment, and exhale slowly. Let go of any tension."
tts_meditation = gTTS(meditation_text, lang='en')
tts_meditation.save("meditation.mp3")
st.audio("meditation.mp3", format="audio/mp3")
# Interactive Storytelling
st.write("### π Short Comforting Story")
# User selects a theme for the story
story_theme = st.selectbox("Choose a theme for your story:", ["hope", "courage", "healing", "friendship", "resilience"])
if st.button("Generate Story"):
story = generate_story(story_theme)
st.text_area("Hereβs your comforting story:", story, height=250)
# Convert story to speech
tts_story = gTTS(story, lang='en')
tts_story.save("story.mp3")
st.audio("story.mp3", format="audio/mp3")
# User input for conversational support
user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
if user_input:
response = generate_response(user_input)
st.text_area("Bot's Response:", response, height=250)
# Text-to-speech for response
tts = gTTS(response, lang='en')
tts.save("response.mp3")
st.audio("response.mp3", format="audio/mp3")
# Enhanced Activity Suggestions
st.write("### π¨ Try a New Activity")
activities = ["journaling", "yoga", "painting", "exercise", "meditation"]
activity_choice = st.selectbox("Pick an activity:", activities)
if st.button("Find Videos"):
try:
search = Search(activity_choice)
results = search.results[:3]
for video in results:
st.write(f"[{video.title}]({video.watch_url})")
except Exception as e:
st.write("Error fetching videos.")
|