RFTSystems commited on
Commit
e046e71
·
verified ·
1 Parent(s): 82f5ccf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -112
app.py CHANGED
@@ -1,16 +1,14 @@
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"]
@@ -32,29 +30,19 @@ def run_epochs(n=50):
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])
@@ -63,134 +51,110 @@ def pretty_metric(g):
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
  )
@@ -199,7 +163,7 @@ def run_manifold(signature_choice="Euclidean (+,+,+,+)", intensity="medium", epo
199
  return "\n".join(ledger)
200
 
201
  # =========================
202
- # Gradio app
203
  # =========================
204
  custom_theme = gr.themes.Base(
205
  primary_hue="cyan",
@@ -209,7 +173,7 @@ custom_theme = gr.themes.Base(
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")
@@ -217,19 +181,13 @@ with gr.Blocks(theme=custom_theme) as demo:
217
  output = gr.Markdown()
218
  run_button.click(fn=run_epochs, inputs=None, outputs=output)
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
 
 
1
  import gradio as gr
2
  import hashlib, random
 
3
 
4
  # =========================
5
+ # Utilities
6
  # =========================
7
  def sha_seal(s: str) -> str:
 
8
  return hashlib.sha512(s.encode()).hexdigest()[:32] + "..."
9
 
10
  # =========================
11
+ # Codex symbolic mutation (50 epochs)
12
  # =========================
13
  operators = ["\\sin", "\\cos", "\\exp", "\\log", "\\nabla", "\\int", "\\frac{\\partial}{\\partial t}"]
14
  variables = ["x", "y", "t", "\\xi_1", "dP", "d\\Psi", "dT"]
 
30
  base = mutate_formula(base, epoch)
31
  seal = sha_seal(base)
32
  ledger.append(
33
+ f"## Epoch {epoch}\n\n"
34
+ f"$$ {base} $$\n\n"
35
+ f"**Immortality Glyph:** `{seal}`\n\n"
36
+ f"---\n"
 
 
 
 
 
 
 
 
37
  )
38
  return "\n".join(ledger)
39
 
40
  # =========================
41
+ # 4D Manifold Forge (20-epoch lineage)
42
  # =========================
 
43
  coords = ["u", "v", "w", "t"]
44
 
45
  def pretty_metric(g):
 
46
  rows = []
47
  for i in range(4):
48
  row = " & ".join(g[i])
 
51
  return "\\begin{pmatrix}" + mat + "\\end{pmatrix}"
52
 
53
  def det_approx(g):
54
+ # Heuristic evolving invariant (not exact): maps symbolic entries to small positive values
55
+ def val(s):
56
+ base = 1.0
57
+ base += 0.01 * (len(s) % 10)
58
+ base += 0.02 * sum(ch.isalpha() for ch in s)
59
+ return base
60
+ M = [[val(g[i][j]) for j in range(4)] for i in range(4)]
61
+ # Naive LU-like determinant
62
+ import copy
63
+ A = copy.deepcopy(M)
64
+ det = 1.0
65
+ for i in range(4):
66
+ pivot = A[i][i]
67
+ if abs(pivot) < 1e-9:
68
+ return 0.0
69
+ det *= pivot
70
+ for j in range(i+1,4):
71
+ factor = A[j][i]/pivot
72
+ for k in range(i,4):
73
+ A[j][k] -= factor*A[i][k]
74
+ return det
 
 
 
 
 
 
75
 
76
  def signature_hint(g):
 
77
  diag = [g[i][i] for i in range(4)]
78
  pos = sum("exp" in d for d in diag)
79
+ neg = sum("log" in d for d in diag)
80
  osc = sum(("sin" in d) or ("cos" in d) for d in diag)
81
  return f"(+:{pos}, -:{neg}, ~:{osc})"
82
 
83
+ def random_overlay(c):
 
84
  overlays = [
85
  f"1+\\sin({c})",
86
  f"1+\\cos({c})",
87
  f"1+\\exp({c})",
88
+ f"1+\\log\\!\\big(1+{c}^2\\big)"
89
  ]
90
+ return random.choice(overlays)
91
 
92
  def mutate_metric(g, epoch, intensity="medium"):
 
 
93
  idx_pairs = [(0,0),(1,1),(2,2),(3,3),
94
  (0,1),(0,2),(0,3),(1,2),(1,3),(2,3)]
95
  k = 2 if intensity=="low" else (4 if intensity=="medium" else 6)
96
  chosen = random.sample(idx_pairs, min(k, len(idx_pairs)))
 
97
  for (i,j) in chosen:
98
+ ci, cj = coords[i], coords[j]
 
99
  if i == j:
100
+ g[i][j] = random_overlay(ci)
101
  else:
102
  mix = [
103
+ f"\\cos({ci})+\\log\\!\\big(1+{cj}^2\\big)",
104
+ f"\\exp({cj})-\\cos({ci})",
105
+ f"\\sin({ci}{cj})",
106
+ f"\\cos({ci})+\\log\\!\\big(1+{cj}^2\\big)"
107
  ]
108
+ g[i][j] = random.choice(mix)
109
  g[j][i] = g[i][j]
110
  return g
111
 
112
+ def christoffel_snippet():
113
+ return (
114
+ "\\Gamma^{1}_{\\;12} = \\tfrac{1}{2}\\, g^{11}\\!\\left(\\partial_{u} g_{22} + \\partial_{v} g_{12} - \\partial_{v} g_{11}\\right),\\quad"
115
+ "\\Gamma^{2}_{\\;34} = \\tfrac{1}{2}\\, g^{22}\\!\\left(\\partial_{v} g_{44} + \\partial_{t} g_{24} - \\partial_{w} g_{23}\\right),\\quad"
116
+ "\\Gamma^{4}_{\\;13} = \\tfrac{1}{2}\\, g^{44}\\!\\left(\\partial_{u} g_{33} + \\partial_{w} g_{13} - \\partial_{u} g_{34}\\right)"
117
+ )
 
 
 
 
118
 
119
  def scalar_curvature_hint(g):
 
120
  d = det_approx(g)
 
121
  return f"\\mathcal{{R}} \\approx {d:.3f}"
122
 
123
  def run_manifold(signature_choice="Euclidean (+,+,+,+)", intensity="medium", epochs=20):
 
124
  if signature_choice.startswith("Euclidean"):
125
+ g = [["1","0","0","0"],
126
+ ["0","1","0","0"],
127
+ ["0","0","1","0"],
128
+ ["0","0","0","1"]]
129
+ else:
130
+ g = [["1","0","0","0"],
131
+ ["0","1","0","0"],
132
+ ["0","0","1","0"],
133
+ ["0","0","0","-1"]]
 
 
 
 
134
 
135
  ledger = []
136
  for epoch in range(1, epochs+1):
137
  g = mutate_metric(g, epoch, intensity=intensity)
138
+ g_latex = pretty_metric(g)
139
  detg = det_approx(g)
140
  sig = signature_hint(g)
141
+ Gamma = christoffel_snippet()
142
  Rscalar = scalar_curvature_hint(g)
143
+ vol = "dV = \\sqrt{\\det g}\\, du\\, dv\\, dw\\, dt"
 
 
144
  seal = sha_seal(g_latex + Rscalar)
145
 
146
  entry = (
147
  f"## Manifold Epoch {epoch}\n\n"
148
+ f"**Coordinates:** $\\mathbf{{X}}=(u,v,w,t)$\n\n"
149
  f"**Metric** $g_{{ij}}$:\n\n"
150
+ f"$$ {g_latex} $$\n\n"
151
+ f"**Representative connections:**\n\n"
152
+ f"$$ {Gamma} $$\n\n"
153
  f"**Invariants:** \n"
154
  f"- **Determinant:** $\\det g \\approx {detg:.3f}$ \n"
155
  f"- **Signature hint:** `{sig}` \n"
156
+ f"- **Scalar curvature hint:** $$ {Rscalar} $$ \n"
157
+ f"- **Volume element:** $$ {vol} $$\n\n"
158
  f"**Immortality Glyph:** `{seal}`\n\n"
159
  f"---\n"
160
  )
 
163
  return "\n".join(ledger)
164
 
165
  # =========================
166
+ # App UI
167
  # =========================
168
  custom_theme = gr.themes.Base(
169
  primary_hue="cyan",
 
173
 
174
  with gr.Blocks(theme=custom_theme) as demo:
175
  gr.Markdown("# 🌌 Resonance Atlas — The Living Codex")
176
+ gr.Markdown("Choose: 50‑epoch Codex Scrolls or the 4D Manifold Forge (20‑epoch lineage).")
177
 
178
  with gr.Tab("Codex Scrolls"):
179
  gr.Markdown("### 🔢 Live 50 Epoch Run")
 
181
  output = gr.Markdown()
182
  run_button.click(fn=run_epochs, inputs=None, outputs=output)
183
 
 
 
 
 
 
 
 
184
  with gr.Tab("4D Manifold Forge"):
185
+ gr.Markdown("### 🧭 Forge a 4D manifold with evolving metric and invariants")
186
+ signature = gr.Radio(choices=["Euclidean (+,+,+,+)", "Pseudo-Riemannian (+,+,+,-)"],
187
+ value="Euclidean (+,+,+,+)", label="Signature")
188
  intensity = gr.Radio(choices=["low", "medium", "high"], value="medium", label="Overlay intensity")
189
+ epochs = gr.Slider(5, 30, value=20, step=1, label="Epochs")
190
+ run_manifold_btn = gr.Button("Forge 4D Manifold (Lineage)")
191
  manifold_out = gr.Markdown()
192
  run_manifold_btn.click(fn=run_manifold, inputs=[signature, intensity, epochs], outputs=manifold_out)
193