CompanAIon / app.py
Bey007's picture
Update app.py
06bf795 verified
raw
history blame
2.88 kB
import streamlit as st
import torch
from transformers import pipeline
from youtubesearchpython import VideosSearch
# Set up the page configuration for a welcoming appearance
st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="πŸ•ŠοΈ", layout="centered")
# Customizing the app style for a soothing and modern look
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;
border: none;
}
.stButton>button:hover {
background-color: #8FB79A;
}
.stTextInput>div>label {
color: #4C6D7D;
}
</style>
""", unsafe_allow_html=True)
# Title and introduction
st.title("Grief and Loss Support Bot πŸ•ŠοΈ")
st.subheader("We are here for you. πŸ’š Your companion in tough times")
# Load the conversational model pipeline
try:
conversational_pipeline = pipeline(task="conversational", model="microsoft/DialoGPT-medium")
except Exception as e:
st.error(f"Error loading the conversational model: {e}")
# Load the TTS pipeline for generating audio
try:
tts = pipeline(task="text-to-audio", model="espnet/kan-bayashi_ljspeech_vits", device=0 if torch.cuda.is_available() else -1)
except Exception as e:
st.error(f"Error loading the TTS model: {e}")
# User input for conversation
user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
if user_input:
# Generate a conversational response
response = conversational_pipeline(user_input)
response_text = response[0]['generated_text']
st.write("Bot's Response:")
st.write(response_text)
# Generate audio from the response text
try:
audio_output = tts(response_text)
audio_file_path = "response_audio.wav"
with open(audio_file_path, "wb") as f:
f.write(audio_output["audio"])
st.audio(audio_file_path)
except Exception as e:
st.error(f"Error generating audio: {e}")
# Provide hobby suggestions
st.markdown("### Suggested Hobbies and Activities")
hobby_prompt = "Suggest hobbies and activities for emotional well-being:"
hobby_response = pipeline("text-generation", model="gpt2")(hobby_prompt, max_length=50, num_return_sequences=1)
st.write(hobby_response[0]['generated_text'])
# Search for coping videos on YouTube
st.markdown("### Informational YouTube Videos")
video_search = VideosSearch('productive hobbies and coping with grief', limit=3)
results = video_search.result()
for video in results['result']:
st.write(f"[{video['title']}]({video['link']})")