Ahmed9089 commited on
Commit
2bb67a9
·
verified ·
1 Parent(s): 416126e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -5,7 +5,7 @@ matches = re.findall(code_pattern, text)
5
 
6
  def save_code_to_file(code):
7
  """
8
- حفظ الكود في ملف نصي للتحميل.
9
  """
10
  file_path = "codek_output.txt"
11
  try:
@@ -17,10 +17,11 @@ def save_code_to_file(code):
17
 
18
  def chat_and_process(message, history):
19
  """
20
- المنطق الرئيسي للدردشة واستخراج الكود.
21
  """
22
  system_message = "أنت مساعد برمجي خبير تدعى CODEK. تحدث باللغة العربية بأسلوب تقني واحترافي. قدم حلولاً برمجية كاملة ومنظمة داخل صناديق أكواد Markdown دائماً."
23
 
 
24
  messages = [{"role": "system", "content": system_message}]
25
  for chat_turn in history:
26
  messages.append(chat_turn)
@@ -28,58 +29,64 @@ def chat_and_process(message, history):
28
 
29
  response = ""
30
  try:
31
- # طلب التوليد بنظام Streaming
32
  for message_obj in client.chat_completion(messages, max_tokens=2048, stream=True):
33
  token = message_obj.choices[0].delta.content
34
  if token:
35
  response += token
36
  yield response, "", None
37
  except Exception as e:
38
- response = f"حدث خطأ في الاتصال: {str(e)}"
39
  yield response, "", None
40
 
41
- # استخراج وحفظ الكود بعد انتهاء الرد
42
  extracted_code = extract_code(response)
43
  file_path = save_code_to_file(extracted_code) if extracted_code else None
44
  yield response, extracted_code, file_path
45
 
46
- # تصميم واجهة مستخدم مشابهة لـ Gemini (Dark Mode)
47
  custom_css = """
48
- .gradio-container { background-color: #131314 !important; color: #e3e3e3 !important; }
49
- .message-user { background-color: #1e1f20 !important; border-radius: 20px !important; }
50
  .message-bot { background-color: transparent !important; }
51
  footer { display: none !important; }
52
  #code-preview { border: 1px solid #444 !important; border-radius: 8px; background-color: #1e1f20 !important; }
 
53
  """
54
 
55
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
56
  gr.Markdown("<h1 style='text-align: center; color: #8ab4f8;'>CODEK AI Programming Assistant</h1>")
57
 
58
  with gr.Row():
59
- # قسم الدردشة
60
  with gr.Column(scale=2):
61
- chatbot = gr.Chatbot(label="دردشة CODEK", height=500, type="messages")
62
  with gr.Row():
63
  msg_input = gr.Textbox(
64
- placeholder="اسأل CODEK عن أي كود برمجى...",
65
  show_label=False,
66
  scale=4
67
  )
68
  submit_btn = gr.Button("إرسال", variant="primary", scale=1)
69
 
70
- # قسم المعاينة والملفات
71
  with gr.Column(scale=1):
72
- gr.Markdown("### 🛠️ معاينة الكود المستخرج")
73
- code_view = gr.Code(label="Code Preview", language="python", interactive=False, elem_id="code-preview")
74
- file_download = gr.File(label="تحميل ملف الكود (codek_output.txt)")
 
75
 
76
  def user_input(user_message, history):
 
77
  return "", history + [{"role": "user", "content": user_message}]
78
 
79
  def bot_response(history):
 
80
  user_message = history[-1]["content"]
 
81
  current_history = history[:-1]
82
 
 
83
  for chat_res, code_res, file_res in chat_and_process(user_message, current_history):
84
  if history[-1]["role"] == "user":
85
  history.append({"role": "assistant", "content": chat_res})
@@ -87,7 +94,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
87
  history[-1] = {"role": "assistant", "content": chat_res}
88
  yield history, code_res, file_res
89
 
90
- # ربط الأحداث ببعضها
91
  msg_input.submit(user_input, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
92
  bot_response, [chatbot], [chatbot, code_view, file_download]
93
  )
@@ -96,4 +103,5 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
96
  )
97
 
98
  if __name__ == "__main__":
 
99
  demo.queue().launch()
 
5
 
6
  def save_code_to_file(code):
7
  """
8
+ حفظ الكود المستخرج في ملف نصي للتحميل.
9
  """
10
  file_path = "codek_output.txt"
11
  try:
 
17
 
18
  def chat_and_process(message, history):
