Ahmed9089 commited on
Commit
3b21dfe
·
verified ·
1 Parent(s): 2e67a23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -44
app.py CHANGED
@@ -4,55 +4,38 @@ from huggingface_hub import InferenceClient
4
  # استدعاء الموديل
5
  client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
6
 
7
- def respond(message, history):
8
- # نظام التعليمات (البرومبت المخفي)
9
- system_message = "أنت CODEK، أقوى أيجنت ذكاء اصطناعي. تحدث بالعربية بأسلوب أسطوري واكتب أكواد احترافية."
10
 
11
- # تجهيز الرسائل
12
  messages = [{"role": "system", "content": system_message}]
13
-
14
- # معالجة التاريخ ليتناسب مع الإصدار الجديد
15
- for user_msg, ai_msg in history:
16
- if user_msg: messages.append({"role": "user", "content": user_msg})
17
- if ai_msg: messages.append({"role": "assistant", "content": ai_msg})
18
 
19
  messages.append({"role": "user", "content": message})
20
 
21
  response = ""
22
- try:
23
- for message_chunk in client.chat_completion(messages, max_tokens=4000, stream=True):
24
- token = message_chunk.choices[0].delta.content
25
- if token:
26
- response += token
27
- yield response
28
- except Exception as e:
29
- yield f"عذراً يا بطل، حصل مشكلة: {str(e)}"
30
-
31
- # تخصيص الستايل (CSS) عشان يكون "خرافي" بجد
32
- custom_css = """
33
- footer {visibility: hidden}
34
- .gradio-container { background-color: #020617 !important; }
35
- h1 { color: #3b82f6 !important; text-shadow: 0 0 15px #3b82f6; font-family: 'Arial', sans-serif; text-align: center; padding: 20px; }
36
- .message.user { background-color: #1e293b !important; border: 1px solid #3b82f6 !important; }
37
- .message.assistant { background-color: #0f172a !important; border: 1px solid #9333ea !important; }
38
- """
39
 
40
  # بناء الواجهة
41
- with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="purple")) as demo:
42
- gr.Markdown("# ⚡ CODEK ULTIMATE AGENT")
43
-
44
- # واجهة الشات بستايل أبسط لمنع الـ Errors
45
- chatbot = gr.Chatbot(bubble_full_width=False)
46
- msg = gr.Textbox(placeholder="أمرك يا صاحب الامبراطورية؟", show_label=False)
47
- clear = gr.ClearButton([msg, chatbot], value="مسح المحادثة")
48
-
49
- def chat_action(message, chat_history):
50
- bot_generator = respond(message, chat_history)
51
- chat_history.append((message, ""))
52
- for response in bot_generator:
53
- chat_history[-1] = (message, response)
54
- yield "", chat_history
55
-
56
- msg.submit(chat_action, [msg, chatbot], [msg, chatbot])
57
-
58
- demo.launch()
 
4
  # استدعاء الموديل
5
  client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
6
 
7
+ def respond(message, history: list[dict]):
8
+ # نظام التعليمات
9
+ system_message = "أنت CODEK، أقوى أيجنت ذكاء اصطناعي. تحدث بالعربية واكتب أكواد احترافية."
10
 
 
11
  messages = [{"role": "system", "content": system_message}]
12
+
13
+ # إضافة التاريخ (History) بطريقة متوافقة مع التحديث الجديد
14
+ for msg in history:
15
+ messages.append(msg)
 
16
 
17
  messages.append({"role": "user", "content": message})
18
 
19
  response = ""
20
+ # جلب الرد
21
+ for message_chunk in client.chat_completion(
22
+ messages,
23
+ max_tokens=2048,
24
+ stream=True,
25
+ ):
26
+ token = message_chunk.choices[0].delta.content
27
+ if token:
28
+ response += token
29
+ yield response
 
 
 
 
 
 
 
30
 
31
  # بناء الواجهة
32
+ demo = gr.ChatInterface(
33
+ fn=respond,
34
+ type="messages", # ده المهم جداً للتحديث الجديد
35
+ title="⚡ CODEK ULTIMATE AGENT",
36
+ description="اسأل كودك عن أي كود برمجى أو مشروع ضخم",
37
+ theme="ocean" # ثيم هادي وشيك لحد ما نضمن التشغيل
38
+ )
39
+
40
+ if __name__ == "__main__":
41
+ demo.launch()