Bey007 commited on
Commit
d7596e9
·
verified ·
1 Parent(s): 2e197a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -46
app.py CHANGED
@@ -1,52 +1,42 @@
1
  import streamlit as st
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import pyttsx3
4
  from youtubesearchpython import VideosSearch
 
5
 
6
- # Load model and tokenizer
7
- model_name = "microsoft/DialoGPT-medium"
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = AutoModelForCausalLM.from_pretrained(model_name)
10
 
11
- # Initialize text-to-speech engine
12
- engine = pyttsx3.init()
13
- engine.setProperty('rate', 150)
14
- engine.setProperty('voice', 'english+f3') # Choose a calm, soothing voice
15
-
16
- # Function to generate a response
17
- def chat_with_bot(input_text):
18
- input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt")
19
- response_ids = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
20
- response = tokenizer.decode(response_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
21
- return response
22
-
23
- # Function to search for YouTube videos related to grief and loss
24
- def get_video_links(query):
25
- videos_search = VideosSearch(query, limit=3)
26
- return videos_search.result()["result"]
27
-
28
- # Streamlit app interface
29
  st.title("Grief and Loss Support Bot")
30
- st.write("Welcome! This bot provides support and resources to help you cope with grief and loss.")
31
-
32
- user_input = st.text_input("You: ", placeholder="Type your message here...")
33
- if st.button("Send"):
34
- if user_input:
35
- # Generate and display response
36
- bot_response = chat_with_bot(user_input)
37
- st.write("Bot:", bot_response)
38
-
39
- # Speak the response using text-to-speech
40
- engine.say(bot_response)
41
- engine.runAndWait()
42
-
43
- # Display interactive content links
44
- st.write("Here are some helpful resources:")
45
- video_results = get_video_links("grief and loss support")
46
- for video in video_results:
47
- st.write(f"[{video['title']}]({video['link']})")
48
- else:
49
- st.write("Please enter a message.")
50
 
51
- # Footer
52
- st.write("Remember, you are not alone. If you're in crisis, please reach out to a trusted source or helpline.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")