Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
return "\n\n# --- New Block ---\n\n".join(code_blocks)
|
| 3 |
return ""
|
| 4 |
|
| 5 |
def save_code_to_file(code):
|
| 6 |
"""حفظ الكود في ملف مؤقت لتمكين المستخدم من تحميله."""
|
| 7 |
-
if not code.strip():
|
| 8 |
return None
|
| 9 |
file_path = "codek_output.txt"
|
| 10 |
try:
|
|
@@ -21,7 +22,6 @@ def chat_function(message, history):
|
|
| 21 |
"content": "أنت مساعد برمجي خبير تدعى CODEK. تحدث باللغة العربية بأسلوب تقني واحترافي. قدم حلولاً برمجية كاملة ومنظمة داخل صناديق أكواد Markdown دائماً."
|
| 22 |
}
|
| 23 |
|
| 24 |
-
# بناء سياق المحادثة (قائمة قواميس)
|
| 25 |
messages = [system_msg]
|
| 26 |
for msg in history:
|
| 27 |
messages.append(msg)
|
|
@@ -30,7 +30,6 @@ def chat_function(message, history):
|
|
| 30 |
|
| 31 |
response = ""
|
| 32 |
try:
|
| 33 |
-
# توليد الرد بنظام البث (Streaming)
|
| 34 |
for message_chunk in client.chat_completion(messages, max_tokens=4096, stream=True):
|
| 35 |
token = message_chunk.choices[0].delta.content
|
| 36 |
if token:
|
|
@@ -52,6 +51,7 @@ h1 {
|
|
| 52 |
text-align: center;
|
| 53 |
font-weight: bold !important;
|
| 54 |
font-size: 2.5rem !important;
|
|
|
|
| 55 |
}
|
| 56 |
footer { display: none !important; }
|
| 57 |
"""
|
|
@@ -60,12 +60,12 @@ with gr.Blocks(fill_height=True, css=custom_css) as demo:
|
|
| 60 |
gr.HTML("<h1>⚡ CODEK AI</h1>")
|
| 61 |
|
| 62 |
with gr.Row(equal_height=True):
|
| 63 |
-
# العمود الأيسر: الشات
|
| 64 |
with gr.Column(scale=2):
|
| 65 |
chatbot = gr.Chatbot(
|
| 66 |
height=600,
|
| 67 |
show_label=False,
|
| 68 |
bubble_full_width=False,
|
|
|
|
| 69 |
value=[{"role": "assistant", "content": "مرحباً بك! أنا **CODEK**. أي كود سأكتبه لك، سأعرضه تلقائياً في نافذة المعاينة لتتمكن من تحميله."}]
|
| 70 |
)
|
| 71 |
with gr.Row():
|
|
@@ -77,7 +77,6 @@ with gr.Blocks(fill_height=True, css=custom_css) as demo:
|
|
| 77 |
)
|
| 78 |
submit_btn = gr.Button("إرسال", variant="primary", scale=1)
|
| 79 |
|
| 80 |
-
# العمود الأيمن: المعاينة والتحميل
|
| 81 |
with gr.Column(scale=1):
|
| 82 |
gr.Markdown("### 🛠️ معاينة الكود والتحميل")
|
| 83 |
code_view = gr.Code(
|
|
@@ -89,8 +88,40 @@ with gr.Blocks(fill_height=True, css=custom_css) as demo:
|
|
| 89 |
download_file = gr.File(label="ملف الكود الجاهز")
|
| 90 |
refresh_btn = gr.Button("تحديث المعاينة يدوياً", variant="secondary")
|
| 91 |
|
| 92 |
-
# منطق تحديث واجهة المستخدم
|
| 93 |
def user_step(user_message, history):
|
| 94 |
return "", history + [{"role": "user", "content": user_message}]
|
| 95 |
|
| 96 |
-
def bot_step(history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
code_blocks = re.findall(pattern, text)
|
| 2 |
+
if code_blocks:
|
| 3 |
return "\n\n# --- New Block ---\n\n".join(code_blocks)
|
| 4 |
return ""
|
| 5 |
|
| 6 |
def save_code_to_file(code):
|
| 7 |
"""حفظ الكود في ملف مؤقت لتمكين المستخدم من تحميله."""
|
| 8 |
+
if not code or not code.strip():
|
| 9 |
return None
|
| 10 |
file_path = "codek_output.txt"
|
| 11 |
try:
|
|
|
|
| 22 |
"content": "أنت مساعد برمجي خبير تدعى CODEK. تحدث باللغة العربية بأسلوب تقني واحترافي. قدم حلولاً برمجية كاملة ومنظمة داخل صناديق أكواد Markdown دائماً."
|
| 23 |
}
|
| 24 |
|
|
|
|
| 25 |
messages = [system_msg]
|
| 26 |
for msg in history:
|
| 27 |
messages.append(msg)
|
|
|
|
| 30 |
|
| 31 |
response = ""
|
| 32 |
try:
|
|
|
|
| 33 |
for message_chunk in client.chat_completion(messages, max_tokens=4096, stream=True):
|
| 34 |
token = message_chunk.choices[0].delta.content
|
| 35 |
if token:
|
|
|
|
| 51 |
text-align: center;
|
| 52 |
font-weight: bold !important;
|
| 53 |
font-size: 2.5rem !important;
|
| 54 |
+
margin-bottom: 20px !important;
|
| 55 |
}
|
| 56 |
footer { display: none !important; }
|
| 57 |
"""
|
|
|
|
| 60 |
gr.HTML("<h1>⚡ CODEK AI</h1>")
|
| 61 |
|
| 62 |
with gr.Row(equal_height=True):
|
|
|
|
| 63 |
with gr.Column(scale=2):
|
| 64 |
chatbot = gr.Chatbot(
|
| 65 |
height=600,
|
| 66 |
show_label=False,
|
| 67 |
bubble_full_width=False,
|
| 68 |
+
type="messages",
|
| 69 |
value=[{"role": "assistant", "content": "مرحباً بك! أنا **CODEK**. أي كود سأكتبه لك، سأعرضه تلقائياً في نافذة المعاينة لتتمكن من تحميله."}]
|
| 70 |
)
|
| 71 |
with gr.Row():
|
|
|
|
| 77 |
)
|
| 78 |
submit_btn = gr.Button("إرسال", variant="primary", scale=1)
|
| 79 |
|
|
|
|
| 80 |
with gr.Column(scale=1):
|
| 81 |
gr.Markdown("### 🛠️ معاينة الكود والتحميل")
|
| 82 |
code_view = gr.Code(
|
|
|
|
| 88 |
download_file = gr.File(label="ملف الكود الجاهز")
|
| 89 |
refresh_btn = gr.Button("تحديث المعاينة يدوياً", variant="secondary")
|
| 90 |
|
|
|
|
| 91 |
def user_step(user_message, history):
|
| 92 |
return "", history + [{"role": "user", "content": user_message}]
|
| 93 |
|
| 94 |
+
def bot_step(history):
|
| 95 |
+
user_message = history[-1]["content"]
|
| 96 |
+
response_gen = chat_function(user_message, history[:-1])
|
| 97 |
+
history.append({"role": "assistant", "content": ""})
|
| 98 |
+
for token in response_gen:
|
| 99 |
+
history[-1]["content"] = token
|
| 100 |
+
yield history
|
| 101 |
+
|
| 102 |
+
def update_preview_ui(history):
|
| 103 |
+
if not history or len(history) == 0:
|
| 104 |
+
return "", None
|
| 105 |
+
last_bot_msg = history[-1]["content"]
|
| 106 |
+
extracted = extract_code(last_bot_msg)
|
| 107 |
+
if extracted:
|
| 108 |
+
file_path = save_code_to_file(extracted)
|
| 109 |
+
return extracted, file_path
|
| 110 |
+
return "لا يوجد كود للاستخراج حالياً.", None
|
| 111 |
+
|
| 112 |
+
msg.submit(user_step, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 113 |
+
bot_step, chatbot, chatbot
|
| 114 |
+
).then(
|
| 115 |
+
update_preview_ui, chatbot, [code_view, download_file]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
submit_btn.click(user_step, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 119 |
+
bot_step, chatbot, chatbot
|
| 120 |
+
).then(
|
| 121 |
+
update_preview_ui, chatbot, [code_view, download_file]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
refresh_btn.click(update_preview_ui, chatbot, [code_view, download_file])
|
| 125 |
+
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
demo.launch()
|