Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
| 3 |
import subprocess
|
| 4 |
from pathlib import Path
|
| 5 |
import numpy as np
|
| 6 |
import gradio as gr
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
BASE_DIR = Path(__file__).resolve().parent
|
| 12 |
if str(BASE_DIR) not in sys.path:
|
|
@@ -53,8 +58,25 @@ def compile_cpp_layers() -> str:
|
|
| 53 |
BUILD_STATUS = compile_cpp_layers()
|
| 54 |
|
| 55 |
|
| 56 |
-
def
|
| 57 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
l1_out = (
|
| 59 |
subprocess.run([str(L1_BIN)], capture_output=True, text=True, timeout=5).stdout.strip()
|
| 60 |
if L1_BIN.exists()
|
|
@@ -105,6 +127,12 @@ def execute_full_stack_diagnostics(buffer_scale: float, pqc_layers: int, radial_
|
|
| 105 |
return l1_out, l2_out, l3_out
|
| 106 |
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
with gr.Blocks(title="Q-SNASB & Q-MCT v4.2 Flight Deck") as demo:
|
| 109 |
gr.Markdown(
|
| 110 |
"## 🛰️ Q-SNASB & Q-MCT v4.2: Quantum Space Navigation & Adaptive Surveillance Bot System\n"
|
|
@@ -114,22 +142,13 @@ with gr.Blocks(title="Q-SNASB & Q-MCT v4.2 Flight Deck") as demo:
|
|
| 114 |
|
| 115 |
with gr.Tabs():
|
| 116 |
with gr.Tab("🛰️ Q-MCT v4.2 Flight Deck (6-Mode Simulator)"):
|
| 117 |
-
gr.HTML(
|
| 118 |
-
'<iframe src="/q_mct_flight_deck.html" '
|
| 119 |
-
'style="width:100%; height:880px; border:1px solid #334155; border-radius:10px; background:#060A17;"></iframe>'
|
| 120 |
-
)
|
| 121 |
|
| 122 |
with gr.Tab("📊 Quantum Metrology Benchmarks (CAI & DSAC)"):
|
| 123 |
-
gr.HTML(
|
| 124 |
-
'<iframe src="/metrology_benchmarks.html" '
|
| 125 |
-
'style="width:100%; height:760px; border:1px solid #334155; border-radius:10px; background:#060A17;"></iframe>'
|
| 126 |
-
)
|
| 127 |
|
| 128 |
with gr.Tab("⚛️ Quantum Algorithmic Benchmarks (QUAV & Mean-Field AOA)"):
|
| 129 |
-
gr.HTML(
|
| 130 |
-
'<iframe src="/algorithmic_benchmarks.html" '
|
| 131 |
-
'style="width:100%; height:720px; border:1px solid #334155; border-radius:10px; background:#060A17;"></iframe>'
|
| 132 |
-
)
|
| 133 |
|
| 134 |
with gr.Tab("⚙️ Layer 1–3 Live Execution Console (C++20 & Python)"):
|
| 135 |
gr.Markdown(
|
|
@@ -149,41 +168,18 @@ with gr.Blocks(title="Q-SNASB & Q-MCT v4.2 Flight Deck") as demo:
|
|
| 149 |
l3_box = gr.Textbox(label="Layer 3: Python Hybrid Quantum-Classical Co-Processor", lines=8)
|
| 150 |
|
| 151 |
run_btn.click(
|
| 152 |
-
fn=
|
| 153 |
inputs=[buf_slider, pqc_slider, rad_slider],
|
| 154 |
outputs=[l1_box, l2_box, l3_box],
|
| 155 |
)
|
| 156 |
demo.load(
|
| 157 |
-
fn=
|
| 158 |
inputs=[buf_slider, pqc_slider, rad_slider],
|
| 159 |
outputs=[l1_box, l2_box, l3_box],
|
| 160 |
)
|
| 161 |
|
| 162 |
-
fastapi_app = FastAPI(title="Q-SNASB Gradio Host")
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
@fastapi_app.get("/q_mct_flight_deck.html")
|
| 166 |
-
async def serve_flight_deck():
|
| 167 |
-
return FileResponse(UI_DIR / "q_mct_flight_deck.html")
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
@fastapi_app.get("/metrology_benchmarks.html")
|
| 171 |
-
async def serve_metrology():
|
| 172 |
-
return FileResponse(UI_DIR / "metrology_benchmarks.html")
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
@fastapi_app.get("/algorithmic_benchmarks.html")
|
| 176 |
-
async def serve_algorithms():
|
| 177 |
-
return FileResponse(UI_DIR / "algorithmic_benchmarks.html")
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
@fastapi_app.get("/api/health")
|
| 181 |
-
async def health():
|
| 182 |
-
return JSONResponse({"status": "ok", "cpp_build": BUILD_STATUS})
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
| 186 |
-
|
| 187 |
if __name__ == "__main__":
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import html
|
| 4 |
import subprocess
|
| 5 |
from pathlib import Path
|
| 6 |
import numpy as np
|
| 7 |
import gradio as gr
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
import spaces
|
| 11 |
+
gpu_task = spaces.GPU
|
| 12 |
+
except Exception:
|
| 13 |
+
def gpu_task(fn):
|
| 14 |
+
return fn
|
| 15 |
|
| 16 |
BASE_DIR = Path(__file__).resolve().parent
|
| 17 |
if str(BASE_DIR) not in sys.path:
|
|
|
|
| 58 |
BUILD_STATUS = compile_cpp_layers()
|
| 59 |
|
| 60 |
|
| 61 |
+
def render_embedded_html(filename: str, height_px: int) -> str:
|
| 62 |
+
"""Embeds a standalone HTML5 simulation module inside a sandboxed srcdoc iframe."""
|
| 63 |
+
html_path = UI_DIR / filename
|
| 64 |
+
if not html_path.exists():
|
| 65 |
+
return (
|
| 66 |
+
f'<div style="padding:16px;color:#f87171;background:#0f172a;'
|
| 67 |
+
f'border-radius:8px;font-family:monospace;">Missing UI file: {html_path}</div>'
|
| 68 |
+
)
|
| 69 |
+
raw_html = html_path.read_text(encoding="utf-8")
|
| 70 |
+
escaped_html = html.escape(raw_html, quote=True)
|
| 71 |
+
return (
|
| 72 |
+
f'<iframe srcdoc="{escaped_html}" '
|
| 73 |
+
f'style="width:100%; height:{height_px}px; border:1px solid #334155; '
|
| 74 |
+
f'border-radius:10px; background:#060A17;"></iframe>'
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def run_stack_computation(buffer_scale: float, pqc_layers: int, radial_km: float):
|
| 79 |
+
"""Executes Layer 1 C++20, Layer 2 C++20, and Layer 3 Python Quantum Co-Processor."""
|
| 80 |
l1_out = (
|
| 81 |
subprocess.run([str(L1_BIN)], capture_output=True, text=True, timeout=5).stdout.strip()
|
| 82 |
if L1_BIN.exists()
|
|
|
|
| 127 |
return l1_out, l2_out, l3_out
|
| 128 |
|
| 129 |
|
| 130 |
+
@gpu_task
|
| 131 |
+
def execute_full_stack_gpu(buffer_scale: float, pqc_layers: int, radial_km: float):
|
| 132 |
+
"""ZeroGPU-registered execution hook for interactive button runs."""
|
| 133 |
+
return run_stack_computation(buffer_scale, pqc_layers, radial_km)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
with gr.Blocks(title="Q-SNASB & Q-MCT v4.2 Flight Deck") as demo:
|
| 137 |
gr.Markdown(
|
| 138 |
"## 🛰️ Q-SNASB & Q-MCT v4.2: Quantum Space Navigation & Adaptive Surveillance Bot System\n"
|
|
|
|
| 142 |
|
| 143 |
with gr.Tabs():
|
| 144 |
with gr.Tab("🛰️ Q-MCT v4.2 Flight Deck (6-Mode Simulator)"):
|
| 145 |
+
gr.HTML(render_embedded_html("q_mct_flight_deck.html", 880))
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
with gr.Tab("📊 Quantum Metrology Benchmarks (CAI & DSAC)"):
|
| 148 |
+
gr.HTML(render_embedded_html("metrology_benchmarks.html", 760))
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
with gr.Tab("⚛️ Quantum Algorithmic Benchmarks (QUAV & Mean-Field AOA)"):
|
| 151 |
+
gr.HTML(render_embedded_html("algorithmic_benchmarks.html", 720))
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
with gr.Tab("⚙️ Layer 1–3 Live Execution Console (C++20 & Python)"):
|
| 154 |
gr.Markdown(
|
|
|
|
| 168 |
l3_box = gr.Textbox(label="Layer 3: Python Hybrid Quantum-Classical Co-Processor", lines=8)
|
| 169 |
|
| 170 |
run_btn.click(
|
| 171 |
+
fn=execute_full_stack_gpu,
|
| 172 |
inputs=[buf_slider, pqc_slider, rad_slider],
|
| 173 |
outputs=[l1_box, l2_box, l3_box],
|
| 174 |
)
|
| 175 |
demo.load(
|
| 176 |
+
fn=run_stack_computation,
|
| 177 |
inputs=[buf_slider, pqc_slider, rad_slider],
|
| 178 |
outputs=[l1_box, l2_box, l3_box],
|
| 179 |
)
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
if __name__ == "__main__":
|
| 182 |
+
demo.launch(
|
| 183 |
+
server_name="0.0.0.0",
|
| 184 |
+
server_port=int(os.environ.get("GRADIO_SERVER_PORT", 7860)),
|
| 185 |
+
)
|