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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -33
app.py CHANGED
@@ -1,7 +1,7 @@
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")
@@ -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 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
 
 
 
 
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}")