CompanAIon / app.py
Bey007's picture
Update app.py
dad612d verified
raw
history blame
3.7 kB
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")