import gradio as gr from engine import get_next_planet_state, HabitabilityStatus import time with open('assets/style.css', 'r', encoding='utf-8') as f: RAW_CSS = f.read() with open('assets/planet.js', 'r', encoding='utf-8') as f: RAW_JS = f.read() HEAD_INJECT = f""" """ def init_llm_eager(): print("[AeroSphere] Awakening AeroSphere Cloud Endpoints...") try: from engine import init_llm init_llm() except Exception as e: print(f"Eager loading failed: {e}") print("[AeroSphere] Neural link established. Resolving UI.") return "READY" def handle_interaction(user_input, history, core_state): try: from engine import get_next_planet_state state = get_next_planet_state(user_input, history, core_state) new_core_state = state.model_dump(mode='json') new_core_state["entities"] = [] new_core_state_str = __import__('json').dumps(new_core_state) # Failsafe overlay hide if eager load lagged hide_overlay_str = "" new_history = history + [{"role": "user", "content": user_input}, {"role": "assistant", "content": state.narrative}] history_html = '
' for turn in new_history[-6:]: if turn["role"] == "user": history_html += f'
{turn["content"]}
' else: history_html += f'
{turn["content"][:60]}...
' history_html += '
' badge_val = getattr(state, "habitability", "STERILE") if hasattr(badge_val, "value"): badge_val = badge_val.value badge_color = "warning" if badge_val == "STERILE" else "nominal" badge_html = f'
{badge_val}
TECH: {getattr(state, "tech_level", 0.0):.2f}
' return ( new_history, hide_overlay_str + state.narrative + "", gr.update(label=state.metric_name, value=state.metric_value), gr.update(value=state.button_a_label), gr.update(value=state.button_b_label), "", history_html, new_core_state_str, new_core_state, badge_html, gr.update(value=f"🧬 EVOLVE CIVILIZATION" if getattr(state, "evolution_age", 0) >= 20_000_000 else ("🧬 SEED LIFE" if badge_val != "STERILE" else "🧬 SEED LIFE (LOCKED)"), interactive=(badge_val != "STERILE")) ) except Exception as e: import traceback trace_str = traceback.format_exc() # Fallback dump safely avoiding schema drops! return ( history, f"FATAL UI CRASH: {e}\n{trace_str}", gr.update(label="Crash", value=0), gr.update(value="Crash"), gr.update(value="Crash"), user_input, "Crash State Isolated.", "{}", core_state, '
ERROR
', gr.update(interactive=False) ) def reset_sim(): from engine import PlanetState hadean = PlanetState( narrative="SYSTEM RESET. Hadean eon initiated. The planet is a hostile volcanic fireball.", metric_name="Core Instability", metric_value=99.9, lava_intensity=1.0, cloud_density=0.9, storm_intensity=0.8, ocean_level=0.0, vegetation=0.0, ice_coverage=0.0, land_mass=0.0, planet_color_hex="#ff1e00", atmosphere_color_hex="#ff4400" ) hadean_dump = hadean.model_dump() hadean_dump_str = hadean.model_dump_json() return ( [], # history hadean.narrative + "", gr.update(label=hadean.metric_name, value=hadean.metric_value), gr.update(value=hadean.button_a_label), gr.update(value=hadean.button_b_label), "", # console input '
SYSTEM FORMAT COMPLETE
', hadean_dump_str, # js mapping hadean_dump, # core state '
STERILE
TECH: 0.00
', gr.update(value="🧬 SEED LIFE (LOCKED)", interactive=False) ) bg_html = f"""
ESTABLISHING NEURAL LINK TO GENESIS MATRIX...
Initializing Core LLM Weights into VRAM
""" with gr.Blocks(title="AeroSphere HUD") as demo: history_state = gr.State([]) gr.HTML(bg_html) hadean_default = { "narrative": "SYSTEM RESET. Hadean eon initiated.", "metric_name": "Core Instability", "metric_value": 99.9, "lava_intensity": 1.0, "cloud_density": 0.9, "storm_intensity": 0.8, "ocean_level": 0.0, "vegetation": 0.0, "ice_coverage": 0.0, "land_mass": 0.0, "planet_color_hex": "#ff1e00", "atmosphere_color_hex": "#ff4400" } import json js_inject = gr.Textbox(value=json.dumps(hadean_default), visible=False, elem_id="js_inject") with gr.Row(elem_classes=["aerosphere-habitability-panel"]): gr.HTML('
Planetary Status
') habitability_badge = gr.HTML('
STERILE
TECH: 0.00
') with gr.Row(elem_classes=["aerosphere-body"]): with gr.Column(elem_classes=["aerosphere-node-telemetry", "aerosphere-glass-panel"]): gr.HTML('
Aether Resonance
') gr.HTML('
64
resonance index · hz
') telemetry_slider = gr.Slider(minimum=0, maximum=100, value=64, label="", elem_classes=["aerosphere-range-slider"]) gr.HTML('
') with gr.Column(elem_classes=["aerosphere-node-status", "aerosphere-glass-panel"]): gr.HTML('
Sys Diagnostics
') gr.HTML("""
Hull98.2%
ReactorOnline
Shields72.1%
CommsNominal
""") with gr.Column(elem_classes=["aerosphere-node-actions", "aerosphere-glass-panel"]): gr.HTML('
Action Matrix
') btn_a = gr.Button("Stabilize Core", elem_classes=["aerosphere-action-btn"]) btn_b = gr.Button("Extract Luminescence", elem_classes=["aerosphere-action-btn"]) btn_genesis = gr.Button("✧ Genesis Spark", elem_classes=["aerosphere-action-btn", "special-btn"]) btn_evolve = gr.Button("🧬 SEED LIFE (LOCKED)", interactive=False, elem_classes=["aerosphere-action-btn", "special-btn"]) btn_reset = gr.Button("Format Matrix (Reset)", elem_classes=["aerosphere-action-btn", "danger-btn"]) telemetry_slider = gr.Slider(minimum=0, maximum=100, value=50, step=1, label="Atmospheric Harmony", interactive=False) with gr.Column(elem_classes=["aerosphere-node-narrative", "aerosphere-glass-panel"]): gr.HTML('
Vessel Log · Encrypted
') narrative_text = gr.Markdown("Stardate 2387.04 — Vessel drifts through the Lyrae Nebula. Sensor arrays detect faint particulate resonance at bearing 217 mark 4. The aether envelope holds, though hull micro-fractures along the port nacelle suggest prior engagement with a gravimetric anomaly. Engineering reports reactor output steady at 94.7%. Forward observation confirms visual on a tertiary exoplanet — spectral class M, tidally locked, low albedo. No transponder signatures within effective range.") gr.HTML('
◆ Timestamp: Syncing...
') with gr.Column(elem_classes=["aerosphere-node-console", "aerosphere-glass-panel"]): gr.HTML('
Override Console
') gr.HTML('
aero@vessel:~$
') console_input = gr.Textbox(placeholder="Enter intervention directive...", elem_classes=["aerosphere-console-input"], container=False) btn_export = gr.Button("💾 Export Vessel Logs", elem_classes=["aerosphere-action-btn"]) export_file = gr.File(label="Exported Telemetry Data", visible=False) console_history_html = gr.HTML('
') core_state = gr.State(hadean_default) inputs_list = [console_input, history_state, core_state] outputs_list = [history_state, narrative_text, telemetry_slider, btn_a, btn_b, console_input, console_history_html, js_inject, core_state, habitability_badge, btn_evolve] # Tick Planet Logic def handle_tick(history, cs): from engine import PlanetState, tick_planet state = PlanetState.model_validate(cs) state = tick_planet(state, delta_t=1.0) # approx 1 second of real world time badge_val = state.habitability.value if hasattr(state.habitability, "value") else state.habitability # EXTINCTION EVENT HOOK if badge_val == "STERILE" and (getattr(state, "population", 0) > 0 or getattr(state, "evolution_age", 0) > 0): state.population = 0 state.evolution_age = 0 state.biosphere_richness = 0.0 state.tech_level = 0.0 state.civilization_scale = 0.0 state.narrative = "CRITICAL WARNING: EXTINCTION EVENT. Planetary conditions have plummeted beyond survivable thresholds. All emergent life forms have perished." new_cs = state.model_dump(mode='json') new_cs_str = state.model_dump_json() badge_color = "warning" if badge_val == "STERILE" else "nominal" lbl = "🧬 EVOLVE CIVILIZATION" if getattr(state, "evolution_age", 0) > 0 else ("🧬 SEED LIFE" if badge_val != "STERILE" else "🧬 SEED LIFE (LOCKED)") btn_update = gr.update(value=lbl, interactive=(badge_val != "STERILE")) return new_cs, new_cs_str, f'
{badge_val}
TECH: {state.tech_level:.2f}
', btn_update tick_timer = gr.Timer(value=1) tick_timer.tick(fn=handle_tick, inputs=[history_state, core_state], outputs=[core_state, js_inject, habitability_badge, btn_evolve]) def handle_fast_forward(history, cs): from engine import PlanetState, compute_era, check_habitability, HabitabilityStatus, Era state = PlanetState.model_validate(cs) if state.habitability in ["STERILE", getattr(HabitabilityStatus, "STERILE", "STERILE")]: return cs, cs, f'
STERILE
TECH: 0.00
', gr.update(value="🧬 SEED LIFE (LOCKED)", interactive=False) current_age = getattr(state, "evolution_age", 0) if current_age == 0: # SEED LIFE HARDCODE state.evolution_age = 20_000_000 state.population = 500 state.biosphere_richness = 0.2 state.narrative = "DIRECTIVE EXECUTED: Seed Life. Primordial organics successfully deployed into habitable zones. Biological lifecycle initiated." state.habitability = check_habitability(state) else: # EVOLVE CIVILIZATION state.evolution_age += 85_000_000 era, tech = compute_era(state.evolution_age) state.current_era = getattr(Era, era) if hasattr(Era, era) else era state.tech_level = tech state.population = int(state.population * 1500) if state.population > 0 else 1000000 from engine import get_next_planet_state state = get_next_planet_state(f"Massive time jump: 85 million years elapsed. The planet enters the {era} period.", history, state.model_dump()) state.habitability = check_habitability(state) new_cs = state.model_dump(mode='json') new_cs["entities"] = getattr(state, "entities", []) new_cs_str = __import__('json').dumps(new_cs) badge_val = state.habitability.value if hasattr(state.habitability, "value") else state.habitability badge_color = "warning" if badge_val == "STERILE" else "nominal" bdg = f'
{badge_val}
TECH: {state.tech_level:.2f}
' lbl = "🧬 EVOLVE CIVILIZATION" if getattr(state, "evolution_age", 0) > 0 else "🧬 SEED LIFE" return new_cs, new_cs_str, bdg, gr.update(value=lbl, interactive=True) console_input.submit(fn=handle_interaction, inputs=inputs_list, outputs=outputs_list, js="(user_input, h, cs) => { window.handleAction('submit'); if (window.triggerShockwave) window.triggerShockwave(); return [user_input, h, cs]; }") btn_a.click(fn=lambda h, a_lbl, cs: handle_interaction(f"Execute directive: {a_lbl}", h, cs), inputs=[history_state, btn_a, core_state], outputs=outputs_list, js="(h, a, cs) => { window.handleAction('stabilize'); return [h, a, cs]; }") btn_b.click(fn=lambda h, b_lbl, cs: handle_interaction(f"Execute directive: {b_lbl}", h, cs), inputs=[history_state, btn_b, core_state], outputs=outputs_list, js="(h, b, cs) => { window.handleAction('extract'); return [h, b, cs]; }") btn_genesis.click(fn=lambda h, cs: handle_interaction("Terraform into an Earth-like biological paradise. Set ice_coverage and lava_intensity to 0.0. Increase vegetation to 0.8, ocean_level to 0.6, and land_mass to 0.5, cloud_density to 0.7, storm_intensity to 0.5. Strictly set planet_color_hex to green (#1e7050) and atmosphere_color_hex to sky blue (#40d0a0).", h, cs), inputs=[history_state, core_state], outputs=outputs_list, js="(h, cs) => { window.handleAction('submit'); if (window.triggerShockwave) window.triggerShockwave(); return [h, cs]; }") btn_evolve.click(fn=handle_fast_forward, inputs=[history_state, core_state], outputs=[core_state, js_inject, habitability_badge, btn_evolve], js="(h, cs) => { window.handleAction('extract'); if (window.triggerShockwave) window.triggerShockwave(); return [h, cs]; }") btn_reset.click(fn=reset_sim, inputs=[], outputs=outputs_list, js="() => { window.handleAction('submit'); if (window.triggerShockwave) window.triggerShockwave(); return []; }") init_trigger = gr.Textbox(visible=False, elem_id="hidden_init_trigger") hidden_interact_payload = gr.Textbox(visible=False, elem_id="hidden_interact_payload") hidden_interact_btn = gr.Button("HiddenInteract", visible=False, elem_id="hidden_interact_btn") hidden_interact_btn.click( fn=handle_interaction, inputs=[hidden_interact_payload, history_state, core_state], outputs=outputs_list, js="(p, h, cs) => { window.handleAction('submit'); return [p, h, cs]; }" ) def handle_export(cs): import json, tempfile, os path = os.path.join(tempfile.gettempdir(), "aerosphere_vessel_telemetry.json") with open(path, "w") as f: json.dump(cs, f, indent=4) return gr.update(value=path, visible=True) btn_export.click(fn=handle_export, inputs=[core_state], outputs=[export_file]) js_inject.change(fn=None, inputs=[js_inject], outputs=None, js="(state) => { if (state && window.updatePlanet) { setTimeout(() => window.updatePlanet(state), 300); } }") demo.load(fn=init_llm_eager, inputs=[], outputs=[init_trigger], js="() => { const tryInit = () => { if (window.THREE && window.THREE.OrbitControls && window.initAeroSpherePlanet) { window.initAeroSpherePlanet(); } else { setTimeout(tryInit, 50); } }; tryInit(); return []; }") hide_overlay_js = "() => { let overlay = document.getElementById('aerosphere-loading-overlay'); if (overlay) { overlay.style.opacity = '0'; setTimeout(() => overlay.style.display = 'none', 1000); } }" init_trigger.change(fn=None, inputs=[], outputs=[], js=hide_overlay_js) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860, allowed_paths=["assets"], head=HEAD_INJECT)