Bndo commited on
Commit
112bd19
Β·
verified Β·
1 Parent(s): 21a471c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -9
app.py CHANGED
@@ -1,10 +1,27 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # βœ… Set up Hugging Face Inference API with the correct model
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
7
- # βœ… Define the response function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(value="You are Bandar AI, a friendly chatbot.", label="System message"),
 
 
 
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="πŸ€– Bandar AI Chatbot",
59
- description="πŸš€ Chat with Bandar AI, powered by Zephyr-7B."
 
 
 
 
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