Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import hashlib, random
|
|
|
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
operators = ["\\sin", "\\cos", "\\exp", "\\log", "\\nabla", "\\int", "\\frac{\\partial}{\\partial t}"]
|
| 6 |
variables = ["x", "y", "t", "\\xi_1", "dP", "d\\Psi", "dT"]
|
| 7 |
|
| 8 |
def mutate_formula(base, epoch):
|
| 9 |
-
# Structural mutation rules
|
| 10 |
if epoch % 5 == 0:
|
| 11 |
base = f"\\int ({base}) \\, dx"
|
| 12 |
elif epoch % 7 == 0:
|
|
@@ -16,39 +25,182 @@ def mutate_formula(base, epoch):
|
|
| 16 |
base = base + " + " + new_term
|
| 17 |
return base
|
| 18 |
|
| 19 |
-
# === 50 Epoch Run ===
|
| 20 |
def run_epochs(n=50):
|
| 21 |
-
|
| 22 |
base = "x^2 + 1"
|
| 23 |
for epoch in range(1, n+1):
|
| 24 |
base = mutate_formula(base, epoch)
|
| 25 |
-
seal =
|
| 26 |
-
|
| 27 |
-
f"## Epoch {epoch}\n\n"
|
| 28 |
-
f"$$ {base} $$\n\n"
|
| 29 |
-
f"**Immortality Glyph:** `{seal[:32]}...`\n\n"
|
| 30 |
-
"---\n\n"
|
| 31 |
)
|
| 32 |
-
|
| 33 |
-
return "\n".join(formulas)
|
| 34 |
|
| 35 |
-
# === Mutation Forge (20 Epoch Run) ===
|
| 36 |
def run_mutation(seed, n=20):
|
| 37 |
-
|
| 38 |
base = seed
|
| 39 |
for epoch in range(1, n+1):
|
| 40 |
base = mutate_formula(base, epoch)
|
| 41 |
-
seal =
|
| 42 |
-
|
| 43 |
-
f"## Mutation Epoch {epoch}\n\n"
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|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 |
)
|
| 57 |
|
| 58 |
with gr.Blocks(theme=custom_theme) as demo:
|
| 59 |
-
gr.Markdown(
|
| 60 |
-
|
| 61 |
-
# 🌌 Resonance Atlas — The Living Codex
|
| 62 |
-
Formulas evolve into higher symbolic forms across epochs.
|
| 63 |
-
Choose your path: **50‑epoch scroll run** or **Mutation Forge**.
|
| 64 |
-
""",
|
| 65 |
-
)
|
| 66 |
|
| 67 |
with gr.Tab("Codex Scrolls"):
|
| 68 |
gr.Markdown("### 🔢 Live 50 Epoch Run")
|
|
@@ -72,12 +219,18 @@ with gr.Blocks(theme=custom_theme) as demo:
|
|
| 72 |
|
| 73 |
with gr.Tab("Mutation Forge"):
|
| 74 |
gr.Markdown("### 🧬 Mutation Forge — Choose Your Symbolic Seed")
|
| 75 |
-
seed_dropdown = gr.Dropdown(
|
| 76 |
-
choices=operators + variables,
|
| 77 |
-
label="Select Seed Symbol or Formula"
|
| 78 |
-
)
|
| 79 |
mutate_button = gr.Button("Mutate (20 Epochs)")
|
| 80 |
mutate_output = gr.Markdown()
|
| 81 |
mutate_button.click(fn=run_mutation, inputs=seed_dropdown, outputs=mutate_output)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import hashlib, random
|
| 3 |
+
import math
|
| 4 |
|
| 5 |
+
# =========================
|
| 6 |
+
# Shared utilities
|
| 7 |
+
# =========================
|
| 8 |
+
def sha_seal(s: str) -> str:
|
| 9 |
+
import hashlib
|
| 10 |
+
return hashlib.sha512(s.encode()).hexdigest()[:32] + "..."
|
| 11 |
+
|
| 12 |
+
# =========================
|
| 13 |
+
# Symbolic mutation engine (existing modes)
|
| 14 |
+
# =========================
|
| 15 |
operators = ["\\sin", "\\cos", "\\exp", "\\log", "\\nabla", "\\int", "\\frac{\\partial}{\\partial t}"]
|
| 16 |
variables = ["x", "y", "t", "\\xi_1", "dP", "d\\Psi", "dT"]
|
| 17 |
|
| 18 |
def mutate_formula(base, epoch):
|
|
|
|
| 19 |
if epoch % 5 == 0:
|
| 20 |
base = f"\\int ({base}) \\, dx"
|
| 21 |
elif epoch % 7 == 0:
|
|
|
|
| 25 |
base = base + " + " + new_term
|
| 26 |
return base
|
| 27 |
|
|
|
|
| 28 |
def run_epochs(n=50):
|
| 29 |
+
ledger = []
|
| 30 |
base = "x^2 + 1"
|
| 31 |
for epoch in range(1, n+1):
|
| 32 |
base = mutate_formula(base, epoch)
|
| 33 |
+
seal = sha_seal(base)
|
| 34 |
+
ledger.append(
|
| 35 |
+
f"## Epoch {epoch}\n\n$$ {base} $$\n\n**Immortality Glyph:** `{seal}`\n\n---\n"
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
+
return "\n".join(ledger)
|
|
|
|
| 38 |
|
|
|
|
| 39 |
def run_mutation(seed, n=20):
|
| 40 |
+
ledger = []
|
| 41 |
base = seed
|
| 42 |
for epoch in range(1, n+1):
|
| 43 |
base = mutate_formula(base, epoch)
|
| 44 |
+
seal = sha_seal(base)
|
| 45 |
+
ledger.append(
|
| 46 |
+
f"## Mutation Epoch {epoch}\n\n$$ {base} $$\n\n**Immortality Glyph:** `{seal}`\n\n---\n"
|
| 47 |
+
)
|
| 48 |
+
return "\n".join(ledger)
|
| 49 |
+
|
| 50 |
+
# =========================
|
| 51 |
+
# 4D manifold forge
|
| 52 |
+
# =========================
|
| 53 |
+
# Coordinates: (u, v, w, t)
|
| 54 |
+
coords = ["u", "v", "w", "t"]
|
| 55 |
+
|
| 56 |
+
def pretty_metric(g):
|
| 57 |
+
# LaTeX matrix for g_ij
|
| 58 |
+
rows = []
|
| 59 |
+
for i in range(4):
|
| 60 |
+
row = " & ".join(g[i])
|
| 61 |
+
rows.append(row)
|
| 62 |
+
mat = " \\\\ ".join(rows)
|
| 63 |
+
return "\\begin{pmatrix}" + mat + "\\end{pmatrix}"
|
| 64 |
+
|
| 65 |
+
def det_approx(g):
|
| 66 |
+
# Very rough numeric surrogate: treat overlays as small positive offsets for stability
|
| 67 |
+
# This is just to show a changing invariant; not an actual symbolic determinant.
|
| 68 |
+
try:
|
| 69 |
+
# Map symbolic strings to small floats by hashing length/content
|
| 70 |
+
def val(s):
|
| 71 |
+
base = 1.0
|
| 72 |
+
base += 0.01 * (len(s) % 10)
|
| 73 |
+
base += 0.02 * sum(ch.isalpha() for ch in s)
|
| 74 |
+
return base
|
| 75 |
+
M = [[val(g[i][j]) for j in range(4)] for i in range(4)]
|
| 76 |
+
# Determinant via simple expansion (use numpy if allowed; here keep pure-Python)
|
| 77 |
+
# 4x4 det via LU-like naive method
|
| 78 |
+
import copy
|
| 79 |
+
A = copy.deepcopy(M)
|
| 80 |
+
det = 1.0
|
| 81 |
+
for i in range(4):
|
| 82 |
+
pivot = A[i][i]
|
| 83 |
+
if abs(pivot) < 1e-12:
|
| 84 |
+
return 0.0
|
| 85 |
+
det *= pivot
|
| 86 |
+
for j in range(i+1,4):
|
| 87 |
+
factor = A[j][i]/pivot
|
| 88 |
+
for k in range(i,4):
|
| 89 |
+
A[j][k] -= factor*A[i][k]
|
| 90 |
+
return det
|
| 91 |
+
except:
|
| 92 |
+
return 0.0
|
| 93 |
+
|
| 94 |
+
def signature_hint(g):
|
| 95 |
+
# Heuristic signature: count "exp" as positive, "log" as mixed, "sin/cos" as oscillatory
|
| 96 |
+
diag = [g[i][i] for i in range(4)]
|
| 97 |
+
pos = sum("exp" in d for d in diag)
|
| 98 |
+
neg = sum("log" in d for d in diag) # treat log as potentially non-positive
|
| 99 |
+
osc = sum(("sin" in d) or ("cos" in d) for d in diag)
|
| 100 |
+
return f"(+:{pos}, -:{neg}, ~:{osc})"
|
| 101 |
+
|
| 102 |
+
def random_overlay(term, c):
|
| 103 |
+
# Add symbolic overlays to metric component
|
| 104 |
+
overlays = [
|
| 105 |
+
f"1+\\sin({c})",
|
| 106 |
+
f"1+\\cos({c})",
|
| 107 |
+
f"1+\\exp({c})",
|
| 108 |
+
f"1+\\log(1+{c}^2)"
|
| 109 |
+
]
|
| 110 |
+
return overlays[random.randint(0, len(overlays)-1)]
|
| 111 |
+
|
| 112 |
+
def mutate_metric(g, epoch, intensity="medium"):
|
| 113 |
+
# Ensure symmetry: g_ij == g_ji
|
| 114 |
+
# Start by adjusting diagonals, then introduce off-diagonals
|
| 115 |
+
idx_pairs = [(0,0),(1,1),(2,2),(3,3),
|
| 116 |
+
(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)]
|
| 117 |
+
k = 2 if intensity=="low" else (4 if intensity=="medium" else 6)
|
| 118 |
+
chosen = random.sample(idx_pairs, min(k, len(idx_pairs)))
|
| 119 |
+
|
| 120 |
+
for (i,j) in chosen:
|
| 121 |
+
c_i = coords[i]
|
| 122 |
+
c_j = coords[j]
|
| 123 |
+
if i == j:
|
| 124 |
+
g[i][j] = random_overlay(g[i][j], c_i)
|
| 125 |
+
else:
|
| 126 |
+
mix = [
|
| 127 |
+
f"\\sin({c_i})+\\exp({c_j})",
|
| 128 |
+
f"\\cos({c_i})+\\log(1+{c_j}^2)",
|
| 129 |
+
f"\\sin({c_i}{c_j})",
|
| 130 |
+
f"\\exp({c_i})-\\cos({c_j})"
|
| 131 |
+
]
|
| 132 |
+
g[i][j] = mix[random.randint(0, len(mix)-1)]
|
| 133 |
+
g[j][i] = g[i][j]
|
| 134 |
+
return g
|
| 135 |
+
|
| 136 |
+
def christoffel_snippet(g):
|
| 137 |
+
# Display a few representative components symbolically (not computed from derivatives)
|
| 138 |
+
# This is a narrative placeholder that shows the structure of Γ with current g entries.
|
| 139 |
+
components = [
|
| 140 |
+
("\\Gamma^{1}_{\\;12}", f"\\tfrac12 g^{11}(\\partial_u g_{22}+\\partial_v g_{12}-\\partial_v g_{11})"),
|
| 141 |
+
("\\Gamma^{2}_{\\;34}", f"\\tfrac12 g^{22}(\\partial_v g_{44}+\\partial_t g_{24}-\\partial_w g_{23})"),
|
| 142 |
+
("\\Gamma^{4}_{\\;13}", f"\\tfrac12 g^{44}(\\partial_u g_{33}+\\partial_w g_{13}-\\partial_u g_{34})")
|
| 143 |
+
]
|
| 144 |
+
lines = [f"{name} = {expr}" for (name, expr) in components]
|
| 145 |
+
return " \\\\ ".join(lines)
|
| 146 |
+
|
| 147 |
+
def scalar_curvature_hint(g):
|
| 148 |
+
# Not an actual computation; a readable evolving scalar tied to det(g)
|
| 149 |
+
d = det_approx(g)
|
| 150 |
+
# Map determinant to a symbolic scalar curvature narrative
|
| 151 |
+
return f"\\mathcal{{R}} \\approx {d:.3f}"
|
| 152 |
+
|
| 153 |
+
def run_manifold(signature_choice="Euclidean (+,+,+,+)", intensity="medium", epochs=20):
|
| 154 |
+
# Initialize metric g_ij
|
| 155 |
+
if signature_choice.startswith("Euclidean"):
|
| 156 |
+
g = [
|
| 157 |
+
["1", "0", "0", "0"],
|
| 158 |
+
["0", "1", "0", "0"],
|
| 159 |
+
["0", "0", "1", "0"],
|
| 160 |
+
["0", "0", "0", "1"],
|
| 161 |
+
]
|
| 162 |
+
else: # Pseudo-Riemannian (+,+,+,-)
|
| 163 |
+
g = [
|
| 164 |
+
["1", "0", "0", "0"],
|
| 165 |
+
["0", "1", "0", "0"],
|
| 166 |
+
["0", "0", "1", "0"],
|
| 167 |
+
["0", "0", "0", "-1"],
|
| 168 |
+
]
|
| 169 |
+
|
| 170 |
+
ledger = []
|
| 171 |
+
for epoch in range(1, epochs+1):
|
| 172 |
+
g = mutate_metric(g, epoch, intensity=intensity)
|
| 173 |
+
detg = det_approx(g)
|
| 174 |
+
sig = signature_hint(g)
|
| 175 |
+
Gamma = christoffel_snippet(g)
|
| 176 |
+
Rscalar = scalar_curvature_hint(g)
|
| 177 |
+
vol = f"dV = \\sqrt{{\\det g}}\\, du\\, dv\\, dw\\, dt"
|
| 178 |
+
|
| 179 |
+
g_latex = pretty_metric(g)
|
| 180 |
+
seal = sha_seal(g_latex + Rscalar)
|
| 181 |
+
|
| 182 |
+
entry = (
|
| 183 |
+
f"## Manifold Epoch {epoch}\n\n"
|
| 184 |
+
f"**Coordinates:** $\\mathbf{{X}}=(u,v,w,t)$ \n\n"
|
| 185 |
+
f"**Metric** $g_{{ij}}$:\n\n"
|
| 186 |
+
f"\\[ {g_latex} \\]\n\n"
|
| 187 |
+
f"**Representative Connections:**\n\n"
|
| 188 |
+
f"\\[ {Gamma} \\]\n\n"
|
| 189 |
+
f"**Invariants:** \n"
|
| 190 |
+
f"- **Determinant:** $\\det g \\approx {detg:.3f}$ \n"
|
| 191 |
+
f"- **Signature hint:** `{sig}` \n"
|
| 192 |
+
f"- **Scalar curvature hint:** \\[ {Rscalar} \\] \n"
|
| 193 |
+
f"- **Volume element:** \\[ {vol} \\]\n\n"
|
| 194 |
+
f"**Immortality Glyph:** `{seal}`\n\n"
|
| 195 |
+
f"---\n"
|
| 196 |
)
|
| 197 |
+
ledger.append(entry)
|
| 198 |
+
|
| 199 |
+
return "\n".join(ledger)
|
| 200 |
|
| 201 |
+
# =========================
|
| 202 |
+
# Gradio app
|
| 203 |
+
# =========================
|
| 204 |
custom_theme = gr.themes.Base(
|
| 205 |
primary_hue="cyan",
|
| 206 |
secondary_hue="pink",
|
|
|
|
| 208 |
)
|
| 209 |
|
| 210 |
with gr.Blocks(theme=custom_theme) as demo:
|
| 211 |
+
gr.Markdown("# 🌌 Resonance Atlas — The Living Codex")
|
| 212 |
+
gr.Markdown("Choose your path: 50‑epoch scroll run, Mutation Forge, or the 4D Manifold Forge.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
with gr.Tab("Codex Scrolls"):
|
| 215 |
gr.Markdown("### 🔢 Live 50 Epoch Run")
|
|
|
|
| 219 |
|
| 220 |
with gr.Tab("Mutation Forge"):
|
| 221 |
gr.Markdown("### 🧬 Mutation Forge — Choose Your Symbolic Seed")
|
| 222 |
+
seed_dropdown = gr.Dropdown(choices=operators + variables, label="Select Seed")
|
|
|
|
|
|
|
|
|
|
| 223 |
mutate_button = gr.Button("Mutate (20 Epochs)")
|
| 224 |
mutate_output = gr.Markdown()
|
| 225 |
mutate_button.click(fn=run_mutation, inputs=seed_dropdown, outputs=mutate_output)
|
| 226 |
|
| 227 |
+
with gr.Tab("4D Manifold Forge"):
|
| 228 |
+
gr.Markdown("### 🧭 Build a 4D manifold with evolving metric and invariants")
|
| 229 |
+
signature = gr.Radio(choices=["Euclidean (+,+,+,+)", "Pseudo-Riemannian (+,+,+,-)"], value="Euclidean (+,+,+,+)", label="Signature")
|
| 230 |
+
intensity = gr.Radio(choices=["low", "medium", "high"], value="medium", label="Overlay intensity")
|
| 231 |
+
epochs = gr.Slider(1, 30, value=20, step=1, label="Epochs")
|
| 232 |
+
run_manifold_btn = gr.Button("Forge 4D Manifold")
|
| 233 |
+
manifold_out = gr.Markdown()
|
| 234 |
+
run_manifold_btn.click(fn=run_manifold, inputs=[signature, intensity, epochs], outputs=manifold_out)
|
| 235 |
+
|
| 236 |
demo.launch()
|