Spaces:
Paused
Paused
feat: implement modular neurofit architecture with p5.js visualization and automated training engine
a979b25 | import gradio as gr | |
| import time | |
| import os | |
| import html | |
| # Set DEV_MODE=true or false as environment variable. Default is true. | |
| DEV_MODE = os.getenv("DEV_MODE", "true").lower() == "true" | |
| # Set PORT=<number> as environment variable. Default is 7860. | |
| PORT = int(os.getenv("PORT", "7860")) | |
| # ========================================================================= | |
| # Thứ tự nạp module JS — QUAN TRỌNG: phải đúng thứ tự phụ thuộc | |
| # ========================================================================= | |
| # 1. core : i18n, Theme, State (TRANG_THAI), DOM_REF, Utils | |
| # 2. auto : BangVang, SmartConfig, Monitor, AutoTune, Benchmark | |
| # 3. engine : MangNoron class, Data Generation | |
| # 4. viz : 3 p5.js sketches (BieuDo, Mang, ThongKe) | |
| # 5. ui : Globals, Init, Events, Import/Export, Tabs | |
| # ========================================================================= | |
| JS_MODULES = [ | |
| "neurofit-core.js", | |
| "neurofit-auto.js", | |
| "neurofit-engine.js", | |
| "neurofit-viz.js", | |
| "neurofit-ui.js", | |
| ] | |
| def load_iframe(): | |
| try: | |
| with open("/home/user/app/neurofit_template.html", "r", encoding="utf-8") as f: | |
| template = f.read() | |
| with open("/home/user/app/neurofit.css", "r", encoding="utf-8") as f: | |
| css = f.read() | |
| # Nạp và nối tất cả module JS theo thứ tự phụ thuộc | |
| js_parts = [] | |
| for module_name in JS_MODULES: | |
| module_path = os.path.join("/home/user/app", module_name) | |
| with open(module_path, "r", encoding="utf-8") as f: | |
| js_parts.append(f"// ===== MODULE: {module_name} =====\n{f.read()}") | |
| js = "\n".join(js_parts) | |
| # Build timestamp cho cache busting | |
| build_ts = int(time.time()) | |
| # Inject CSS and JS into the template | |
| full_html = template | |
| if "<style>" not in css: | |
| full_html = full_html.replace("</head>", f" <style>{css}</style>\n</head>") | |
| else: | |
| full_html = full_html.replace("</head>", f" {css}\n</head>") | |
| full_html = full_html.replace("</body>", f" <script>{js}</script>\n</body>") | |
| return f'<iframe srcdoc="{html.escape(full_html)}" style="width:100%; height:95vh; border:none; border-radius: 8px;"></iframe>' | |
| except Exception as e: | |
| return f"Error loading UI: {str(e)}" | |
| with gr.Blocks(title="NeuroFit Pro") as demo: | |
| html_comp = gr.HTML(load_iframe()) | |
| if DEV_MODE: | |
| with gr.Row(): | |
| reload_btn = gr.Button("🔄 Reload UI (Clear Cache)", variant="secondary", size="sm") | |
| reload_btn.click(load_iframe, outputs=html_comp) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=PORT, | |
| share=False, | |
| ssr_mode=False, | |
| css="footer {visibility: hidden}", | |
| allowed_paths=["/home/user/app"] | |
| ) | |