Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,26 @@
|
|
| 1 |
matches = re.findall(code_pattern, text)
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def save_code_to_file(code):
|
| 5 |
"""
|
| 6 |
-
حفظ الكود
|
| 7 |
"""
|
| 8 |
file_path = "codek_output.txt"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def chat_and_process(message, history):
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
system_message = "أنت مساعد برمجي خبير تدعى CODEK. تحدث باللغة العربية بأسلوب تقني واحترافي. قدم حلولاً برمجية كاملة ومنظمة داخل صناديق أكواد Markdown دائماً."
|
| 18 |
|
| 19 |
-
# بناء سياق الرسائل بالتنسيق المطلوب لـ InferenceClient
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
for chat_turn in history:
|
| 22 |
messages.append(chat_turn)
|
|
@@ -24,7 +28,7 @@ def chat_and_process(message, history):
|
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
try:
|
| 27 |
-
# طلب ال
|
| 28 |
for message_obj in client.chat_completion(messages, max_tokens=2048, stream=True):
|
| 29 |
token = message_obj.choices[0].delta.content
|
| 30 |
if token:
|
|
@@ -34,25 +38,25 @@ def chat_and_process(message, history):
|
|
| 34 |
response = f"حدث خطأ في الاتصال: {str(e)}"
|
| 35 |
yield response, "", None
|
| 36 |
|
| 37 |
-
#
|
| 38 |
extracted_code = extract_code(response)
|
| 39 |
file_path = save_code_to_file(extracted_code) if extracted_code else None
|
| 40 |
-
|
| 41 |
yield response, extracted_code, file_path
|
| 42 |
|
| 43 |
-
# تصميم
|
| 44 |
custom_css = """
|
| 45 |
.gradio-container { background-color: #131314 !important; color: #e3e3e3 !important; }
|
| 46 |
.message-user { background-color: #1e1f20 !important; border-radius: 20px !important; }
|
| 47 |
.message-bot { background-color: transparent !important; }
|
| 48 |
footer { display: none !important; }
|
| 49 |
-
#code-preview { border: 1px solid #444 !important; border-radius: 8px; }
|
| 50 |
"""
|
| 51 |
|
| 52 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 53 |
gr.Markdown("<h1 style='text-align: center; color: #8ab4f8;'>CODEK AI Programming Assistant</h1>")
|
| 54 |
|
| 55 |
with gr.Row():
|
|
|
|
| 56 |
with gr.Column(scale=2):
|
| 57 |
chatbot = gr.Chatbot(label="دردشة CODEK", height=500, type="messages")
|
| 58 |
with gr.Row():
|
|
@@ -63,11 +67,11 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 63 |
)
|
| 64 |
submit_btn = gr.Button("إرسال", variant="primary", scale=1)
|
| 65 |
|
|
|
|
| 66 |
with gr.Column(scale=1):
|
| 67 |
gr.Markdown("### 🛠️ معاينة الكود المستخرج")
|
| 68 |
code_view = gr.Code(label="Code Preview", language="python", interactive=False, elem_id="code-preview")
|
| 69 |
file_download = gr.File(label="تحميل ملف الكود (codek_output.txt)")
|
| 70 |
-
refresh_btn = gr.Button("تحديث المعاينة يدوياً")
|
| 71 |
|
| 72 |
def user_input(user_message, history):
|
| 73 |
return "", history + [{"role": "user", "content": user_message}]
|
|
@@ -83,6 +87,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 83 |
history[-1] = {"role": "assistant", "content": chat_res}
|
| 84 |
yield history, code_res, file_res
|
| 85 |
|
|
|
|
| 86 |
msg_input.submit(user_input, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 87 |
bot_response, [chatbot], [chatbot, code_view, file_download]
|
| 88 |
)
|
|
|
|
| 1 |
matches = re.findall(code_pattern, text)
|
| 2 |
+
if matches:
|
| 3 |
+
return "\n\n# --- New Snippet ---\n".join(matches)
|
| 4 |
+
return ""
|
| 5 |
|
| 6 |
def save_code_to_file(code):
|
| 7 |
"""
|
| 8 |
+
حفظ الكود في ملف نصي للتحميل.
|
| 9 |
"""
|
| 10 |
file_path = "codek_output.txt"
|
| 11 |
+
try:
|
| 12 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 13 |
+
f.write(code)
|
| 14 |
+
return file_path
|
| 15 |
+
except Exception:
|
| 16 |
+
return None
|
| 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 |
|
| 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:
|
|
|
|
| 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():
|
|
|
|
| 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}]
|
|
|
|
| 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 |
)
|