CompanAIon / app.py
Bey007's picture
Update app.py
c017ee5 verified
raw
history blame
3.32 kB
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer
from gtts import gTTS
import torch
# Load DialoGPT model and tokenizer from Hugging Face
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
# 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 using DialoGPT
def generate_response(user_input):
# Encode the input text and generate a response
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
bot_input_ids = new_user_input_ids
chat_history_ids = model.generate(bot_input_ids, max_length=150, pad_token_id=tokenizer.eos_token_id, temperature=0.7, top_k=50, repetition_penalty=1.2)
# Decode the response to text
chat_history_ids = chat_history_ids[:, bot_input_ids.shape[-1]:] # remove the input from the response
bot_output = tokenizer.decode(chat_history_ids[0], skip_special_tokens=True)
# Add empathetic and coping suggestions
response = f"{bot_output}\n\nHere's something you could try to help cope with how you're feeling:\n"
if "crying" in user_input.lower():
response += "Sometimes letting yourself cry is a necessary release. Itโ€™s okay to feel what you're feeling. Also, trying to reach out to a friend or family member for support can be really comforting."
elif "overwhelmed" in user_input.lower():
response += "Overwhelm can often feel like too much to handle, but try taking it one step at a time. Break your tasks down into small pieces and remember to take breaks. You don't have to do it all at once."
else:
response += "Taking a moment to breathe deeply, meditate, or even take a short walk can help you regain some balance in this difficult moment."
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")