CompanAIon / app.py
Bey007's picture
Update app.py
b4bd0de verified
raw
history blame
3.02 kB
import streamlit as st
from transformers import pipeline
from gtts import gTTS
from youtubesearchpython import VideosSearch
import os
# Initialize conversational models
empathy_bot = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-empathy")
general_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
# Set up Streamlit page
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
st.title("Grief and Loss Support Bot 🌿")
st.subheader("Your compassionate companion in tough times πŸ’š")
# Get user input
user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
# Check if user has entered text
if user_input:
# Run sentiment analysis for crisis detection
sentiment = sentiment_analysis(user_input)[0]
# Use empathy model for more specific support if the user is distressed
if sentiment['label'] == 'NEGATIVE' and sentiment['score'] > 0.8:
response = empathy_bot(user_input, max_length=150)[0]['generated_text']
else:
response = general_bot(user_input, max_length=150)[0]['generated_text']
# Display response
st.text_area("Bot's Response:", response, height=150)
# Text-to-speech output
tts = gTTS(response, lang='en')
audio_file = "response.mp3"
tts.save(audio_file)
st.audio(audio_file, format="audio/mp3", use_container_width=True)
# Suggest a productive activity based on detected keywords
if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad"]):
st.info("Here's a suggestion to help you cope:")
hobbies = ["journaling", "yoga", "painting"]
activity = st.selectbox("Choose an activity you'd like to try:", hobbies)
# Search YouTube for videos related to the selected activity
videosSearch = VideosSearch(activity, limit=2)
video_links = [result['link'] for result in videosSearch.result()['result']]
for link in video_links:
st.write(link)
# Crisis resources
crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
if any(keyword in user_input.lower() for keyword in crisis_keywords):
st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.")
st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")