Bey007 commited on
Commit
7645fdc
Β·
verified Β·
1 Parent(s): d77c966

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -41
app.py CHANGED
@@ -4,8 +4,10 @@ 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
@@ -26,64 +28,42 @@ 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
- try:
35
- sentiment = sentiment_analysis(user_input)[0]
36
- sentiment_label = sentiment['label'].lower()
37
- st.write(f"Sentiment detected: {sentiment_label}") # Debugging line
38
- except Exception as e:
39
- st.write(f"Error in sentiment analysis: {e}") # Error handling
40
 
41
- # Generate empathetic response from the conversational bot
42
- try:
43
- bot_response = conversational_bot(user_input, max_length=150)[0]['generated_text']
44
- st.write(f"Bot Response: {bot_response}") # Debugging line
45
- except Exception as e:
46
- bot_response = "I'm really sorry you're feeling this way. It must be difficult. Let's talk about it."
47
- st.write(f"Error generating bot response: {e}") # Error handling
48
- st.write("Using fallback response.")
49
 
50
  # Display response
51
- st.text_area("Bot's Response:", bot_response, height=150)
52
 
53
  # Text-to-speech output
54
- tts = gTTS(bot_response, lang='en')
55
  audio_file = "response.mp3"
56
  tts.save(audio_file)
57
  st.audio(audio_file, format="audio/mp3")
58
 
59
- # Suggest an activity
60
- if sentiment_label == "negative":
61
- st.info("Here's a suggestion to help lift your spirits:")
62
- activities = {
63
- "relaxation": ["mindful breathing", "yoga"],
64
- "creative": ["painting", "writing in a journal"],
65
- "exercise": ["a short walk", "stretching"],
66
- "self-care": ["listening to calming music", "meditation"]
67
- }
68
 
69
- # Choose activity based on keyword in input
70
- if any(keyword in user_input.lower() for keyword in ["lonely", "lost", "sad", "down"]):
71
- activity_category = "self-care"
72
- elif any(keyword in user_input.lower() for keyword in ["stress", "pressure", "overwhelmed"]):
73
- activity_category = "relaxation"
74
- else:
75
- activity_category = "creative"
76
-
77
- suggested_activity = activities[activity_category]
78
- st.write(f"Activity Suggestion: {', '.join(suggested_activity)}")
79
-
80
  # Search YouTube for videos related to the selected activity
81
- search = Search(suggested_activity[0])
82
  search_results = search.results[:2] # limit results to 2 videos
83
  for video in search_results:
84
  st.write(f"[{video.title}]({video.watch_url})")
85
 
86
- # Crisis resources for critical keywords
87
  crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
88
  if any(keyword in user_input.lower() for keyword in crisis_keywords):
89
  st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.")
 
4
  from pytube import Search
5
  import os
6
 
7
+ # Initialize conversational model for empathetic dialogue
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
 
28
  st.subheader("Your compassionate companion in tough times πŸ’š")
29
 
30
  # Get user input
31
+ user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
32
 
33
  # Check if user has entered text
34
  if user_input:
35
+ # Run sentiment analysis to check for distress
36
+ sentiment = sentiment_analysis(user_input)[0]
37
+
38
+ # Generate empathetic response (model generates responses with empathy)
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 you cope:")
57
+ hobbies = ["journaling", "yoga", "painting"]
58
+ activity = st.selectbox("Choose an activity you'd like to try:", hobbies)
 
 
 
 
59
 
 
 
 
 
 
 
 
 
 
 
 
60
  # Search YouTube for videos related to the selected activity
61
+ search = Search(activity)
62
  search_results = search.results[:2] # limit results to 2 videos
63
  for video in search_results:
64
  st.write(f"[{video.title}]({video.watch_url})")
65
 
66
+ # Crisis resources
67
  crisis_keywords = ["help", "suicide", "depressed", "emergency", "hurt", "lost"]
68
  if any(keyword in user_input.lower() for keyword in crisis_keywords):
69
  st.warning("It seems like you might be in distress. Please reach out to a crisis hotline or a trusted individual.")