Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import hashlib, random | |
| import math | |
| # ========================= | |
| # Shared utilities | |
| # ========================= | |
| def sha_seal(s: str) -> str: | |
| import hashlib | |
| return hashlib.sha512(s.encode()).hexdigest()[:32] + "..." | |
| # ========================= | |
| # Symbolic mutation engine (existing modes) | |
| # ========================= | |
| 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$$ {base} $$\n\n**Immortality Glyph:** `{seal}`\n\n---\n" | |
| ) | |
| return "\n".join(ledger) | |
| def run_mutation(seed, n=20): | |
| ledger = [] | |
| base = seed | |
| for epoch in range(1, n+1): | |
| base = mutate_formula(base, epoch) | |
| seal = sha_seal(base) | |
| ledger.append( | |
| f"## Mutation Epoch {epoch}\n\n$$ {base} $$\n\n**Immortality Glyph:** `{seal}`\n\n---\n" | |
| ) | |
| return "\n".join(ledger) | |
| # ========================= | |
| # 4D manifold forge | |
| # ========================= | |
| # Coordinates: (u, v, w, t) | |
| coords = ["u", "v", "w", "t"] | |
| def pretty_metric(g): | |
| # LaTeX matrix for g_ij | |
| 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): | |
| # Very rough numeric surrogate: treat overlays as small positive offsets for stability | |
| # This is just to show a changing invariant; not an actual symbolic determinant. | |
| try: | |
| # Map symbolic strings to small floats by hashing length/content | |
| 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)] | |
| # Determinant via simple expansion (use numpy if allowed; here keep pure-Python) | |
| # 4x4 det via LU-like naive method | |
| import copy | |
| A = copy.deepcopy(M) | |
| det = 1.0 | |
| for i in range(4): | |
| pivot = A[i][i] | |
| if abs(pivot) < 1e-12: | |
| 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 | |
| except: | |
| return 0.0 | |
| def signature_hint(g): | |
| # Heuristic signature: count "exp" as positive, "log" as mixed, "sin/cos" as oscillatory | |
| 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) # treat log as potentially non-positive | |
| osc = sum(("sin" in d) or ("cos" in d) for d in diag) | |
| return f"(+:{pos}, -:{neg}, ~:{osc})" | |
| def random_overlay(term, c): | |
| # Add symbolic overlays to metric component | |
| overlays = [ | |
| f"1+\\sin({c})", | |
| f"1+\\cos({c})", | |
| f"1+\\exp({c})", | |
| f"1+\\log(1+{c}^2)" | |
| ] | |
| return overlays[random.randint(0, len(overlays)-1)] | |
| def mutate_metric(g, epoch, intensity="medium"): | |
| # Ensure symmetry: g_ij == g_ji | |
| # Start by adjusting diagonals, then introduce off-diagonals | |
| 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: | |
| c_i = coords[i] | |
| c_j = coords[j] | |
| if i == j: | |
| g[i][j] = random_overlay(g[i][j], c_i) | |
| else: | |
| mix = [ | |
| f"\\sin({c_i})+\\exp({c_j})", | |
| f"\\cos({c_i})+\\log(1+{c_j}^2)", | |
| f"\\sin({c_i}{c_j})", | |
| f"\\exp({c_i})-\\cos({c_j})" | |
| ] | |
| g[i][j] = mix[random.randint(0, len(mix)-1)] | |
| g[j][i] = g[i][j] | |
| return g | |
| def christoffel_snippet(g): | |
| # Display a few representative components symbolically (not computed from derivatives) | |
| # This is a narrative placeholder that shows the structure of Γ with current g entries. | |
| components = [ | |
| ("\\Gamma^{1}_{\\;12}", f"\\tfrac12 g^{11}(\\partial_u g_{22}+\\partial_v g_{12}-\\partial_v g_{11})"), | |
| ("\\Gamma^{2}_{\\;34}", f"\\tfrac12 g^{22}(\\partial_v g_{44}+\\partial_t g_{24}-\\partial_w g_{23})"), | |
| ("\\Gamma^{4}_{\\;13}", f"\\tfrac12 g^{44}(\\partial_u g_{33}+\\partial_w g_{13}-\\partial_u g_{34})") | |
| ] | |
| lines = [f"{name} = {expr}" for (name, expr) in components] | |
| return " \\\\ ".join(lines) | |
| def scalar_curvature_hint(g): | |
| # Not an actual computation; a readable evolving scalar tied to det(g) | |
| d = det_approx(g) | |
| # Map determinant to a symbolic scalar curvature narrative | |
| return f"\\mathcal{{R}} \\approx {d:.3f}" | |
| def run_manifold(signature_choice="Euclidean (+,+,+,+)", intensity="medium", epochs=20): | |
| # Initialize metric g_ij | |
| if signature_choice.startswith("Euclidean"): | |
| g = [ | |
| ["1", "0", "0", "0"], | |
| ["0", "1", "0", "0"], | |
| ["0", "0", "1", "0"], | |
| ["0", "0", "0", "1"], | |
| ] | |
| else: # Pseudo-Riemannian (+,+,+,-) | |
| 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) | |
| detg = det_approx(g) | |
| sig = signature_hint(g) | |
| Gamma = christoffel_snippet(g) | |
| Rscalar = scalar_curvature_hint(g) | |
| vol = f"dV = \\sqrt{{\\det g}}\\, du\\, dv\\, dw\\, dt" | |
| g_latex = pretty_metric(g) | |
| 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) | |
| # ========================= | |
| # Gradio app | |
| # ========================= | |
| 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 your path: 50‑epoch scroll run, Mutation Forge, or the 4D Manifold Forge.") | |
| 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("Mutation Forge"): | |
| gr.Markdown("### 🧬 Mutation Forge — Choose Your Symbolic Seed") | |
| seed_dropdown = gr.Dropdown(choices=operators + variables, label="Select Seed") | |
| mutate_button = gr.Button("Mutate (20 Epochs)") | |
| mutate_output = gr.Markdown() | |
| mutate_button.click(fn=run_mutation, inputs=seed_dropdown, outputs=mutate_output) | |
| with gr.Tab("4D Manifold Forge"): | |
| gr.Markdown("### 🧭 Build 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(1, 30, value=20, step=1, label="Epochs") | |
| run_manifold_btn = gr.Button("Forge 4D Manifold") | |
| manifold_out = gr.Markdown() | |
| run_manifold_btn.click(fn=run_manifold, inputs=[signature, intensity, epochs], outputs=manifold_out) | |
| demo.launch() |