Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,22 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
from pytube import Search
|
|
|
5 |
import os
|
6 |
|
7 |
-
# Initialize conversational
|
8 |
conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
9 |
-
|
10 |
-
# Initialize sentiment analysis
|
11 |
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Set up Streamlit page
|
14 |
st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
|
15 |
st.markdown("""
|
@@ -32,34 +40,42 @@ user_input = st.text_input("Share what's on your mind...", placeholder="Type her
|
|
32 |
|
33 |
# Check if user has entered text
|
34 |
if user_input:
|
35 |
-
# Run sentiment analysis
|
36 |
sentiment = sentiment_analysis(user_input)[0]
|
37 |
|
38 |
-
#
|
39 |
response = conversational_bot(user_input, max_length=150)[0]['generated_text']
|
40 |
|
41 |
-
# Ensure response does not repeat what the user said, and is supportive
|
42 |
-
if user_input.lower() in response.lower():
|
43 |
-
response = "I understand how you're feeling. You're not alone in this. I'm here to listen and help."
|
44 |
-
|
45 |
# Display response
|
46 |
st.text_area("Bot's Response:", response, height=150)
|
47 |
|
|
|
|
|
|
|
|
|
48 |
# Text-to-speech output
|
49 |
-
tts = gTTS(response, lang='en')
|
50 |
audio_file = "response.mp3"
|
51 |
tts.save(audio_file)
|
52 |
-
st.audio(audio_file, format="audio/mp3")
|
53 |
|
54 |
# Suggest a productive activity based on detected keywords
|
55 |
-
if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad"]):
|
56 |
-
st.info("Here's a suggestion to help
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
# Search YouTube for videos related to the selected activity
|
61 |
search = Search(activity)
|
62 |
-
search_results = search.results[:
|
63 |
for video in search_results:
|
64 |
st.write(f"[{video.title}]({video.watch_url})")
|
65 |
|
|
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
from pytube import Search
|
5 |
+
import random
|
6 |
import os
|
7 |
|
8 |
+
# Initialize conversational models
|
9 |
conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
|
|
|
|
10 |
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
11 |
|
12 |
+
# Motivational quotes list
|
13 |
+
motivational_quotes = [
|
14 |
+
"You are stronger than you think. Keep going, one step at a time.",
|
15 |
+
"It's okay to take a break. Your mental health matters too.",
|
16 |
+
"This moment is tough, but it will pass. Stay strong.",
|
17 |
+
"Believe in yourself. You've faced challenges before and overcome them.",
|
18 |
+
"Don't give up. Progress is progress, no matter how small."
|
19 |
+
]
|
20 |
+
|
21 |
# Set up Streamlit page
|
22 |
st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
|
23 |
st.markdown("""
|
|
|
40 |
|
41 |
# Check if user has entered text
|
42 |
if user_input:
|
43 |
+
# Run sentiment analysis for crisis detection
|
44 |
sentiment = sentiment_analysis(user_input)[0]
|
45 |
|
46 |
+
# Use conversational bot for responses
|
47 |
response = conversational_bot(user_input, max_length=150)[0]['generated_text']
|
48 |
|
|
|
|
|
|
|
|
|
49 |
# Display response
|
50 |
st.text_area("Bot's Response:", response, height=150)
|
51 |
|
52 |
+
# Motivational quote
|
53 |
+
quote = random.choice(motivational_quotes)
|
54 |
+
st.markdown(f"### 💬 Motivation: _{quote}_")
|
55 |
+
|
56 |
# Text-to-speech output
|
57 |
+
tts = gTTS(f"{response} {quote}", lang='en')
|
58 |
audio_file = "response.mp3"
|
59 |
tts.save(audio_file)
|
60 |
+
st.audio(audio_file, format="audio/mp3") # Removed use_container_width=True
|
61 |
|
62 |
# Suggest a productive activity based on detected keywords
|
63 |
+
if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad", "overwhelmed", "stressed"]):
|
64 |
+
st.info("Here's a suggestion to help lift your spirits:")
|
65 |
+
activities = {
|
66 |
+
"relaxation": ["listening to calming music", "meditation", "deep breathing exercises"],
|
67 |
+
"creative": ["journaling", "painting", "writing poetry"],
|
68 |
+
"exercise": ["light yoga", "stretching", "taking a walk outside"],
|
69 |
+
"self-care": ["taking a warm bath", "getting a good night's sleep", "reading a book"]
|
70 |
+
}
|
71 |
+
category = st.selectbox("Choose an activity category:", ["relaxation", "creative", "exercise", "self-care"])
|
72 |
+
activity = random.choice(activities[category])
|
73 |
+
|
74 |
+
st.write(f"Activity Suggestion: {activity}")
|
75 |
+
|
76 |
# Search YouTube for videos related to the selected activity
|
77 |
search = Search(activity)
|
78 |
+
search_results = search.results[:2] # limit results to 2 videos
|
79 |
for video in search_results:
|
80 |
st.write(f"[{video.title}]({video.watch_url})")
|
81 |
|