Spaces:
Sleeping
Sleeping
Update app.py
#1
by
Mariam444 - opened
app.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import
|
| 4 |
import re
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# ✅ تنظيف المدخلات من السبام
|
| 7 |
OFF_TOPIC_REGEX = r"(http|www|buy now|discount|subscribe|follow me|click here)"
|
| 8 |
|
|
@@ -13,6 +16,7 @@ def is_safe_to_process(text: str) -> bool:
|
|
| 13 |
return False
|
| 14 |
return True
|
| 15 |
|
|
|
|
| 16 |
def respond(
|
| 17 |
message,
|
| 18 |
history: list[dict[str, str]],
|
|
@@ -20,11 +24,9 @@ def respond(
|
|
| 20 |
max_tokens,
|
| 21 |
temperature,
|
| 22 |
top_p,
|
| 23 |
-
|
| 24 |
-
request: gr.Request, # 👈 دي اللي بتسحب بيانات اليوزر من موقعك
|
| 25 |
):
|
| 26 |
-
# 1
|
| 27 |
-
# موقعك هيبعت الـ Iframe كدا: src=".../?username=Koko"
|
| 28 |
user_name = "User"
|
| 29 |
if request:
|
| 30 |
user_name = request.query_params.get("username", "User")
|
|
@@ -33,27 +35,30 @@ def respond(
|
|
| 33 |
yield f"Hello {user_name}, I'm here to support your emotional well-being. How can I help you today?"
|
| 34 |
return
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
return
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
# 2
|
| 43 |
-
# بنحقن الاسم في الـ System Message عشان الموديل يناديه باسمه صح
|
| 44 |
personalized_system = (
|
| 45 |
f"{system_message} "
|
| 46 |
f"The user's name is {user_name}. Always address them by this name naturally. "
|
| 47 |
f"Do not use formal headers like 'Dear'."
|
| 48 |
)
|
| 49 |
-
|
| 50 |
-
# الـ History هنا هتكون اللي مبعوثة من الـ Frontend بتاعك فقط
|
| 51 |
-
# الموديل هياخد آخر 10 رسايل عشان الذاكرة تكون قوية بس سريعة
|
| 52 |
messages = [{"role": "system", "content": personalized_system}]
|
| 53 |
-
messages.extend(history[-10:])
|
| 54 |
messages.append({"role": "user", "content": message})
|
| 55 |
|
| 56 |
response = ""
|
|
|
|
| 57 |
try:
|
| 58 |
for msg in client.chat_completion(
|
| 59 |
messages,
|
|
@@ -69,15 +74,17 @@ def respond(
|
|
| 69 |
token = msg.choices[0].delta.content or ""
|
| 70 |
response += token
|
| 71 |
yield response
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
yield f"Connection error: {str(e)}. Please try refreshing."
|
| 74 |
|
| 75 |
-
|
|
|
|
| 76 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 77 |
with gr.Sidebar():
|
| 78 |
gr.Markdown("## 🌿 Therapy Assistant")
|
| 79 |
-
gr.LoginButton()
|
| 80 |
gr.Markdown("---")
|
|
|
|
| 81 |
sys_msg = gr.Textbox(
|
| 82 |
value=(
|
| 83 |
"You are a compassionate mental health assistant. "
|
|
@@ -88,6 +95,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
| 88 |
label="System Persona",
|
| 89 |
lines=5
|
| 90 |
)
|
|
|
|
| 91 |
tokens = gr.Slider(128, 1024, value=512, label="Max Response Length")
|
| 92 |
temp = gr.Slider(0.1, 1.5, value=0.7, label="Temperature")
|
| 93 |
top_p_val = gr.Slider(0.1, 1.0, value=0.9, label="Top-p")
|
|
@@ -96,13 +104,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
| 96 |
respond,
|
| 97 |
type="messages",
|
| 98 |
additional_inputs=[sys_msg, tokens, temp, top_p_val],
|
| 99 |
-
# خلي بالك الـ Examples لازم تطابق عدد الـ inputs
|
| 100 |
-
examples=[
|
| 101 |
-
["I've been feeling very anxious lately.", sys_msg.value, 512, 0.7, 0.9],
|
| 102 |
-
["Ana ta3ban awee w msh 3aref anam.", sys_msg.value, 512, 0.7, 0.9]
|
| 103 |
-
],
|
| 104 |
cache_examples=False,
|
| 105 |
)
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
# ✅ هنستخدم التوكن من Secrets
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
+
|
| 9 |
# ✅ تنظيف المدخلات من السبام
|
| 10 |
OFF_TOPIC_REGEX = r"(http|www|buy now|discount|subscribe|follow me|click here)"
|
| 11 |
|
|
|
|
| 16 |
return False
|
| 17 |
return True
|
| 18 |
|
| 19 |
+
|
| 20 |
def respond(
|
| 21 |
message,
|
| 22 |
history: list[dict[str, str]],
|
|
|
|
| 24 |
max_tokens,
|
| 25 |
temperature,
|
| 26 |
top_p,
|
| 27 |
+
request: gr.Request, # 👈 مازلنا بنستخدمها عشان نسحب username
|
|
|
|
| 28 |
):
|
| 29 |
+
# 1️⃣ سحب Username من الـ URL
|
|
|
|
| 30 |
user_name = "User"
|
| 31 |
if request:
|
| 32 |
user_name = request.query_params.get("username", "User")
|
|
|
|
| 35 |
yield f"Hello {user_name}, I'm here to support your emotional well-being. How can I help you today?"
|
| 36 |
return
|
| 37 |
|
| 38 |
+
# ❌ مفيش تسجيل دخول خلاص
|
| 39 |
+
if not HF_TOKEN:
|
| 40 |
+
yield "Server configuration error. Missing HF_TOKEN."
|
| 41 |
return
|
| 42 |
|
| 43 |
+
# ✅ إنشاء العميل بالتوكن الثابت
|
| 44 |
+
client = InferenceClient(
|
| 45 |
+
token=HF_TOKEN,
|
| 46 |
+
model="HuggingFaceH4/zephyr-7b-beta"
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
# 2️⃣ تخصيص الشخصية بالاسم
|
|
|
|
| 50 |
personalized_system = (
|
| 51 |
f"{system_message} "
|
| 52 |
f"The user's name is {user_name}. Always address them by this name naturally. "
|
| 53 |
f"Do not use formal headers like 'Dear'."
|
| 54 |
)
|
| 55 |
+
|
|
|
|
|
|
|
| 56 |
messages = [{"role": "system", "content": personalized_system}]
|
| 57 |
+
messages.extend(history[-10:])
|
| 58 |
messages.append({"role": "user", "content": message})
|
| 59 |
|
| 60 |
response = ""
|
| 61 |
+
|
| 62 |
try:
|
| 63 |
for msg in client.chat_completion(
|
| 64 |
messages,
|
|
|
|
| 74 |
token = msg.choices[0].delta.content or ""
|
| 75 |
response += token
|
| 76 |
yield response
|
| 77 |
+
|
| 78 |
except Exception as e:
|
| 79 |
yield f"Connection error: {str(e)}. Please try refreshing."
|
| 80 |
|
| 81 |
+
|
| 82 |
+
# ✅ UI بدون تسجيل دخول
|
| 83 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 84 |
with gr.Sidebar():
|
| 85 |
gr.Markdown("## 🌿 Therapy Assistant")
|
|
|
|
| 86 |
gr.Markdown("---")
|
| 87 |
+
|
| 88 |
sys_msg = gr.Textbox(
|
| 89 |
value=(
|
| 90 |
"You are a compassionate mental health assistant. "
|
|
|
|
| 95 |
label="System Persona",
|
| 96 |
lines=5
|
| 97 |
)
|
| 98 |
+
|
| 99 |
tokens = gr.Slider(128, 1024, value=512, label="Max Response Length")
|
| 100 |
temp = gr.Slider(0.1, 1.5, value=0.7, label="Temperature")
|
| 101 |
top_p_val = gr.Slider(0.1, 1.0, value=0.9, label="Top-p")
|
|
|
|
| 104 |
respond,
|
| 105 |
type="messages",
|
| 106 |
additional_inputs=[sys_msg, tokens, temp, top_p_val],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
cache_examples=False,
|
| 108 |
)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
+
demo.launch()
|