Spaces:
Running
Running
| import io | |
| import sys | |
| import os | |
| import subprocess | |
| import traceback | |
| import time | |
| import base64 | |
| import re | |
| import gradio as gr | |
| # فحص وتثبيت المكتبات المطلوبة تلقائياً | |
| for lib in ["matplotlib", "numpy", "plotly"]: | |
| try: | |
| __import__(lib) | |
| except ImportError: | |
| try: subprocess.check_call([sys.executable, "-m", "pip", "install", lib], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
| except: pass | |
| # الذاكرة الحية المشتركة للتحديث في الوقت الحقيقي | |
| live_terminal_inputs = [] | |
| def update_inputs_live(user_text): | |
| global live_terminal_inputs | |
| # تحديث مصفوفة المدخلات فوراً في الوقت الحقيقي أثناء تشغيل الأداة | |
| live_terminal_inputs = user_text.splitlines() if user_text else [] | |
| return f"⚡ [تحديث حي]: تم تطبيق التغييرات في الوقت الحقيقي! الحجم الحالي: ({len(live_terminal_inputs)}) سطر." | |
| def pydroid_live_engine(code): | |
| global live_terminal_inputs | |
| if not code or not code.strip(): | |
| return "⚠️ [Pydroid Core]: لا يوجد كود داخل المحرر لتشغيله.", "الرجاء كتابة كود بايثون...", None, None | |
| # تنظيف مخلفات الرسوميات السابقة | |
| for f in ["live_frame.png", "live_simulation.html"]: | |
| if os.path.exists(f): | |
| try: os.remove(f) | |
| except: pass | |
| # حقن آلية قراءة ذكية تقرأ دائماً من الذاكرة الحية المحدثة في الوقت الحقيقي | |
| modified_code = f""" | |
| import builtins | |
| import time | |
| def _pydroid_live_input(prompt=""): | |
| if prompt: print(prompt, end="") | |
| # محرك فحص ذكي ينتظر أو يقرأ التحديثات المباشرة من الزر الحي | |
| # إذا كانت القائمة تحتوي على بيانات، يستهلك السطر الأول فوراً | |
| global live_terminal_inputs | |
| from __main__ import live_terminal_inputs | |
| if live_terminal_inputs: | |
| val = live_terminal_inputs.pop(0) # سحب المدخل فوراً وتمريره للأداة | |
| print(val) # طباعته في الكونسول | |
| return val | |
| return "" # إرجاع نص فارغ لمنع الانهيار في حال عدم وجود مدخلات جديدة | |
| builtins.input = _pydroid_live_input | |
| """ + code | |
| # توجيه القنوات بشكل آمن مفرط لحماية السيرفر من الـ Crash Loop | |
| old_stdout, old_stderr = sys.stdout, sys.stderr | |
| redirected_output = sys.stdout = io.StringIO() | |
| redirected_error = sys.stderr = io.StringIO() | |
| exec_env = {} | |
| error_occurred = False | |
| start_time = time.time() | |
| try: | |
| # تنفيذ السكربت البرمجي مباشرة | |
| exec(modified_code, exec_env) | |
| except Exception: | |
| error_occurred = True | |
| print(traceback.format_exc(), file=redirected_error) | |
| runtime_duration = round(time.time() - start_time, 2) | |
| stdout_text = redirected_output.getvalue() | |
| stderr_text = redirected_error.getvalue() | |
| # إعادة قنوات النظام الأصلية فوراً لحماية بيئة السيرفر | |
| sys.stdout, sys.stderr = old_stdout, old_stderr | |
| # معالجة واجهات العرض التفاعلية 3D والثابتة | |
| interactive_visual_html = None | |
| if os.path.exists("live_simulation.html"): | |
| try: | |
| with open("live_simulation.html", "r", encoding="utf-8") as f: | |
| html_content = f.read() | |
| b64_encoded = base64.b64encode(html_content.encode('utf-8')).decode('utf-8') | |
| interactive_visual_html = f'<iframe src="data:text/html;base64,{b64_encoded}" style="width:100%; height:520px; border:none; border-radius:10px; background:#050507;"></iframe>' | |
| except Exception as e: | |
| stderr_text += f"\n[Error 3D Render]: {e}" | |
| static_frame_img = "live_frame.png" if os.path.exists("live_frame.png") else None | |
| if error_occurred or stderr_text: | |
| status_msg = f"❌ انتهى التشغيل بوجود خطأ | المستغرق: {runtime_duration} ثانية" | |
| console_final = f"{stdout_text}\n⚠️ --- تفاصيل خطأ السكربت ---\n{stderr_text}" | |
| return status_msg, console_final, static_frame_img, interactive_visual_html | |
| else: | |
| status_msg = f"✅ اكتمل التنفيذ بنجاح كلي! | الزمن: {runtime_duration} ثانية" | |
| console_final = stdout_text if stdout_text else "ℹ️ تم تنفيذ السكربت بنجاح." | |
| return status_msg, console_final, static_frame_img, interactive_visual_html | |
| # 🎨 هندسة مظهر الواجهة الاحترافي المتوافق كلياً مع شروط Gradio 6 | |
| ui_theme_css = """ | |
| body, .gradio-container { background-color: #0b0c10 !important; font-family: 'Consolas', monospace; } | |
| .pydroid-header { background: linear-gradient(135deg, #1f2833, #12141c); border: 1px solid #45f3ff; border-radius: 12px; padding: 15px; text-align: center; margin-bottom: 15px; } | |
| .pydroid-header h1 { color: #45f3ff; margin: 0; font-size: 24px; text-shadow: 0 0 10px rgba(69, 243, 255, 0.3); } | |
| .section-lbl { color: #66fcf1; font-weight: bold; font-size: 14px; margin-bottom: 5px; } | |
| .live-btn { background-color: #ff007f !important; color: white !important; font-weight: bold !important; border: none !important; } | |
| .live-btn:hover { background-color: #e60072 !important; box-shadow: 0 0 10px rgba(255, 0, 127, 0.5) !important; } | |
| """ | |
| with gr.Blocks() as demo: | |
| gr.HTML("<div class='pydroid-header'><h1>🖥️ PYDROID LIVE CORE V9.0</h1><p>تعديل وحقن البيانات في الوقت الحقيقي للأدوات الحرة</p></div>") | |
| with gr.Row(): | |
| system_status = gr.Textbox(label="مراقب حالة معالجة السيرفر المباشر", value="🟢 النظام مستقر تماماً وجاهز...", interactive=False) | |
| with gr.Row(): | |
| # عمود محرر الأكواد (يسار) | |
| with gr.Column(scale=3): | |
| gr.HTML("<div class='section-lbl'>📝 محرر أكواد بايثون الحُر</div>") | |
| code_editor = gr.Code( | |
| language="python", | |
| lines=14, | |
| label=None, | |
| value="# اكتب كود الأداة هنا\nimport time\nprint('بدء فحص الأداة الحية...')\nuser_data = input('أدخل البيانات هنا: ')\nprint('تم استقبال التغيير الحي بنجاح:', user_data)" | |
| ) | |
| execute_btn = gr.Button("⚡ إطلاق وتشغيل السكربت الرئيسي", variant="primary", size="lg") | |
| # عمود التحكم والمدخلات الفورية (يمين) | |
| with gr.Column(scale=2): | |
| gr.HTML("<div class='section-lbl'>📥 صندوق التغييرات والمدخلات المباشرة</div>") | |
| terminal_box = gr.Textbox( | |
| lines=6, | |
| label=None, | |
| placeholder="اكتب التغييرات، الأرقام أو اليوزرات هنا بحرية تامة..." | |
| ) | |
| # زر إرسال التغييرات في الوقت الحقيقي بجانب المربع مباشرة | |
| live_update_btn = gr.Button("⚡ إرسال التغييرات في الوقت الحقيقي", elem_classes=["live-btn"], size="lg") | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.HTML("<div class='section-lbl' style='margin-top:15px;'>📺 شاشات عرض النتائج وبث المحاكاة والتفاعل</div>") | |
| with gr.Tabs(): | |
| with gr.Tab("📋 مخرجات الطرفية النصية (Console Out)"): | |
| console_view = gr.Textbox(lines=11, label=None, interactive=False) | |
| with gr.Tab("🌐 لوحة التحكم التفاعلية 3D"): | |
| visual_3d_html = gr.HTML() | |
| with gr.Tab("🖼️ شاشة البث الصوري الثابت"): | |
| visual_static_img = gr.Image(label=None, interactive=False) | |
| # ربط زر التحديث الحي بالذاكرة المشتركة فوراً بمجرد كبسه | |
| live_update_btn.click( | |
| fn=update_inputs_live, | |
| inputs=[terminal_box], | |
| outputs=[system_status] | |
| ) | |
| # ربط كبسة التشغيل الفوري بالمعالج المستقر | |
| execute_btn.click( | |
| fn=pydroid_live_engine, | |
| inputs=[code_editor], | |
| outputs=[system_status, console_view, visual_static_img, visual_3d_html] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(css=ui_theme_css, theme=gr.themes.Default()) | |