CompanAIon / app.py
Bey007's picture
Update app.py
8f34be7 verified
raw
history blame
3.19 kB
import streamlit as st
from transformers import pipeline
from gtts import gTTS
from pytube import Search
import random
import os
# Initialize conversational and sentiment analysis models
conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
# 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: #E8F1F2; border-radius: 5px; }
.stButton>button { background-color: #A9D0B6; color: white; }
.stButton>button:hover { background-color: #8FB79A; }
.stTextInput>div>label { color: #4C6D7D; }
</style>
""", unsafe_allow_html=True)
# Title and description
st.title("Grief and Loss Support Bot 🌿")
st.subheader("Your compassionate companion in tough times πŸ’š")
# User input
user_input = st.text_input("How are you feeling today?", placeholder="Share your thoughts here...", max_chars=500)
# Generate response
if user_input:
# Analyze sentiment
sentiment = sentiment_analysis(user_input)[0]
# Use conversational bot for response
response = conversational_bot(user_input, max_length=150)[0]['generated_text']
st.text_area("Bot's Response:", response, height=150)
# Text-to-speech conversion
tts = gTTS(response, lang='en')
audio_file = "response.mp3"
tts.save(audio_file)
st.audio(audio_file, format="audio/mp3")
# Suggest activity based on mood keywords
mood_keywords = {
"positive": ["try journaling your feelings", "practice yoga", "learn a new recipe"],
"neutral": ["take a short walk", "listen to calming music", "try some mindful breathing"],
"negative": ["explore creative activities like painting", "watch a motivational video", "write down small goals to feel organized"]
}
mood_type = "neutral" # Default category
if any(word in user_input.lower() for word in ["lonely", "lost", "sad", "stressed"]):
mood_type = "negative"
elif sentiment["label"] == "POSITIVE":
mood_type = "positive"
suggested_activity = random.choice(mood_keywords[mood_type])
st.info("Here's a suggestion to help lift your spirits:")
st.write(f"**Activity Suggestion:** {suggested_activity}")
# YouTube search for video resources
search = Search(suggested_activity)
st.write("Recommended Videos:")
for video in search.results[:2]: # Display top 2 videos
st.write(f"[{video.title}]({video.watch_url})")
# Crisis intervention resources
crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
if any(keyword in user_input.lower() for keyword in crisis_keywords):
st.warning("It sounds like you're going through a very tough time. Please reach out to someone you trust or a professional for support.")
st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")