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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -37
app.py CHANGED
@@ -1,13 +1,9 @@
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
@@ -43,42 +39,49 @@ st.markdown("""
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)
 
1
  import streamlit as st
2
+ import torch
3
  from transformers import pipeline
 
 
4
  from youtubesearchpython import VideosSearch
5
 
6
+ # Set up the page configuration for a welcoming appearance
 
 
 
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 and modern look
 
39
  st.title("Grief and Loss Support Bot πŸ•ŠοΈ")
40
  st.subheader("We are here for you. πŸ’š Your companion in tough times")
41
 
42
+ # Load the conversational model pipeline
43
+ try:
44
+ conversational_pipeline = pipeline(task="conversational", model="microsoft/DialoGPT-medium")
45
+ except Exception as e:
46
+ st.error(f"Error loading the conversational model: {e}")
47
+
48
+ # Load the TTS pipeline for generating audio
49
+ try:
50
+ tts = pipeline(task="text-to-audio", model="espnet/kan-bayashi_ljspeech_vits", device=0 if torch.cuda.is_available() else -1)
51
+ except Exception as e:
52
+ st.error(f"Error loading the TTS model: {e}")
53
+
54
  # User input for conversation
55
  user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
56
 
57
  if user_input:
58
+ # Generate a conversational response
59
+ response = conversational_pipeline(user_input)
60
+ response_text = response[0]['generated_text']
61
+
62
+ st.write("Bot's Response:")
63
+ st.write(response_text)
 
 
 
64
 
65
+ # Generate audio from the response text
66
+ try:
67
+ audio_output = tts(response_text)
68
+ audio_file_path = "response_audio.wav"
69
+ with open(audio_file_path, "wb") as f:
70
+ f.write(audio_output["audio"])
71
+ st.audio(audio_file_path)
72
+ except Exception as e:
73
+ st.error(f"Error generating audio: {e}")
74
 
75
+ # Provide hobby suggestions
76
+ st.markdown("### Suggested Hobbies and Activities")
77
+ hobby_prompt = "Suggest hobbies and activities for emotional well-being:"
78
+ hobby_response = pipeline("text-generation", model="gpt2")(hobby_prompt, max_length=50, num_return_sequences=1)
79
+ st.write(hobby_response[0]['generated_text'])
 
 
 
80
 
81
+ # Search for coping videos on YouTube
82
+ st.markdown("### Informational YouTube Videos")
83
+ video_search = VideosSearch('productive hobbies and coping with grief', limit=3)
84
+ results = video_search.result()
85
+ for video in results['result']:
86
+ st.write(f"[{video['title']}]({video['link']})")
87