Update app.py
Browse files
app.py
CHANGED
|
@@ -2,63 +2,106 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
hf_token = os.environ.get("HF_TOKEN")
|
| 7 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct", token=hf_token)
|
| 8 |
|
| 9 |
def chat_function(message, history):
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# إضافة رسالة المستخدم
|
| 18 |
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
response = ""
|
| 21 |
try:
|
| 22 |
-
|
|
|
|
| 23 |
token = message_chunk.choices[0].delta.content
|
| 24 |
if token:
|
| 25 |
response += token
|
| 26 |
yield response
|
| 27 |
except Exception as e:
|
| 28 |
-
yield f"حدث خطأ: {str(e)}"
|
| 29 |
|
| 30 |
-
# الستا
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
with gr.Blocks() as demo:
|
| 34 |
-
gr.HTML("<h1
|
| 35 |
|
| 36 |
-
#
|
| 37 |
chatbot = gr.Chatbot(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
-
msg = gr.Textbox(
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
def
|
| 49 |
-
return "", history + [
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
yield history
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# إعداد العميل باستخدام التوكن السري
|
| 6 |
hf_token = os.environ.get("HF_TOKEN")
|
| 7 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct", token=hf_token)
|
| 8 |
|
| 9 |
def chat_function(message, history):
|
| 10 |
+
"""
|
| 11 |
+
دالة المعالجة المركزية التي ترسل الرسائل إلى الموديل وترجع الرد.
|
| 12 |
+
تستخدم نظام القوائم (Lists) المتوافق مع النسخ القديمة.
|
| 13 |
+
"""
|
| 14 |
+
# رسالة النظام لتحديد شخصية CODEK
|
| 15 |
+
system_msg = {"role": "system", "content": "أنت مساعد برمجي خبير تدعى CODEK. تحدث باللغة العربية بأسلوب تقني واحترافي. قدم حلولاً برمجية كاملة ومنظمة."}
|
| 16 |
|
| 17 |
+
# بناء قائمة الرسائل من التاريخ (تحويل من Tuples إلى Dicts للموديل)
|
| 18 |
+
messages = [system_msg]
|
| 19 |
+
for user_part, assistant_part in history:
|
| 20 |
+
if user_part:
|
| 21 |
+
messages.append({"role": "user", "content": user_part})
|
| 22 |
+
if assistant_part:
|
| 23 |
+
messages.append({"role": "assistant", "content": assistant_part})
|
| 24 |
|
| 25 |
+
# إضافة رسالة المستخدم الحالية
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
try:
|
| 30 |
+
# طلب الرد بنظام البث (Streaming)
|
| 31 |
+
for message_chunk in client.chat_completion(messages, max_tokens=3000, stream=True):
|
| 32 |
token = message_chunk.choices[0].delta.content
|
| 33 |
if token:
|
| 34 |
response += token
|
| 35 |
yield response
|
| 36 |
except Exception as e:
|
| 37 |
+
yield f"⚠️ حدث خطأ في الاتصال بالموديل: {str(e)}"
|
| 38 |
|
| 39 |
+
# تصميم الواجهة باستخدام CSS مخصص لمحاكاة Dark Mode الخاص بـ Gemini
|
| 40 |
+
custom_css = """
|
| 41 |
+
body, .gradio-container {
|
| 42 |
+
background-color: #131314 !important;
|
| 43 |
+
color: #e3e3e3 !important;
|
| 44 |
+
}
|
| 45 |
+
.chatbot-container .message {
|
| 46 |
+
border-radius: 12px !important;
|
| 47 |
+
}
|
| 48 |
+
h1 {
|
| 49 |
+
background: linear-gradient(90deg, #4285f4, #9b72cb, #d96570);
|
| 50 |
+
-webkit-background-clip: text;
|
| 51 |
+
-webkit-text-fill-color: transparent;
|
| 52 |
+
text-align: center;
|
| 53 |
+
font-weight: bold !important;
|
| 54 |
+
}
|
| 55 |
+
footer {
|
| 56 |
+
display: none !important;
|
| 57 |
+
}
|
| 58 |
+
"""
|
| 59 |
|
| 60 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 61 |
+
gr.HTML("<h1>⚡ CODEK AI</h1>")
|
| 62 |
|
| 63 |
+
# مكون الشات بوت بالنظام الكلاسيكي (Tuples) لضمان التوافق
|
| 64 |
chatbot = gr.Chatbot(
|
| 65 |
+
height=600,
|
| 66 |
+
show_label=False,
|
| 67 |
+
# رسالة الترحيب كأول عنصر في القائمة
|
| 68 |
+
value=[[None, "مرحباً بك! أنا **CODEK**، رفيقك في رحلة البرمجة. كيف يمكنني مساعدتك اليوم؟"]]
|
| 69 |
)
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
+
msg = gr.Textbox(
|
| 73 |
+
placeholder="اسأل CODEK عن أي شيء برمجياً...",
|
| 74 |
+
show_label=False,
|
| 75 |
+
scale=8,
|
| 76 |
+
container=False
|
| 77 |
+
)
|
| 78 |
+
submit_btn = gr.Button("إرسال", variant="primary", scale=1)
|
| 79 |
|
| 80 |
+
# دالة تحديث واجهة المستخدم (إضافة رسالة المستخدم)
|
| 81 |
+
def user_step(user_message, history):
|
| 82 |
+
return "", history + [[user_message, None]]
|
| 83 |
|
| 84 |
+
# دالة توليد رد البوت (تحديث التاريخ بالرد الجديد)
|
| 85 |
+
def bot_step(history):
|
| 86 |
+
user_message = history[-1][0]
|
| 87 |
+
# استدعاء دالة الشات
|
| 88 |
+
response_gen = chat_function(user_message, history[:-1])
|
| 89 |
+
history[-1][1] = ""
|
| 90 |
+
for token in response_gen:
|
| 91 |
+
history[-1][1] = token
|
| 92 |
yield history
|
| 93 |
|
| 94 |
+
# ربط الأحداث
|
| 95 |
+
msg.submit(user_step, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 96 |
+
bot_step, chatbot, chatbot
|
| 97 |
+
)
|
| 98 |
+
submit_btn.click(user_step, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 99 |
+
bot_step, chatbot, chatbot
|
| 100 |
+
)
|
| 101 |
|
| 102 |
+
# التشغيل النهائي
|
| 103 |
if __name__ == "__main__":
|
| 104 |
+
demo.launch(
|
| 105 |
+
css=custom_css,
|
| 106 |
+
theme=gr.themes.Default(primary_hue="blue", neutral_hue="slate")
|
| 107 |
+
)
|