RFT_Omega_API / app.py
RFTSystems's picture
Update app.py
a13a736 verified
# ============================================================
# Rendered Frame Theory — Stabilising System Verification Panel
# Author: Liam Grinstead | RFT Systems | All Rights Reserved
# ============================================================
import json, random
from datetime import datetime
import numpy as np
import gradio as gr
# ------------------ About / Legal ---------------------------
RFT_VERSION = "v4.0-verification-panel"
RFT_DOI = "https://doi.org/10.5281/zenodo.17466722"
LEGAL_NOTICE = (
"All Rights Reserved — RFT-IPURL v1.0 (UK / Berne). "
"Research validation use only. No reverse-engineering without written consent."
)
# ------------------ System Profiles -------------------------
PROFILES = {
"AI / Neural": {"base": (0.86, 0.80), "w": (0.65, 0.35)},
"SpaceX / Aerospace": {"base": (0.84, 0.79), "w": (0.60, 0.40)},
"Energy / RHES": {"base": (0.83, 0.78), "w": (0.55, 0.45)},
"Extreme Perturbation": {"base": (0.82, 0.77), "w": (0.50, 0.50)},
}
# ------------------ Simulation Core -------------------------
def _rng(seed: int):
return np.random.RandomState(seed)
def simulate_step(rng, profile, sigma, dist):
base_q, base_z = PROFILES[profile]["base"]
wq, wz = PROFILES[profile]["w"]
if dist == "uniform":
qn = rng.uniform(-sigma, sigma)
zn = rng.uniform(-sigma * 0.8, sigma * 0.8)
else:
qn = rng.normal(0, sigma)
zn = rng.normal(0, sigma * 0.8)
q = float(np.clip(base_q + wq * qn, 0.0, 0.99))
z = float(np.clip(base_z + wz * zn, 0.0, 0.99))
variance = abs(qn) + abs(zn)
if variance > 0.15:
status = "critical"
elif variance > 0.07:
status = "perturbed"
else:
status = "nominal"
return {"σ": round(sigma, 6), "QΩ": q, "ζ_sync": z, "status": status}
# ------------------ Simulation Runner -----------------------
def run(profile, dist, sigma, seed, samples):
rng = _rng(int(seed))
results = [simulate_step(rng, profile, sigma, dist) for _ in range(samples)]
q_mean = np.mean([r["QΩ"] for r in results])
z_mean = np.mean([r["ζ_sync"] for r in results])
majority = max(
["nominal", "perturbed", "critical"],
key=lambda s: sum(1 for r in results if r["status"] == s),
)
summary = {
"profile": profile,
"noise_scale": sigma,
"distribution": dist,
"QΩ_mean": round(float(q_mean), 6),
"ζ_sync_mean": round(float(z_mean), 6),
"status_majority": majority,
"timestamp_utc": datetime.utcnow().isoformat() + "Z",
"rft_notice": LEGAL_NOTICE,
}
return summary, json.dumps(summary, indent=2)
# ------------------ File Saver -------------------------------
def save_run_log(run_json_str):
try:
data = json.loads(run_json_str)
filename = f"RFT_Omega_Run_{datetime.utcnow().strftime('%Y-%m-%dT%H-%M-%SZ')}.json"
with open(filename, "w") as f:
json.dump(data, f, indent=2)
return filename
except Exception as e:
return None
# ------------------ Gradio Interface ------------------------
with gr.Blocks(title="Rendered Frame Theory — Stabilising System Verification Panel") as demo:
gr.Markdown(
f"## 🧠 Rendered Frame Theory — Stabilising System Verification Panel \n"
f"**Version:** {RFT_VERSION} \n"
f"**DOI:** [{RFT_DOI}]({RFT_DOI}) \n"
f"{LEGAL_NOTICE}"
)
gr.Markdown(
"""
### 🧩 How to Use
1️⃣ Select a **System Profile** (AI / Neural, SpaceX / Aerospace, Energy / RHES, Extreme Perturbation).
2️⃣ Choose a **Noise Distribution** (gauss or uniform).
3️⃣ Adjust **Noise Scale (σ)** to simulate environmental perturbations.
4️⃣ Press **Run Simulation** — results will show mean QΩ (stability) and ζ_sync (coherence) across samples.
5️⃣ Use **Save Run Log** to download your test record as a JSON file with timestamp.
**Interpretation:**
- `Nominal` → Stable harmonic equilibrium
- `Perturbed` → Transitional / adaptive state
- `Critical` → Instability threshold reached
Each output is time-stamped, reproducible, and protected under RFT-IPURL v1.0.
"""
)
with gr.Row():
profile = gr.Dropdown(list(PROFILES.keys()), label="System Profile", value="AI / Neural")
dist = gr.Radio(["gauss", "uniform"], label="Noise Distribution", value="gauss")
with gr.Row():
sigma = gr.Slider(0.0, 0.3, value=0.05, step=0.01, label="Noise Scale (σ)")
seed = gr.Number(value=123, label="Seed (integer)")
samples = gr.Slider(1, 20, value=5, step=1, label="Samples per run")
run_btn = gr.Button("Run Simulation")
output = gr.JSON(label="Simulation Results")
hidden_json = gr.Textbox(visible=False)
save_btn = gr.Button("💾 Save Run Log")
download_file = gr.File(label="Download Saved Log")
run_btn.click(run, inputs=[profile, dist, sigma, seed, samples], outputs=[output, hidden_json])
save_btn.click(save_run_log, inputs=[hidden_json], outputs=[download_file])
# ------------------ Launch -------------------------------
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", share=False, ssr_mode=False)