Spaces:
Sleeping
Sleeping
File size: 3,698 Bytes
fd64912 56b5bd3 b4bd0de 7f87605 9216a0a dad612d 56b5bd3 f1985ef dad612d 8f34be7 9216a0a 8f34be7 9dfe35d 8f34be7 9216a0a dad612d 8f34be7 9216a0a dad612d 7645fdc 9216a0a 7f87605 2951ecb dad612d 30d702b dad612d f1985ef dad612d 503bff0 dad612d 9216a0a 80d269f f1985ef |
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 |
import streamlit as st
from transformers import pipeline, GPT2LMHeadModel, GPT2Tokenizer
from gtts import gTTS
import random
# Load GPT-2 model and tokenizer from Hugging Face
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")
# 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 to the bot
st.title("Grief and Loss Support Bot 🌿")
st.subheader("Your compassionate companion in tough times 💚")
# 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 = []
# Function to generate a more empathetic and focused response
def generate_response(user_input):
# Predefined empathetic responses for cases of sadness and overwhelming stress
empathy_responses = [
"I'm really sorry you're going through this. It’s okay to feel this way, and I’m here to help you process it.",
"I understand how overwhelming things can feel right now. You're not alone. It’s important to take things one step at a time.",
"It sounds really tough, but reaching out is a big first step. You’re doing great. Take a deep breath. You're not alone in this."
]
# Tailored coping suggestions based on the user's input
activity_suggestions = {
"journaling": "Journaling is a great way to process your emotions. Write down whatever comes to mind to help release the feelings you're carrying.",
"yoga": "Yoga can help you relax and find calm. Simple breathing exercises or gentle stretches might ease the tension you're feeling.",
"meditation": "Mindful meditation can help you center yourself and reduce stress. Even a few minutes can make a big difference.",
"exercise": "Physical activity can lift your mood and clear your mind. A short walk or some light exercise could help you feel better."
}
# Pick a relevant empathetic response
response = random.choice(empathy_responses)
# Based on keywords in the input, provide a relevant activity suggestion
if "exam" in user_input.lower() or "study" in user_input.lower():
activity = "journaling"
elif "stress" in user_input.lower() or "overwhelmed" in user_input.lower():
activity = "yoga"
else:
activity = random.choice(list(activity_suggestions.keys()))
# Add a coping activity suggestion to the response
response += f"\n\nHere's something you could try to help cope with how you're feeling:\n{activity_suggestions[activity]}"
return response
# Check if the user has typed something
if user_input:
# Generate the empathetic response
response = generate_response(user_input)
# Store and show the new response
st.session_state.previous_responses.append(response)
st.text_area("Bot's Response:", response, height=250)
# Text-to-speech output (optional)
tts = gTTS(response, lang='en')
audio_file = "response.mp3"
tts.save(audio_file)
st.audio(audio_file, format="audio/mp3")
|