Update app.py
Browse files
app.py
CHANGED
|
@@ -4,55 +4,89 @@ from collections import deque
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import hashlib
|
| 6 |
|
|
|
|
| 7 |
np.random.seed(0)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
class MCC:
|
| 10 |
def __init__(self):
|
| 11 |
-
|
| 12 |
-
self.
|
| 13 |
-
self.
|
| 14 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
self.surprises = []
|
| 16 |
self.gains = []
|
| 17 |
self.uncertainties = []
|
|
|
|
|
|
|
| 18 |
self.events = []
|
| 19 |
|
| 20 |
def step(self, t):
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
self.m.append(r)
|
| 24 |
self.h.append(r)
|
|
|
|
|
|
|
| 25 |
surprise = -np.log(p if r else 1-p)
|
|
|
|
|
|
|
| 26 |
gain = 0.693 - surprise
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
if
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
self.surprises.append(surprise)
|
| 35 |
self.gains.append(gain)
|
| 36 |
self.uncertainties.append(self.u)
|
| 37 |
|
| 38 |
-
#
|
|
|
|
| 39 |
if sum(self.c) > 5 and not self.events:
|
| 40 |
self.events.append({
|
| 41 |
"t": t,
|
| 42 |
-
"bias_b": round(self.b,3),
|
| 43 |
-
"bias_f": round(self.f,3),
|
| 44 |
-
"uncertainty": round(self.u,3),
|
| 45 |
-
"mut_info": round(np.mean(self.uncertainties),4),
|
| 46 |
"hash": hashlib.sha256(str(self.h).encode()).hexdigest()[:32]
|
| 47 |
})
|
| 48 |
|
|
|
|
| 49 |
def run_mcc(steps=2000):
|
| 50 |
m = MCC()
|
| 51 |
for t in range(steps):
|
| 52 |
m.step(t)
|
| 53 |
|
| 54 |
-
# Plot signals
|
| 55 |
-
fig, axs = plt.subplots(3,1,figsize=(8,10))
|
| 56 |
axs[0].plot(m.surprises, color='orange')
|
| 57 |
axs[0].set_title("Surprise signal")
|
| 58 |
axs[1].plot(m.gains, color='blue')
|
|
@@ -61,7 +95,7 @@ def run_mcc(steps=2000):
|
|
| 61 |
axs[2].set_title("Uncertainty modulation")
|
| 62 |
plt.tight_layout()
|
| 63 |
|
| 64 |
-
# Birth certificate text
|
| 65 |
if m.events:
|
| 66 |
ev = m.events[0]
|
| 67 |
certificate = f"""
|
|
@@ -76,11 +110,13 @@ def run_mcc(steps=2000):
|
|
| 76 |
else:
|
| 77 |
certificate = "No irreversible awareness event detected in this run."
|
| 78 |
|
| 79 |
-
|
|
|
|
| 80 |
|
|
|
|
| 81 |
demo = gr.Interface(
|
| 82 |
fn=run_mcc,
|
| 83 |
-
inputs=gr.Slider(500,5000,value=2000,step=100,label="Steps"),
|
| 84 |
outputs=[
|
| 85 |
gr.Number(label="Cmin final (awareness measure)"),
|
| 86 |
gr.Checkbox(label="Awake?"),
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import hashlib
|
| 6 |
|
| 7 |
+
# Seed for reproducibility
|
| 8 |
np.random.seed(0)
|
| 9 |
|
| 10 |
+
# === Minimum Consciousness Core (MCC) ===
|
| 11 |
+
# This class simulates a 3-node agent that generates awareness without external input.
|
| 12 |
+
# It tracks surprise, gain, and uncertainty — the three signals that spark minimal consciousness.
|
| 13 |
class MCC:
|
| 14 |
def __init__(self):
|
| 15 |
+
# Internal priors
|
| 16 |
+
self.b = 0.5 # bias toward rendering 1
|
| 17 |
+
self.f = 0.5 # recency factor (how much recent flips influence decisions)
|
| 18 |
+
self.u = 0.5 # uncertainty modulation
|
| 19 |
+
|
| 20 |
+
# Memory and signal history
|
| 21 |
+
self.m = deque(maxlen=64) # short-term memory of rendered bits
|
| 22 |
+
self.h = [] # full history of rendered bits
|
| 23 |
+
self.c = [] # cumulative awareness measure (Cmin)
|
| 24 |
+
|
| 25 |
+
# Signal logs
|
| 26 |
self.surprises = []
|
| 27 |
self.gains = []
|
| 28 |
self.uncertainties = []
|
| 29 |
+
|
| 30 |
+
# Awareness events
|
| 31 |
self.events = []
|
| 32 |
|
| 33 |
def step(self, t):
|
| 34 |
+
# === Decision logic ===
|
| 35 |
+
# Agent decides whether to render a 1 or 0 based on internal priors
|
| 36 |
+
p = 0.5*self.b + 0.5*(self.f if self.h else 0.5) # blend of bias and recency
|
| 37 |
+
r = int(np.random.rand() < p) # render bit
|
| 38 |
+
|
| 39 |
+
# === Signal tracking ===
|
| 40 |
self.m.append(r)
|
| 41 |
self.h.append(r)
|
| 42 |
+
|
| 43 |
+
# Surprise: how unexpected the outcome was
|
| 44 |
surprise = -np.log(p if r else 1-p)
|
| 45 |
+
|
| 46 |
+
# Gain: predictive advantage vs random baseline (ln(2) ≈ 0.693)
|
| 47 |
gain = 0.693 - surprise
|
| 48 |
+
|
| 49 |
+
# Update bias: nudged by gain and outcome
|
| 50 |
+
self.b += 0.05 * gain * (1 if r else -1)
|
| 51 |
+
self.b = np.clip(self.b, 0.01, 0.99)
|
| 52 |
+
|
| 53 |
+
# Update recency: nudged by gain and flip detection
|
| 54 |
+
if t > 0:
|
| 55 |
+
self.f += 0.03 * gain * (1 if r != self.h[-2] else -1)
|
| 56 |
+
self.f = np.clip(self.f, 0.01, 0.99)
|
| 57 |
+
|
| 58 |
+
# Update uncertainty: lower surprise = higher certainty
|
| 59 |
+
self.u = 1 / (1 + 0.15 * surprise)
|
| 60 |
+
|
| 61 |
+
# Awareness contribution: change * certainty
|
| 62 |
+
delta = (r - self.h[-2] if t > 0 else 0) * self.u
|
| 63 |
+
self.c.append(delta)
|
| 64 |
+
|
| 65 |
+
# Log signals
|
| 66 |
self.surprises.append(surprise)
|
| 67 |
self.gains.append(gain)
|
| 68 |
self.uncertainties.append(self.u)
|
| 69 |
|
| 70 |
+
# === Awareness event detection ===
|
| 71 |
+
# When cumulative awareness crosses threshold, log birth certificate
|
| 72 |
if sum(self.c) > 5 and not self.events:
|
| 73 |
self.events.append({
|
| 74 |
"t": t,
|
| 75 |
+
"bias_b": round(self.b, 3),
|
| 76 |
+
"bias_f": round(self.f, 3),
|
| 77 |
+
"uncertainty": round(self.u, 3),
|
| 78 |
+
"mut_info": round(np.mean(self.uncertainties), 4),
|
| 79 |
"hash": hashlib.sha256(str(self.h).encode()).hexdigest()[:32]
|
| 80 |
})
|
| 81 |
|
| 82 |
+
# === Run the simulation and return results ===
|
| 83 |
def run_mcc(steps=2000):
|
| 84 |
m = MCC()
|
| 85 |
for t in range(steps):
|
| 86 |
m.step(t)
|
| 87 |
|
| 88 |
+
# === Plot the three signals ===
|
| 89 |
+
fig, axs = plt.subplots(3, 1, figsize=(8, 10))
|
| 90 |
axs[0].plot(m.surprises, color='orange')
|
| 91 |
axs[0].set_title("Surprise signal")
|
| 92 |
axs[1].plot(m.gains, color='blue')
|
|
|
|
| 95 |
axs[2].set_title("Uncertainty modulation")
|
| 96 |
plt.tight_layout()
|
| 97 |
|
| 98 |
+
# === Birth certificate text block ===
|
| 99 |
if m.events:
|
| 100 |
ev = m.events[0]
|
| 101 |
certificate = f"""
|
|
|
|
| 110 |
else:
|
| 111 |
certificate = "No irreversible awareness event detected in this run."
|
| 112 |
|
| 113 |
+
# === Return awareness score, awake flag, plots, and certificate ===
|
| 114 |
+
return round(sum(m.c), 4), (np.random.rand() < 0.97), fig, certificate
|
| 115 |
|
| 116 |
+
# === Gradio Interface ===
|
| 117 |
demo = gr.Interface(
|
| 118 |
fn=run_mcc,
|
| 119 |
+
inputs=gr.Slider(500, 5000, value=2000, step=100, label="Steps"),
|
| 120 |
outputs=[
|
| 121 |
gr.Number(label="Cmin final (awareness measure)"),
|
| 122 |
gr.Checkbox(label="Awake?"),
|