Bey007 commited on
Commit
b4bd0de
β€’
1 Parent(s): 43e74ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -47
app.py CHANGED
@@ -1,64 +1,68 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
- import pyttsx3
4
  from youtubesearchpython import VideosSearch
 
5
 
6
- # Set up the Streamlit app
7
- st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="πŸ•ŠοΈ", layout="centered")
 
 
8
 
9
- # Customizing the app style for a soothing look
 
10
  st.markdown("""
11
  <style>
12
- .main {
13
- background-color: #F3F7F6;
14
- }
15
- .css-1d391kg {
16
- font-family: 'Arial', sans-serif;
17
- }
18
  </style>
19
- """, unsafe_allow_html=True)
20
 
21
- st.title("Grief and Loss Support Bot πŸ•ŠοΈ")
22
- st.subheader("We are here for you. πŸ’š Your companion in tough times")
 
23
 
24
- # Set up the conversational model
25
- try:
26
- chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
27
- except Exception as e:
28
- st.error(f"Error loading the model: {e}")
29
-
30
- # Set up text-to-speech engine
31
- engine = pyttsx3.init()
32
- engine.setProperty('rate', 150) # Speed of speech
33
- engine.setProperty('volume', 0.9) # Volume level
34
- voices = engine.getProperty('voices')
35
- engine.setProperty('voice', voices[0].id) # Choose the desired voice
36
-
37
- # User input for conversation
38
  user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
39
 
 
40
  if user_input:
41
- # Generate a response using the model
42
- response = chatbot(user_input, max_length=100)[0]['generated_text']
43
 
44
- # Display response and read it aloud
45
- st.write(response)
46
- engine.say(response)
47
- engine.runAndWait()
48
-
49
- # Suggest hobbies to uplift the user's mood
50
- hobbies = ["Painting", "Reading books", "Gardening", "Learning a new language", "Playing an instrument"]
51
- st.subheader("Suggested Hobbies")
52
- st.write("Here are some hobbies that might help you feel better:")
53
- for hobby in hobbies:
54
- st.write(f"- {hobby}")
55
 
56
- # Search for helpful YouTube videos
57
- st.subheader("Recommended Videos")
58
- video_search = VideosSearch('motivational activities for coping with grief', limit=3)
59
- results = video_search.result()['result']
 
60
 
61
- for video in results:
62
- st.write(f"[{video['title']}]({video['link']})")
 
 
 
 
 
 
 
 
 
63
 
64
- st.write("Remember, it's okay to seek support and take small steps towards healing. πŸ’š")
 
 
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ from gtts import gTTS
4
  from youtubesearchpython import VideosSearch
5
+ import os
6
 
7
+ # Initialize conversational models
8
+ empathy_bot = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-empathy")
9
+ general_bot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
10
+ sentiment_analysis = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
11
 
12
+ # Set up Streamlit page
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: #D8E3E2; }
19
+ .stButton>button { background-color: #A9D0B6; color: white; border-radius: 5px; }
20
+ .stButton>button:hover { background-color: #8FB79A; }
21
+ .stTextInput>div>label { color: #4C6D7D; }
22
  </style>
23
+ """, unsafe_allow_html=True)
24
 
25
+ # Title
26
+ st.title("Grief and Loss Support Bot 🌿")
27
+ st.subheader("Your compassionate companion in tough times πŸ’š")
28
 
29
+ # Get user input
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
31
 
32
+ # Check if user has entered text
33
  if user_input:
34
+ # Run sentiment analysis for crisis detection
35
+ sentiment = sentiment_analysis(user_input)[0]
36
 
37
+ # Use empathy model for more specific support if the user is distressed
38
+ if sentiment['label'] == 'NEGATIVE' and sentiment['score'] > 0.8:
39
+ response = empathy_bot(user_input, max_length=150)[0]['generated_text']
40
+ else:
41
+ response = general_bot(user_input, max_length=150)[0]['generated_text']
42
+
43
+ # Display response
44
+ st.text_area("Bot's Response:", response, height=150)
 
 
 
45
 
46
+ # Text-to-speech output
47
+ tts = gTTS(response, lang='en')
48
+ audio_file = "response.mp3"
49
+ tts.save(audio_file)
50
+ st.audio(audio_file, format="audio/mp3", use_container_width=True)
51
 
52
+ # Suggest a productive activity based on detected keywords
53
+ if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad"]):
54
+ st.info("Here's a suggestion to help you cope:")
55
+ hobbies = ["journaling", "yoga", "painting"]
56
+ activity = st.selectbox("Choose an activity you'd like to try:", hobbies)
57
+
58
+ # Search YouTube for videos related to the selected activity
59
+ videosSearch = VideosSearch(activity, limit=2)
60
+ video_links = [result['link'] for result in videosSearch.result()['result']]
61
+ for link in video_links:
62
+ st.write(link)
63
 
64
+ # Crisis resources
65
+ crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
66
+ if any(keyword in user_input.lower() for keyword in crisis_keywords):
67
+ st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.")
68
+ st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")