Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import torch
|
3 |
-
from transformers import pipeline
|
4 |
-
|
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")
|
@@ -39,49 +39,39 @@ st.markdown("""
|
|
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
|
|
|
43 |
try:
|
44 |
-
|
|
|
|
|
45 |
except Exception as e:
|
46 |
st.error(f"Error loading the conversational model: {e}")
|
47 |
|
48 |
-
#
|
49 |
try:
|
50 |
-
|
|
|
|
|
51 |
except Exception as e:
|
52 |
-
st.error(f"Error
|
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 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
st.
|
72 |
-
except Exception as e:
|
73 |
-
st.error(f"Error generating audio: {e}")
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
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 |
|
|
|
|
|
|
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")
|
|
|
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}")
|