tarrasyed19472007 commited on
Commit
db9d2dc
·
verified ·
1 Parent(s): c8bbd1e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -20
app.py CHANGED
@@ -5,14 +5,19 @@ from transformers import pipeline
5
  import speech_recognition as sr
6
  from gtts import gTTS
7
  from io import BytesIO
 
 
8
 
9
- # Set your Hugging Face API key
10
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = "doctor app"
11
 
12
- # Load the Hugging Face model using text-generation
13
- chatbot = pipeline("text-generation", model="thrishala/mental_health_chatbot")
 
 
 
14
 
15
- # Function to get voice input using Whisper
16
  def get_voice_input():
17
  recognizer = sr.Recognizer()
18
  with sr.Microphone() as source:
@@ -20,7 +25,8 @@ def get_voice_input():
20
  audio = recognizer.listen(source)
21
  st.write("Recognizing...")
22
  try:
23
- text = recognizer.recognize_whisper(audio)
 
24
  return text
25
  except sr.UnknownValueError:
26
  st.error("Sorry, I could not understand the audio.")
@@ -31,11 +37,15 @@ def get_voice_input():
31
 
32
  # Function to generate voice response using gTTS
33
  def speak(text):
34
- tts = gTTS(text=text, lang='en')
35
- audio_file = BytesIO()
36
- tts.save(audio_file)
37
- audio_file.seek(0)
38
- return audio_file
 
 
 
 
39
 
40
  # Streamlit app layout
41
  st.title("Mental Health Chatbot")
@@ -47,11 +57,15 @@ if st.button("Speak"):
47
  if user_input:
48
  st.write(f"You: {user_input}")
49
  # Get response from the chatbot
50
- response = chatbot(user_input, max_length=150, num_return_sequences=1)[0]['generated_text']
51
- st.write(f"Bot: {response}")
52
- # Generate voice response
53
- audio_output = speak(response)
54
- st.audio(audio_output, format="audio/mp3")
 
 
 
 
55
 
56
  # Text input
57
  user_input = st.text_input("Type your message:")
@@ -59,8 +73,12 @@ if st.button("Send"):
59
  if user_input:
60
  st.write(f"You: {user_input}")
61
  # Get response from the chatbot
62
- response = chatbot(user_input, max_length=150, num_return_sequences=1)[0]['generated_text']
63
- st.write(f"Bot: {response}")
64
- # Generate voice response
65
- audio_output = speak(response)
66
- st.audio(audio_output, format="audio/mp3")
 
 
 
 
 
5
  import speech_recognition as sr
6
  from gtts import gTTS
7
  from io import BytesIO
8
+ from pydub import AudioSegment
9
+ import tempfile
10
 
11
+ # Set Hugging Face API key (replace 'your_hugging_face_api_key' with your actual API key)
12
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = "doctor app"
13
 
14
+ # Load the Hugging Face model for text generation
15
+ try:
16
+ chatbot = pipeline("text-generation", model="thrishala/mental_health_chatbot")
17
+ except Exception as e:
18
+ st.error(f"Error loading model: {e}")
19
 
20
+ # Function to capture voice input using SpeechRecognition and OpenAI Whisper
21
  def get_voice_input():
22
  recognizer = sr.Recognizer()
23
  with sr.Microphone() as source:
 
25
  audio = recognizer.listen(source)
26
  st.write("Recognizing...")
27
  try:
28
+ # Converting audio to text using OpenAI Whisper if available, fallback to Google's API otherwise
29
+ text = recognizer.recognize_google(audio)
30
  return text
31
  except sr.UnknownValueError:
32
  st.error("Sorry, I could not understand the audio.")
 
37
 
38
  # Function to generate voice response using gTTS
39
  def speak(text):
40
+ try:
41
+ tts = gTTS(text=text, lang='en')
42
+ audio_file = BytesIO()
43
+ tts.write_to_fp(audio_file)
44
+ audio_file.seek(0)
45
+ return audio_file
46
+ except Exception as e:
47
+ st.error(f"Error generating audio response: {e}")
48
+ return None
49
 
50
  # Streamlit app layout
51
  st.title("Mental Health Chatbot")
 
57
  if user_input:
58
  st.write(f"You: {user_input}")
59
  # Get response from the chatbot
60
+ try:
61
+ response = chatbot(user_input, max_length=150, num_return_sequences=1)[0]['generated_text']
62
+ st.write(f"Bot: {response}")
63
+ # Generate voice response
64
+ audio_output = speak(response)
65
+ if audio_output:
66
+ st.audio(audio_output, format="audio/mp3")
67
+ except Exception as e:
68
+ st.error(f"Error generating response: {e}")
69
 
70
  # Text input
71
  user_input = st.text_input("Type your message:")
 
73
  if user_input:
74
  st.write(f"You: {user_input}")
75
  # Get response from the chatbot
76
+ try:
77
+ response = chatbot(user_input, max_length=150, num_return_sequences=1)[0]['generated_text']
78
+ st.write(f"Bot: {response}")
79
+ # Generate voice response
80
+ audio_output = speak(response)
81
+ if audio_output:
82
+ st.audio(audio_output, format="audio/mp3")
83
+ except Exception as e:
84
+ st.error(f"Error generating response: {e}")