19
  """
20
+ المنطق الرئيسي للدردشة واستخراج الكود ومعالجة الردود.
21
  """
22
  system_message = "أنت مساعد برمجي خبير تدعى CODEK. تحدث باللغة العربية بأسلوب تقني واحترافي. قدم حلولاً برمجية كاملة ومنظمة داخل صناديق أكواد Markdown دائماً."
23
 
24
+ # بناء سياق الرسائل المتوافق مع InferenceClient
25
  messages = [{"role": "system", "content": system_message}]
26
  for chat_turn in history:
27
  messages.append(chat_turn)
 
29
 
30
  response = ""
31
  try:
32
+ # طلب التوليد بنظام البث (Streaming)
33
  for message_obj in client.chat_completion(messages, max_tokens=2048, stream=True):
34
  token = message_obj.choices[0].delta.content
35
  if token:
36
  response += token
37
  yield response, "", None
38
  except Exception as e:
39
+ response = f"حدث خطأ في الاتصال بالنموذج: {str(e)}"
40
  yield response, "", None
41
 
42
+ # استخراج وحفظ الكود بعد اكتمال الرد النهائي
43
  extracted_code = extract_code(response)
44
  file_path = save_code_to_file(extracted_code) if extracted_code else None
45
  yield response, extracted_code, file_path
46
 
47
+ # تصميم واجهة مستخدم احترافية (Dark Mode) بأسلوب Gemini
48
  custom_css = """
49
+ .gradio-container { background-color: #131314 !important; color: #e3e3e3 !important; font-family: 'Segoe UI', sans-serif; }
50
+ .message-user { background-color: #1e1f20 !important; border-radius: 15px !important; border: 1px solid #333 !important; }
51
  .message-bot { background-color: transparent !important; }
52
  footer { display: none !important; }
53
  #code-preview { border: 1px solid #444 !important; border-radius: 8px; background-color: #1e1f20 !important; }
54
+ .gr-button-primary { background-color: #8ab4f8 !important; color: #131314 !important; }
55
  """
56
 
57
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
58
  gr.Markdown("<h1 style='text-align: center; color: #8ab4f8;'>CODEK AI Programming Assistant</h1>")
59
 
60
  with gr.Row():
61
+ # القسم الأيسر: واجهة الدردشة
62
  with gr.Column(scale=2):
63
+ chatbot = gr.Chatbot(label="محادثة CODEK AI", height=550, type="messages")
64
  with gr.Row():
65
  msg_input = gr.Textbox(
66
+ placeholder="اكتب استفسارك البرمجي هنا...",
67
  show_label=False,
68
  scale=4
69
  )
70
  submit_btn = gr.Button("إرسال", variant="primary", scale=1)
71
 
72
+ # القسم الأيمن: أدوات الكود والتحميل
73
  with gr.Column(scale=1):
74
+ gr.Markdown("### 🛠️ مخرجات الكود")
75
+ code_view = gr.Code(label="معاينة الكود المستخرج", language="python", interactive=False, elem_id="code-preview")
76
+ file_download = gr.File(label="تحميل ملف المخرجات")
77
+ gr.Markdown("<p style='font-size: 0.8em; color: #777;'>يتم تحديث المعاينة والملف تلقائياً عند انتهاء رد البوت.</p>")
78
 
79
  def user_input(user_message, history):
80
+ # تصفير حقل الإدخال وإضافة رسالة المستخدم للتاريخ
81
  return "", history + [{"role": "user", "content": user_message}]
82
 
83
  def bot_response(history):
84
+ # الحصول على آخر رسالة م��سلة
85
  user_message = history[-1]["content"]
86
+ # سياق المحادثة قبل الرسالة الحالية
87
  current_history = history[:-1]
88
 
89
+ # استدعاء المحرك البرمجي وتحديث الواجهة تدريجياً
90
  for chat_res, code_res, file_res in chat_and_process(user_message, current_history):
91
  if history[-1]["role"] == "user":
92
  history.append({"role": "assistant", "content": chat_res})
 
94
  history[-1] = {"role": "assistant", "content": chat_res}
95
  yield history, code_res, file_res
96
 
97
+ # ربط أحداث الإرسال (زر الإرسال + مفتاح Enter)
98
  msg_input.submit(user_input, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
99
  bot_response, [chatbot], [chatbot, code_view, file_download]
100
  )
 
103
  )
104
 
105
  if __name__ == "__main__":
106
+ # تفعيل قائمة الانتظار لضمان استقرار البث (Streaming)
107
  demo.queue().launch()