Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import random
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
# ✅
|
| 7 |
MENTAL_KEYWORDS = [
|
| 8 |
# English
|
| 9 |
"depression", "depressed", "anxiety", "anxious", "panic", "stress", "sad", "lonely",
|
|
@@ -20,29 +19,6 @@ MENTAL_KEYWORDS = [
|
|
| 20 |
"مشاعر", "مضغوط", "قلقان", "وحدي", "مش مبسوط", "زهقان", "ضايق", "تعب", "مش مرتاح",
|
| 21 |
]
|
| 22 |
|
| 23 |
-
# ✅ Off-topic keywords (EN + AR)
|
| 24 |
-
OFF_TOPIC = [
|
| 25 |
-
# English
|
| 26 |
-
"recipe", "song", "music", "lyrics", "joke", "funny", "laugh", "code", "python", "program",
|
| 27 |
-
"game", "food", "cook", "movie", "film", "series", "sport", "football", "instagram",
|
| 28 |
-
"tiktok", "money", "business", "crypto", "ai", "computer",
|
| 29 |
-
# Arabic
|
| 30 |
-
"نكتة", "ضحك", "اغنية", "اغاني", "طبخ", "اكل", "فيلم", "مسلسل", "كورة", "رياضة",
|
| 31 |
-
"بيزنس", "فلوس", "العاب", "لعبة", "كود", "برمجة", "ذكاء اصطناعي"
|
| 32 |
-
]
|
| 33 |
-
|
| 34 |
-
# ✅ Random natural off-topic responses
|
| 35 |
-
OFF_TOPIC_RESPONSES = [
|
| 36 |
-
"I'm here to help with emotional and mental well-being. Let's focus on how you're feeling, coping, or managing your emotions today.",
|
| 37 |
-
"I specialize in mental and emotional health conversations. Tell me what’s been on your mind lately.",
|
| 38 |
-
"Let’s bring it back to how you’ve been feeling — I’m here to help you talk through emotions, stress, or challenges.",
|
| 39 |
-
"My goal is to support your mental health. How have things been emotionally for you lately?",
|
| 40 |
-
"I’m here for emotional and mental support only. What’s been bothering you recently?",
|
| 41 |
-
"Let's focus on your thoughts and feelings — I can help you process or manage them better.",
|
| 42 |
-
"It sounds like you might be going off-topic. Can we talk about how you’ve been feeling instead?",
|
| 43 |
-
"Let’s keep this space focused on your emotions and well-being. What’s been heavy on your mind lately?",
|
| 44 |
-
]
|
| 45 |
-
|
| 46 |
# ✅ Detect Arabic characters
|
| 47 |
def contains_arabic(text: str) -> bool:
|
| 48 |
return bool(re.search(r"[\u0600-\u06FF]", text))
|
|
@@ -52,10 +28,6 @@ def is_mental_health_related(text: str) -> bool:
|
|
| 52 |
text_lower = text.lower()
|
| 53 |
has_arabic = contains_arabic(text_lower)
|
| 54 |
|
| 55 |
-
# If message includes off-topic Arabic or English terms → block it
|
| 56 |
-
if any(word in text_lower for word in OFF_TOPIC):
|
| 57 |
-
return False
|
| 58 |
-
|
| 59 |
# If it has mental-related Arabic/English → allow
|
| 60 |
if any(word in text_lower for word in MENTAL_KEYWORDS):
|
| 61 |
return True
|
|
@@ -64,10 +36,9 @@ def is_mental_health_related(text: str) -> bool:
|
|
| 64 |
if has_arabic:
|
| 65 |
return True
|
| 66 |
|
| 67 |
-
# Default fallback
|
| 68 |
return False
|
| 69 |
|
| 70 |
-
|
| 71 |
# ✅ Main response function
|
| 72 |
def respond(
|
| 73 |
message,
|
|
@@ -79,7 +50,7 @@ def respond(
|
|
| 79 |
hf_token: gr.OAuthToken,
|
| 80 |
):
|
| 81 |
if not is_mental_health_related(message):
|
| 82 |
-
yield
|
| 83 |
return
|
| 84 |
|
| 85 |
locked_system_message = (
|
|
@@ -110,7 +81,6 @@ def respond(
|
|
| 110 |
response += token
|
| 111 |
yield response
|
| 112 |
|
| 113 |
-
|
| 114 |
# ✅ Gradio interface setup
|
| 115 |
chatbot = gr.ChatInterface(
|
| 116 |
respond,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
+
# ✅ Mental health keywords (EN + AR + transliterated Arabic)
|
| 6 |
MENTAL_KEYWORDS = [
|
| 7 |
# English
|
| 8 |
"depression", "depressed", "anxiety", "anxious", "panic", "stress", "sad", "lonely",
|
|
|
|
| 19 |
"مشاعر", "مضغوط", "قلقان", "وحدي", "مش مبسوط", "زهقان", "ضايق", "تعب", "مش مرتاح",
|
| 20 |
]
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# ✅ Detect Arabic characters
|
| 23 |
def contains_arabic(text: str) -> bool:
|
| 24 |
return bool(re.search(r"[\u0600-\u06FF]", text))
|
|
|
|
| 28 |
text_lower = text.lower()
|
| 29 |
has_arabic = contains_arabic(text_lower)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# If it has mental-related Arabic/English → allow
|
| 32 |
if any(word in text_lower for word in MENTAL_KEYWORDS):
|
| 33 |
return True
|
|
|
|
| 36 |
if has_arabic:
|
| 37 |
return True
|
| 38 |
|
| 39 |
+
# Default fallback → block
|
| 40 |
return False
|
| 41 |
|
|
|
|
| 42 |
# ✅ Main response function
|
| 43 |
def respond(
|
| 44 |
message,
|
|
|
|
| 50 |
hf_token: gr.OAuthToken,
|
| 51 |
):
|
| 52 |
if not is_mental_health_related(message):
|
| 53 |
+
yield "I can only respond to mental health and emotional well-being topics."
|
| 54 |
return
|
| 55 |
|
| 56 |
locked_system_message = (
|
|
|
|
| 81 |
response += token
|
| 82 |
yield response
|
| 83 |
|
|
|
|
| 84 |
# ✅ Gradio interface setup
|
| 85 |
chatbot = gr.ChatInterface(
|
| 86 |
respond,
|