Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,52 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import
|
| 3 |
-
|
| 4 |
-
import os
|
| 5 |
from youtubesearchpython import VideosSearch
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def detect_crisis(input_text):
|
| 31 |
-
return any(word in input_text.lower() for word in crisis_keywords)
|
| 32 |
-
|
| 33 |
-
# Function to suggest hobbies
|
| 34 |
-
def suggest_hobbies():
|
| 35 |
-
return ["Painting", "Gardening", "Writing", "Yoga", "Learning an instrument"]
|
| 36 |
-
|
| 37 |
-
# App header
|
| 38 |
st.title("Grief and Loss Support Bot")
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
-
|
| 55 |
-
response = chatbot(user_input)
|
| 56 |
-
generated_text = response[0]['generated_text']
|
| 57 |
-
|
| 58 |
-
# Append to history and display response
|
| 59 |
-
st.session_state['history'].append(("You", user_input))
|
| 60 |
-
st.session_state['history'].append(("Bot", generated_text))
|
| 61 |
-
|
| 62 |
-
# Display conversation history
|
| 63 |
-
for speaker, text in st.session_state['history']:
|
| 64 |
-
st.write(f"**{speaker}:** {text}")
|
| 65 |
-
|
| 66 |
-
# Voice output
|
| 67 |
-
speak_text(generated_text)
|
| 68 |
-
|
| 69 |
-
# Display hobby suggestions
|
| 70 |
-
st.markdown("### Hobby Suggestions")
|
| 71 |
-
st.markdown(", ".join(suggest_hobbies()))
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
st.
|
| 75 |
-
video_links = get_video_links("coping with grief and loss", max_results=3)
|
| 76 |
-
for title, link in video_links:
|
| 77 |
-
st.markdown(f"- [{title}]({link})")
|
|
|
|
| 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.")
|
|
|
|
|
|
|
|
|