Bey007 commited on
Commit
b1dc82e
Β·
verified Β·
1 Parent(s): d7596e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -32
app.py CHANGED
@@ -1,42 +1,84 @@
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 the chatbot pipeline using a pre-trained model from Hugging Face
8
  chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
9
 
10
- # Streamlit app title
11
- st.title("Grief and Loss Support Bot")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # Text input for user queries
14
- user_input = st.text_input("You:", "")
 
 
 
 
15
 
16
- # Respond to user input
17
  if user_input:
18
- # Generate a response from the chatbot
19
- response = chatbot(user_input, max_length=50, num_return_sequences=1)[0]['generated_text']
20
- st.text_area("Bot:", response, height=100)
21
-
22
- # Convert the response to speech using gTTS and save as an audio file
23
- tts = gTTS(text=response, lang='en')
24
- tts.save("response.mp3")
25
- audio_file = open("response.mp3", "rb")
26
- audio_bytes = audio_file.read()
27
- st.audio(audio_bytes, format="audio/mp3")
28
- audio_file.close()
29
- os.remove("response.mp3") # Clean up the audio file after playback
30
-
31
- # YouTube search functionality for coping resources
32
- st.header("Helpful Videos")
33
- search_query = st.text_input("Enter a topic for video suggestions:")
34
- if search_query:
35
- # Search for videos using YouTubeSearchPython
36
- video_search = VideosSearch(search_query, limit=3)
37
- results = video_search.result()
38
- for video in results['result']:
39
- st.write(f"[{video['title']}]({video['link']})")
40
-
41
- # Display a note for users
42
- st.write("**Note:** This bot is designed for general emotional support. For urgent help, please reach out to professional resources.")
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
  from gtts import gTTS
 
4
  import os
5
+ from youtubesearchpython import VideosSearch
6
 
7
+ # Initialize the conversational model
8
  chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
9
 
10
+ # Streamlit app layout with positive, calming design
11
+ st.set_page_config(page_title="Grief and Loss Support Bot", page_icon="πŸ•ŠοΈ", layout="centered")
12
+
13
+ # Customizing the app style for a soothing and modern look
14
+ st.markdown("""
15
+ <style>
16
+ .css-1d391kg {
17
+ background-color: #F3F7F6;
18
+ }
19
+ .css-ffhzg2 {
20
+ font-size: 1.5em;
21
+ font-weight: 500;
22
+ color: #4C6D7D;
23
+ }
24
+ .stTextInput>div>div>input {
25
+ background-color: #D8E3E2;
26
+ }
27
+ .stButton>button {
28
+ background-color: #A9D0B6;
29
+ color: white;
30
+ border-radius: 5px;
31
+ border: none;
32
+ }
33
+ .stButton>button:hover {
34
+ background-color: #8FB79A;
35
+ }
36
+ .stTextInput>div>label {
37
+ color: #4C6D7D;
38
+ }
39
+ </style>
40
+ """, unsafe_allow_html=True)
41
 
42
+ # Title and introduction
43
+ st.title("Grief and Loss Support Bot πŸ•ŠοΈ")
44
+ st.subheader("We are here for you. πŸ’š Your companion in tough times")
45
+
46
+ # User input for conversation
47
+ user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
48
 
 
49
  if user_input:
50
+ # Generate a compassionate response from the bot
51
+ response = chatbot(user_input, max_length=150)[0]['generated_text']
52
+ st.text_area("Bot's Response:", response, height=150)
53
+
54
+ # Empathetic voice output using gTTS
55
+ tts = gTTS(response, lang='en')
56
+ audio_file = "response.mp3"
57
+ tts.save(audio_file)
58
+ st.audio(audio_file, format="audio/mp3", use_container_width=True)
59
+
60
+ # Crisis keyword detection for emergency help
61
+ crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
62
+ if any(keyword in user_input.lower() for keyword in crisis_keywords):
63
+ st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual for support. You're not alone.")
64
+ st.write("[Find emergency resources here](https://www.helpguide.org/find-help.htm)")
65
+
66
+ # Productive hobby suggestions
67
+ if st.button("Need something to do? Here are some activities to help you cope:"):
68
+ st.write("- Journaling your thoughts and emotions πŸ“–")
69
+ st.write("- Painting or drawing your feelings 🎨")
70
+ st.write("- Taking a walk in nature 🌳")
71
+ st.write("- Listening to soothing music 🎢")
72
+ st.write("- Practicing deep breathing or meditation 🧘")
73
+ st.write("- Trying out a new hobby like knitting or gardening 🌱")
74
+
75
+ # YouTube content suggestions for coping with grief
76
+ if st.button("Here are some videos that may help you:"):
77
+ videos_search = VideosSearch('coping with grief', limit=3)
78
+ results = videos_search.result()['result']
79
+ for i, video in enumerate(results):
80
+ st.write(f"{i + 1}. [{video['title']}]({video['link']})")
81
+
82
+ # Clean up the audio file after playback
83
+ if os.path.exists(audio_file):
84
+ os.remove(audio_file)