Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# β
Connect to Hugging Face Zephyr-7B API
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
-
# β
|
| 8 |
MENTAL_HEALTH_PROMPT = (
|
| 9 |
"You are Bandar AI, an empathetic mental health AI assistant. "
|
| 10 |
"You provide emotional support, self-care advice, and stress management tips. "
|
|
@@ -22,7 +24,24 @@ def is_mental_health_related(message):
|
|
| 22 |
]
|
| 23 |
return any(word in message.lower() for word in keywords)
|
| 24 |
|
| 25 |
-
# β
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def respond(
|
| 27 |
message,
|
| 28 |
history: list[tuple[str, str]],
|
|
@@ -62,31 +81,43 @@ def respond(
|
|
| 62 |
response += token
|
| 63 |
yield response # β
Stream output correctly
|
| 64 |
|
| 65 |
-
# β
Improved
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
value=MENTAL_HEALTH_PROMPT,
|
| 71 |
label="System message (Defines AI behavior)",
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
value=
|
| 79 |
-
step=0.
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
)
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# β
Run the chatbot
|
| 92 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import pyttsx3 # For text-to-speech
|
| 4 |
+
import speech_recognition as sr # For speech-to-text
|
| 5 |
|
| 6 |
# β
Connect to Hugging Face Zephyr-7B API
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
+
# β
Mental Health System Message
|
| 10 |
MENTAL_HEALTH_PROMPT = (
|
| 11 |
"You are Bandar AI, an empathetic mental health AI assistant. "
|
| 12 |
"You provide emotional support, self-care advice, and stress management tips. "
|
|
|
|
| 24 |
]
|
| 25 |
return any(word in message.lower() for word in keywords)
|
| 26 |
|
| 27 |
+
# β
Speech-to-Text Function (Voice Input)
|
| 28 |
+
def speech_to_text(audio):
|
| 29 |
+
recognizer = sr.Recognizer()
|
| 30 |
+
with sr.AudioFile(audio) as source:
|
| 31 |
+
audio_data = recognizer.record(source)
|
| 32 |
+
try:
|
| 33 |
+
return recognizer.recognize_google(audio) # Convert speech to text
|
| 34 |
+
except sr.UnknownValueError:
|
| 35 |
+
return "β Sorry, I couldn't understand the audio."
|
| 36 |
+
|
| 37 |
+
# β
Text-to-Speech Function (Voice Output)
|
| 38 |
+
def text_to_speech(response_text):
|
| 39 |
+
engine = pyttsx3.init()
|
| 40 |
+
engine.save_to_file(response_text, "response.mp3")
|
| 41 |
+
engine.runAndWait()
|
| 42 |
+
return "response.mp3"
|
| 43 |
+
|
| 44 |
+
# β
Chat Response Function
|
| 45 |
def respond(
|
| 46 |
message,
|
| 47 |
history: list[tuple[str, str]],
|
|
|
|
| 81 |
response += token
|
| 82 |
yield response # β
Stream output correctly
|
| 83 |
|
| 84 |
+
# β
Improved Modern UI
|
| 85 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 86 |
+
gr.Markdown(
|
| 87 |
+
"""
|
| 88 |
+
# π§ Bandar AI - Your Mental Health Companion
|
| 89 |
+
### π Chat about emotions, anxiety, and self-care. Get support when you need it.
|
| 90 |
+
"""
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
system_message = gr.Textbox(
|
| 95 |
value=MENTAL_HEALTH_PROMPT,
|
| 96 |
label="System message (Defines AI behavior)",
|
| 97 |
+
interactive=False,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
chat_interface = gr.ChatInterface(
|
| 101 |
+
respond,
|
| 102 |
+
additional_inputs=[
|
| 103 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"),
|
| 104 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 105 |
+
gr.Slider(
|
| 106 |
+
minimum=0.1,
|
| 107 |
+
maximum=1.0,
|
| 108 |
+
value=0.95,
|
| 109 |
+
step=0.05,
|
| 110 |
+
label="Top-p (nucleus sampling)",
|
| 111 |
+
),
|
| 112 |
+
],
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
voice_input = gr.Microphone(label="π€ Speak", type="filepath")
|
| 117 |
+
voice_output = gr.Audio(label="π AI Response", type="file")
|
| 118 |
+
|
| 119 |
+
voice_input.change(speech_to_text, inputs=voice_input, outputs=chat_interface.input_component)
|
| 120 |
+
chat_interface.output_component.change(text_to_speech, inputs=chat_interface.output_component, outputs=voice_output)
|
| 121 |
|
| 122 |
# β
Run the chatbot
|
| 123 |
if __name__ == "__main__":
|