Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -204,77 +204,98 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 204 |
await websocket.send_json({"type": "history", "data": history_msgs})
|
| 205 |
|
| 206 |
async def _emit_state():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
while True:
|
| 208 |
if not system.ready:
|
| 209 |
await asyncio.sleep(system.tick_delay)
|
| 210 |
continue
|
| 211 |
|
| 212 |
try:
|
| 213 |
-
# 1.
|
| 214 |
-
#
|
| 215 |
bg_noise = system.noise.get_blended_noise(size=40*40)
|
| 216 |
-
|
| 217 |
-
# Stats for Wick Rotation
|
| 218 |
stats = system.noise.get_source_stats()
|
| 219 |
base_ntp = stats.get('ntp_offset', 0.0)
|
| 220 |
off = system.sync_config['offset']
|
| 221 |
total_offset = base_ntp + off
|
| 222 |
|
| 223 |
-
# Input Unitary (Braid Field)
|
| 224 |
-
# FIX: text_comm.last_text_unitary in 1014e is a Tensor (Braid Field)
|
| 225 |
current_braid = system.text_comm.last_text_unitary if system.text_comm else 0.0
|
| 226 |
coupling = system.sync_config['coupling']
|
| 227 |
|
| 228 |
-
|
| 229 |
-
# Note: SciMindCommunicator.step accepts braid field + ntp_offset
|
| 230 |
-
metrics_raw = system.holo.step(bg_noise, current_braid * coupling, ntp_offset=total_offset)
|
| 231 |
|
| 232 |
-
# Decay the tensor signal (Memory Fade)
|
| 233 |
if isinstance(system.text_comm.last_text_unitary, torch.Tensor):
|
| 234 |
system.text_comm.last_text_unitary *= 0.95
|
| 235 |
|
| 236 |
-
#
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
entropy_val = float(system.holo.surprisal)
|
| 240 |
-
ci = float(system.holo.causal_integrity)
|
| 241 |
-
phases = system.holo.phases.tolist()
|
| 242 |
-
|
| 243 |
-
# Advanced Metrics
|
| 244 |
-
godel_gap = system.calculate_godel_gap()
|
| 245 |
-
|
| 246 |
-
metrics = {
|
| 247 |
-
"causal_integrity": ci,
|
| 248 |
-
"vorticity": vorticity, # Chern
|
| 249 |
-
"coherence": coherence,
|
| 250 |
-
"godel_gap": godel_gap,
|
| 251 |
-
"entropy": entropy_val
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
# Generate Formula
|
| 255 |
-
formula_data = system.formula_engine.generate(coherence, metrics)
|
| 256 |
-
|
| 257 |
-
vocab_stats = {
|
| 258 |
-
"total": len(system.vocab.user_words) if system.vocab else 0,
|
| 259 |
-
"top": system.vocab.get_top_terms(5) if system.vocab else []
|
| 260 |
-
}
|
| 261 |
|
| 262 |
-
#
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
except Exception as e:
|
| 275 |
-
# print(f"
|
| 276 |
pass
|
| 277 |
|
|
|
|
| 278 |
await asyncio.sleep(system.tick_delay)
|
| 279 |
|
| 280 |
async def _receive_messages():
|
|
@@ -286,13 +307,9 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 286 |
text = msg['text']
|
| 287 |
if system.ready and system.text_comm:
|
| 288 |
noise = system.noise.get_blended_noise(size=64)
|
| 289 |
-
|
| 290 |
-
# Process message (Imprints Braid, Decodes Response)
|
| 291 |
response_text = system.text_comm.process_message(text, noise)
|
| 292 |
|
| 293 |
-
# Sync Learning Update
|
| 294 |
metrics = system.holo.get_metrics()
|
| 295 |
-
|
| 296 |
system.learner.record_trial(
|
| 297 |
system.sync_config['offset'],
|
| 298 |
system.sync_config['coupling'],
|
|
@@ -306,7 +323,6 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 306 |
"data": new_msgs
|
| 307 |
})
|
| 308 |
|
| 309 |
-
# Auto Save
|
| 310 |
system.mgr.save_global_state(
|
| 311 |
system.vocab.get_state(),
|
| 312 |
system.learner.get_state(),
|
|
@@ -316,9 +332,8 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
| 316 |
except WebSocketDisconnect:
|
| 317 |
pass
|
| 318 |
except Exception as e:
|
| 319 |
-
|
| 320 |
|
| 321 |
-
# Run both loops
|
| 322 |
emit_task = asyncio.create_task(_emit_state())
|
| 323 |
receive_task = asyncio.create_task(_receive_messages())
|
| 324 |
|
|
|
|
| 204 |
await websocket.send_json({"type": "history", "data": history_msgs})
|
| 205 |
|
| 206 |
async def _emit_state():
|
| 207 |
+
# --- ADAPTIVE NETWORK THROTTLE ---
|
| 208 |
+
# Startwert: Konservativ (2 FPS)
|
| 209 |
+
current_broadcast_interval = 0.5
|
| 210 |
+
last_broadcast_time = 0
|
| 211 |
+
|
| 212 |
while True:
|
| 213 |
if not system.ready:
|
| 214 |
await asyncio.sleep(system.tick_delay)
|
| 215 |
continue
|
| 216 |
|
| 217 |
try:
|
| 218 |
+
# 1. PHYSIK SCHRITT (Hochfrequent, läuft immer)
|
| 219 |
+
# ---------------------------------------------
|
| 220 |
bg_noise = system.noise.get_blended_noise(size=40*40)
|
|
|
|
|
|
|
| 221 |
stats = system.noise.get_source_stats()
|
| 222 |
base_ntp = stats.get('ntp_offset', 0.0)
|
| 223 |
off = system.sync_config['offset']
|
| 224 |
total_offset = base_ntp + off
|
| 225 |
|
|
|
|
|
|
|
| 226 |
current_braid = system.text_comm.last_text_unitary if system.text_comm else 0.0
|
| 227 |
coupling = system.sync_config['coupling']
|
| 228 |
|
| 229 |
+
system.holo.step(bg_noise, current_braid * coupling, ntp_offset=total_offset)
|
|
|
|
|
|
|
| 230 |
|
|
|
|
| 231 |
if isinstance(system.text_comm.last_text_unitary, torch.Tensor):
|
| 232 |
system.text_comm.last_text_unitary *= 0.95
|
| 233 |
|
| 234 |
+
# 2. NETZWERK SCHRITT (Adaptiv)
|
| 235 |
+
# -----------------------------
|
| 236 |
+
now = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
+
# Prüfen, ob wir wieder senden dürfen
|
| 239 |
+
if now - last_broadcast_time > current_broadcast_interval:
|
| 240 |
+
|
| 241 |
+
# MESSUNG STARTEN
|
| 242 |
+
send_start_time = time.time()
|
| 243 |
+
|
| 244 |
+
# --- Daten vorbereiten (Teuer!) ---
|
| 245 |
+
metrics_raw = system.holo.get_metrics()
|
| 246 |
+
phases = system.holo.phases.tolist()
|
| 247 |
+
maps = system.holo.get_maps()
|
| 248 |
+
|
| 249 |
+
metrics = {
|
| 250 |
+
"causal_integrity": float(metrics_raw['causal_integrity']),
|
| 251 |
+
"vorticity": float(metrics_raw['vorticity']),
|
| 252 |
+
"coherence": float(metrics_raw['fidelity']),
|
| 253 |
+
"godel_gap": system.calculate_godel_gap(),
|
| 254 |
+
"entropy": float(metrics_raw['surprisal'])
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
formula_data = system.formula_engine.generate(metrics['coherence'], metrics)
|
| 258 |
+
vocab_stats = {
|
| 259 |
+
"total": len(system.vocab.user_words) if system.vocab else 0,
|
| 260 |
+
"top": system.vocab.get_top_terms(5) if system.vocab else []
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
# --- Senden (Kann blockieren!) ---
|
| 264 |
+
await websocket.send_json({
|
| 265 |
+
"type": "state",
|
| 266 |
+
"metrics": metrics,
|
| 267 |
+
"phases": phases,
|
| 268 |
+
"maps": maps,
|
| 269 |
+
"vocab": vocab_stats,
|
| 270 |
+
"formula": formula_data,
|
| 271 |
+
"ntp_status": f"NTP: {base_ntp:+.4f}"
|
| 272 |
+
})
|
| 273 |
+
|
| 274 |
+
last_broadcast_time = time.time()
|
| 275 |
+
|
| 276 |
+
# MESSUNG BEENDET
|
| 277 |
+
send_duration = last_broadcast_time - send_start_time
|
| 278 |
+
|
| 279 |
+
# --- DYNAMISCHE ANPASSUNG ---
|
| 280 |
+
# Regel: Das Senden soll maximal 20% der Zeit beanspruchen.
|
| 281 |
+
# Wenn Senden 0.1s dauert, warten wir 0.5s Pause.
|
| 282 |
+
# Wenn Senden 0.01s dauert, warten wir nur 0.05s Pause.
|
| 283 |
+
|
| 284 |
+
target_interval = send_duration * 5.0
|
| 285 |
+
|
| 286 |
+
# Clamping:
|
| 287 |
+
# Nicht schneller als 10 FPS (0.1s)
|
| 288 |
+
# Nicht langsamer als 0.5 FPS (2.0s) - damit man nicht denkt es sei abgestürzt
|
| 289 |
+
current_broadcast_interval = max(0.1, min(target_interval, 2.0))
|
| 290 |
+
|
| 291 |
+
# Optional: Debugging im Log (kannst du auskommentieren)
|
| 292 |
+
# print(f"Net Load: {send_duration:.3f}s -> New Interval: {current_broadcast_interval:.3f}s")
|
| 293 |
+
|
| 294 |
except Exception as e:
|
| 295 |
+
# print(f"Emit Error: {e}")
|
| 296 |
pass
|
| 297 |
|
| 298 |
+
# Schlafen basierend auf Physik-Tuner (z.B. 30 FPS)
|
| 299 |
await asyncio.sleep(system.tick_delay)
|
| 300 |
|
| 301 |
async def _receive_messages():
|
|
|
|
| 307 |
text = msg['text']
|
| 308 |
if system.ready and system.text_comm:
|
| 309 |
noise = system.noise.get_blended_noise(size=64)
|
|
|
|
|
|
|
| 310 |
response_text = system.text_comm.process_message(text, noise)
|
| 311 |
|
|
|
|
| 312 |
metrics = system.holo.get_metrics()
|
|
|
|
| 313 |
system.learner.record_trial(
|
| 314 |
system.sync_config['offset'],
|
| 315 |
system.sync_config['coupling'],
|
|
|
|
| 323 |
"data": new_msgs
|
| 324 |
})
|
| 325 |
|
|
|
|
| 326 |
system.mgr.save_global_state(
|
| 327 |
system.vocab.get_state(),
|
| 328 |
system.learner.get_state(),
|
|
|
|
| 332 |
except WebSocketDisconnect:
|
| 333 |
pass
|
| 334 |
except Exception as e:
|
| 335 |
+
pass
|
| 336 |
|
|
|
|
| 337 |
emit_task = asyncio.create_task(_emit_state())
|
| 338 |
receive_task = asyncio.create_task(_receive_messages())
|
| 339 |
|