CompanAIon / app.py
Bey007's picture
Update app.py
b893c3a verified
raw
history blame
3.55 kB
import streamlit as st
from llama_cpp import Llama
from gtts import gTTS
from io import BytesIO
import random
import requests
from youtubesearchpython import VideosSearch
# Initialize the empathy bot using llama_cpp
llm = Llama.from_pretrained(
repo_id="featherless-ai-quants/HyunCello-KULLM3-empathy-v1.0-GGUF",
filename="HyunCello-KULLM3-empathy-v1.0-IQ4_XS.gguf",
)
# Function to process the user's input and generate a dynamic response
def generate_dynamic_response(user_input):
# Generate an empathetic response using the Llama model
response = llm.create_chat_completion(
messages=[{"role": "user", "content": user_input}]
)
empathetic_response = response['choices'][0]['message']['content']
# Possible activities to suggest
activities = {
"relaxation": [
"Deep Breathing Exercises",
"Guided Meditation",
"Mindfulness Breathing"
],
"creative": [
"Journaling your thoughts",
"Drawing or doodling",
"Writing a poem"
],
"exercise": [
"Try some yoga",
"Go for a walk outside",
"Stretching exercises to release tension"
],
"self-care": [
"Take a warm bath",
"Listen to calming music",
"Treat yourself with some relaxation time"
]
}
# Logic to determine context of user input (e.g., stress, sadness, etc.)
if "sad" in user_input or "overwhelmed" in user_input:
suggested_activity = random.choice(activities["relaxation"])
elif "bored" in user_input or "unmotivated" in user_input:
suggested_activity = random.choice(activities["creative"])
elif "stressed" in user_input or "tired" in user_input:
suggested_activity = random.choice(activities["exercise"])
else:
suggested_activity = random.choice(activities["self-care"])
# Search YouTube for relevant videos based on the activity suggestion
video_keywords = suggested_activity
video_results = search_youtube_videos(video_keywords)
return empathetic_response, suggested_activity, video_results
# Function to search for YouTube videos
def search_youtube_videos(query):
videos_search = VideosSearch(query, limit=3)
results = videos_search.result()
# Collect video details
video_links = []
for video in results['videos']:
title = video['title']
link = video['link']
video_links.append((title, link))
return video_links
# Streamlit UI
st.title("Grief and Loss Support Bot 🌿")
st.markdown("Your compassionate companion in tough times πŸ’š")
user_input = st.text_input("Share what's on your mind...")
if user_input:
empathetic_response, suggested_activity, video_results = generate_dynamic_response(user_input)
# Display the empathetic response
st.write(f"**Bot's Response:**")
st.write(empathetic_response)
# Suggest an activity
st.write(f"Here's a suggestion to help you cope:")
st.write(f"**Activity to try:** {suggested_activity}")
# Display YouTube video suggestions
if video_results:
st.write("Here are some YouTube videos that may help:")
for title, link in video_results:
st.write(f"[{title}]({link})")
# Generate and play an audio response using gTTS
tts = gTTS(empathetic_response, lang='en')
audio_file = BytesIO()
tts.save(audio_file)
audio_file.seek(0)
st.audio(audio_file, format="audio/mp3", use_container_width=True)