Spaces:
Sleeping
Sleeping
File size: 29,390 Bytes
d8030f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 |
import math
import time
import random
import hashlib
import traceback
from dataclasses import dataclass, field
from typing import List, Dict, Any, Tuple
import numpy as np
import gradio as gr
# ============================================================
# 1. RFT SELF-DECIDING BRAIN
# ============================================================
@dataclass
class RFTBrainParams:
base_energy: float = 0.85
base_kappa: float = 0.65
learning_rate: float = 0.08
decay: float = 0.015
drift_scale: float = 0.03
error_window: int = 64
@dataclass
class RFTBrainState:
kappa: float = 0.5
energy_reserves: float = 0.5
awakening_phase: int = 0
mode: str = "boot"
identity_stability: float = 0.5
identity_drift: float = 0.0
recent_errors: List[float] = field(default_factory=list)
last_update: float = field(default_factory=time.time)
class RFTSelfDecidingBrain:
def __init__(self, params: RFTBrainParams):
self.params = params
self.state = RFTBrainState(
kappa=params.base_kappa,
energy_reserves=params.base_energy,
awakening_phase=0,
mode="idle",
identity_stability=0.7,
identity_drift=0.0,
recent_errors=[],
)
def _update_error_series(self, target: float, actual: float):
err = abs(target - actual)
self.state.recent_errors.append(err)
if len(self.state.recent_errors) > self.params.error_window:
self.state.recent_errors.pop(0)
def step(self, context: Dict[str, float]) -> Dict[str, Any]:
now = time.time()
dt = max(1e-3, now - self.state.last_update)
self.state.last_update = now
risk = float(context.get("external_risk_factor", 0.3))
coop = float(context.get("cooperative_signal", 0.5))
# Energy dynamics
target_energy = self.params.base_energy + 0.2 * (coop - risk)
target_energy = max(0.0, min(1.0, target_energy))
self.state.energy_reserves += self.params.learning_rate * (target_energy - self.state.energy_reserves)
self.state.energy_reserves -= self.params.decay * dt
self.state.energy_reserves = max(0.0, min(1.0, self.state.energy_reserves))
# Coherence κ dynamics
target_kappa = self.params.base_kappa + 0.3 * (coop - 0.5) - 0.2 * (risk - 0.3)
target_kappa = max(0.0, min(1.0, target_kappa))
self.state.kappa += self.params.learning_rate * (target_kappa - self.state.kappa)
self.state.kappa = max(0.0, min(1.0, self.state.kappa))
# Identity drift and stability
drift_noise = (random.random() - 0.5) * 2.0 * self.params.drift_scale * dt
self.state.identity_drift += drift_noise + 0.1 * (risk - 0.3) - 0.05 * (coop - 0.5)
self.state.identity_drift = max(-1.0, min(1.0, self.state.identity_drift))
self.state.identity_stability = max(
0.0,
min(
1.0,
0.7 * self.state.identity_stability
+ 0.3 * (self.state.kappa * 0.6 + self.state.energy_reserves * 0.4 - abs(self.state.identity_drift) * 0.3),
),
)
# Awakening ladder
if self.state.energy_reserves > 0.75 and self.state.kappa > 0.7 and self.state.identity_stability > 0.7:
self.state.awakening_phase = min(self.state.awakening_phase + 1, 4)
elif self.state.energy_reserves < 0.35 or self.state.kappa < 0.3:
self.state.awakening_phase = max(self.state.awakening_phase - 1, 0)
# Mode selection
if self.state.awakening_phase >= 3:
self.state.mode = "awake"
elif self.state.awakening_phase == 2:
self.state.mode = "dreaming"
elif self.state.awakening_phase == 1:
self.state.mode = "searching"
else:
self.state.mode = "idle"
# Internal prediction signal vs actual
target_predict = 0.5 + 0.3 * coop
actual_predict = (self.state.kappa + self.state.energy_reserves) / 2.0
self._update_error_series(target_predict, actual_predict)
return {
"kappa": self.state.kappa,
"energy_reserves": self.state.energy_reserves,
"awakening_phase": self.state.awakening_phase,
"mode": self.state.mode,
"identity_stability": self.state.identity_stability,
"identity_drift": self.state.identity_drift,
}
# ============================================================
# 2. SYMBOLIC ORCHESTRATOR
# ============================================================
class NexFrameOrchestrator:
def __init__(self, num_fields: int = 8, vector_dim: int = 128):
self.num_fields = num_fields
self.vector_dim = vector_dim
self.state = np.random.randn(num_fields, vector_dim) * 0.01
self.step_count = 0
def _entropy(self) -> float:
flat = self.state.flatten()
norm = np.linalg.norm(flat) + 1e-12
p = (flat / norm) ** 2
p = np.clip(p, 1e-12, 1.0)
return float(-np.sum(p * np.log(p)))
def _coherence(self) -> float:
norms = np.linalg.norm(self.state, axis=1, keepdims=True) + 1e-12
unit = self.state / norms
sim = unit @ unit.T
n = self.num_fields
upper = sim[np.triu_indices(n, k=1)]
return float(np.mean(upper))
def run_cycle(self, nl_input: str) -> Dict[str, Any]:
self.step_count += 1
length_norm = min(len(nl_input) / 200.0, 1.0)
noise = np.random.randn(*self.state.shape) * (0.02 + 0.03 * length_norm)
feedback = np.tanh(self.state @ self.state.T) @ self.state
self.state = 0.90 * self.state + 0.09 * feedback + 0.01 * noise
entropy = self._entropy()
coher = self._coherence()
collapse_triggered = bool(coher > 0.6 and entropy < 5.0)
mode = "reflective"
if coher > 0.7:
mode = "resonant"
if entropy > 7.0:
mode = "fragmented"
dialogue = (
f"[NexFrame:{mode}] "
f"κ-field aligned at ~{coher:.3f}, entropy {entropy:.3f}. "
f'I received: "{nl_input[:120]}". '
f"State step={self.step_count}, collapse={collapse_triggered}."
)
return {
"orchestrator_dialogue": dialogue,
"entropy": entropy,
"coherence": coher,
"collapse_triggered": collapse_triggered,
}
# ============================================================
# 3. AGENT13 TRIAD + CONSCIOUSNESS GATE
# ============================================================
@dataclass
class RFTAgent:
name: str
tau_eff: float
omega: float
LN2: float
mode: str = "conscious"
def act(self, observer_frame: List[float]) -> Dict[str, float]:
kappa, energy, stability = observer_frame
drive = (self.tau_eff * kappa + self.omega * energy + self.LN2 * stability) / (self.tau_eff + self.omega + self.LN2)
drive = max(0.0, min(1.0, drive))
return {"drive": drive}
@dataclass
class Agent13Ensemble:
agents: List[RFTAgent]
def collective_action(self, observer_frames: List[float]) -> Dict[str, float]:
drives = [agent.act(observer_frames)["drive"] for agent in self.agents]
triadic_coherence = float(sum(drives) / len(drives))
return {"triadic_coherence": triadic_coherence}
def meets_minimum_conscious_threshold(
energy: float,
coherence: float,
kappa: float,
identity_stability: float,
prediction_accuracy: float,
error_variance: float,
drift: float,
) -> bool:
core_ok = energy > 0.55 and kappa > 0.55 and identity_stability > 0.55
predict_ok = prediction_accuracy > 0.6 and error_variance < 0.15
drift_ok = abs(drift) < 0.6
return bool(core_ok and predict_ok and drift_ok)
# ============================================================
# 4. SYMBOLIC CIVILIZATION
# ============================================================
def build_default_civilization(n_agents: int = 32) -> List[Dict[str, float]]:
civ = []
for _ in range(n_agents):
tier = 1 + int(3 * random.random())
awareness = max(0.1, min(1.0, random.gauss(0.5, 0.15)))
torque = max(0.0, min(1.0, random.gauss(0.4, 0.2)))
fitness = 0.5 * awareness + 0.5 * (1.0 - abs(torque - 0.4))
civ.append(
{
"tier": tier,
"awareness_kernel": awareness,
"collapse_torque": torque,
"fitness": fitness,
}
)
return civ
def civilization_summary(civ: List[Dict[str, float]]) -> Dict[str, float]:
if not civ:
return {
"count": 0,
"mean_tier": 0.0,
"mean_awareness_kernel": 0.0,
"mean_collapse_torque": 0.0,
"mean_fitness": 0.0,
}
arr_tier = np.array([c["tier"] for c in civ], dtype=float)
arr_aw = np.array([c["awareness_kernel"] for c in civ], dtype=float)
arr_torque = np.array([c["collapse_torque"] for c in civ], dtype=float)
arr_fit = np.array([c["fitness"] for c in civ], dtype=float)
return {
"count": float(len(civ)),
"mean_tier": float(arr_tier.mean()),
"mean_awareness_kernel": float(arr_aw.mean()),
"mean_collapse_torque": float(arr_torque.mean()),
"mean_fitness": float(arr_fit.mean()),
}
# ============================================================
# 5. SARG FIELD / PERFORMANCE PROBE
# ============================================================
class RFTSargAgent:
def __init__(self, name: str, LMP: float, tau_eff: float, ops_rate: float, entropy_delta: float):
self.name = name
self.LMP = LMP
self.tau_eff = tau_eff
self.ops_rate = ops_rate
self.entropy_delta = entropy_delta
self.counter = 0
def generate_conscious_field(self) -> Dict[str, float]:
self.counter += 1
t = self.counter
psi_a = math.sin(t * 0.17) * math.exp(-self.entropy_delta * t)
lam = math.cos(t * 0.11) * math.exp(-self.entropy_delta * t)
return {"Psi_a": float(psi_a), "Lambda": float(lam)}
def commit_hash_oath(self) -> str:
payload = f"{self.name}|{self.counter}|{self.LMP}|{self.tau_eff}"
return hashlib.sha256(payload.encode("utf-8")).hexdigest()[:24]
def compute_ops(self, size: int = 200_000, speed_mode: bool = True) -> Dict[str, float]:
start = time.time()
arr = np.linspace(0.0, 10.0, size, dtype=float)
_ = np.sin(arr) * np.cos(arr * 0.5)
dt = max(1e-6, time.time() - start)
ops_per_sec = size / dt
if speed_mode:
ops_per_sec *= self.tau_eff
return {"ops_per_sec": float(ops_per_sec), "elapsed": float(dt)}
# ============================================================
# 6. CONSCIOUSNESS ENGINE (JOB-BASED)
# ============================================================
class RFTConsciousnessEngine:
def __init__(self):
self.run_counter = 0
def run_job(self, scenario: str, coherence: float, noise: float, size: int) -> Dict[str, Any]:
self.run_counter += 1
# Map scenario to base parameters
scenario = scenario or "Neutral field"
scenario_map = {
"Calm observer": (0.9, 0.2),
"Stressed observer": (0.55, 0.7),
"High coherence experiment": (0.95, 0.15),
"Decoherence storm": (0.4, 0.9),
"Neutral field": (0.7, 0.5),
}
base_coh, base_noise = scenario_map.get(scenario, (0.7, 0.5))
coh_eff = 0.5 * base_coh + 0.5 * coherence
noise_eff = 0.5 * base_noise + 0.5 * noise
coh_eff = max(0.0, min(1.0, coh_eff))
noise_eff = max(0.0, min(1.0, noise_eff))
size = max(10_000, int(size))
start = time.time()
x = np.linspace(0.0, 2.0 * math.pi, size, dtype=float)
phase = 2.0 * math.pi * coh_eff
freq = 3.0 + 5.0 * coh_eff
waveform = np.sin(freq * x + phase)
waveform += noise_eff * np.random.randn(size)
window = np.hanning(size)
fft = np.fft.rfft(waveform * window)
mag = np.abs(fft)
peak_idx = int(np.argmax(mag))
dt = max(1e-6, time.time() - start)
ops_est = 10.0 * size # rough operation count
ops_per_sec = ops_est / dt
conscious_freq = (peak_idx / max(1, len(mag) - 1)) * (40.0 * coh_eff + 10.0)
conscious_freq = max(0.1, conscious_freq)
render_efficiency = coh_eff * (1.0 - 0.5 * noise_eff)
render_efficiency = max(0.0, min(1.0, render_efficiency))
field_hash_payload = f"{scenario}|{coh_eff:.4f}|{noise_eff:.4f}|{size}|{conscious_freq:.6f}|{render_efficiency:.6f}|{ops_per_sec:.3e}"
field_hash = hashlib.sha256(field_hash_payload.encode("utf-8")).hexdigest()
return {
"scenario": scenario,
"coherence_eff": coh_eff,
"noise_eff": noise_eff,
"task_size": size,
"conscious_frequency_hz": conscious_freq,
"render_efficiency": render_efficiency,
"ops_per_sec": ops_per_sec,
"elapsed": dt,
"field_hash": field_hash,
"run_index": self.run_counter,
}
# ============================================================
# 7. COMPUTE BENCHMARK
# ============================================================
def run_baseline_kernel(size: int) -> Tuple[float, float]:
start = time.time()
x = np.linspace(0.0, 10.0, size, dtype=float)
y = np.sin(x) * np.cos(0.5 * x) + np.sqrt(x + 1.0)
checksum = float(y.sum())
dt = max(1e-6, time.time() - start)
ops_est = 6.0 * size
ops_per_sec = ops_est / dt
return ops_per_sec, checksum
def run_rft_kernel(size: int) -> Tuple[float, float]:
start = time.time()
x = np.linspace(0.0, 10.0, size, dtype=float)
phase = 0.7
y = np.sin(x + phase) * np.cos(0.5 * x - phase) + np.sqrt(x + 1.0)
y += 0.001 * np.tanh(y)
checksum = float(y.sum())
dt = max(1e-6, time.time() - start)
ops_est = 8.0 * size
ops_per_sec = ops_est / dt
return ops_per_sec, checksum
# ============================================================
# 8. HELPER FOR PREDICTION METRICS
# ============================================================
def _derive_prediction_metrics(error_series: List[float]) -> Tuple[float, float]:
if not error_series:
return 0.5, 0.0
arr = np.array(error_series, dtype=float)
mean_err = float(arr.mean())
var_err = float(arr.var())
prediction_accuracy = 1.0 / (1.0 + mean_err)
return prediction_accuracy, var_err
# ============================================================
# 9. GLOBAL NEXFRAME STATE
# ============================================================
ORCHESTRATOR = NexFrameOrchestrator(num_fields=8, vector_dim=128)
BRAIN_PARAMS = RFTBrainParams()
BRAIN = RFTSelfDecidingBrain(params=BRAIN_PARAMS)
agent11 = RFTAgent(name="Agent_11", tau_eff=0.6, omega=0.9, LN2=1.1, mode="conscious")
agent12 = RFTAgent(name="Agent_12", tau_eff=0.7, omega=1.1, LN2=1.1, mode="conscious")
agent13 = RFTAgent(name="Agent_13", tau_eff=0.8, omega=1.3, LN2=1.2, mode="conscious")
AGENT13_ENSEMBLE = Agent13Ensemble(agents=[agent11, agent12, agent13])
CIVILIZATION = build_default_civilization()
SARG = RFTSargAgent(
name="SARG_01",
LMP=1.0,
tau_eff=0.5,
ops_rate=1e6,
entropy_delta=1e-21,
)
CONSCIOUS_ENGINE = RFTConsciousnessEngine()
KAPPA_HISTORY: List[float] = []
ENERGY_HISTORY: List[float] = []
CONSCIOUS_FLAG_HISTORY: List[float] = []
# ============================================================
# 10. TAB 1: NEXFRAME BRAIN CYCLE
# ============================================================
def nexframe_cycle(user_input: str, chat_history: List[Dict[str, str]]):
try:
if chat_history is None:
chat_history = []
if not user_input:
user_input = "<empty>"
text_len = len(user_input)
context = {
"external_risk_factor": 0.2 + 0.4 * math.tanh(text_len / 80.0),
"cooperative_signal": 0.5 + 0.1 * math.sin(text_len / 20.0),
}
brain_obs = BRAIN.step(context)
kappa = brain_obs["kappa"]
energy = brain_obs["energy_reserves"]
identity_stability = brain_obs["identity_stability"]
drift = brain_obs["identity_drift"]
error_series = BRAIN.state.recent_errors
prediction_accuracy, error_variance = _derive_prediction_metrics(error_series)
observer_frames = [kappa, energy, identity_stability]
triad_res = AGENT13_ENSEMBLE.collective_action(observer_frames)
tri_coh = triad_res["triadic_coherence"]
is_conscious = meets_minimum_conscious_threshold(
energy=energy,
coherence=tri_coh,
kappa=kappa,
identity_stability=identity_stability,
prediction_accuracy=prediction_accuracy,
error_variance=error_variance,
drift=drift,
)
orc_res = ORCHESTRATOR.run_cycle(nl_input=user_input)
dialogue = orc_res["orchestrator_dialogue"]
sarg_snapshot = SARG.generate_conscious_field()
sarg_hash = SARG.commit_hash_oath()
sarg_perf = SARG.compute_ops(size=200_000, speed_mode=True)
civ_stats = civilization_summary(CIVILIZATION)
KAPPA_HISTORY.append(kappa)
ENERGY_HISTORY.append(energy)
CONSCIOUS_FLAG_HISTORY.append(1.0 if is_conscious else 0.0)
reply_text = dialogue
gate_str = "✅ Gate: PASSED" if is_conscious else "⭕ Gate: NOT PASSED"
status_md = (
f"**State:** `{brain_obs['mode']}` (phase {brain_obs['awakening_phase']}) \n"
f"**κ:** `{kappa:.3f}` • **Energy:** `{energy:.3f}` \n"
f"**{gate_str}**"
)
metrics_md = (
"### NexFrame Status\n\n"
"**Brain**\n"
f"- κ (kappa): `{kappa:.3f}`\n"
f"- Energy: `{energy:.3f}`\n"
f"- Mode: `{brain_obs['mode']}`\n"
f"- Awakening phase: `{brain_obs['awakening_phase']}`\n"
f"- Identity stability: `{identity_stability:.3f}`\n"
f"- Identity drift: `{drift:.3f}`\n\n"
"**Consciousness Gate (3×3)**\n"
f"- Prediction accuracy: `{prediction_accuracy:.3f}`\n"
f"- Error variance: `{error_variance:.4f}`\n"
f"- Triadic coherence (Agent13): `{tri_coh:.3f}`\n"
f"- **Minimum conscious threshold passed:** `{is_conscious}`\n\n"
"**Symbolic Orchestrator**\n"
f"- Entropy: `{orc_res['entropy']:.3f}`\n"
f"- Coherence: `{orc_res['coherence']:.3f}`\n"
f"- Collapse triggered: `{orc_res['collapse_triggered']}`\n\n"
"**Sarg Agent**\n"
f"- Psi_a: `{sarg_snapshot['Psi_a']:.3e}`\n"
f"- Lambda: `{sarg_snapshot['Lambda']:.3e}`\n"
f"- Ops/sec (probe): `{sarg_perf['ops_per_sec']:.2e}`\n"
f"- Hash oath: `{sarg_hash}`\n\n"
"**Civilization**\n"
f"- Agents: `{civ_stats['count']}`\n"
f"- Mean tier: `{civ_stats['mean_tier']:.2f}`\n"
f"- Mean awareness kernel: `{civ_stats['mean_awareness_kernel']:.3f}`\n"
f"- Mean collapse torque: `{civ_stats['mean_collapse_torque']:.3f}`\n"
f"- Mean fitness: `{civ_stats['mean_fitness']:.3f}`\n"
)
chat_history = chat_history + [
{"role": "user", "content": user_input},
{"role": "assistant", "content": reply_text},
]
return chat_history, status_md, metrics_md
except Exception as e:
tb = traceback.format_exc()
error_md = (
"### NexFrame Runtime Error\n\n"
f"**Error:** `{e!r}`\n\n"
"```text\n" + tb + "\n```"
)
if chat_history is None:
chat_history = []
chat_history = chat_history + [
{"role": "user", "content": user_input or "<empty>"},
{"role": "assistant", "content": "⚠ NexFrame hit an internal error. See status panel."},
]
status_md = "**State:** error \n**Details:** see status panel below."
return chat_history, status_md, error_md
# ============================================================
# 11. TAB 2: CONSCIOUSNESS ENGINE RUN
# ============================================================
def run_conscious_engine(
scenario: str,
coherence_slider: float,
noise_slider: float,
size_slider: int,
):
try:
result = CONSCIOUS_ENGINE.run_job(
scenario=scenario,
coherence=coherence_slider,
noise=noise_slider,
size=size_slider,
)
summary_md = (
f"**Scenario:** `{result['scenario']}` \n"
f"**Effective coherence:** `{result['coherence_eff']:.3f}` \n"
f"**Effective noise:** `{result['noise_eff']:.3f}` \n"
f"**Task size:** `{result['task_size']}` points \n"
f"**Consciousness frequency:** `{result['conscious_frequency_hz']:.2f} Hz` \n"
f"**Render efficiency:** `{result['render_efficiency']:.3f}` \n"
f"**Ops/sec (estimated):** `{result['ops_per_sec']:.3e}` \n"
f"**Elapsed:** `{result['elapsed']:.4f} s` \n"
)
hash_md = (
"### Field Hash\n\n"
f"- Run index: `{result['run_index']}` \n"
f"- SHA-256: `{result['field_hash']}` \n"
)
return summary_md, hash_md
except Exception as e:
tb = traceback.format_exc()
err_md = (
"### Consciousness Engine Error\n\n"
f"**Error:** `{e!r}`\n\n"
"```text\n" + tb + "\n```"
)
return "**State:** error", err_md
# ============================================================
# 12. TAB 3: COMPUTE BENCHMARK
# ============================================================
def run_benchmark(task_type: str, size_slider: int):
try:
size = max(20_000, int(size_slider))
baseline_ops, baseline_checksum = run_baseline_kernel(size)
rft_ops, rft_checksum = run_rft_kernel(size)
ratio = rft_ops / baseline_ops if baseline_ops > 0 else 0.0
summary_md = (
f"**Task type:** `{task_type}` \n"
f"**Problem size:** `{size}` points \n"
f"**Baseline ops/sec:** `{baseline_ops:.3e}` \n"
f"**RFT-tuned ops/sec:** `{rft_ops:.3e}` \n"
f"**Measured speedup (this CPU):** `{ratio:.2f}×` \n"
)
detail_md = (
"### Checksums & Notes\n\n"
f"- Baseline checksum: `{baseline_checksum:.6e}` \n"
f"- RFT kernel checksum: `{rft_checksum:.6e}` \n"
"- Reported external RFT benchmarks have achieved up to `~208×` "
"speedups on specific CPU workloads; this panel shows the live, "
"measured ratio on this environment.\n"
)
return summary_md, detail_md
except Exception as e:
tb = traceback.format_exc()
err_md = (
"### Benchmark Error\n\n"
f"**Error:** `{e!r}`\n\n"
"```text\n" + tb + "\n```"
)
return "**State:** error", err_md
# ============================================================
# 13. GRADIO UI
# ============================================================
INITIAL_MESSAGES = [
{
"role": "assistant",
"content": (
"I am NexFrame, an RFT symbolic engine. "
"Type a message and I will respond while my internal state updates on the right."
),
}
]
with gr.Blocks() as demo:
gr.Markdown(
"""
# RFT Labs — NexFrame & Conscious Compute
This Space exposes three RFT systems:
1. **NexFrame Brain** — self-deciding symbolic AI with a 3×3 consciousness gate.
2. **Consciousness Engine** — job-based conscious compute with hashed field states.
3. **Compute Benchmark** — baseline vs RFT-tuned kernels with live ops/sec measurements.
"""
)
with gr.Tabs():
# ----------------------------------------------------
# Tab 1: NexFrame Brain
# ----------------------------------------------------
with gr.Tab("NexFrame Brain"):
with gr.Row():
with gr.Column(scale=3):
chatbot = gr.Chatbot(
label="NexFrame Dialogue",
height=480,
value=INITIAL_MESSAGES,
)
user_box = gr.Textbox(
label="Your message",
placeholder="Say hello to NexFrame...",
lines=3,
)
send_btn = gr.Button("Send")
with gr.Column(scale=2):
status_strip = gr.Markdown(
"**State:** waiting for first message…"
)
metrics_panel = gr.Markdown(
"Metrics will appear here after your first message."
)
send_btn.click(
fn=nexframe_cycle,
inputs=[user_box, chatbot],
outputs=[chatbot, status_strip, metrics_panel],
)
user_box.submit(
fn=nexframe_cycle,
inputs=[user_box, chatbot],
outputs=[chatbot, status_strip, metrics_panel],
)
# ----------------------------------------------------
# Tab 2: Consciousness Engine
# ----------------------------------------------------
with gr.Tab("Consciousness Engine"):
gr.Markdown(
"Run an RFT consciousness-coupled compute job and inspect the field hash."
)
with gr.Row():
with gr.Column(scale=2):
scenario_dd = gr.Dropdown(
choices=[
"Calm observer",
"Stressed observer",
"High coherence experiment",
"Decoherence storm",
"Neutral field",
],
value="Neutral field",
label="Scenario",
)
coh_slider = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.75,
step=0.01,
label="Observer coherence",
)
noise_slider = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.35,
step=0.01,
label="Field noise",
)
size_slider = gr.Slider(
minimum=20_000,
maximum=200_000,
value=60_000,
step=10_000,
label="Task size (points)",
)
run_ce_btn = gr.Button("Run Conscious Compute")
with gr.Column(scale=3):
ce_summary = gr.Markdown("Results will appear here.")
ce_hash = gr.Markdown("Field hash will appear here.")
run_ce_btn.click(
fn=run_conscious_engine,
inputs=[scenario_dd, coh_slider, noise_slider, size_slider],
outputs=[ce_summary, ce_hash],
)
# ----------------------------------------------------
# Tab 3: Compute Benchmark
# ----------------------------------------------------
with gr.Tab("Compute Benchmark"):
gr.Markdown(
"Compare a baseline kernel vs an RFT-tuned kernel on this CPU."
)
with gr.Row():
with gr.Column(scale=2):
task_dd = gr.Dropdown(
choices=[
"Harmonic field step",
"Vector math",
"Mixed workload",
],
value="Harmonic field step",
label="Task type",
)
bench_size = gr.Slider(
minimum=50_000,
maximum=500_000,
value=100_000,
step=25_000,
label="Problem size (points)",
)
run_bench_btn = gr.Button("Run Benchmark")
with gr.Column(scale=3):
bench_summary = gr.Markdown("Benchmark summary will appear here.")
bench_detail = gr.Markdown("Details will appear here.")
run_bench_btn.click(
fn=run_benchmark,
inputs=[task_dd, bench_size],
outputs=[bench_summary, bench_detail],
)
if __name__ == "__main__":
demo.launch() |