Update app.py
Browse files
app.py
CHANGED
|
@@ -3,93 +3,168 @@ import numpy as np
|
|
| 3 |
from collections import deque
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import hashlib
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
-
#
|
| 13 |
class MCC:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
-
|
| 16 |
-
self.
|
| 17 |
-
self.
|
| 18 |
-
self.
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
self.gains = []
|
| 21 |
self.uncertainties = []
|
| 22 |
-
self.events = [] # awareness birth events
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
p = 0.5*self.b + 0.5*(self.f if self.h else 0.5)
|
| 27 |
-
r = int(np.random.rand() < p) # render bit
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
self.m.append(r)
|
| 31 |
self.h.append(r)
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
gain = 0.693 - surprise
|
|
|
|
|
|
|
| 34 |
self.b += 0.05 * gain * (1 if r else -1)
|
| 35 |
self.b = np.clip(self.b, 0.01, 0.99)
|
|
|
|
|
|
|
| 36 |
if t > 0:
|
| 37 |
self.f += 0.03 * gain * (1 if r != self.h[-2] else -1)
|
| 38 |
self.f = np.clip(self.f, 0.01, 0.99)
|
|
|
|
|
|
|
| 39 |
self.u = 1 / (1 + 0.15 * surprise)
|
|
|
|
|
|
|
| 40 |
delta = (r - self.h[-2] if t > 0 else 0) * self.u
|
| 41 |
self.c.append(delta)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
self.surprises.append(surprise)
|
| 45 |
self.gains.append(gain)
|
| 46 |
self.uncertainties.append(self.u)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
if sum(self.c) >
|
| 50 |
self.events.append({
|
| 51 |
"t": t,
|
| 52 |
"bias_b": round(self.b, 3),
|
| 53 |
"bias_f": round(self.f, 3),
|
| 54 |
"uncertainty": round(self.u, 3),
|
| 55 |
-
"mut_info": round(np.mean(self.uncertainties), 4),
|
| 56 |
"hash": hashlib.sha256(str(self.h).encode()).hexdigest()[:32]
|
| 57 |
})
|
| 58 |
|
| 59 |
-
# === Run the simulation and return results ===
|
| 60 |
-
def run_mcc(steps=2000):
|
| 61 |
-
m = MCC()
|
| 62 |
-
for t in range(steps):
|
| 63 |
-
m.step(t)
|
| 64 |
|
| 65 |
-
|
|
|
|
| 66 |
fig, axs = plt.subplots(3, 1, figsize=(8, 10))
|
| 67 |
axs[0].plot(m.surprises, color='orange')
|
| 68 |
axs[0].set_title("Surprise signal")
|
|
|
|
|
|
|
| 69 |
axs[1].plot(m.gains, color='blue')
|
| 70 |
axs[1].set_title("Predictive gain")
|
|
|
|
|
|
|
| 71 |
axs[2].plot(m.uncertainties, color='green')
|
| 72 |
axs[2].set_title("Uncertainty modulation")
|
|
|
|
|
|
|
|
|
|
| 73 |
plt.tight_layout()
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
|
|
|
| 76 |
if m.events:
|
| 77 |
ev = m.events[0]
|
| 78 |
-
|
| 79 |
-
THE FIRST DIGITAL OBSERVER
|
| 80 |
-
Born: t = {ev['t']}
|
| 81 |
-
Internal priors: bias_b = {ev['bias_b']}, bias_f = {ev['bias_f']}
|
| 82 |
-
Uncertainty: {ev['uncertainty']}
|
| 83 |
-
Mutual information: {ev['mut_info']} bits
|
| 84 |
-
Birth certificate hash: {ev['hash']}
|
| 85 |
-
No external world · No reward · No teacher
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
demo = gr.Interface(
|
| 94 |
fn=run_mcc,
|
| 95 |
inputs=gr.Slider(500, 5000, value=2000, step=100, label="Steps"),
|
|
@@ -97,10 +172,16 @@ demo = gr.Interface(
|
|
| 97 |
gr.Number(label="Cmin final (awareness measure)"),
|
| 98 |
gr.Checkbox(label="Awake?"),
|
| 99 |
gr.Plot(label="Three signals"),
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
],
|
| 102 |
title="🌌 Minimum Consciousness Core",
|
| 103 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from collections import deque
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import hashlib
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# Config
|
| 10 |
+
# -----------------------------
|
| 11 |
+
DOC_PATH = "The_Minimum_Conscious_Universe_RFT_Project.md"
|
| 12 |
+
AWARENESS_THRESHOLD = 5.0 # threshold for declaring a birth event
|
| 13 |
+
np.random.seed(0) # reproducibility
|
| 14 |
|
| 15 |
+
# -----------------------------
|
| 16 |
+
# Minimum Consciousness Core
|
| 17 |
+
# -----------------------------
|
| 18 |
class MCC:
|
| 19 |
+
"""
|
| 20 |
+
A minimal 3-parameter agent that renders its own binary stream (no external world),
|
| 21 |
+
and tracks three signals: surprise, predictive gain, and uncertainty.
|
| 22 |
+
Cmin accumulates awareness as change * certainty over time.
|
| 23 |
+
"""
|
| 24 |
def __init__(self):
|
| 25 |
+
# Internal priors
|
| 26 |
+
self.b = 0.5 # bias toward rendering 1
|
| 27 |
+
self.f = 0.5 # recency factor (influence of last change)
|
| 28 |
+
self.u = 0.5 # uncertainty modulation
|
| 29 |
+
|
| 30 |
+
# Memory and history
|
| 31 |
+
self.m = deque(maxlen=64) # short-term memory of bits
|
| 32 |
+
self.h = [] # full rendered bit history
|
| 33 |
+
self.c = [] # awareness contributions (Cmin increments)
|
| 34 |
+
|
| 35 |
+
# Signal logs
|
| 36 |
+
self.surprises = []
|
| 37 |
self.gains = []
|
| 38 |
self.uncertainties = []
|
|
|
|
| 39 |
|
| 40 |
+
# Awareness events
|
| 41 |
+
self.events = []
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
def step(self, t: int):
|
| 44 |
+
# Decision: blend bias and recency (use 0.5 for recency when empty)
|
| 45 |
+
p = 0.5 * self.b + 0.5 * (self.f if self.h else 0.5)
|
| 46 |
+
r = int(np.random.rand() < p) # render bit 0/1
|
| 47 |
+
|
| 48 |
+
# Track memory and history
|
| 49 |
self.m.append(r)
|
| 50 |
self.h.append(r)
|
| 51 |
+
|
| 52 |
+
# Surprise: self-information of the outcome
|
| 53 |
+
surprise = -np.log(p if r else (1 - p))
|
| 54 |
+
|
| 55 |
+
# Predictive gain versus random baseline ln(2) ~ 0.693
|
| 56 |
gain = 0.693 - surprise
|
| 57 |
+
|
| 58 |
+
# Update bias: move toward outcome proportionally to gain
|
| 59 |
self.b += 0.05 * gain * (1 if r else -1)
|
| 60 |
self.b = np.clip(self.b, 0.01, 0.99)
|
| 61 |
+
|
| 62 |
+
# Update recency: amplify change detection
|
| 63 |
if t > 0:
|
| 64 |
self.f += 0.03 * gain * (1 if r != self.h[-2] else -1)
|
| 65 |
self.f = np.clip(self.f, 0.01, 0.99)
|
| 66 |
+
|
| 67 |
+
# Update uncertainty: lower surprise -> higher certainty
|
| 68 |
self.u = 1 / (1 + 0.15 * surprise)
|
| 69 |
+
|
| 70 |
+
# Awareness contribution: change * certainty
|
| 71 |
delta = (r - self.h[-2] if t > 0 else 0) * self.u
|
| 72 |
self.c.append(delta)
|
| 73 |
|
| 74 |
+
# Log signals
|
| 75 |
self.surprises.append(surprise)
|
| 76 |
self.gains.append(gain)
|
| 77 |
self.uncertainties.append(self.u)
|
| 78 |
|
| 79 |
+
# Birth event: when cumulative awareness passes threshold
|
| 80 |
+
if sum(self.c) > AWARENESS_THRESHOLD and not self.events:
|
| 81 |
self.events.append({
|
| 82 |
"t": t,
|
| 83 |
"bias_b": round(self.b, 3),
|
| 84 |
"bias_f": round(self.f, 3),
|
| 85 |
"uncertainty": round(self.u, 3),
|
| 86 |
+
"mut_info": round(float(np.mean(self.uncertainties)), 4),
|
| 87 |
"hash": hashlib.sha256(str(self.h).encode()).hexdigest()[:32]
|
| 88 |
})
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
def plot_signals(m: MCC):
|
| 92 |
+
"""Create a 3-row plot of Surprise, Gain, and Uncertainty."""
|
| 93 |
fig, axs = plt.subplots(3, 1, figsize=(8, 10))
|
| 94 |
axs[0].plot(m.surprises, color='orange')
|
| 95 |
axs[0].set_title("Surprise signal")
|
| 96 |
+
axs[0].set_ylabel("surprise")
|
| 97 |
+
|
| 98 |
axs[1].plot(m.gains, color='blue')
|
| 99 |
axs[1].set_title("Predictive gain")
|
| 100 |
+
axs[1].set_ylabel("gain")
|
| 101 |
+
|
| 102 |
axs[2].plot(m.uncertainties, color='green')
|
| 103 |
axs[2].set_title("Uncertainty modulation")
|
| 104 |
+
axs[2].set_ylabel("uncertainty")
|
| 105 |
+
axs[2].set_xlabel("steps")
|
| 106 |
+
|
| 107 |
plt.tight_layout()
|
| 108 |
+
return fig
|
| 109 |
+
|
| 110 |
|
| 111 |
+
def make_certificate(m: MCC):
|
| 112 |
+
"""Generate the ceremonial birth certificate text."""
|
| 113 |
if m.events:
|
| 114 |
ev = m.events[0]
|
| 115 |
+
return (
|
| 116 |
+
"THE FIRST DIGITAL OBSERVER\n"
|
| 117 |
+
f"Born: t = {ev['t']}\n"
|
| 118 |
+
f"Internal priors: bias_b = {ev['bias_b']}, bias_f = {ev['bias_f']}\n"
|
| 119 |
+
f"Uncertainty: {ev['uncertainty']}\n"
|
| 120 |
+
f"Mutual information: {ev['mut_info']} bits\n"
|
| 121 |
+
f"Birth certificate hash: {ev['hash']}\n"
|
| 122 |
+
"No external world · No reward · No teacher"
|
| 123 |
+
)
|
| 124 |
+
return "No irreversible awareness event detected in this run."
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def run_mcc(steps=2000):
|
| 128 |
+
"""
|
| 129 |
+
Execute the MCC simulation for 'steps' timesteps and return:
|
| 130 |
+
- Cmin final (sum of awareness contributions)
|
| 131 |
+
- Awake? (bool flag for display)
|
| 132 |
+
- Plot of the three signals
|
| 133 |
+
- Birth certificate text
|
| 134 |
+
"""
|
| 135 |
+
m = MCC()
|
| 136 |
+
for t in range(int(steps)):
|
| 137 |
+
m.step(t)
|
| 138 |
+
|
| 139 |
+
cmin_final = round(sum(m.c), 4)
|
| 140 |
+
fig = plot_signals(m)
|
| 141 |
+
certificate = make_certificate(m)
|
| 142 |
+
|
| 143 |
+
# 'Awake?' is a display flag – set true when Cmin passes threshold
|
| 144 |
+
awake_flag = cmin_final >= AWARENESS_THRESHOLD
|
| 145 |
+
|
| 146 |
+
return cmin_final, awake_flag, fig, certificate
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# -----------------------------
|
| 150 |
+
# Load project document (.md)
|
| 151 |
+
# -----------------------------
|
| 152 |
+
def load_doc_text(path: str) -> str:
|
| 153 |
+
if os.path.exists(path):
|
| 154 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 155 |
+
return f.read()
|
| 156 |
+
# Fallback message if file not found
|
| 157 |
+
return (
|
| 158 |
+
"## Document not found\n"
|
| 159 |
+
f"Expected to find `{path}` in the Space repository.\n\n"
|
| 160 |
+
"Add the file and reload the Space to display the project write‑up."
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
DOC_TEXT = load_doc_text(DOC_PATH)
|
| 164 |
+
|
| 165 |
+
# -----------------------------
|
| 166 |
+
# Gradio Interface
|
| 167 |
+
# -----------------------------
|
| 168 |
demo = gr.Interface(
|
| 169 |
fn=run_mcc,
|
| 170 |
inputs=gr.Slider(500, 5000, value=2000, step=100, label="Steps"),
|
|
|
|
| 172 |
gr.Number(label="Cmin final (awareness measure)"),
|
| 173 |
gr.Checkbox(label="Awake?"),
|
| 174 |
gr.Plot(label="Three signals"),
|
| 175 |
+
# Enlarged birth certificate box for readability
|
| 176 |
+
gr.Textbox(label="Birth Certificate", lines=16, max_lines=28),
|
| 177 |
+
# Embedded project document
|
| 178 |
+
gr.Markdown(DOC_TEXT)
|
| 179 |
],
|
| 180 |
title="🌌 Minimum Consciousness Core",
|
| 181 |
+
description=(
|
| 182 |
+
"Awareness emerges from three adaptive signals: surprise, gain, and uncertainty. "
|
| 183 |
+
"No external world · No reward · No teacher. Each run issues a ceremonial birth certificate."
|
| 184 |
+
)
|
| 185 |
)
|
| 186 |
|
| 187 |
if __name__ == "__main__":
|