Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,100 @@
|
|
| 1 |
-
|
| 2 |
-
#
|
| 3 |
-
from flask import Flask, jsonify, request, render_template_string
|
| 4 |
-
except ImportError:
|
| 5 |
-
# رسالة خطأ في حال عدم وجود المكتبة
|
| 6 |
-
print("Error: Flask is not installed. Run 'pip install flask'")
|
| 7 |
-
exit(1)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
app = Flask(__name__)
|
| 11 |
-
|
| 12 |
-
# إعدادات دعم اللغة العربية في مخرجات JSON
|
| 13 |
-
app.config['JSON_AS_ASCII'] = False
|
| 14 |
-
|
| 15 |
-
# قالب HTML بسيط مدمج لعرض الواجهة الرسومية
|
| 16 |
-
HTML_TEMPLATE = """
|
| 17 |
-
<!DOCTYPE html>
|
| 18 |
-
<html lang="ar" dir="rtl">
|
| 19 |
-
<head>
|
| 20 |
-
<meta charset="UTF-8">
|
| 21 |
-
<title>CODEK AI Dashboard</title>
|
| 22 |
-
<style>
|
| 23 |
-
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f7f6; margin: 0; display: flex; justify-content: center; align-items: center; height: 100vh; }
|
| 24 |
-
.card { background: white; padding: 2rem; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; }
|
| 25 |
-
h1 { color: #2c3e50; }
|
| 26 |
-
.status { color: #27ae60; font-weight: bold; }
|
| 27 |
-
</style>
|
| 28 |
-
</head>
|
| 29 |
-
<body>
|
| 30 |
-
<div class="card">
|
| 31 |
-
<h1>مرحباً بك في نظام CODEK AI</h1>
|
| 32 |
-
<p>حالة النظام الآن: <span class="status">يعمل بكفاءة</span></p>
|
| 33 |
-
<p>الإصدار الحالي: 2.0.0</p>
|
| 34 |
-
</div>
|
| 35 |
-
</body>
|
| 36 |
-
</html>
|
| 37 |
-
"""
|
| 38 |
-
|
| 39 |
-
@app.route('/')
|
| 40 |
-
def home():
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
"""
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
def process_data():
|
| 48 |
"""
|
| 49 |
-
|
| 50 |
"""
|
| 51 |
-
|
| 52 |
-
if not data:
|
| 53 |
-
return jsonify({"error": "لم يتم إرسال بيانات"}), 400
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
"
|
| 59 |
-
"
|
| 60 |
-
}
|
| 61 |
-
return jsonify(response)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
if __name__ ==
|
| 82 |
-
|
| 83 |
-
app.run(debug=True, port=5000)
|
|
|
|
| 1 |
+
matches = re.findall(code_pattern, text)
|
| 2 |
+
return "\n\n# --- New Snippet ---\n".join(matches) if matches else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def save_code_to_file(code):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
+
حفظ الكود المستخرج في ملف نصي للتحميل.
|
| 7 |
"""
|
| 8 |
+
file_path = "codek_output.txt"
|
| 9 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 10 |
+
f.write(code)
|
| 11 |
+
return file_path
|
| 12 |
|
| 13 |
+
def chat_and_process(message, history):
|
|
|
|
| 14 |
"""
|
| 15 |
+
دالة المعالجة الرئيسية: ترسل الرسالة للنموذج، تستخرج الكود، وتحدث الواجهة.
|
| 16 |
"""
|
| 17 |
+
system_message = "أنت مساعد برمجي خبير تدعى CODEK. تحدث باللغة العربية بأسلوب تقني واحترافي. قدم حلولاً برمجية كاملة ومنظمة داخل صناديق أكواد Markdown دائماً."
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# بناء سياق الرسائل
|
| 20 |
+
messages = [{"role": "system", "content": system_message}]
|
| 21 |
+
for user_msg, bot_msg in history:
|
| 22 |
+
messages.append({"role": "user", "content": user_msg})
|
| 23 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 24 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
| 25 |
|
| 26 |
+
response = ""
|
| 27 |
+
try:
|
| 28 |
+
# طلب الرد من النموذج
|
| 29 |
+
for message_obj in client.chat_completion(messages, max_tokens=2048, stream=True):
|
| 30 |
+
token = message_obj.choices[0].delta.content
|
| 31 |
+
response += token
|
| 32 |
+
yield response, "", None # تحديث نص الدردشة تدريجياً
|
| 33 |
+
except Exception as e:
|
| 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 |
+
# تصميم CSS مخصص ليشبه واجهة Gemini (Dark Mode)
|
| 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 |
+
# العمود الأيسر: الدردشة
|
| 57 |
+
with gr.Column(scale=2):
|
| 58 |
+
chatbot = gr.Chatbot(label="دردشة CODEK", height=500, type="messages")
|
| 59 |
+
with gr.Row():
|
| 60 |
+
msg_input = gr.Textbox(
|
| 61 |
+
placeholder="اسأل CODEK عن أي كود برمجى...",
|
| 62 |
+
show_label=False,
|
| 63 |
+
scale=4
|
| 64 |
+
)
|
| 65 |
+
submit_btn = gr.Button("إرسال", variant="primary", scale=1)
|
| 66 |
+
|
| 67 |
+
# العمود الأيمن: المعاينة والتحميل
|
| 68 |
+
with gr.Column(scale=1):
|
| 69 |
+
gr.Markdown("### 🛠️ معاينة الكود المستخرج")
|
| 70 |
+
code_view = gr.Code(label="Code Preview", language="python", interactive=False, elem_id="code-preview")
|
| 71 |
+
file_download = gr.File(label="تحميل ملف الكود (codek_output.txt)")
|
| 72 |
+
refresh_btn = gr.Button("تحديث المعاينة يدوياً")
|
| 73 |
+
|
| 74 |
+
# منطق العمل عند الإرسال
|
| 75 |
+
def user_input(user_message, history):
|
| 76 |
+
return "", history + [{"role": "user", "content": user_message}]
|
| 77 |
+
|
| 78 |
+
def bot_response(history):
|
| 79 |
+
user_message = history[-1]["content"]
|
| 80 |
+
# تحويل التاريخ لصيغة تناسب الدالة (list of tuples)
|
| 81 |
+
formatted_history = [(history[i]["content"], history[i+1]["content"]) for i in range(0, len(history)-1, 2)]
|
| 82 |
+
|
| 83 |
+
# استدعاء دالة المعالجة
|
| 84 |
+
for chat_res, code_res, file_res in chat_and_process(user_message, formatted_history):
|
| 85 |
+
history[-1] = {"role": "user", "content": user_message}
|
| 86 |
+
if len(history) % 2 == 1:
|
| 87 |
+
history.append({"role": "assistant", "content": chat_res})
|
| 88 |
+
else:
|
| 89 |
+
history[-1] = {"role": "assistant", "content": chat_res}
|
| 90 |
+
yield history, code_res, file_res
|
| 91 |
+
|
| 92 |
+
msg_input.submit(user_input, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 93 |
+
bot_response, [chatbot], [chatbot, code_view, file_download]
|
| 94 |
+
)
|
| 95 |
+
submit_btn.click(user_input, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 96 |
+
bot_response, [chatbot], [chatbot, code_view, file_download]
|
| 97 |
+
)
|
| 98 |
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
demo.queue().launch()
|
|
|