Download pec5d/api/routes.py from Codexcoder/pec5d-module: direct link, hf CLI and curl.
- Browser
- Download file 11.1 kB
-
https://huggingface.co/datasets/Codexcoder/pec5d-module/resolve/af8e0599ca175fdd4fcc358b6359e4fbaecd5f99/pec5d/api/routes.py
- Command line
-
hf download hf://datasets/Codexcoder/pec5d-module@af8e0599ca175fdd4fcc358b6359e4fbaecd5f99/pec5d/api/routes.py
-
curl -L -o routes.py https://huggingface.co/datasets/Codexcoder/pec5d-module/resolve/af8e0599ca175fdd4fcc358b6359e4fbaecd5f99/pec5d/api/routes.py
11.1 kB
| # pec5d/api/routes.py | |
| """ | |
| PEC5D Full Evolution β API endpoints under /api/v1/pec5d/*. | |
| Lazy singleton boot: POST /initialize brings up the full hierarchy | |
| (cores, memory, subconscious, sandbox, pattern bridge, IPT, sensor, | |
| coral line, agency, chamber); other endpoints 503 until then. | |
| """ | |
| from __future__ import annotations | |
| import time | |
| from typing import Any, Dict, List, Optional | |
| from fastapi import APIRouter, HTTPException | |
| from pydantic import BaseModel, Field | |
| router = APIRouter(prefix="/api/v1/pec5d", tags=["pec5d"]) | |
| # ββ Lazy singletons βββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| hierarchy = None | |
| memory = None | |
| subconscious = None | |
| sandbox = None | |
| bridge = None | |
| ipt = None | |
| sensor = None | |
| chamber = None | |
| INITIALIZED_AT = None | |
| class PatternIn(BaseModel): | |
| source: str = "signal" | |
| signal: Dict[str, Any] = Field(default_factory=dict) | |
| class ComputeIn(BaseModel): | |
| vector: List[float] | |
| class ProposalIn(BaseModel): | |
| title: str | |
| body: str | |
| proposer: str = "agency" | |
| # ββ Lifecycle βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def initialize_pec5d() -> Dict[str, Any]: | |
| """Boot the full PEC5D hierarchy (idempotent).""" | |
| global hierarchy, memory, subconscious, sandbox, bridge | |
| global ipt, sensor, chamber, INITIALIZED_AT | |
| if hierarchy is None: | |
| from pec5d.core import CoreHierarchy | |
| from pec5d.holographic_memory import HolographicMemory | |
| from pec5d.subconscious_nexus import SubconsciousNexus | |
| from pec5d.sandbox import Sandbox | |
| from pec5d.pattern_bridge import PatternBridge | |
| from pec5d.ipt import InvisiblePressureTheory | |
| from pec5d.ipt_sensor import IPTSensor | |
| from pec5d.agency import ZenithChamber | |
| hierarchy = CoreHierarchy() | |
| hierarchy.initialize() | |
| memory = HolographicMemory().initialize() | |
| subconscious = SubconsciousNexus().initialize() | |
| sandbox = Sandbox().initialize() | |
| bridge = PatternBridge().initialize() | |
| ipt = InvisiblePressureTheory().initialize() | |
| sensor = IPTSensor("mini").initialize() | |
| chamber = ZenithChamber().initialize() | |
| INITIALIZED_AT = time.time() | |
| return { | |
| "status": "initialized", | |
| "version": "β.0.0", | |
| "hierarchy": hierarchy.status(), | |
| "memory": memory.get_state(), | |
| "subconscious": subconscious.get_state(), | |
| "sandbox": sandbox.get_state(), | |
| "pattern_bridge": bridge.get_state(), | |
| "ipt": ipt.get_state(), | |
| "sensor": sensor.get_state(), | |
| "chamber": chamber.get_state(), | |
| } | |
| # ββ Hierarchy / cores βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def pec5d_status() -> Dict[str, Any]: | |
| if hierarchy is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return { | |
| "version": "β.0.0", | |
| "hierarchy": hierarchy.status(), | |
| "memory": memory.get_state(), | |
| "subconscious": subconscious.get_state(), | |
| "sandbox": sandbox.get_state(), | |
| "pattern_bridge": bridge.get_state(), | |
| "ipt": ipt.get_state(), | |
| "sensor": sensor.get_state(), | |
| } | |
| async def list_cores() -> Dict[str, Any]: | |
| if hierarchy is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return hierarchy.status() | |
| async def get_core(name: str) -> Dict[str, Any]: | |
| if hierarchy is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| try: | |
| core = hierarchy.get_core(name) | |
| except KeyError as exc: | |
| raise HTTPException(status_code=404, detail=str(exc)) from exc | |
| return core.get_state() | |
| # ββ Memory ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def memory_state() -> Dict[str, Any]: | |
| if memory is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return memory.get_state() | |
| async def memory_write(body: Dict[str, Any]) -> Dict[str, Any]: | |
| if memory is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| layer = int(body.get("layer", 1)) | |
| key = body.get("key", f"mem_{time.time():.0f}") | |
| value = body.get("value") | |
| try: | |
| return memory.write(layer, key, value) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=400, detail=str(exc)) from exc | |
| # ββ Subconscious ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def subconscious_state() -> Dict[str, Any]: | |
| if subconscious is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return subconscious.get_state() | |
| async def subconscious_scan() -> Dict[str, Any]: | |
| if subconscious is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return subconscious.scan() | |
| # ββ Sandbox βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def sandbox_state() -> Dict[str, Any]: | |
| if sandbox is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return sandbox.get_state() | |
| async def sandbox_spawn(body: Dict[str, Any]) -> Dict[str, Any]: | |
| if sandbox is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| module = body.get("module", "quantum_testbed") | |
| try: | |
| state = sandbox.spawn(module, body.get("config")) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=400, detail=str(exc)) from exc | |
| return state.to_dict() | |
| # ββ Pattern bridge ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def bridge_detect(body: PatternIn) -> Dict[str, Any]: | |
| """Detect β synthesize β validate β deploy an emerged pattern.""" | |
| if bridge is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| pattern = bridge.detect(body.source, body.signal) | |
| bridge.synthesize(pattern, module_name=body.source) | |
| validation = bridge.validate(pattern, coherence=float(body.signal.get("coherence", 0.9))) | |
| deployment = bridge.deploy(pattern) if validation.get("validated") else None | |
| return {"pattern": pattern.to_dict(), "validation": validation, "deployment": deployment} | |
| async def bridge_state() -> Dict[str, Any]: | |
| if bridge is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return bridge.get_state() | |
| # ββ IPT βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def ipt_state() -> Dict[str, Any]: | |
| if ipt is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return ipt.get_state() | |
| async def ipt_pipeline(body: Dict[str, Any]) -> Dict[str, Any]: | |
| if ipt is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return ipt.run_pipeline(body) | |
| async def ipt_gradient() -> Dict[str, Any]: | |
| if ipt is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| import numpy as np | |
| rng = np.random.default_rng() | |
| return ipt.pressure_gradient(rng.random(5) + 0.1, rng.random(5) + 0.1) | |
| async def sensor_state() -> Dict[str, Any]: | |
| if sensor is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return sensor.get_state() | |
| async def sensor_read() -> Dict[str, Any]: | |
| if sensor is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return sensor.read() | |
| # ββ Coral βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def coral_products() -> Dict[str, Any]: | |
| """Coral product line index.""" | |
| if hierarchy is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| from pec5d.coral import CORAL_PRODUCTS | |
| return {"products": list(CORAL_PRODUCTS.keys())} | |
| async def coral_operate(product: str) -> Dict[str, Any]: | |
| """Operate a coral product (drone/spacecraft/bio_chip/home).""" | |
| if hierarchy is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| from pec5d.coral import CORAL_PRODUCTS | |
| cls = CORAL_PRODUCTS.get(product) | |
| if cls is None: | |
| raise HTTPException(status_code=404, detail=f"unknown coral product '{product}'") | |
| instance = cls().initialize() | |
| if product == "drone": | |
| return instance.deploy_mission() | |
| if product == "spacecraft": | |
| return instance.mission() | |
| if product == "bio_chip": | |
| return instance.compute([1.0, 1.618, 2.0]) | |
| return instance.design() | |
| # ββ Agency & chamber ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def chamber_propose(body: ProposalIn) -> Dict[str, Any]: | |
| if chamber is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return chamber.submit_proposal(body.title, body.body, body.proposer) | |
| async def chamber_vote(proposal_id: str) -> Dict[str, Any]: | |
| if chamber is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| result = chamber.resonance_vote(proposal_id) | |
| if "error" in result: | |
| raise HTTPException(status_code=404, detail=result["error"]) | |
| return result | |
| async def chamber_state() -> Dict[str, Any]: | |
| if chamber is None: | |
| raise HTTPException(status_code=503, detail="PEC5D not initialized") | |
| return chamber.get_state() | |