Bey007 commited on
Commit
9dfe35d
Β·
verified Β·
1 Parent(s): c74657d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -38
app.py CHANGED
@@ -2,73 +2,79 @@ import streamlit as st
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 and sentiment analysis 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
- # Streamlit page configuration
13
  st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
14
  st.markdown("""
15
  <style>
16
  .css-1d391kg { background-color: #F3F7F6; }
17
  .css-ffhzg2 { font-size: 1.5em; font-weight: 500; color: #4C6D7D; }
18
- .stTextInput>div>div>input { background-color: #E8F1F2; border-radius: 5px; }
19
- .stButton>button { background-color: #A9D0B6; color: white; }
20
  .stButton>button:hover { background-color: #8FB79A; }
21
  .stTextInput>div>label { color: #4C6D7D; }
22
  </style>
23
  """, unsafe_allow_html=True)
24
 
25
- # Title and description
26
  st.title("Grief and Loss Support Bot 🌿")
27
  st.subheader("Your compassionate companion in tough times πŸ’š")
28
 
29
- # User input
30
- user_input = st.text_input("How are you feeling today?", placeholder="Share your thoughts here...", max_chars=500)
31
 
32
- # Generate response
33
  if user_input:
34
- # Analyze sentiment
35
  sentiment = sentiment_analysis(user_input)[0]
 
36
 
37
- # Use conversational bot for response
38
- response = conversational_bot(user_input, max_length=150)[0]['generated_text']
39
- st.text_area("Bot's Response:", response, height=150)
 
 
40
 
41
- # Text-to-speech conversion
42
- tts = gTTS(response, lang='en')
43
  audio_file = "response.mp3"
44
  tts.save(audio_file)
45
  st.audio(audio_file, format="audio/mp3")
46
 
47
- # Suggest activity based on mood keywords
48
- mood_keywords = {
49
- "positive": ["try journaling your feelings", "practice yoga", "learn a new recipe"],
50
- "neutral": ["take a short walk", "listen to calming music", "try some mindful breathing"],
51
- "negative": ["explore creative activities like painting", "watch a motivational video", "write down small goals to feel organized"]
52
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- mood_type = "neutral" # Default category
55
- if any(word in user_input.lower() for word in ["lonely", "lost", "sad", "stressed"]):
56
- mood_type = "negative"
57
- elif sentiment["label"] == "POSITIVE":
58
- mood_type = "positive"
59
 
60
- suggested_activity = random.choice(mood_keywords[mood_type])
61
- st.info("Here's a suggestion to help lift your spirits:")
62
- st.write(f"**Activity Suggestion:** {suggested_activity}")
63
-
64
- # YouTube search for video resources
65
- search = Search(suggested_activity)
66
- st.write("Recommended Videos:")
67
- for video in search.results[:2]: # Display top 2 videos
68
- st.write(f"[{video.title}]({video.watch_url})")
69
-
70
- # Crisis intervention resources
71
  crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
72
  if any(keyword in user_input.lower() for keyword in crisis_keywords):
73
- 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.")
74
  st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")
 
2
  from transformers import pipeline
3
  from gtts import gTTS
4
  from pytube import Search
 
5
  import os
6
 
7
+ # Initialize models
8
  conversational_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
9
  sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
10
 
11
+ # Set up Streamlit page
12
  st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="🌿", layout="centered")
13
  st.markdown("""
14
  <style>
15
  .css-1d391kg { background-color: #F3F7F6; }
16
  .css-ffhzg2 { font-size: 1.5em; font-weight: 500; color: #4C6D7D; }
17
+ .stTextInput>div>div>input { background-color: #D8E3E2; }
18
+ .stButton>button { background-color: #A9D0B6; color: white; border-radius: 5px; }
19
  .stButton>button:hover { background-color: #8FB79A; }
20
  .stTextInput>div>label { color: #4C6D7D; }
21
  </style>
22
  """, unsafe_allow_html=True)
23
 
24
+ # Title
25
  st.title("Grief and Loss Support Bot 🌿")
26
  st.subheader("Your compassionate companion in tough times πŸ’š")
27
 
28
+ # Get user input
29
+ user_input = st.text_input("How are you feeling today?", placeholder="Share what's on your mind...", max_chars=500)
30
 
31
+ # Check if user has entered text
32
  if user_input:
33
+ # Run sentiment analysis to assess mood
34
  sentiment = sentiment_analysis(user_input)[0]
35
+ sentiment_label = sentiment['label'].lower()
36
 
37
+ # Generate empathetic response
38
+ bot_response = conversational_bot(user_input, max_length=150)[0]['generated_text']
39
+
40
+ # Display response
41
+ st.text_area("Bot's Response:", bot_response, height=150)
42
 
43
+ # Text-to-speech output
44
+ tts = gTTS(bot_response, lang='en')
45
  audio_file = "response.mp3"
46
  tts.save(audio_file)
47
  st.audio(audio_file, format="audio/mp3")
48
 
49
+ # Suggest an activity
50
+ if sentiment_label == "negative":
51
+ st.info("Here's a suggestion to help lift your spirits:")
52
+ activities = {
53
+ "relaxation": ["mindful breathing", "yoga"],
54
+ "creative": ["painting", "writing in a journal"],
55
+ "exercise": ["a short walk", "stretching"],
56
+ "self-care": ["listening to calming music", "meditation"]
57
+ }
58
+
59
+ # Choose activity based on keyword in input
60
+ if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad", "down"]):
61
+ activity_category = "self-care"
62
+ elif any(keyword in user_input.lower() for keyword in ["stress", "pressure", "overwhelmed"]):
63
+ activity_category = "relaxation"
64
+ else:
65
+ activity_category = "creative"
66
+
67
+ suggested_activity = activities[activity_category]
68
+ st.write(f"Activity Suggestion: {suggested_activity}")
69
 
70
+ # Search YouTube for videos related to the selected activity
71
+ search = Search(suggested_activity[0])
72
+ search_results = search.results[:2] # limit results to 2 videos
73
+ for video in search_results:
74
+ st.write(f"[{video.title}]({video.watch_url})")
75
 
76
+ # Crisis resources for critical keywords
 
 
 
 
 
 
 
 
 
 
77
  crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
78
  if any(keyword in user_input.lower() for keyword in crisis_keywords):
79
+ st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.")
80
  st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")