Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.responses import FileResponse, JSONResponse
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
from quantum_co_processor.q_navigation_engine import (
|
| 8 |
ObstaclePolygon,
|
|
@@ -10,66 +18,175 @@ from quantum_co_processor.q_navigation_engine import (
|
|
| 10 |
PQCQuantumRLAttitudeController,
|
| 11 |
RelativisticSpacetimeQFIEstimator,
|
| 12 |
)
|
| 13 |
-
import numpy as np
|
| 14 |
-
|
| 15 |
-
app = FastAPI(title="Q-SNASB & Q-MCT v4.2 Flight Deck", version="4.2.0")
|
| 16 |
|
| 17 |
-
BASE_DIR = Path(__file__).resolve().parent
|
| 18 |
UI_DIR = BASE_DIR / "ui"
|
| 19 |
-
BUILD_DIR =
|
|
|
|
| 20 |
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
text=True,
|
| 34 |
-
timeout=5,
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
text=True,
|
| 42 |
-
timeout=5,
|
| 43 |
)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
obstacles = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
planner = QUAVTrajectoryPlanner(start_xy=(0.0, 0.0), end_xy=(1000.0, 400.0), obstacles=obstacles)
|
| 48 |
candidates = [
|
| 49 |
[(0.0, 0.0), (500.0, 210.0), (1000.0, 400.0)],
|
| 50 |
[(0.0, 0.0), (480.0, 460.0), (1000.0, 400.0)],
|
|
|
|
| 51 |
]
|
| 52 |
quav_res = planner.evaluate_candidate_paths(candidates)
|
| 53 |
|
| 54 |
-
qrl = PQCQuantumRLAttitudeController(n_qubits=4, n_layers=
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
qfim_res = RelativisticSpacetimeQFIEstimator.compute_qfim_and_qcrb(
|
| 57 |
-
r_meters=
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
},
|
| 70 |
-
}
|
| 71 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
| 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 |
+
import uvicorn
|
| 8 |
from fastapi import FastAPI
|
| 9 |
from fastapi.responses import FileResponse, JSONResponse
|
| 10 |
+
|
| 11 |
+
BASE_DIR = Path(__file__).resolve().parent
|
| 12 |
+
if str(BASE_DIR) not in sys.path:
|
| 13 |
+
sys.path.insert(0, str(BASE_DIR))
|
| 14 |
|
| 15 |
from quantum_co_processor.q_navigation_engine import (
|
| 16 |
ObstaclePolygon,
|
|
|
|
| 18 |
PQCQuantumRLAttitudeController,
|
| 19 |
RelativisticSpacetimeQFIEstimator,
|
| 20 |
)
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
| 22 |
UI_DIR = BASE_DIR / "ui"
|
| 23 |
+
BUILD_DIR = Path("/tmp/qsnasb_build")
|
| 24 |
+
BUILD_DIR.mkdir(parents=True, exist_ok=True)
|
| 25 |
|
| 26 |
+
L1_BIN = BUILD_DIR / "quantum_core_driver"
|
| 27 |
+
L2_BIN = BUILD_DIR / "swarm_wci_dtn_node"
|
| 28 |
|
| 29 |
+
|
| 30 |
+
def compile_cpp_layers() -> str:
|
| 31 |
+
"""Compiles C++20 Layer 1 Firmware and Layer 2 Swarm Middleware on startup."""
|
| 32 |
+
logs = []
|
| 33 |
+
targets = [
|
| 34 |
+
(BASE_DIR / "firmware" / "quantum_core_driver.cpp", L1_BIN, "Layer 1 Firmware"),
|
| 35 |
+
(BASE_DIR / "middleware" / "swarm_wci_dtn_node.cpp", L2_BIN, "Layer 2 Middleware"),
|
| 36 |
+
]
|
| 37 |
+
for src, out_bin, label in targets:
|
| 38 |
+
if not src.exists():
|
| 39 |
+
logs.append(f"[WARN] {label} source not found at {src}")
|
| 40 |
+
continue
|
| 41 |
+
res = subprocess.run(
|
| 42 |
+
["g++", "-std=c++20", "-O3", str(src), "-o", str(out_bin)],
|
| 43 |
+
capture_output=True,
|
| 44 |
+
text=True,
|
| 45 |
+
)
|
| 46 |
+
if res.returncode == 0:
|
| 47 |
+
logs.append(f"[OK] Compiled {label} -> {out_bin}")
|
| 48 |
+
else:
|
| 49 |
+
logs.append(f"[ERR] {label} build failed:\n{res.stderr}")
|
| 50 |
+
return "\n".join(logs)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
BUILD_STATUS = compile_cpp_layers()
|
| 54 |
|
| 55 |
|
| 56 |
+
def execute_full_stack_diagnostics(buffer_scale: float, pqc_layers: int, radial_km: float):
|
| 57 |
+
"""Runs Layer 1 C++20, Layer 2 C++20, and Layer 3 Python Quantum Co-Processor."""
|
| 58 |
+
l1_out = (
|
| 59 |
+
subprocess.run([str(L1_BIN)], capture_output=True, text=True, timeout=5).stdout.strip()
|
| 60 |
+
if L1_BIN.exists()
|
| 61 |
+
else f"Layer 1 binary unavailable.\n{BUILD_STATUS}"
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
+
l2_out = (
|
| 65 |
+
subprocess.run([str(L2_BIN)], capture_output=True, text=True, timeout=5).stdout.strip()
|
| 66 |
+
if L2_BIN.exists()
|
| 67 |
+
else f"Layer 2 binary unavailable.\n{BUILD_STATUS}"
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Layer 3: QUAV-QAOA Trajectory Planner + PQC-QRL + Relativistic Schwarzschild QFIM
|
| 71 |
+
obstacles = [
|
| 72 |
+
ObstaclePolygon(
|
| 73 |
+
center_xy=(500.0, 200.0),
|
| 74 |
+
radius_m=120.0,
|
| 75 |
+
buffer_scale_sx_sy=(buffer_scale, buffer_scale),
|
| 76 |
+
)
|
| 77 |
+
]
|
| 78 |
planner = QUAVTrajectoryPlanner(start_xy=(0.0, 0.0), end_xy=(1000.0, 400.0), obstacles=obstacles)
|
| 79 |
candidates = [
|
| 80 |
[(0.0, 0.0), (500.0, 210.0), (1000.0, 400.0)],
|
| 81 |
[(0.0, 0.0), (480.0, 460.0), (1000.0, 400.0)],
|
| 82 |
+
[(0.0, 0.0), (200.0, -350.0), (750.0, 680.0), (1000.0, 400.0)],
|
| 83 |
]
|
| 84 |
quav_res = planner.evaluate_candidate_paths(candidates)
|
| 85 |
|
| 86 |
+
qrl = PQCQuantumRLAttitudeController(n_qubits=4, n_layers=int(pqc_layers))
|
| 87 |
+
obs_state = np.array([0.12, -0.08, 0.04, -0.15])
|
| 88 |
+
q_actions = qrl.forward_expectation_z(obs_state)
|
| 89 |
+
q_grad = qrl.symmetric_difference_gradient(obs_state, action_idx=int(np.argmax(q_actions)))
|
| 90 |
+
|
| 91 |
qfim_res = RelativisticSpacetimeQFIEstimator.compute_qfim_and_qcrb(
|
| 92 |
+
r_meters=radial_km * 1000.0,
|
| 93 |
+
photon_freq_hz=3.84e14,
|
| 94 |
)
|
| 95 |
|
| 96 |
+
l3_out = (
|
| 97 |
+
"=== Q-SNASB LAYER 3 QUANTUM CO-PROCESSOR TELEMETRY ===\n"
|
| 98 |
+
f"QUAV-QAOA Selected Path : Path #{quav_res['selected_path_index']} "
|
| 99 |
+
f"(Length: {quav_res['selected_path_length_m']:.2f} m)\n"
|
| 100 |
+
f"Mean-Field Lyapunov Max : {quav_res['lyapunov_hardness']:.5f}\n"
|
| 101 |
+
f"PQC-QRL Pauli-Z Expects : {np.round(q_actions, 4).tolist()}\n"
|
| 102 |
+
f"Sym-Diff Gradient Norm : {np.linalg.norm(q_grad):.4f} ({4 * int(pqc_layers) * 2} params)\n"
|
| 103 |
+
f"Relativistic QFIM F_rr : {qfim_res['qfim_rr']:.3e} m^-2\n"
|
| 104 |
+
f"QCRB Radial Std Dev : {qfim_res['qcrb_radial_std_m']:.3e} m"
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
+
return l1_out, l2_out, l3_out
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# Build Gradio 4-Tab Flight Deck Interface
|
| 110 |
+
with gr.Blocks(title="Q-SNASB & Q-MCT v4.2 Flight Deck", theme=gr.themes.Soft()) as demo:
|
| 111 |
+
gr.Markdown(
|
| 112 |
+
"## 🛰️ Q-SNASB & Q-MCT v4.2: Quantum Space Navigation & Adaptive Surveillance Bot System\n"
|
| 113 |
+
"Switch between the **6-Mode Interactive Flight Deck**, **Quantum Metrology Benchmarks**, "
|
| 114 |
+
"**Quantum Algorithmic Benchmarks**, and the **Live C++20 + Python Telemetry Console**."
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Tabs():
|
| 118 |
+
with gr.Tab("🛰️ Q-MCT v4.2 Flight Deck (6-Mode Simulator)"):
|
| 119 |
+
gr.HTML(
|
| 120 |
+
'<iframe src="/q_mct_flight_deck.html" '
|
| 121 |
+
'style="width:100%; height:880px; border:1px solid #334155; border-radius:10px; background:#060A17;"></iframe>'
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
with gr.Tab("📊 Quantum Metrology Benchmarks (CAI & DSAC)"):
|
| 125 |
+
gr.HTML(
|
| 126 |
+
'<iframe src="/metrology_benchmarks.html" '
|
| 127 |
+
'style="width:100%; height:760px; border:1px solid #334155; border-radius:10px; background:#060A17;"></iframe>'
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
with gr.Tab("⚛️ Quantum Algorithmic Benchmarks (QUAV & Mean-Field AOA)"):
|
| 131 |
+
gr.HTML(
|
| 132 |
+
'<iframe src="/algorithmic_benchmarks.html" '
|
| 133 |
+
'style="width:100%; height:720px; border:1px solid #334155; border-radius:10px; background:#060A17;"></iframe>'
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
with gr.Tab("⚙️ Layer 1–3 Live Execution Console (C++20 & Python)"):
|
| 137 |
+
gr.Markdown(
|
| 138 |
+
"Execute the compiled **C++20 Layer 1 Quantum Firmware**, **C++20 Layer 2 Swarm WCI/DTN Middleware**, "
|
| 139 |
+
"and **Python Layer 3 Quantum Co-Processor** with custom orbital and obstacle parameters."
|
| 140 |
+
)
|
| 141 |
+
with gr.Row():
|
| 142 |
+
buf_slider = gr.Slider(1.0, 2.5, value=1.5, step=0.1, label="Obstacle Safety Buffer (sx, sy)")
|
| 143 |
+
pqc_slider = gr.Slider(2, 8, value=3, step=1, label="PQC-QRL Entanglement Layers (L)")
|
| 144 |
+
rad_slider = gr.Slider(6600.0, 42164.0, value=7000.0, step=100.0, label="Orbital Radius r (km)")
|
| 145 |
+
|
| 146 |
+
run_btn = gr.Button("🚀 Execute Full 4-Layer Quantum Stack", variant="primary")
|
| 147 |
+
|
| 148 |
+
with gr.Row():
|
| 149 |
+
l1_box = gr.Textbox(label="Layer 1: C++20 Quantum Core Firmware (CAI / GGT / DSAC)", lines=9)
|
| 150 |
+
l2_box = gr.Textbox(label="Layer 2: C++20 Swarm WCI, GCC-AKF & QKD-DTN Middleware", lines=9)
|
| 151 |
+
l3_box = gr.Textbox(label="Layer 3: Python Hybrid Quantum-Classical Co-Processor", lines=8)
|
| 152 |
+
|
| 153 |
+
run_btn.click(
|
| 154 |
+
fn=execute_full_stack_diagnostics,
|
| 155 |
+
inputs=[buf_slider, pqc_slider, rad_slider],
|
| 156 |
+
outputs=[l1_box, l2_box, l3_box],
|
| 157 |
+
)
|
| 158 |
+
demo.load(
|
| 159 |
+
fn=execute_full_stack_diagnostics,
|
| 160 |
+
inputs=[buf_slider, pqc_slider, rad_slider],
|
| 161 |
+
outputs=[l1_box, l2_box, l3_box],
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# FastAPI routes serving the standalone HTML5 modules cleanly inside the Gradio iframes
|
| 165 |
+
fastapi_app = FastAPI(title="Q-SNASB Gradio Host")
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
@fastapi_app.get("/q_mct_flight_deck.html")
|
| 169 |
+
async def serve_flight_deck():
|
| 170 |
+
return FileResponse(UI_DIR / "q_mct_flight_deck.html")
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
@fastapi_app.get("/metrology_benchmarks.html")
|
| 174 |
+
async def serve_metrology():
|
| 175 |
+
return FileResponse(UI_DIR / "metrology_benchmarks.html")
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
@fastapi_app.get("/algorithmic_benchmarks.html")
|
| 179 |
+
async def serve_algorithms():
|
| 180 |
+
return FileResponse(UI_DIR / "algorithmic_benchmarks.html")
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
@fastapi_app.get("/api/health")
|
| 184 |
+
async def health():
|
| 185 |
+
return JSONResponse({"status": "ok", "cpp_build": BUILD_STATUS})
|
| 186 |
+
|
| 187 |
|
| 188 |
+
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
| 189 |
|
| 190 |
+
if __name__ == "__main__":
|
| 191 |
+
port = int(os.environ.get("GRADIO_SERVER_PORT", 7860))
|
| 192 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|