Bey007 commited on
Commit
a5fe917
Β·
verified Β·
1 Parent(s): 09abbb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -51
app.py CHANGED
@@ -1,77 +1,64 @@
1
  import streamlit as st
2
- import torch
3
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
  import pyttsx3
 
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
10
  st.markdown("""
11
  <style>
12
- .css-1d391kg {
13
  background-color: #F3F7F6;
14
  }
15
- .css-ffhzg2 {
16
- font-size: 1.5em;
17
- font-weight: 500;
18
- color: #4C6D7D;
19
- }
20
- .stTextInput>div>div>input {
21
- background-color: #D8E3E2;
22
- }
23
- .stButton>button {
24
- background-color: #A9D0B6;
25
- color: white;
26
- border-radius: 5px;
27
- border: none;
28
- }
29
- .stButton>button:hover {
30
- background-color: #8FB79A;
31
- }
32
- .stTextInput>div>label {
33
- color: #4C6D7D;
34
  }
35
  </style>
36
  """, unsafe_allow_html=True)
37
 
38
- # Title and introduction
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 model and tokenizer for text generation
43
- model_name = "microsoft/DialoGPT-medium"
44
  try:
45
- tokenizer = AutoTokenizer.from_pretrained(model_name)
46
- model = AutoModelForCausalLM.from_pretrained(model_name)
47
- text_gen_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
48
  except Exception as e:
49
- st.error(f"Error loading the conversational model: {e}")
50
 
51
- # Initialize the TTS engine
52
- try:
53
- tts_engine = pyttsx3.init()
54
- tts_engine.setProperty('rate', 150) # Adjust the speed of speech
55
- tts_engine.setProperty('voice', tts_engine.getProperty('voices')[0].id) # Choose the first voice option
56
- except Exception as e:
57
- st.error(f"Error initializing the TTS engine: {e}")
58
 
59
  # User input for conversation
60
  user_input = st.text_input("Share what's on your mind...", placeholder="Type here...", max_chars=500)
61
 
62
  if user_input:
63
- # Generate a conversational response
64
- try:
65
- response = text_gen_pipeline(user_input, max_length=100, num_return_sequences=1)
66
- response_text = response[0]['generated_text']
67
-
68
- st.write("Bot's Response:")
69
- st.write(response_text)
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- # Convert the response text to speech
72
- if st.button("Play Response Audio"):
73
- tts_engine.say(response_text)
74
- tts_engine.runAndWait()
75
 
76
- except Exception as e:
77
- st.error(f"Error generating response: {e}")
 
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. πŸ’š")