Bndo commited on
Commit
90fac1c
·
verified ·
1 Parent(s): bd02034

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -73
app.py CHANGED
@@ -1,106 +1,59 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # ✅ Use a multilingual model for Arabic & English support
5
- client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
6
 
7
  # ✅ Mental Health System Message
8
  MENTAL_HEALTH_PROMPT = (
9
- "أنت بندر AI، مساعد صحي نفسي متعاطف. "
10
- "تقدم دعمًا عاطفيًا، نصائح للعناية بالنفس، وتقنيات لإدارة التوتر. "
11
- "لا تقدم تشخيصات طبية أبدًا، ويجب عليك دائمًا تشجيع المستخدمين على طلب المساعدة المتخصصة عند الحاجة.\n"
12
- "You are Bandar AI, an empathetic mental health AI assistant. "
13
- "You provide emotional support, self-care advice, and stress management tips. "
14
- "You DO NOT give medical diagnoses. Always encourage users to seek professional help if needed.\n"
15
  )
16
 
17
- # ✅ Mental health keywords
18
- MENTAL_HEALTH_KEYWORDS = [
19
- "قلق", "توتر", "اكتئاب", "حزين", "علاج", "عواطف", "صحة نفسية",
20
- "دعم", "رعاية ذاتية", "علاج نفسي", "إرشاد", "استرخاء", "الرفاهية",
21
- "ضغط نفسي", "مساعدة", "أشعر بالحزن", "أحتاج إلى دعم", "قلق شديد",
22
- "anxious", "stress", "depressed", "lonely", "overwhelmed", "panic",
23
- "mental health", "self-care", "therapy", "sad", "burnout",
24
- "emotions", "counseling", "support", "well-being", "anxiety",
25
- "depression", "happiness", "relaxation", "coping", "mindfulness"
26
- ]
27
-
28
  # ✅ Function to check if the message is related to mental health
29
  def is_mental_health_related(message):
30
- return any(word in message.lower() for word in MENTAL_HEALTH_KEYWORDS)
 
 
 
 
 
 
31
 
32
  # ✅ Chat Response Function
33
- def respond(
34
- message,
35
- history: list[tuple[str, str]],
36
- system_message,
37
- max_tokens,
38
- temperature,
39
- top_p,
40
- ):
41
  # 🔴 Reject unrelated topics
42
  if not is_mental_health_related(message):
43
- return "❌ عذرًا، يمكنني فقط مناقشة مواضيع الصحة النفسية.\nSorry, I can only discuss mental health topics."
44
-
45
- messages = [{"role": "system", "content": system_message}]
46
 
47
- # ✅ Add chat history
 
 
48
  for val in history:
49
- if val[0]:
50
- messages.append({"role": "user", "content": val[0]})
51
- if val[1]:
52
- messages.append({"role": "assistant", "content": val[1]})
53
 
54
- # ✅ Add the new user input
55
  messages.append({"role": "user", "content": message})
56
 
57
  response = ""
58
 
59
  # ✅ Stream responses from Hugging Face API
60
- for message in client.chat_completion(
61
- messages,
62
- max_tokens=max_tokens,
63
- stream=True,
64
- temperature=temperature,
65
- top_p=top_p,
66
- ):
67
- token = message.choices[0].delta.content
68
  response += token
69
  yield response # ✅ Stream output correctly
70
 
71
- # ✅ Modern UI
72
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
73
  gr.Markdown(
74
  """
75
- <h1 style="text-align: center; color: #ffffff;">🧠 بندر AI - رفيقك للصحة النفسية</h1>
76
- <p style="text-align: center; font-size: 18px; color: #dddddd;">
77
- 💙 تحدث عن مشاعرك، التوتر، والعناية الذاتية. احصل على الدعم عندما تحتاج إليه.
78
- </p>
79
- """,
80
- elem_id="title"
81
  )
82
 
83
- # Chat Interface with Improved Design
84
- chat_interface = gr.ChatInterface(
85
- respond,
86
- additional_inputs=[
87
- gr.Slider(minimum=1, maximum=1024, value=512, step=1, label="🔠 أقصى عدد من الكلمات"),
88
- gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="🔥 درجة العشوائية"),
89
- gr.Slider(
90
- minimum=0.1,
91
- maximum=1.0,
92
- value=0.95,
93
- step=0.05,
94
- label="🎯 Top-p (اختيار الكلمات)",
95
- ),
96
- ],
97
- chatbot=gr.Chatbot(
98
- bubble_radius=10, # ✅ Rounded bubbles
99
- height=400, # ✅ Fixed height for better layout
100
- avatar_images=("🧑‍💻", "🤖"), # ✅ User and AI icons
101
- ),
102
- type="messages", # ✅ Fixes deprecated warning
103
- )
104
 
105
  # ✅ Run the chatbot
106
  if __name__ == "__main__":
 
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 System Message
8
  MENTAL_HEALTH_PROMPT = (
9
+ "أنت مساعد ذكاء اصطناعي للصحة النفسية. "
10
+ "أنت تقدم الدعم العاطفي، نصائح الرعاية الذاتية، وإدارة التوتر. "
11
+ "لا تقدم تشخيصات طبية. شجع المستخدمين دائمًا على طلب المساعدة المهنية عند الحاجة."
 
 
 
12
  )
13
 
 
 
 
 
 
 
 
 
 
 
 
14
  # ✅ Function to check if the message is related to mental health
15
  def is_mental_health_related(message):
16
+ keywords = [
17
+ "قلق", "توتر", "اكتئاب", "وحدة", "إجهاد", "نوبات هلع",
18
+ "صحة نفسية", "رعاية ذاتية", "علاج", "حزن", "احتراق وظيفي",
19
+ "عواطف", "استشارة", "دعم", "رفاهية", "تفكير إيجابي", "راحة",
20
+ "coping", "therapy", "anxiety", "stress", "depression",
21
+ ]
22
+ return any(word in message.lower() for word in keywords)
23
 
24
  # ✅ Chat Response Function
25
+ def respond(message, history):
 
 
 
 
 
 
 
26
  # 🔴 Reject unrelated topics
27
  if not is_mental_health_related(message):
28
+ return "❌ يمكنني فقط مناقشة مواضيع الصحة النفسية. الرجاء طرح أسئلة عن العواطف، الرعاية الذاتية، أو إدارة التوتر."
 
 
29
 
30
+ # ✅ Prepare conversation
31
+ messages = [{"role": "system", "content": MENTAL_HEALTH_PROMPT}]
32
+
33
  for val in history:
34
+ if val[0]: messages.append({"role": "user", "content": val[0]})
35
+ if val[1]: messages.append({"role": "assistant", "content": val[1]})
 
 
36
 
 
37
  messages.append({"role": "user", "content": message})
38
 
39
  response = ""
40
 
41
  # ✅ Stream responses from Hugging Face API
42
+ for msg in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
43
+ token = msg.choices[0].delta.content
 
 
 
 
 
 
44
  response += token
45
  yield response # ✅ Stream output correctly
46
 
47
+ # ✅ Clean & Modern UI
48
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
49
  gr.Markdown(
50
  """
51
+ # 🧠 **Bandar AI - Your Mental Health Companion**
52
+ ### 💙 تحدث عن مشاعرك، القلق، والتوتر. نحن هنا لدعمك.
53
+ """
 
 
 
54
  )
55
 
56
+ chatbot = gr.ChatInterface(respond)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  # ✅ Run the chatbot
59
  if __name__ == "__main__":