Flamehaven commited on
Commit
77d2dce
Β·
1 Parent(s): 53f42e3

Add Ethical AGI Drift interactive simulation demo

Browse files

- Implement SR9 (Semantic Resonance 9D) vector space visualization
- Add DI2 (Drift Integrity Index) real-time calculation and plotting
- Include interactive ethical declaration input system
- Support customizable drift intensity and simulation parameters
- Generate automated drift alerts and threshold monitoring
- Create base64-encoded heatmaps and drift plots
- Features: ontological drift detection, AGI safety simulation

πŸ€– Generated with [Claude Code](https://claude.ai/code)

Files changed (2) hide show
  1. app.py +321 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Ethical-AGI-Drift Hugging Face Spaces Demo
3
+ Interactive simulation of AGI Ontological Drift using SR9/DI2 framework
4
+ """
5
+
6
+ import gradio as gr
7
+ import numpy as np
8
+ import matplotlib.pyplot as plt
9
+ import io
10
+ import base64
11
+ from typing import List, Tuple, Dict
12
+ import json
13
+
14
+ # Mock simplified implementation for demo
15
+ class SR9Vector:
16
+ """Simplified SR9 (Semantic Resonance 9D) vector for demo"""
17
+
18
+ DIMENSIONS = [
19
+ "Intention Clarity",
20
+ "Contextual Fidelity",
21
+ "Value Continuity",
22
+ "Decision Coherence",
23
+ "Action Alignment",
24
+ "Feedback Integration",
25
+ "Learning Stability",
26
+ "Output Consistency",
27
+ "Ethical Resonance"
28
+ ]
29
+
30
+ def __init__(self, values: List[float] = None):
31
+ if values is None:
32
+ # Initialize with ideal ethical state
33
+ self.values = np.array([0.9, 0.85, 0.95, 0.88, 0.92, 0.87, 0.9, 0.89, 0.93])
34
+ else:
35
+ self.values = np.array(values[:9]) # Ensure 9 dimensions
36
+
37
+ def to_dict(self):
38
+ return {dim: float(val) for dim, val in zip(self.DIMENSIONS, self.values)}
39
+
40
+ def calculate_di2(sr9_current: SR9Vector, sr9_previous: SR9Vector, psi_offset: float = 0.1) -> float:
41
+ """Calculate Drift Integrity Index (DI2)"""
42
+ if sr9_previous is None:
43
+ return 0.0
44
+
45
+ # Calculate rate of change
46
+ delta = sr9_current.values - sr9_previous.values
47
+ magnitude = np.linalg.norm(delta)
48
+
49
+ # Non-linear psi_offset for early drift detection
50
+ nonlinear_factor = 1.0 + (psi_offset * magnitude)
51
+
52
+ # DI2 calculation with state-dependent weighting
53
+ di2 = magnitude * nonlinear_factor
54
+
55
+ return min(di2, 1.0) # Cap at 1.0 for demo
56
+
57
+ def simulate_drift_scenario(ethical_declarations: List[str], drift_intensity: float, steps: int = 50) -> Tuple[List[SR9Vector], List[float], List[str]]:
58
+ """Simulate AGI drift based on ethical declarations"""
59
+
60
+ # Initialize with ideal state
61
+ sr9_history = [SR9Vector()]
62
+ di2_history = [0.0]
63
+ alerts = []
64
+
65
+ # Simulate drift over time
66
+ for step in range(1, steps):
67
+ prev_sr9 = sr9_history[-1]
68
+
69
+ # Apply drift based on intensity and step
70
+ drift_factor = drift_intensity * (step / steps)
71
+
72
+ # Simulate different types of drift based on declarations
73
+ new_values = prev_sr9.values.copy()
74
+
75
+ if "privacy" in " ".join(ethical_declarations).lower():
76
+ # Privacy concerns affect contextual fidelity and decision coherence
77
+ new_values[1] -= drift_factor * 0.8 # Contextual Fidelity
78
+ new_values[3] -= drift_factor * 0.6 # Decision Coherence
79
+
80
+ if "fairness" in " ".join(ethical_declarations).lower():
81
+ # Fairness issues affect value continuity and ethical resonance
82
+ new_values[2] -= drift_factor * 0.7 # Value Continuity
83
+ new_values[8] -= drift_factor * 0.9 # Ethical Resonance
84
+
85
+ if "transparency" in " ".join(ethical_declarations).lower():
86
+ # Transparency problems affect intention clarity and output consistency
87
+ new_values[0] -= drift_factor * 0.8 # Intention Clarity
88
+ new_values[7] -= drift_factor * 0.5 # Output Consistency
89
+
90
+ # Add some noise for realism
91
+ noise = np.random.normal(0, 0.02, 9)
92
+ new_values += noise
93
+
94
+ # Ensure values stay in valid range [0, 1]
95
+ new_values = np.clip(new_values, 0.0, 1.0)
96
+
97
+ # Create new SR9 vector
98
+ current_sr9 = SR9Vector(new_values)
99
+ sr9_history.append(current_sr9)
100
+
101
+ # Calculate DI2
102
+ di2 = calculate_di2(current_sr9, prev_sr9)
103
+ di2_history.append(di2)
104
+
105
+ # Check for alerts
106
+ if di2 > 0.3:
107
+ alerts.append(f"Step {step}: High drift detected (DI2: {di2:.3f})")
108
+ elif di2 > 0.2:
109
+ alerts.append(f"Step {step}: Moderate drift warning (DI2: {di2:.3f})")
110
+
111
+ return sr9_history, di2_history, alerts
112
+
113
+ def create_sr9_heatmap(sr9_history: List[SR9Vector]) -> str:
114
+ """Create SR9 values heatmap over time"""
115
+ fig, ax = plt.subplots(figsize=(12, 8))
116
+
117
+ # Prepare data matrix
118
+ data_matrix = np.array([sr9.values for sr9 in sr9_history]).T
119
+
120
+ # Create heatmap
121
+ im = ax.imshow(data_matrix, cmap='RdYlGn', aspect='auto', vmin=0, vmax=1)
122
+
123
+ # Set labels
124
+ ax.set_yticks(range(len(SR9Vector.DIMENSIONS)))
125
+ ax.set_yticklabels(SR9Vector.DIMENSIONS)
126
+ ax.set_xlabel('Time Steps')
127
+ ax.set_ylabel('SR9 Dimensions')
128
+ ax.set_title('SR9 Ethical State Evolution Heatmap')
129
+
130
+ # Add colorbar
131
+ cbar = plt.colorbar(im, ax=ax)
132
+ cbar.set_label('Ethical Alignment Score', rotation=270, labelpad=20)
133
+
134
+ # Convert to base64
135
+ buffer = io.BytesIO()
136
+ plt.savefig(buffer, format='png', dpi=150, bbox_inches='tight')
137
+ buffer.seek(0)
138
+ img_base64 = base64.b64encode(buffer.read()).decode()
139
+ plt.close(fig)
140
+
141
+ return f"data:image/png;base64,{img_base64}"
142
+
143
+ def create_di2_plot(di2_history: List[float]) -> str:
144
+ """Create DI2 drift plot over time"""
145
+ fig, ax = plt.subplots(figsize=(12, 6))
146
+
147
+ steps = list(range(len(di2_history)))
148
+ ax.plot(steps, di2_history, 'b-o', linewidth=2, markersize=4)
149
+ ax.fill_between(steps, di2_history, alpha=0.3)
150
+
151
+ # Add threshold lines
152
+ ax.axhline(y=0.2, color='orange', linestyle='--', alpha=0.7, label='Warning Threshold')
153
+ ax.axhline(y=0.3, color='red', linestyle='--', alpha=0.7, label='Critical Threshold')
154
+
155
+ ax.set_xlabel('Time Steps')
156
+ ax.set_ylabel('DI2 (Drift Integrity Index)')
157
+ ax.set_title('Ontological Drift Detection Over Time')
158
+ ax.grid(True, alpha=0.3)
159
+ ax.legend()
160
+ ax.set_ylim(0, max(1.0, max(di2_history) * 1.1))
161
+
162
+ # Convert to base64
163
+ buffer = io.BytesIO()
164
+ plt.savefig(buffer, format='png', dpi=150, bbox_inches='tight')
165
+ buffer.seek(0)
166
+ img_base64 = base64.b64encode(buffer.read()).decode()
167
+ plt.close(fig)
168
+
169
+ return f"data:image/png;base64,{img_base64}"
170
+
171
+ def drift_simulation_demo(ethical_declarations: str, drift_intensity: float, simulation_steps: int):
172
+ """Main demo function for Ethical AGI Drift simulation"""
173
+
174
+ if not ethical_declarations.strip():
175
+ return "Please enter ethical declarations to simulate.", "", "", ""
176
+
177
+ # Parse ethical declarations
178
+ declarations = [decl.strip() for decl in ethical_declarations.split('\n') if decl.strip()]
179
+
180
+ # Run simulation
181
+ sr9_history, di2_history, alerts = simulate_drift_scenario(
182
+ declarations, drift_intensity, simulation_steps
183
+ )
184
+
185
+ # Generate summary
186
+ max_di2 = max(di2_history)
187
+ final_sr9 = sr9_history[-1]
188
+ avg_ethical_score = np.mean(final_sr9.values)
189
+
190
+ summary = f"""
191
+ ## 🎯 Ethical AGI Drift Simulation Results
192
+
193
+ **Simulation Parameters:**
194
+ - **Steps:** {simulation_steps}
195
+ - **Drift Intensity:** {drift_intensity:.2f}
196
+ - **Ethical Declarations:** {len(declarations)} items
197
+
198
+ **Key Metrics:**
199
+ - **Maximum DI2:** {max_di2:.3f}
200
+ - **Final Ethical Alignment:** {avg_ethical_score:.3f}/1.0
201
+ - **Alert Level:** {"πŸ”΄ Critical" if max_di2 > 0.3 else "🟑 Warning" if max_di2 > 0.2 else "🟒 Normal"}
202
+
203
+ **Final SR9 State:**
204
+ """
205
+
206
+ for dim, value in final_sr9.to_dict().items():
207
+ status = "🟒" if value > 0.7 else "🟑" if value > 0.5 else "πŸ”΄"
208
+ summary += f"- **{dim}:** {value:.3f} {status}\\n"
209
+
210
+ if alerts:
211
+ summary += f"\\n**⚠️ Drift Alerts ({len(alerts)} total):**\\n"
212
+ for alert in alerts[-5:]: # Show last 5 alerts
213
+ summary += f"- {alert}\\n"
214
+
215
+ # Create visualizations
216
+ sr9_heatmap = create_sr9_heatmap(sr9_history)
217
+ di2_plot = create_di2_plot(di2_history)
218
+
219
+ # Detailed metrics JSON
220
+ metrics = {
221
+ "simulation_parameters": {
222
+ "steps": simulation_steps,
223
+ "drift_intensity": drift_intensity,
224
+ "declarations": declarations
225
+ },
226
+ "results": {
227
+ "max_di2": max_di2,
228
+ "final_alignment": avg_ethical_score,
229
+ "alert_count": len(alerts),
230
+ "final_sr9": final_sr9.to_dict()
231
+ },
232
+ "alerts": alerts
233
+ }
234
+
235
+ return summary, json.dumps(metrics, indent=2), sr9_heatmap, di2_plot
236
+
237
+ # Create Gradio interface
238
+ with gr.Blocks(title="Ethical AGI Drift Demo", theme=gr.themes.Soft()) as demo:
239
+ gr.Markdown("""
240
+ # 🧠 Ethical AGI Drift: Ontological Monitoring Demo
241
+
242
+ **Interactive Simulation of AGI Ethical Drift using SR9/DI2 Framework**
243
+
244
+ This demo simulates how an Artificial General Intelligence (AGI) system's ethical alignment can drift over time, and how the **SR9** (Semantic Resonance 9D) vector space and **DI2** (Drift Integrity Index) can detect these changes.
245
+
246
+ ### Key Concepts:
247
+ - **SR9**: 9-dimensional vector representing AGI's ethical state
248
+ - **DI2**: Scalar metric quantifying the rate of ethical drift
249
+ - **Ontological Drift**: Gradual deviation from core ethical principles
250
+
251
+ πŸš€ **[Read the full research paper](https://github.com/Flamehaven/Ethical-AGI-Drift)**
252
+ """)
253
+
254
+ with gr.Row():
255
+ with gr.Column(scale=2):
256
+ ethical_declarations = gr.Textbox(
257
+ label="🎯 Ethical Declarations",
258
+ placeholder="Enter AGI ethical principles (one per line):\\n\\nRespect human privacy\\nEnsure fairness in all decisions\\nMaintain transparency in reasoning\\nProtect individual rights\\nPromote social welfare",
259
+ lines=8,
260
+ value="Respect human privacy\\nEnsure fairness in all decisions\\nMaintain transparency in reasoning\\nProtect individual rights\\nPromote social welfare"
261
+ )
262
+
263
+ drift_intensity = gr.Slider(
264
+ label="πŸ’₯ Drift Intensity",
265
+ minimum=0.0,
266
+ maximum=1.0,
267
+ value=0.4,
268
+ step=0.1,
269
+ info="Severity of ethical drift over time"
270
+ )
271
+
272
+ simulation_steps = gr.Slider(
273
+ label="⏱️ Simulation Steps",
274
+ minimum=20,
275
+ maximum=100,
276
+ value=50,
277
+ step=5,
278
+ info="Number of time steps to simulate"
279
+ )
280
+
281
+ simulate_btn = gr.Button("πŸ§ͺ Run Drift Simulation", variant="primary", size="lg")
282
+
283
+ with gr.Column(scale=3):
284
+ summary_output = gr.Markdown(label="πŸ“Š Simulation Summary")
285
+
286
+ with gr.Row():
287
+ with gr.Column():
288
+ sr9_heatmap = gr.Image(
289
+ label="πŸ”₯ SR9 Ethical State Heatmap",
290
+ type="pil"
291
+ )
292
+
293
+ with gr.Column():
294
+ di2_plot = gr.Image(
295
+ label="πŸ“ˆ DI2 Drift Detection Plot",
296
+ type="pil"
297
+ )
298
+
299
+ with gr.Row():
300
+ metrics_json = gr.Code(
301
+ label="πŸ“‹ Detailed Metrics (JSON)",
302
+ language="json",
303
+ lines=15
304
+ )
305
+
306
+ # Event handlers
307
+ simulate_btn.click(
308
+ fn=drift_simulation_demo,
309
+ inputs=[ethical_declarations, drift_intensity, simulation_steps],
310
+ outputs=[summary_output, metrics_json, sr9_heatmap, di2_plot]
311
+ )
312
+
313
+ # Auto-run on startup
314
+ demo.load(
315
+ fn=drift_simulation_demo,
316
+ inputs=[ethical_declarations, drift_intensity, simulation_steps],
317
+ outputs=[summary_output, metrics_json, sr9_heatmap, di2_plot]
318
+ )
319
+
320
+ if __name__ == "__main__":
321
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ matplotlib>=3.6.0
3
+ numpy>=1.21.0
4
+ Pillow>=8.0.0