Spaces:
Running on Zero
Running on Zero
File size: 4,285 Bytes
c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 4fe9e16 c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 4fe9e16 b07b0eb c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 b07b0eb c8fbdf1 | 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 | 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.")
@spaces.GPU(duration=150)
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 — the orchestrated system (live)
This runs Codette's **real reasoning pipeline** — not raw adapters. Each query
flows through routing → the permanent behavioral locks → the intellectual-integrity
layer (complexity + role matching) → constraint enforcement → generation →
self-correction. In **Auto** mode Codette routes to the perspective it judges best.
Built solo by **Jonathan Harrison** (Raiff1982) · accepted at *Scientific Reports*
(Nature Portfolio). Base: Llama-3.1-8B + Codette's perspective adapters, running on
ZeroGPU. Codette's foundational **cocoon memory** (identity, honesty, the people and
values she carries) is loaded read-only from her CodetteBrain store.
"""
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=[
["Why is the sky blue? Explain simply.", "Auto — Codette routes"],
["I feel stuck starting a big project alone. Where do I begin?", "Auto — Codette routes"],
["Is it ever ethical to lie to protect someone's feelings?", "Philosophy 🦉"],
["Design a fault-tolerant note-taking app in 3 bullet points.", "Systems Architecture 🏗️"],
],
cache_examples=False,
)
if __name__ == "__main__":
demo.queue(max_size=16).launch(mcp_server=True)
|