Bndo commited on
Commit
78d8741
·
verified ·
1 Parent(s): 1285836

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -33
app.py CHANGED
@@ -1,26 +1,35 @@
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 (Supports Arabic)
8
  MENTAL_HEALTH_PROMPT = (
9
  "أنت بندر AI، مساعد صحي نفسي متعاطف. "
10
  "تقدم دعمًا عاطفيًا، نصائح للعناية بالنفس، وتقنيات لإدارة التوتر. "
11
- "لا تقدم تشخيصات طبية أبدًا، ويجب عليك دائمًا تشجيع المستخدمين على طلب المساعدة المتخصصة عند الحاجة. "
12
- "ها هو نص المستخدم:\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
- "قلق", "توتر", "اكتئاب", "حزين", "علاج", "عواطف", "صحة نفسية",
19
- "دعم", "رعاية ذاتية", "علاج نفسي", "إرشاد", "استرخاء", "الرفاهية"
20
- ]
21
- return any(word in message.lower() for word in keywords)
22
 
23
- # ✅ Chat Response Function (Arabic Support)
24
  def respond(
25
  message,
26
  history: list[tuple[str, str]],
@@ -29,9 +38,9 @@ def respond(
29
  temperature,
30
  top_p,
31
  ):
32
- # 🔴 Reject unrelated topics with a response
33
  if not is_mental_health_related(message):
34
- return "❌ عذرًا، يمكنني فقط مناقشة مواضيع الصحة النفسية. يرجى السؤال عن العواطف، الرعاية الذاتية، أو إدارة التوتر."
35
 
36
  messages = [{"role": "system", "content": system_message}]
37
 
@@ -42,8 +51,8 @@ def respond(
42
  if val[1]:
43
  messages.append({"role": "assistant", "content": val[1]})
44
 
45
- # ✅ Add the new user input
46
- messages.append({"role": "user", "content": message})
47
 
48
  response = ""
49
 
@@ -59,13 +68,16 @@ def respond(
59
  response += token
60
  yield response # ✅ Stream output correctly
61
 
62
- # ✅ Modern Arabic UI
63
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
64
  gr.Markdown(
65
  """
66
- # 🧠 بندر AI - رفيقك للصحة النفسية
67
- ### 💙 تحدث عن مشاعرك، التوتر، والعناية الذاتية. احصل على الدعم عندما تحتاج إليه.
68
- """
 
 
 
69
  )
70
 
71
  with gr.Row():
@@ -73,23 +85,31 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
73
  value=MENTAL_HEALTH_PROMPT,
74
  label="رسالة النظام (تحديد سلوك الذكاء الاصطناعي)",
75
  interactive=False,
 
76
  )
77
 
78
- chat_interface = gr.ChatInterface(
79
- respond,
80
- additional_inputs=[
81
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="أقصى عدد من الكلمات"),
82
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="درجة العشوائية"),
83
- gr.Slider(
84
- minimum=0.1,
85
- maximum=1.0,
86
- value=0.95,
87
- step=0.05,
88
- label="Top-p (اختيار الكلمات)",
 
 
 
 
 
 
 
 
89
  ),
90
- ],
91
- type="messages", # ✅ Fixes deprecated warning
92
- )
93
 
94
  # ✅ Run the chatbot
95
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # ✅ Use a multilingual model for better Arabic & English support
5
+ client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
6
 
7
+ # ✅ Mental Health System Message (Arabic & English)
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 (Arabic & English)
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 (Arabic & English Support)
33
  def respond(
34
  message,
35
  history: list[tuple[str, str]],
 
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
 
 
51
  if val[1]:
52
  messages.append({"role": "assistant", "content": val[1]})
53
 
54
+ # ✅ Add the new user input (Ensure UTF-8 encoding)
55
+ messages.append({"role": "user", "content": message.encode("utf-8").decode("utf-8")})
56
 
57
  response = ""
58
 
 
68
  response += token
69
  yield response # ✅ Stream output correctly
70
 
71
+ # ✅ Beautiful 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
  with gr.Row():
 
85
  value=MENTAL_HEALTH_PROMPT,
86
  label="رسالة النظام (تحديد سلوك الذكاء الاصطناعي)",
87
  interactive=False,
88
+ visible=False, # ✅ Hide system message box for a cleaner UI
89
  )
90
 
91
+ # Chat Interface with Improved Design
92
+ with gr.Box():
93
+ chat_interface = gr.ChatInterface(
94
+ respond,
95
+ additional_inputs=[
96
+ gr.Slider(minimum=1, maximum=1024, value=512, step=1, label="🔠 أقصى عدد من الكلمات"),
97
+ gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="🔥 درجة العشوائية"),
98
+ gr.Slider(
99
+ minimum=0.1,
100
+ maximum=1.0,
101
+ value=0.95,
102
+ step=0.05,
103
+ label="🎯 Top-p (اختيار الكلمات)",
104
+ ),
105
+ ],
106
+ chatbot=gr.Chatbot(
107
+ bubble_radius=10, # ✅ Rounded bubbles
108
+ height=400, # ✅ Fixed height for better layout
109
+ avatar_images=("🧑‍💻", "🤖"), # ✅ User and AI icons
110
  ),
111
+ type="messages", # ✅ Fixes deprecated warning
112
+ )
 
113
 
114
  # ✅ Run the chatbot
115
  if __name__ == "__main__":