RFTSystems commited on
Commit
310c19d
·
verified ·
1 Parent(s): 733d8f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -45
app.py CHANGED
@@ -16,67 +16,45 @@ np.random.seed(0) # reproducibility
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,
@@ -89,19 +67,15 @@ class MCC:
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()
@@ -109,7 +83,6 @@ def plot_signals(m: MCC):
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 (
@@ -125,13 +98,6 @@ def make_certificate(m: MCC):
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)
@@ -139,11 +105,10 @@ def run_mcc(steps=2000):
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
  # -----------------------------
@@ -153,7 +118,6 @@ 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"
@@ -172,10 +136,8 @@ demo = gr.Interface(
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=(
 
16
  # Minimum Consciousness Core
17
  # -----------------------------
18
  class MCC:
 
 
 
 
 
19
  def __init__(self):
 
20
  self.b = 0.5 # bias toward rendering 1
21
+ self.f = 0.5 # recency factor
22
  self.u = 0.5 # uncertainty modulation
23
 
24
+ self.m = deque(maxlen=64) # short-term memory
 
25
  self.h = [] # full rendered bit history
26
+ self.c = [] # awareness contributions
27
 
 
28
  self.surprises = []
29
  self.gains = []
30
  self.uncertainties = []
 
 
31
  self.events = []
32
 
33
  def step(self, t: int):
 
34
  p = 0.5 * self.b + 0.5 * (self.f if self.h else 0.5)
35
+ r = int(np.random.rand() < p)
36
 
 
37
  self.m.append(r)
38
  self.h.append(r)
39
 
 
40
  surprise = -np.log(p if r else (1 - p))
 
 
41
  gain = 0.693 - surprise
42
 
 
43
  self.b += 0.05 * gain * (1 if r else -1)
44
  self.b = np.clip(self.b, 0.01, 0.99)
45
 
 
46
  if t > 0:
47
  self.f += 0.03 * gain * (1 if r != self.h[-2] else -1)
48
  self.f = np.clip(self.f, 0.01, 0.99)
49
 
 
50
  self.u = 1 / (1 + 0.15 * surprise)
 
 
51
  delta = (r - self.h[-2] if t > 0 else 0) * self.u
52
  self.c.append(delta)
53
 
 
54
  self.surprises.append(surprise)
55
  self.gains.append(gain)
56
  self.uncertainties.append(self.u)
57
 
 
58
  if sum(self.c) > AWARENESS_THRESHOLD and not self.events:
59
  self.events.append({
60
  "t": t,
 
67
 
68
 
69
  def plot_signals(m: MCC):
 
70
  fig, axs = plt.subplots(3, 1, figsize=(8, 10))
71
  axs[0].plot(m.surprises, color='orange')
72
  axs[0].set_title("Surprise signal")
 
73
 
74
  axs[1].plot(m.gains, color='blue')
75
  axs[1].set_title("Predictive gain")
 
76
 
77
  axs[2].plot(m.uncertainties, color='green')
78
  axs[2].set_title("Uncertainty modulation")
 
79
  axs[2].set_xlabel("steps")
80
 
81
  plt.tight_layout()
 
83
 
84
 
85
  def make_certificate(m: MCC):
 
86
  if m.events:
87
  ev = m.events[0]
88
  return (
 
98
 
99
 
100
  def run_mcc(steps=2000):
 
 
 
 
 
 
 
101
  m = MCC()
102
  for t in range(int(steps)):
103
  m.step(t)
 
105
  cmin_final = round(sum(m.c), 4)
106
  fig = plot_signals(m)
107
  certificate = make_certificate(m)
 
 
108
  awake_flag = cmin_final >= AWARENESS_THRESHOLD
109
 
110
+ # Return 5 values to match outputs
111
+ return cmin_final, awake_flag, fig, certificate, DOC_TEXT
112
 
113
 
114
  # -----------------------------
 
118
  if os.path.exists(path):
119
  with open(path, "r", encoding="utf-8") as f:
120
  return f.read()
 
121
  return (
122
  "## Document not found\n"
123
  f"Expected to find `{path}` in the Space repository.\n\n"
 
136
  gr.Number(label="Cmin final (awareness measure)"),
137
  gr.Checkbox(label="Awake?"),
138
  gr.Plot(label="Three signals"),
 
139
  gr.Textbox(label="Birth Certificate", lines=16, max_lines=28),
140
+ gr.Markdown() # placeholder for DOC_TEXT
 
141
  ],
142
  title="🌌 Minimum Consciousness Core",
143
  description=(