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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -36
app.py CHANGED
@@ -1,10 +1,10 @@
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
  "تقدم دعمًا عاطفيًا، نصائح للعناية بالنفس، وتقنيات لإدارة التوتر. "
@@ -14,7 +14,7 @@ MENTAL_HEALTH_PROMPT = (
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
  "دعم", "رعاية ذاتية", "علاج نفسي", "إرشاد", "استرخاء", "الرفاهية",
@@ -29,7 +29,7 @@ MENTAL_HEALTH_KEYWORDS = [
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]],
@@ -40,7 +40,7 @@ def respond(
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,8 +51,8 @@ def respond(
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,7 +68,7 @@ def respond(
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
  """
@@ -80,36 +80,27 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
80
  elem_id="title"
81
  )
82
 
83
- with gr.Row():
84
- system_message = gr.Textbox(
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__":
 
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
  "تقدم دعمًا عاطفيًا، نصائح للعناية بالنفس، وتقنيات لإدارة التوتر. "
 
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
  "دعم", "رعاية ذاتية", "علاج نفسي", "إرشاد", "استرخاء", "الرفاهية",
 
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]],
 
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
55
+ messages.append({"role": "user", "content": message})
56
 
57
  response = ""
58
 
 
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
  """
 
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__":