Update app.py
Browse files
app.py
CHANGED
|
@@ -7,20 +7,14 @@ 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(interaction)
|
| 19 |
-
# لو التاريخ نظام قائمة (قديم)
|
| 20 |
-
elif isinstance(interaction, (list, tuple)):
|
| 21 |
-
if interaction[0]: messages.append({"role": "user", "content": interaction[0]})
|
| 22 |
-
if interaction[1]: messages.append({"role": "assistant", "content": interaction[1]})
|
| 23 |
-
|
| 24 |
messages.append({"role": "user", "content": message})
|
| 25 |
|
| 26 |
response = ""
|
|
@@ -33,31 +27,38 @@ def chat_function(message, history):
|
|
| 33 |
except Exception as e:
|
| 34 |
yield f"حدث خطأ: {str(e)}"
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# الشات بوت
|
| 41 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
msg = gr.Textbox(placeholder="اكتب سؤالك هنا...", show_label=False, scale=9)
|
| 45 |
btn = gr.Button("إرسال", variant="primary", scale=1)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
def user_action(user_message, history):
|
| 49 |
-
return "", history + [
|
| 50 |
|
| 51 |
def bot_action(history):
|
| 52 |
-
user_message = history[-1][
|
| 53 |
response_generator = chat_function(user_message, history[:-1])
|
| 54 |
-
history
|
| 55 |
for token in response_generator:
|
| 56 |
-
history[-1][
|
| 57 |
yield history
|
| 58 |
|
| 59 |
msg.submit(user_action, [msg, chatbot], [msg, chatbot], queue=False).then(bot_action, chatbot, chatbot)
|
| 60 |
btn.click(user_action, [msg, chatbot], [msg, chatbot], queue=False).then(bot_action, chatbot, chatbot)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
|
|
|
|
|
|
| 7 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct", token=hf_token)
|
| 8 |
|
| 9 |
def chat_function(message, history):
|
| 10 |
+
# نظام التعليمات بتنسيق Gradio 6 الجديد
|
| 11 |
+
messages = [{"role": "system", "content": "أنت CODEK، مساعد برمجة محترف. تحدث بالعربية واكتب أكواد كاملة."}]
|
| 12 |
+
|
| 13 |
+
# إضافة التاريخ (اللي هو أصلاً بقا بيجي قواميس في النسخة دي)
|
| 14 |
+
for msg in history:
|
| 15 |
+
messages.append(msg)
|
| 16 |
+
|
| 17 |
+
# إضافة رسالة المستخدم
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
response = ""
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
yield f"حدث خطأ: {str(e)}"
|
| 29 |
|
| 30 |
+
# الستايل الدارك (Gemini)
|
| 31 |
+
css = ".gradio-container {background-color: #131314 !important; color: white !important;}"
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.HTML("<h1 style='text-align:center; color:#4285f4; font-family:Arial;'>⚡ CODEK AI</h1>")
|
| 35 |
|
| 36 |
+
# إجبار الشات بوت على استخدام نظام المراسلة (type='messages')
|
| 37 |
+
chatbot = gr.Chatbot(
|
| 38 |
+
type="messages",
|
| 39 |
+
height=550,
|
| 40 |
+
value=[{"role": "assistant", "content": "مرحباً بك! أنا **CODEK**، خبير البرمجة الخاص بك. كيف يمكنني مساعدتك اليوم؟"}]
|
| 41 |
+
)
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
msg = gr.Textbox(placeholder="اكتب سؤالك هنا...", show_label=False, scale=9)
|
| 45 |
btn = gr.Button("إرسال", variant="primary", scale=1)
|
| 46 |
|
| 47 |
+
# معالجة الإرسال بنظام القواميس
|
| 48 |
def user_action(user_message, history):
|
| 49 |
+
return "", history + [{"role": "user", "content": user_message}]
|
| 50 |
|
| 51 |
def bot_action(history):
|
| 52 |
+
user_message = history[-1]["content"]
|
| 53 |
response_generator = chat_function(user_message, history[:-1])
|
| 54 |
+
history.append({"role": "assistant", "content": ""})
|
| 55 |
for token in response_generator:
|
| 56 |
+
history[-1]["content"] = token
|
| 57 |
yield history
|
| 58 |
|
| 59 |
msg.submit(user_action, [msg, chatbot], [msg, chatbot], queue=False).then(bot_action, chatbot, chatbot)
|
| 60 |
btn.click(user_action, [msg, chatbot], [msg, chatbot], queue=False).then(bot_action, chatbot, chatbot)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
+
# نقل الـ CSS للـ launch عشان الـ Warning يختفي
|
| 64 |
+
demo.launch(css=css)
|