Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 pydub import AudioSegment
|
8 |
+
from io import BytesIO
|
9 |
+
|
10 |
+
# Set your Hugging Face API key
|
11 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "doctor app"
|
12 |
+
|
13 |
+
# Load the Hugging Face model
|
14 |
+
chatbot = pipeline("conversational", model="thrishala/mental_health_chatbot")
|
15 |
+
|
16 |
+
# Function to get voice input using Whisper
|
17 |
+
def get_voice_input():
|
18 |
+
recognizer = sr.Recognizer()
|
19 |
+
with sr.Microphone() as source:
|
20 |
+
st.write("Listening...")
|
21 |
+
audio = recognizer.listen(source)
|
22 |
+
st.write("Recognizing...")
|
23 |
+
try:
|
24 |
+
text = recognizer.recognize_whisper(audio)
|
25 |
+
return text
|
26 |
+
except sr.UnknownValueError:
|
27 |
+
st.error("Sorry, I could not understand the audio.")
|
28 |
+
return None
|
29 |
+
except sr.RequestError as e:
|
30 |
+
st.error(f"Could not request results; {e}")
|
31 |
+
return None
|
32 |
+
|
33 |
+
# Function to generate voice response using gTTS
|
34 |
+
def speak(text):
|
35 |
+
tts = gTTS(text=text, lang='en')
|
36 |
+
audio_file = BytesIO()
|
37 |
+
tts.save(audio_file)
|
38 |
+
audio_file.seek(0)
|
39 |
+
return audio_file
|
40 |
+
|
41 |
+
# Streamlit app layout
|
42 |
+
st.title("Mental Health Chatbot")
|
43 |
+
st.write("Talk to your mental health assistant!")
|
44 |
+
|
45 |
+
# Voice input button
|
46 |
+
if st.button("Speak"):
|
47 |
+
user_input = get_voice_input()
|
48 |
+
if user_input:
|
49 |
+
st.write(f"You: {user_input}")
|
50 |
+
# Get response from the chatbot
|
51 |
+
response = chatbot(user_input)[0]['generated_text']
|
52 |
+
st.write(f"Bot: {response}")
|
53 |
+
# Generate voice response
|
54 |
+
audio_output = speak(response)
|
55 |
+
st.audio(audio_output, format="audio/mp3")
|
56 |
+
|
57 |
+
# Text input
|
58 |
+
user_input = st.text_input("Type your message:")
|
59 |
+
if st.button("Send"):
|
60 |
+
if user_input:
|
61 |
+
st.write(f"You: {user_input}")
|
62 |
+
# Get response from the chatbot
|
63 |
+
response = chatbot(user_input)[0]['generated_text']
|
64 |
+
st.write(f"Bot: {response}")
|
65 |
+
# Generate voice response
|
66 |
+
audio_output = speak(response)
|
67 |
+
st.audio(audio_output, format="audio/mp3")
|