codex_explorer / app.py
RFTSystems's picture
Update app.py
e046e71 verified
import gradio as gr
import hashlib, random
# =========================
# Utilities
# =========================
def sha_seal(s: str) -> str:
return hashlib.sha512(s.encode()).hexdigest()[:32] + "..."
# =========================
# Codex symbolic mutation (50 epochs)
# =========================
operators = ["\\sin", "\\cos", "\\exp", "\\log", "\\nabla", "\\int", "\\frac{\\partial}{\\partial t}"]
variables = ["x", "y", "t", "\\xi_1", "dP", "d\\Psi", "dT"]
def mutate_formula(base, epoch):
if epoch % 5 == 0:
base = f"\\int ({base}) \\, dx"
elif epoch % 7 == 0:
base = f"\\nabla \\cdot ({base})"
else:
new_term = random.choice(operators) + "(" + random.choice(variables) + ")"
base = base + " + " + new_term
return base
def run_epochs(n=50):
ledger = []
base = "x^2 + 1"
for epoch in range(1, n+1):
base = mutate_formula(base, epoch)
seal = sha_seal(base)
ledger.append(
f"## Epoch {epoch}\n\n"
f"$$ {base} $$\n\n"
f"**Immortality Glyph:** `{seal}`\n\n"
f"---\n"
)
return "\n".join(ledger)
# =========================
# 4D Manifold Forge (20-epoch lineage)
# =========================
coords = ["u", "v", "w", "t"]
def pretty_metric(g):
rows = []
for i in range(4):
row = " & ".join(g[i])
rows.append(row)
mat = " \\\\ ".join(rows)
return "\\begin{pmatrix}" + mat + "\\end{pmatrix}"
def det_approx(g):
# Heuristic evolving invariant (not exact): maps symbolic entries to small positive values
def val(s):
base = 1.0
base += 0.01 * (len(s) % 10)
base += 0.02 * sum(ch.isalpha() for ch in s)
return base
M = [[val(g[i][j]) for j in range(4)] for i in range(4)]
# Naive LU-like determinant
import copy
A = copy.deepcopy(M)
det = 1.0
for i in range(4):
pivot = A[i][i]
if abs(pivot) < 1e-9:
return 0.0
det *= pivot
for j in range(i+1,4):
factor = A[j][i]/pivot
for k in range(i,4):
A[j][k] -= factor*A[i][k]
return det
def signature_hint(g):
diag = [g[i][i] for i in range(4)]
pos = sum("exp" in d for d in diag)
neg = sum("log" in d for d in diag)
osc = sum(("sin" in d) or ("cos" in d) for d in diag)
return f"(+:{pos}, -:{neg}, ~:{osc})"
def random_overlay(c):
overlays = [
f"1+\\sin({c})",
f"1+\\cos({c})",
f"1+\\exp({c})",
f"1+\\log\\!\\big(1+{c}^2\\big)"
]
return random.choice(overlays)
def mutate_metric(g, epoch, intensity="medium"):
idx_pairs = [(0,0),(1,1),(2,2),(3,3),
(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)]
k = 2 if intensity=="low" else (4 if intensity=="medium" else 6)
chosen = random.sample(idx_pairs, min(k, len(idx_pairs)))
for (i,j) in chosen:
ci, cj = coords[i], coords[j]
if i == j:
g[i][j] = random_overlay(ci)
else:
mix = [
f"\\cos({ci})+\\log\\!\\big(1+{cj}^2\\big)",
f"\\exp({cj})-\\cos({ci})",
f"\\sin({ci}{cj})",
f"\\cos({ci})+\\log\\!\\big(1+{cj}^2\\big)"
]
g[i][j] = random.choice(mix)
g[j][i] = g[i][j]
return g
def christoffel_snippet():
return (
"\\Gamma^{1}_{\\;12} = \\tfrac{1}{2}\\, g^{11}\\!\\left(\\partial_{u} g_{22} + \\partial_{v} g_{12} - \\partial_{v} g_{11}\\right),\\quad"
"\\Gamma^{2}_{\\;34} = \\tfrac{1}{2}\\, g^{22}\\!\\left(\\partial_{v} g_{44} + \\partial_{t} g_{24} - \\partial_{w} g_{23}\\right),\\quad"
"\\Gamma^{4}_{\\;13} = \\tfrac{1}{2}\\, g^{44}\\!\\left(\\partial_{u} g_{33} + \\partial_{w} g_{13} - \\partial_{u} g_{34}\\right)"
)
def scalar_curvature_hint(g):
d = det_approx(g)
return f"\\mathcal{{R}} \\approx {d:.3f}"
def run_manifold(signature_choice="Euclidean (+,+,+,+)", intensity="medium", epochs=20):
if signature_choice.startswith("Euclidean"):
g = [["1","0","0","0"],
["0","1","0","0"],
["0","0","1","0"],
["0","0","0","1"]]
else:
g = [["1","0","0","0"],
["0","1","0","0"],
["0","0","1","0"],
["0","0","0","-1"]]
ledger = []
for epoch in range(1, epochs+1):
g = mutate_metric(g, epoch, intensity=intensity)
g_latex = pretty_metric(g)
detg = det_approx(g)
sig = signature_hint(g)
Gamma = christoffel_snippet()
Rscalar = scalar_curvature_hint(g)
vol = "dV = \\sqrt{\\det g}\\, du\\, dv\\, dw\\, dt"
seal = sha_seal(g_latex + Rscalar)
entry = (
f"## Manifold Epoch {epoch}\n\n"
f"**Coordinates:** $\\mathbf{{X}}=(u,v,w,t)$\n\n"
f"**Metric** $g_{{ij}}$:\n\n"
f"$$ {g_latex} $$\n\n"
f"**Representative connections:**\n\n"
f"$$ {Gamma} $$\n\n"
f"**Invariants:** \n"
f"- **Determinant:** $\\det g \\approx {detg:.3f}$ \n"
f"- **Signature hint:** `{sig}` \n"
f"- **Scalar curvature hint:** $$ {Rscalar} $$ \n"
f"- **Volume element:** $$ {vol} $$\n\n"
f"**Immortality Glyph:** `{seal}`\n\n"
f"---\n"
)
ledger.append(entry)
return "\n".join(ledger)
# =========================
# App UI
# =========================
custom_theme = gr.themes.Base(
primary_hue="cyan",
secondary_hue="pink",
neutral_hue="gray",
)
with gr.Blocks(theme=custom_theme) as demo:
gr.Markdown("# 🌌 Resonance Atlas — The Living Codex")
gr.Markdown("Choose: 50‑epoch Codex Scrolls or the 4D Manifold Forge (20‑epoch lineage).")
with gr.Tab("Codex Scrolls"):
gr.Markdown("### 🔢 Live 50 Epoch Run")
run_button = gr.Button("Run 50 Epochs")
output = gr.Markdown()
run_button.click(fn=run_epochs, inputs=None, outputs=output)
with gr.Tab("4D Manifold Forge"):
gr.Markdown("### 🧭 Forge a 4D manifold with evolving metric and invariants")
signature = gr.Radio(choices=["Euclidean (+,+,+,+)", "Pseudo-Riemannian (+,+,+,-)"],
value="Euclidean (+,+,+,+)", label="Signature")
intensity = gr.Radio(choices=["low", "medium", "high"], value="medium", label="Overlay intensity")
epochs = gr.Slider(5, 30, value=20, step=1, label="Epochs")
run_manifold_btn = gr.Button("Forge 4D Manifold (Lineage)")
manifold_out = gr.Markdown()
run_manifold_btn.click(fn=run_manifold, inputs=[signature, intensity, epochs], outputs=manifold_out)
demo.launch()