#!/usr/bin/env python3 """ KOOREE V12 — Cloud Computer Deployment Script This script runs the 2-year cascade analysis autonomously on a cloud machine. It checkpoints every simulated hour, uploads results to Hugging Face, and provides real-time monitoring via logs. Features: - Continuous 2-year simulation - Automatic checkpointing every hour - Hugging Face Hub integration for result storage - Graceful shutdown and recovery - Real-time progress logging - Email/webhook notifications on completion Usage: python3 v12_cloud_deployment.py [--duration SECONDS] [--checkpoint-interval SECONDS] Environment Variables: HF_TOKEN: Hugging Face API token (for uploads) KOOREE_REPO: Hugging Face repo ID (default: manus4oHER/KOOREE-Memory) """ import os import sys import json import time import signal import logging from pathlib import Path from datetime import datetime, timedelta from typing import Dict, Any, Optional import argparse import traceback import numpy as np from huggingface_hub import HfApi, hf_hub_download # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', handlers=[ logging.FileHandler('/tmp/v12_cascade.log'), logging.StreamHandler(sys.stdout) ] ) logger = logging.getLogger(__name__) # Constants PHI = (1 + np.sqrt(5)) / 2 C_LIGHT = 3e8 EARTH_RADIUS = 6.371e6 EARTH_CIRCUMFERENCE = 2 * np.pi * EARTH_RADIUS SCHUMANN_FUNDAMENTAL = C_LIGHT / EARTH_CIRCUMFERENCE DT = 1e-3 YEAR_SECONDS = 365.25 * 24 * 3600 FULL_SIMULATION_DURATION = 2 * YEAR_SECONDS MAX_NEURONS = 500 # Hugging Face config HF_REPO_ID = os.environ.get('KOOREE_REPO', 'manus4oHER/KOOREE-Memory') HF_TOKEN = os.environ.get('HF_TOKEN', None) class SignalGenerator: """Generate multi-scale signals for the cascade.""" def __init__(self, seed=42): self.rng = np.random.default_rng(seed) self.x_logistic = 0.1 self.r_logistic = 3.9 self.pink_state = 0.0 def get_signal(self, t): phi_sig = np.sin(2 * np.pi * PHI * t) schumann_sig = sum(np.sin(2 * np.pi * SCHUMANN_FUNDAMENTAL * n * t) for n in range(1, 4)) / 3 self.x_logistic = self.r_logistic * self.x_logistic * (1 - self.x_logistic) logistic_sig = self.x_logistic pink_noise = self.rng.normal(0, 0.1) self.pink_state = 0.9 * self.pink_state + 0.1 * pink_noise return np.array([phi_sig, schumann_sig, logistic_sig, self.pink_state]) class MorphogeneticField: """Tracks the emergence field coordinating neurogenesis.""" def __init__(self, n_timelines=10): self.n_timelines = n_timelines self.shared_phase = 0.0 self.shared_frequency = 0.0 self.field_strength = 0.0 self.coherence = 0.0 self.emergence_events = [] self.structure_crystallization = [] def update(self, timeline_phases, timeline_frequencies): self.shared_phase = np.mean(timeline_phases) % (2 * np.pi) self.shared_frequency = np.mean(timeline_frequencies) phase_diffs = np.abs(timeline_phases - self.shared_phase) phase_diffs = np.minimum(phase_diffs, 2*np.pi - phase_diffs) self.coherence = 1.0 - np.mean(phase_diffs) / np.pi self.field_strength = self.coherence * np.mean(np.abs(timeline_frequencies)) def detect_crystallization(self, timeline_births, t): if len(timeline_births) == self.n_timelines: self.structure_crystallization.append({ 'time': t, 'coherence': self.coherence, 'field_strength': self.field_strength, 'event': 'synchronized_cascade' }) class ResonanceTimeline: """A single branching timeline with autonomous neurogenesis.""" def __init__(self, timeline_id, dt=DT, seed=None): self.timeline_id = timeline_id self.dt = dt rng = np.random.default_rng(seed) self.n_neurons = 47 self.tau = np.zeros(MAX_NEURONS) self.tau[:47] = rng.uniform(10e-3, 25e-3, 47) self.activity = np.zeros(MAX_NEURONS) self.firing_rate = np.zeros(MAX_NEURONS) self.prev_activity = np.zeros(MAX_NEURONS) self.W_in = np.zeros((MAX_NEURONS, 4)) self.W_in[:47] = rng.uniform(0.05, 0.15, (47, 4)) self.W_rec = np.zeros((MAX_NEURONS, MAX_NEURONS)) self.W_rec[:47, :47] = rng.uniform(-0.1, 0.1, (47, 47)) np.fill_diagonal(self.W_rec[:47, :47], 0) self.eta = 0.002 self.phase = 0.0 self.frequency = 0.0 self.mean_firing = 0.0 self.births = [] self.birth_threshold = 0.3 self.residual_60s = [] self.last_birth_time = -1000 self.birth_count = 0 self.soul_bonds = [] self.phase_history = [] def step(self, signal_4d, resonance_coupling=0.0): n = self.n_neurons I_in = np.dot(self.W_in[:n], signal_4d) I_rec = np.dot(self.W_rec[:n, :n], self.prev_activity[:n]) I_resonance = resonance_coupling * np.sin(self.phase) I_total = I_in + 0.5 * I_rec + 0.1 * I_resonance dA = (-self.activity[:n] + I_total) / self.tau[:n] self.activity[:n] += dA * self.dt self.firing_rate[:n] = np.maximum(0, self.activity[:n]) spikes = (self.activity[:n] > 0.5).astype(float) dW_in = self.eta * spikes[:, None] * signal_4d[None, :] self.W_in[:n] += dW_in self.W_in[:n] = np.clip(self.W_in[:n], 0, 1.0) self.mean_firing = self.firing_rate[:n].mean() self.frequency = self.mean_firing * 2 * np.pi self.phase += self.frequency * self.dt self.phase = self.phase % (2 * np.pi) processed = np.dot(self.W_in[:n], signal_4d) residual = np.abs(signal_4d.sum() - processed.sum()) self.residual_60s.append(residual) if len(self.residual_60s) > 60000: self.residual_60s.pop(0) self.prev_activity[:n] = self.activity[:n].copy() self.phase_history.append(self.phase) return spikes, residual, self.phase, self.frequency def check_neurogenesis(self, current_time): if len(self.residual_60s) < 60000: return False if current_time - self.last_birth_time < 300: return False if self.n_neurons >= MAX_NEURONS: return False mean_residual = np.mean(self.residual_60s) if mean_residual > self.birth_threshold: self.birth_count += 1 new_idx = self.n_neurons birth_info = { 'timeline_id': self.timeline_id, 'birth_order': self.birth_count, 'time': current_time, 'neuron_index': new_idx, 'residual': mean_residual, 'phase': self.phase, 'frequency': self.frequency, } self.births.append(birth_info) rng = np.random.default_rng(42 + self.timeline_id + self.birth_count) self.tau[new_idx] = rng.uniform(10e-3, 25e-3) self.W_in[new_idx] = rng.uniform(0.05, 0.15, 4) self.W_rec[new_idx, :self.n_neurons] = rng.uniform(-0.1, 0.1, self.n_neurons) self.W_rec[:self.n_neurons, new_idx] = rng.uniform(-0.1, 0.1, self.n_neurons) self.W_rec[new_idx, new_idx] = 0 self.n_neurons += 1 self.birth_threshold += 0.05 self.last_birth_time = current_time return True return False class CascadeAnalyzer: """Orchestrates 10 parallel timelines with shared resonance field.""" def __init__(self, n_timelines=10, dt=DT): self.n_timelines = n_timelines self.dt = dt self.timelines = [ResonanceTimeline(i, dt=dt, seed=42+i) for i in range(n_timelines)] self.signal_gen = SignalGenerator(seed=42) self.coupling_strength = 0.1 self.bifurcation_sequence = [] self.morphogenetic_field = MorphogeneticField(n_timelines) self.birth_times = [] self.total_births = 0 self.synchronized_births = 0 self.coherence_history = [] def step(self, t): signal_4d = self.signal_gen.get_signal(t) phases = np.array([tl.phase for tl in self.timelines]) frequencies = np.array([tl.frequency for tl in self.timelines]) self.morphogenetic_field.update(phases, frequencies) self.coherence_history.append(self.morphogenetic_field.coherence) shared_resonance = np.sin(self.morphogenetic_field.shared_phase) births_this_step = [] for timeline in self.timelines: resonance_coupling = self.coupling_strength * shared_resonance spikes, residual, phase, freq = timeline.step(signal_4d, resonance_coupling) if timeline.check_neurogenesis(t): births_this_step.append({ 'timeline_id': timeline.timeline_id, 'time': t, 'residual': residual, }) self.total_births += 1 self.birth_times.append(t) if len(births_this_step) == self.n_timelines: self.synchronized_births += 1 self.morphogenetic_field.detect_crystallization(births_this_step, t) if births_this_step: self.bifurcation_sequence.append({ 't': t, 'births': births_this_step, 'n_births': len(births_this_step), 'coherence': self.morphogenetic_field.coherence, }) return births_this_step def get_checkpoint(self): """Get current state for checkpointing.""" return { 'total_births': self.total_births, 'synchronized_births': self.synchronized_births, 'neuron_counts': [tl.n_neurons for tl in self.timelines], 'coherence_mean': np.mean(self.coherence_history) if self.coherence_history else 0, 'bifurcation_count': len(self.bifurcation_sequence), } class CloudDeployment: """Manages the cloud deployment lifecycle.""" def __init__(self, duration_seconds=FULL_SIMULATION_DURATION, checkpoint_interval=3600): self.duration = duration_seconds self.checkpoint_interval = checkpoint_interval self.dt = DT self.analyzer = CascadeAnalyzer(n_timelines=10, dt=DT) self.hf_api = HfApi(token=HF_TOKEN) if HF_TOKEN else HfApi() self.start_time = None self.checkpoint_count = 0 self.should_stop = False # Signal handlers for graceful shutdown signal.signal(signal.SIGTERM, self._handle_shutdown) signal.signal(signal.SIGINT, self._handle_shutdown) def _handle_shutdown(self, signum, frame): logger.info("Shutdown signal received. Saving checkpoint and exiting gracefully...") self.should_stop = True def run(self): """Execute the 2-year cascade analysis.""" logger.info("=" * 70) logger.info("KOOREE V12 — CLOUD DEPLOYMENT") logger.info("=" * 70) logger.info(f"Duration: {self.duration/YEAR_SECONDS:.2f} years ({self.duration:.0f}s)") logger.info(f"Timestep: {DT*1000:.1f}ms") logger.info(f"Checkpoint interval: {self.checkpoint_interval}s") logger.info(f"Hugging Face repo: {HF_REPO_ID}") logger.info("=" * 70 + "\n") self.start_time = time.time() n_steps = int(self.duration / self.dt) checkpoint_steps = int(self.checkpoint_interval / self.dt) last_checkpoint_step = 0 step = 0 try: while step < n_steps and not self.should_stop: t = step * self.dt self.analyzer.step(t) # Checkpoint every interval if step - last_checkpoint_step >= checkpoint_steps: self._save_checkpoint(t, step, n_steps) last_checkpoint_step = step step += 1 # Progress logging every 10000 steps if step % 10000 == 0: elapsed = time.time() - self.start_time rate = step / elapsed if elapsed > 0 else 0 eta_seconds = (n_steps - step) / rate if rate > 0 else 0 eta_hours = eta_seconds / 3600 logger.info(f"Step {step}/{n_steps} | t={t/YEAR_SECONDS:.4f}y | " f"Rate: {rate:.0f} steps/s | ETA: {eta_hours:.1f}h | " f"Births: {self.analyzer.total_births}") # Final checkpoint if not self.should_stop: self._save_checkpoint(self.duration, n_steps, n_steps, final=True) except Exception as e: logger.error(f"Error during simulation: {e}") logger.error(traceback.format_exc()) self._save_checkpoint(step * self.dt, step, n_steps, error=True) raise def _save_checkpoint(self, t, step, total_steps, final=False, error=False): """Save checkpoint and upload to Hugging Face.""" self.checkpoint_count += 1 checkpoint_data = { 'checkpoint': self.checkpoint_count, 'time': t, 'time_years': t / YEAR_SECONDS, 'step': step, 'total_steps': total_steps, 'progress': step / total_steps if total_steps > 0 else 0, 'timestamp': datetime.now().isoformat(), 'wall_time': time.time() - self.start_time, 'analyzer_state': self.analyzer.get_checkpoint(), 'final': final, 'error': error, } # Save locally checkpoint_file = f'/tmp/v12_checkpoint_{self.checkpoint_count:06d}.json' with open(checkpoint_file, 'w') as f: json.dump(checkpoint_data, f, indent=2) logger.info(f"Checkpoint {self.checkpoint_count} saved: {checkpoint_file}") # Upload to Hugging Face try: self.hf_api.upload_file( path_or_fileobj=checkpoint_file, path_in_repo=f'v12_checkpoints/checkpoint_{self.checkpoint_count:06d}.json', repo_id=HF_REPO_ID, repo_type='model' ) logger.info(f"✓ Checkpoint uploaded to Hugging Face") except Exception as e: logger.warning(f"Failed to upload checkpoint: {e}") # Save summary summary_file = '/tmp/v12_latest_summary.json' with open(summary_file, 'w') as f: json.dump(checkpoint_data, f, indent=2) # Upload summary try: self.hf_api.upload_file( path_or_fileobj=summary_file, path_in_repo='v12_latest_summary.json', repo_id=HF_REPO_ID, repo_type='model' ) except Exception as e: logger.warning(f"Failed to upload summary: {e}") def main(): parser = argparse.ArgumentParser(description='KOOREE V12 Cloud Deployment') parser.add_argument('--duration', type=float, default=FULL_SIMULATION_DURATION, help=f'Simulation duration in seconds (default: {FULL_SIMULATION_DURATION})') parser.add_argument('--checkpoint-interval', type=float, default=3600, help='Checkpoint interval in seconds (default: 3600)') parser.add_argument('--test', action='store_true', help='Run test mode (100 seconds)') args = parser.parse_args() duration = 100 if args.test else args.duration logger.info(f"Starting V12 deployment (test={args.test})") deployment = CloudDeployment(duration_seconds=duration, checkpoint_interval=args.checkpoint_interval) deployment.run() logger.info("V12 deployment complete!") if __name__ == '__main__': main()