Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
# ✅
|
| 5 |
-
client = InferenceClient("
|
| 6 |
|
| 7 |
# ✅ Mental Health System Message
|
| 8 |
MENTAL_HEALTH_PROMPT = (
|
| 9 |
-
"أنت
|
| 10 |
-
"تقدم دعم
|
| 11 |
-
"لا تقدم تشخيصات طبية
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 "❌
|
| 44 |
-
|
| 45 |
-
messages = [{"role": "system", "content": system_message}]
|
| 46 |
|
| 47 |
-
# ✅
|
|
|
|
|
|
|
| 48 |
for val in history:
|
| 49 |
-
if val[0]:
|
| 50 |
-
|
| 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
|
| 61 |
-
|
| 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 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
</p>
|
| 79 |
-
""",
|
| 80 |
-
elem_id="title"
|
| 81 |
)
|
| 82 |
|
| 83 |
-
|
| 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__":
|