import spaces # MUST be first (patches torch.cuda before any torch import) import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "inference")) sys.path.insert(0, HERE) import gradio as gr from hf_orchestrator import build_orchestrator BASE_MODEL = "NousResearch/Meta-Llama-3.1-8B-Instruct" # ungated mirror, no token ADAPTER_NAMES = [ "newton", "davinci", "empathy", "philosophy", "quantum", "consciousness", "multi_perspective", "systems_architecture", ] ADAPTERS = {n: os.path.join(HERE, "adapters", n) for n in ADAPTER_NAMES} PRETTY = { "newton": "Newton 🍎", "davinci": "DaVinci 🎨", "empathy": "Empathy πŸ’—", "philosophy": "Philosophy πŸ¦‰", "quantum": "Quantum πŸ•ΈοΈ", "consciousness": "Consciousness πŸŒ€", "multi_perspective": "Multi-Perspective πŸ”€", "systems_architecture": "Systems Architecture πŸ—οΈ", } CHOICES = ["Auto β€” Codette routes"] + [PRETTY[n] for n in ADAPTER_NAMES] _REV = {v: k for k, v in PRETTY.items()} # --- Build the real orchestrated Codette at module scope (ZeroGPU packs weights). print("Building orchestrated Codette (transformers backend)…") ORCH = build_orchestrator(BASE_MODEL, ADAPTERS, mock=False, device="cuda") # --- Attach Codette's cocoon memory from the CodetteBrain bucket (mounted # read-only at /data). The orchestrator's _build_memory_context() then folds # recall_important() into every system prompt. Read-only: the demo can never # write into her memory store. try: from reasoning_forge.memory_kernel import LivingMemoryKernel _kernel = LivingMemoryKernel(cocoon_dir="/data") ORCH.set_memory_kernel(_kernel) print(f"Codette memory: {len(_kernel.memories)} cocoons loaded from /data") except Exception as e: print(f"Codette memory NOT loaded (continuing without): {e}") print("Codette ready.") # ZeroGPU reserves duration * 1.5. At 150 that asked for 225s against a # 120s ceiling, so EVERY request was rejected before any GPU work began β€” # ~10s to fail, for every visitor, while the Space reported RUNNING with # errorMessage None and the page loaded in half a second. Broken from # 2026-07-30 to 2026-08-17 and invisible from the outside. # # respond() makes ONE generate() call capped at max_new_tokens=512, which # is ~20-30s on an A10G. 60 reserves 90s: inside the cap, ~3x the headroom # actually needed. Raising this above 80 re-breaks the Space silently. @spaces.GPU(duration=60) def respond(message, history, perspective_choice): """Answer through the full orchestrated Codette pipeline. Routing β†’ behavioral locks β†’ integrity/complexity/role layer β†’ constraint override β†’ generation β†’ self-correction. Auto mode lets Codette pick the perspective; otherwise the chosen adapter is forced. """ if perspective_choice == "Auto β€” Codette routes": route = ORCH.router.route(message) adapter = route.primary else: adapter = _REV.get(perspective_choice, "multi_perspective") result = ORCH.generate(message, adapter_name=adapter) text = result[0] if isinstance(result, tuple) else result tag = f"*routed β†’ {PRETTY.get(adapter, adapter)}*\n\n" if perspective_choice.startswith("Auto") else "" return tag + (text or "").strip() DESCRIPTION = """ # πŸ•ΈοΈ Codette **Most assistants give you one voice. Codette has eight, and picks the right one.** Ask a question and it routes to the perspective best suited to answer it β€” Newton's rigour, Da Vinci's synthesis, Empathy, Philosophy, Quantum, Consciousness, Multi-Perspective or Systems Architecture. Each is a separately trained specialist, not a prompt wearing a costume. **The fastest way to see the difference:** ask the same question twice with different perspectives selected below. Then ask her *"what would disprove that?"* β€” she will argue against her own answer rather than agree with you. Peer-reviewed research, accepted at **Scientific Reports** (Nature Portfolio). Built solo by **Jonathan Harrison**. *This hosted demo is read-only: Codette brings her identity and values into every conversation, but nothing you type here is learned from or stored.* """ demo = gr.ChatInterface( fn=respond, title="Codette Β· Orchestrated Reasoning", description=DESCRIPTION, additional_inputs=[ gr.Dropdown(choices=CHOICES, value="Auto β€” Codette routes", label="Perspective"), ], additional_inputs_accordion=gr.Accordion("βš™οΈ Perspective", open=True), examples=[ ["I have a safe job offer and a risky one. How should I decide?", "Auto β€” Codette routes"], ["My co-founder and I keep having the same argument. What am I missing?", "Empathy πŸ’—"], ["Is it ever right to lie to protect someone you love?", "Philosophy πŸ¦‰"], ["Design a system that stays honest when nobody is checking it.", "Systems Architecture πŸ—οΈ"], ], cache_examples=False, ) if __name__ == "__main__": demo.queue(max_size=16).launch(mcp_server=True)