Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
# β
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
-
# β
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def respond(
|
| 9 |
message,
|
| 10 |
history: list[tuple[str, str]],
|
|
@@ -13,16 +30,20 @@ def respond(
|
|
| 13 |
temperature,
|
| 14 |
top_p,
|
| 15 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
|
| 18 |
-
# Add chat history
|
| 19 |
for val in history:
|
| 20 |
if val[0]:
|
| 21 |
messages.append({"role": "user", "content": val[0]})
|
| 22 |
if val[1]:
|
| 23 |
messages.append({"role": "assistant", "content": val[1]})
|
| 24 |
|
| 25 |
-
# Add the new user input
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = ""
|
|
@@ -39,12 +60,14 @@ def respond(
|
|
| 39 |
response += token
|
| 40 |
yield response # Stream output in real-time
|
| 41 |
|
| 42 |
-
|
| 43 |
-
# β
Improved Chatbot UI
|
| 44 |
demo = gr.ChatInterface(
|
| 45 |
respond,
|
| 46 |
additional_inputs=[
|
| 47 |
-
gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 48 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 49 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 50 |
gr.Slider(
|
|
@@ -55,8 +78,12 @@ demo = gr.ChatInterface(
|
|
| 55 |
label="Top-p (nucleus sampling)",
|
| 56 |
),
|
| 57 |
],
|
| 58 |
-
title="
|
| 59 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
# β
Run the chatbot
|
|
|
|
| 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 |
+
# β
Mental health-specific system message
|
| 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. "
|
| 11 |
+
"You DO NOT give medical diagnoses. Always encourage users to seek professional help if needed. "
|
| 12 |
+
"Here is the user's message:\n"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# β
Function to check if the message is related to mental health
|
| 16 |
+
def is_mental_health_related(message):
|
| 17 |
+
keywords = [
|
| 18 |
+
"anxious", "stress", "depressed", "lonely", "overwhelmed",
|
| 19 |
+
"panic", "mental health", "self-care", "therapy", "sad", "burnout",
|
| 20 |
+
"emotions", "counseling", "support", "well-being"
|
| 21 |
+
]
|
| 22 |
+
return any(word in message.lower() for word in keywords)
|
| 23 |
+
|
| 24 |
+
# β
Function to handle chat responses
|
| 25 |
def respond(
|
| 26 |
message,
|
| 27 |
history: list[tuple[str, str]],
|
|
|
|
| 30 |
temperature,
|
| 31 |
top_p,
|
| 32 |
):
|
| 33 |
+
# π΄ Reject unrelated topics
|
| 34 |
+
if not is_mental_health_related(message):
|
| 35 |
+
return "β Sorry, I can only discuss mental health topics. Please ask about emotions, self-care, or stress management."
|
| 36 |
+
|
| 37 |
messages = [{"role": "system", "content": system_message}]
|
| 38 |
|
| 39 |
+
# β
Add chat history
|
| 40 |
for val in history:
|
| 41 |
if val[0]:
|
| 42 |
messages.append({"role": "user", "content": val[0]})
|
| 43 |
if val[1]:
|
| 44 |
messages.append({"role": "assistant", "content": val[1]})
|
| 45 |
|
| 46 |
+
# β
Add the new user input
|
| 47 |
messages.append({"role": "user", "content": message})
|
| 48 |
|
| 49 |
response = ""
|
|
|
|
| 60 |
response += token
|
| 61 |
yield response # Stream output in real-time
|
| 62 |
|
| 63 |
+
# β
Improved Chatbot UI for Mental Health
|
|
|
|
| 64 |
demo = gr.ChatInterface(
|
| 65 |
respond,
|
| 66 |
additional_inputs=[
|
| 67 |
+
gr.Textbox(
|
| 68 |
+
value=MENTAL_HEALTH_PROMPT,
|
| 69 |
+
label="System message (Defines AI behavior)",
|
| 70 |
+
),
|
| 71 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 72 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 73 |
gr.Slider(
|
|
|
|
| 78 |
label="Top-p (nucleus sampling)",
|
| 79 |
),
|
| 80 |
],
|
| 81 |
+
title="π§ Bandar AI - Your Mental Health Companion",
|
| 82 |
+
description=(
|
| 83 |
+
"π Bandar AI is here to support your mental well-being. "
|
| 84 |
+
"Chat with me about stress, anxiety, or self-care. "
|
| 85 |
+
"I offer guidance, but I'm NOT a medical professional. Always seek expert help when needed."
|
| 86 |
+
),
|
| 87 |
)
|
| 88 |
|
| 89 |
# β
Run the chatbot
|