cjo93 commited on
Commit
f7c4865
·
verified ·
1 Parent(s): dc23e1a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +380 -0
app.py ADDED
@@ -0,0 +1,380 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from dataclasses import dataclass
4
+ from typing import Dict, List, Any
5
+ import re
6
+
7
+ # =========================
8
+ # PHYSICS ENGINE (DEFRAG v2)
9
+ # =========================
10
+
11
+ @dataclass
12
+ class VectorState:
13
+ """
14
+ Represents the user's dynamic state in 3D space.
15
+ Ranges: -10.0 (Collapse) to +10.0 (Sovereign)
16
+
17
+ Axes:
18
+ - X (Resilience): Root/Solar Plexus/Sacral → Vitality/Stress capacity
19
+ - Y (Autonomy): Heart/G-Center → Identity/Will/Self-direction
20
+ - Z (Connectivity): Throat/Ajna → Expression/Social integration
21
+ """
22
+ resilience: float
23
+ autonomy: float
24
+ connectivity: float
25
+
26
+ def to_array(self) -> np.ndarray:
27
+ return np.array([self.resilience, self.autonomy, self.connectivity])
28
+
29
+ @staticmethod
30
+ def from_array(arr: np.ndarray) -> "VectorState":
31
+ return VectorState(
32
+ resilience=float(arr[0]),
33
+ autonomy=float(arr[1]),
34
+ connectivity=float(arr[2]),
35
+ )
36
+
37
+ def magnitude(self) -> float:
38
+ return float(np.linalg.norm(self.to_array()))
39
+
40
+ def distance_from_baseline(self, baseline: "VectorState") -> float:
41
+ diff = self.to_array() - baseline.to_array()
42
+ return float(np.linalg.norm(diff))
43
+
44
+
45
+ @dataclass
46
+ class PhysicsConstants:
47
+ """
48
+ Physics properties derived from Human Design blueprint.
49
+ These govern how a specific user reacts to stress.
50
+ """
51
+ mass: float # Resistance to change (1-10)
52
+ permeability: float # Environmental sensitivity (0-1.5)
53
+ elasticity_vector: np.ndarray # Dimensional recovery rates [X, Y, Z]
54
+ coupling_matrix: np.ndarray # 3×3 interaction matrix
55
+
56
+
57
+ class BlueprintPhysics:
58
+ """
59
+ Converts Human Design blueprint into physics constants.
60
+ Pure mathematical derivation. No randomness.
61
+ """
62
+ PROFILE_MASS_TABLE: Dict[str, float] = {
63
+ "1": 9.0,
64
+ "2": 8.0,
65
+ "3": 7.0,
66
+ "4": 4.0,
67
+ "5": 3.0,
68
+ "6": 2.0,
69
+ }
70
+
71
+ AUTHORITY_ELASTICITY: Dict[str, float] = {
72
+ "Emotional": 0.3,
73
+ "Lunar": 0.2,
74
+ "Self-Projected": 0.5,
75
+ "Environmental": 0.5,
76
+ "Ego": 0.6,
77
+ "Sacral": 0.8,
78
+ "Splenic": 0.95,
79
+ }
80
+
81
+ @staticmethod
82
+ def derive(blueprint: Dict) -> PhysicsConstants:
83
+ mass = BlueprintPhysics._calculate_mass(blueprint.get("profile", "3/5"))
84
+ elasticity_vector = BlueprintPhysics._calculate_elasticity(
85
+ blueprint.get("authority", "Emotional"),
86
+ blueprint.get("open_centers", []),
87
+ blueprint.get("defined_centers", []),
88
+ )
89
+ coupling_matrix = BlueprintPhysics._build_coupling_matrix(
90
+ blueprint.get("open_centers", []),
91
+ blueprint.get("defined_centers", []),
92
+ )
93
+ permeability = len(blueprint.get("open_centers", [])) * 0.15
94
+
95
+ return PhysicsConstants(
96
+ mass=mass,
97
+ permeability=permeability,
98
+ elasticity_vector=elasticity_vector,
99
+ coupling_matrix=coupling_matrix,
100
+ )
101
+
102
+ @staticmethod
103
+ def _calculate_mass(profile: str) -> float:
104
+ try:
105
+ p_line, d_line = profile.split("/")
106
+ p_mass = BlueprintPhysics.PROFILE_MASS_TABLE.get(p_line, 5.0)
107
+ d_mass = BlueprintPhysics.PROFILE_MASS_TABLE.get(d_line, 5.0)
108
+ return (p_mass + d_mass) / 2.0
109
+ except Exception:
110
+ return 5.0
111
+
112
+ @staticmethod
113
+ def _calculate_elasticity(
114
+ authority: str,
115
+ open_centers: List[str],
116
+ defined_centers: List[str],
117
+ ) -> np.ndarray:
118
+ base = BlueprintPhysics.AUTHORITY_ELASTICITY.get(authority, 0.5)
119
+ elasticity = np.array([base, base, base])
120
+
121
+ if "SACRAL" in open_centers:
122
+ elasticity[0] *= 0.5
123
+ if "ROOT" in open_centers:
124
+ elasticity[0] *= 0.7
125
+ if "SOLAR_PLEXUS" in open_centers:
126
+ elasticity[0] *= 0.75
127
+ if "SACRAL" in defined_centers:
128
+ elasticity[0] *= 1.3
129
+ if "ROOT" in defined_centers:
130
+ elasticity[0] *= 1.2
131
+
132
+ if "HEART" in open_centers:
133
+ elasticity[1] *= 0.6
134
+ if "G_CENTER" in open_centers:
135
+ elasticity[1] *= 0.7
136
+ if "HEART" in defined_centers:
137
+ elasticity[1] *= 1.4
138
+ if "G_CENTER" in defined_centers:
139
+ elasticity[1] *= 1.3
140
+
141
+ if "THROAT" in open_centers:
142
+ elasticity[2] *= 0.7
143
+ if "AJNA" in open_centers:
144
+ elasticity[2] *= 0.8
145
+ if "THROAT" in defined_centers:
146
+ elasticity[2] *= 1.2
147
+ if "AJNA" in defined_centers:
148
+ elasticity[2] *= 1.15
149
+
150
+ elasticity = np.clip(elasticity, 0.1, 2.0)
151
+ return elasticity
152
+
153
+ @staticmethod
154
+ def _build_coupling_matrix(
155
+ open_centers: List[str],
156
+ defined_centers: List[str],
157
+ ) -> np.ndarray:
158
+ m = np.eye(3, dtype=float)
159
+
160
+ if "HEAD" in open_centers:
161
+ m[0, 2] += 0.25
162
+ if "AJNA" in open_centers:
163
+ m[1, 2] += 0.3
164
+ if "THROAT" in open_centers:
165
+ m[2, 1] += 0.4
166
+ if "G_CENTER" in open_centers:
167
+ m[1, 2] += 0.6
168
+ if "HEART" in open_centers:
169
+ m[1, 2] += 0.5
170
+ m[1, 0] += 0.3
171
+ m[0, 2] += 0.2
172
+ if "SOLAR_PLEXUS" in open_centers:
173
+ m[0, 2] += 0.4
174
+ if "SACRAL" in open_centers:
175
+ m[0, 1] += 0.3
176
+ if "SPLEEN" in open_centers:
177
+ m[0, 2] += 0.35
178
+ if "ROOT" in open_centers:
179
+ m[0, 2] += 0.3
180
+ m[1, 2] += 0.2
181
+
182
+ if "SOLAR_PLEXUS" in defined_centers:
183
+ m[0, 2] = max(1.0, m[0, 2] - 0.2)
184
+ if "HEART" in defined_centers:
185
+ m[1, 2] = max(1.0, m[1, 2] - 0.3)
186
+ if "G_CENTER" in defined_centers:
187
+ m[1, 2] = max(1.0, m[1, 2] - 0.25)
188
+
189
+ return m
190
+
191
+
192
+ class PhysicsSolver:
193
+ @staticmethod
194
+ def calculate_impact(
195
+ current_state: VectorState,
196
+ stress_vector: VectorState,
197
+ physics: PhysicsConstants,
198
+ ) -> VectorState:
199
+ curr_vec = current_state.to_array()
200
+ stress_vec = stress_vector.to_array()
201
+
202
+ amplified_force = stress_vec * (1.0 + physics.permeability)
203
+ acceleration = amplified_force / physics.mass
204
+ coupled_acceleration = np.dot(physics.coupling_matrix, acceleration)
205
+ recovery_force = curr_vec * physics.elasticity_vector * 0.1
206
+ new_vec = curr_vec + coupled_acceleration + recovery_force
207
+ new_vec = np.clip(new_vec, -10.0, 10.0)
208
+ return VectorState.from_array(new_vec)
209
+
210
+ @staticmethod
211
+ def predict_recovery_time(
212
+ current_state: VectorState,
213
+ baseline_state: VectorState,
214
+ physics: PhysicsConstants,
215
+ tolerance: float = 0.5,
216
+ ) -> int:
217
+ distance = current_state.distance_from_baseline(baseline_state)
218
+ avg_elasticity = float(np.mean(physics.elasticity_vector))
219
+ time_constant = physics.mass / max(avg_elasticity, 0.1)
220
+ if distance < tolerance:
221
+ return 0
222
+ recovery_time = -time_constant * np.log(tolerance / distance)
223
+ return int(max(recovery_time, 0))
224
+
225
+
226
+ # =========================
227
+ # STRESS MAPPER
228
+ # =========================
229
+
230
+ @dataclass(frozen=True)
231
+ class AxisWeights:
232
+ resilience: float
233
+ autonomy: float
234
+ connectivity: float
235
+
236
+
237
+ class StressMapper:
238
+ MIN_SEVERITY = 1
239
+ MAX_SEVERITY = 10
240
+ MIN_FORCE = 0.5
241
+ MAX_FORCE = 6.0
242
+
243
+ CATEGORY_WEIGHTS: Dict[str, AxisWeights] = {
244
+ "burnout": AxisWeights(-1.0, -0.4, -0.1),
245
+ "overwork": AxisWeights(-0.9, -0.3, -0.1),
246
+ "fatigue": AxisWeights(-0.8, -0.2, -0.1),
247
+ "exhaustion": AxisWeights(-1.0, -0.3, -0.2),
248
+
249
+ "rejection": AxisWeights(-0.3, -1.0, -0.6),
250
+ "abandon": AxisWeights(-0.2, -1.0, -0.6),
251
+ "shame": AxisWeights(-0.2, -0.9, -0.5),
252
+ "failure": AxisWeights(-0.3, -0.8, -0.4),
253
+ "criticized": AxisWeights(-0.2, -0.8, -0.5),
254
+
255
+ "conflict": AxisWeights(-0.2, -0.5, -0.9),
256
+ "argument": AxisWeights(-0.1, -0.4, -0.9),
257
+ "breakup": AxisWeights(-0.3, -0.7, -1.0),
258
+ "lonely": AxisWeights(-0.1, -0.3, -0.9),
259
+ "isolation": AxisWeights(-0.1, -0.4, -1.0),
260
+
261
+ "money": AxisWeights(-0.7, -0.8, -0.2),
262
+ "debt": AxisWeights(-0.8, -0.9, -0.2),
263
+ "bills": AxisWeights(-0.7, -0.7, -0.2),
264
+ "job": AxisWeights(-0.6, -0.6, -0.3),
265
+ "career": AxisWeights(-0.5, -0.7, -0.3),
266
+
267
+ "prove": AxisWeights(-0.5, -0.9, -0.3),
268
+ "pressure": AxisWeights(-0.6, -0.7, -0.4),
269
+ "deadline": AxisWeights(-0.6, -0.7, -0.3),
270
+
271
+ "sick": AxisWeights(-0.9, -0.3, -0.2),
272
+ "illness": AxisWeights(-1.0, -0.3, -0.2),
273
+ "injury": AxisWeights(-1.0, -0.2, -0.2),
274
+
275
+ "stuck": AxisWeights(-0.4, -0.9, -0.3),
276
+ "lost": AxisWeights(-0.3, -1.0, -0.4),
277
+ "purposeless": AxisWeights(-0.2, -1.0, -0.4),
278
+ "directionless": AxisWeights(-0.2, -0.9, -0.4),
279
+
280
+ "anxiety": AxisWeights(-0.8, -0.5, -0.7),
281
+ "panic": AxisWeights(-0.9, -0.4, -0.8),
282
+ "fear": AxisWeights(-0.7, -0.5, -0.7),
283
+ }
284
+
285
+ DEFAULT_WEIGHTS = AxisWeights(-0.6, -0.6, -0.5)
286
+
287
+ @classmethod
288
+ def map_event_to_stress(cls, context: str, severity_numeric: int) -> VectorState:
289
+ norm = cls._normalize_severity(severity_numeric)
290
+ tokens = cls._tokenize(context)
291
+ agg_x = agg_y = agg_z = 0.0
292
+ hits = 0
293
+
294
+ for t in tokens:
295
+ if t in cls.CATEGORY_WEIGHTS:
296
+ w = cls.CATEGORY_WEIGHTS[t]
297
+ agg_x += w.resilience
298
+ agg_y += w.autonomy
299
+ agg_z += w.connectivity
300
+ hits += 1
301
+
302
+ if hits == 0:
303
+ base = cls.DEFAULT_WEIGHTS
304
+ agg_x, agg_y, agg_z = base.resilience, base.autonomy, base.connectivity
305
+ hits = 1
306
+
307
+ avg_x, avg_y, avg_z = agg_x / hits, agg_y / hits, agg_z / hits
308
+
309
+ return VectorState(
310
+ resilience=avg_x * norm,
311
+ autonomy=avg_y * norm,
312
+ connectivity=avg_z * norm,
313
+ )
314
+
315
+ @classmethod
316
+ def _normalize_severity(cls, severity: int) -> float:
317
+ s = max(cls.MIN_SEVERITY, min(cls.MAX_SEVERITY, int(severity)))
318
+ t = (s - cls.MIN_SEVERITY) / (cls.MAX_SEVERITY - cls.MIN_SEVERITY)
319
+ return cls.MIN_FORCE + t * (cls.MAX_FORCE - cls.MIN_FORCE)
320
+
321
+ @staticmethod
322
+ def _tokenize(text: str) -> List[str]:
323
+ if not text:
324
+ return []
325
+ text = text.lower()
326
+ tokens = re.split(r"[^a-z]+", text)
327
+ return [t for t in tokens if t]
328
+
329
+
330
+ # =========================
331
+ # INVERSION ENGINE (v1)
332
+ # =========================
333
+
334
+ class InversionEngine:
335
+ @staticmethod
336
+ def process_event(
337
+ blueprint: Dict[str, Any],
338
+ event_context: str,
339
+ severity_numeric: int,
340
+ vector_state: VectorState,
341
+ ) -> Dict[str, Any]:
342
+ diagnosis = InversionEngine._diagnose(vector_state)
343
+ script = InversionEngine._script(vector_state)
344
+ experiments = InversionEngine._experiments(vector_state)
345
+ seda_locked = severity_numeric >= 8
346
+
347
+ return {
348
+ "diagnosis": diagnosis,
349
+ "script": script,
350
+ "script_source": "heuristic_v1",
351
+ "experiments": experiments,
352
+ "seda_locked": seda_locked,
353
+ "seda_keywords": ["SEDA"] if seda_locked else [],
354
+ }
355
+
356
+ @staticmethod
357
+ def _diagnose(state: VectorState) -> str:
358
+ if state.resilience < 3.0 and state.autonomy < 3.0:
359
+ return "Acute burnout with identity depletion."
360
+ if state.resilience < 3.0:
361
+ return "Resilience system overloaded."
362
+ if state.autonomy < 3.0:
363
+ return "Autonomy and self-direction destabilized."
364
+ if state.connectivity < 3.5:
365
+ return "Social connectivity suppressed."
366
+ return "System under manageable stress."
367
+
368
+ @staticmethod
369
+ def _script(state: VectorState) -> str:
370
+ parts = []
371
+ if state.resilience < 3.0:
372
+ parts.append(
373
+ "Pause output for 24 hours and reduce external demands to the minimum you can safely hold."
374
+ )
375
+ if state.autonomy < 3.0:
376
+ parts.append(
377
+ "Do not make major decisions alone; speak them out loud with a trusted person before acting."
378
+ )
379
+ if state.connectivity < 3.5:
380
+ parts.app