tarrasyed19472007 commited on
Commit
70d48b0
·
verified ·
1 Parent(s): 10e952b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import torch
4
+ 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:
19
+ st.write("Listening...")
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.")
27
+ return None
28
+ except sr.RequestError as e:
29
+ st.error(f"Could not request results; {e}")
30
+ return None
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")
42
+ st.write("Talk to your mental health assistant!")
43
+
44
+ # Voice input button
45
+ if st.button("Speak"):
46
+ user_input = get_voice_input()
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:")
58
+ 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")