Spaces:
Running
Running
chore(sync): mirror backend .py + Dockerfile to Space (hf-sync-backend)
Browse filesAutomated backend sync from szl-holdings/a11oy main via hf-sync-backend.
Updated (differed from the Space): szl_alloy_models.py
Deleted (gone from the repo + Dockerfile COPY set): (none)
Keeps the Space-built backend (serve.py + the Dockerfile-COPY'd .py
modules) identical to GitHub main so the Space never rebuilds from a
stale backend, new endpoints don't 404 there, and orphaned modules
removed from the repo don't linger in the Space tree.
- szl_alloy_models.py +69 -7
szl_alloy_models.py
CHANGED
|
@@ -434,7 +434,8 @@ def _softmax(xs: list[float], temp: float = 1.0) -> list[float]:
|
|
| 434 |
return [e / z for e in exps]
|
| 435 |
|
| 436 |
def route(task_hint: str, lam: float, candidates: Optional[list[dict]] = None,
|
| 437 |
-
prefer_code: bool = True, temp: float = 0.7
|
|
|
|
| 438 |
"""C20-stable, W7-5-bracketed model selection over the open-weight alloy.
|
| 439 |
|
| 440 |
C20 (proven, Mathlib-free order-preservation core): the softmax over per-model
|
|
@@ -448,10 +449,18 @@ def route(task_hint: str, lam: float, candidates: Optional[list[dict]] = None,
|
|
| 448 |
lies inside the envelope.
|
| 449 |
|
| 450 |
Founder directive: weight the router toward DeepSeek for CODING tasks.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
"""
|
| 452 |
pool = candidates if candidates is not None else [m for m in ALLOY_ROSTER if _production_eligible(m)]
|
|
|
|
|
|
|
| 453 |
if not pool:
|
| 454 |
-
return {"error": "no production-eligible open-weight models in pool"
|
|
|
|
| 455 |
|
| 456 |
_hint = (task_hint or "").lower()
|
| 457 |
_code_kw = ("code", "edit", "debug", "refactor", "implement", "pr", "fix",
|
|
@@ -575,14 +584,19 @@ def consensus(votes: list[dict], require_high_stakes: bool = False) -> dict:
|
|
| 575 |
def alloy_governed_suggest(prompt: str, task_hint: str = "code", lam: float = 0.9,
|
| 576 |
calib_scores: Optional[list[float]] = None,
|
| 577 |
do_consensus: bool = False,
|
| 578 |
-
sign_fn=None, max_tokens: int = 256
|
|
|
|
| 579 |
"""One unified alloy call:
|
| 580 |
route (C20/W7-5) -> generate (REAL local OR honest tower label)
|
| 581 |
-> conformal band (W5-3/W7-4) -> optional consensus (C10-C12)
|
| 582 |
-> signed receipt (host signer, P6).
|
| 583 |
NEVER fabricates output. If no local backend, served_locally=False + honest label.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
"""
|
| 585 |
-
decision = route(task_hint, lam)
|
| 586 |
if "error" in decision:
|
| 587 |
return {"error": decision["error"]}
|
| 588 |
chosen = decision["chosen"]
|
|
@@ -675,7 +689,14 @@ def register(app, ns: str = "a11oy", sign_fn=None) -> dict:
|
|
| 675 |
POST /api/a11oy/v1/alloy/route — C20/W7-5 model selection
|
| 676 |
POST /api/a11oy/v1/alloy/conformal — W5-3/W7-4 calibrated band
|
| 677 |
POST /api/a11oy/v1/alloy/consensus — C10-C12 Byzantine vote
|
| 678 |
-
POST /api/a11oy/v1/alloy/suggest — governed suggest (route+gen+band+receipt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
"""
|
| 680 |
from fastapi.responses import JSONResponse
|
| 681 |
from fastapi import Request
|
|
@@ -748,7 +769,45 @@ def register(app, ns: str = "a11oy", sign_fn=None) -> dict:
|
|
| 748 |
body.get("prompt", ""), body.get("task_hint", "code"),
|
| 749 |
float(body.get("lambda", body.get("lam", 0.9))),
|
| 750 |
body.get("calib_scores"), bool(body.get("do_consensus", False)),
|
| 751 |
-
sign_fn=sign_fn, max_tokens=int(body.get("max_tokens", 256))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 752 |
|
| 753 |
for p in (base + "/roster", stripped + "/roster"):
|
| 754 |
_routes_first(p, _roster, ["GET"])
|
|
@@ -762,7 +821,10 @@ def register(app, ns: str = "a11oy", sign_fn=None) -> dict:
|
|
| 762 |
_routes_first(p, _consensus, ["POST"])
|
| 763 |
for p in (base + "/suggest", stripped + "/suggest"):
|
| 764 |
_routes_first(p, _suggest, ["POST"])
|
|
|
|
|
|
|
| 765 |
|
| 766 |
-
return {"ns": ns,
|
|
|
|
| 767 |
"alloy_models": len(ALLOY_ROSTER), "unification": unify,
|
| 768 |
"backend_available": backend_available()}
|
|
|
|
| 434 |
return [e / z for e in exps]
|
| 435 |
|
| 436 |
def route(task_hint: str, lam: float, candidates: Optional[list[dict]] = None,
|
| 437 |
+
prefer_code: bool = True, temp: float = 0.7,
|
| 438 |
+
force_tier: Optional[str] = None) -> dict:
|
| 439 |
"""C20-stable, W7-5-bracketed model selection over the open-weight alloy.
|
| 440 |
|
| 441 |
C20 (proven, Mathlib-free order-preservation core): the softmax over per-model
|
|
|
|
| 449 |
lies inside the envelope.
|
| 450 |
|
| 451 |
Founder directive: weight the router toward DeepSeek for CODING tasks.
|
| 452 |
+
|
| 453 |
+
`force_tier` (optional) RESTRICTS the candidate pool to a single tier_band
|
| 454 |
+
(e.g. "demo_cpu") BEFORE scoring. This is how the public HTTP demo route
|
| 455 |
+
exercises the CPU-serveable bundled GGUF: without it the capable tower tier
|
| 456 |
+
always outscores the demo tier and the local model is never selected.
|
| 457 |
"""
|
| 458 |
pool = candidates if candidates is not None else [m for m in ALLOY_ROSTER if _production_eligible(m)]
|
| 459 |
+
if force_tier:
|
| 460 |
+
pool = [m for m in pool if m.get("tier_band") == force_tier]
|
| 461 |
if not pool:
|
| 462 |
+
return {"error": ("no production-eligible open-weight models in pool"
|
| 463 |
+
+ (" for tier_band=%r" % force_tier if force_tier else ""))}
|
| 464 |
|
| 465 |
_hint = (task_hint or "").lower()
|
| 466 |
_code_kw = ("code", "edit", "debug", "refactor", "implement", "pr", "fix",
|
|
|
|
| 584 |
def alloy_governed_suggest(prompt: str, task_hint: str = "code", lam: float = 0.9,
|
| 585 |
calib_scores: Optional[list[float]] = None,
|
| 586 |
do_consensus: bool = False,
|
| 587 |
+
sign_fn=None, max_tokens: int = 256,
|
| 588 |
+
force_tier: Optional[str] = None) -> dict:
|
| 589 |
"""One unified alloy call:
|
| 590 |
route (C20/W7-5) -> generate (REAL local OR honest tower label)
|
| 591 |
-> conformal band (W5-3/W7-4) -> optional consensus (C10-C12)
|
| 592 |
-> signed receipt (host signer, P6).
|
| 593 |
NEVER fabricates output. If no local backend, served_locally=False + honest label.
|
| 594 |
+
|
| 595 |
+
`force_tier` pins the router to a single tier_band. Pass "demo_cpu" to make
|
| 596 |
+
this call actually run the bundled CPU GGUF through `_local_generate` — the
|
| 597 |
+
public /demo route and `tier=demo_cpu` on /suggest use this.
|
| 598 |
"""
|
| 599 |
+
decision = route(task_hint, lam, force_tier=force_tier)
|
| 600 |
if "error" in decision:
|
| 601 |
return {"error": decision["error"]}
|
| 602 |
chosen = decision["chosen"]
|
|
|
|
| 689 |
POST /api/a11oy/v1/alloy/route — C20/W7-5 model selection
|
| 690 |
POST /api/a11oy/v1/alloy/conformal — W5-3/W7-4 calibrated band
|
| 691 |
POST /api/a11oy/v1/alloy/consensus — C10-C12 Byzantine vote
|
| 692 |
+
POST /api/a11oy/v1/alloy/suggest — governed suggest (route+gen+band+receipt);
|
| 693 |
+
accepts {"tier":"demo_cpu"} to pin the tier
|
| 694 |
+
GET /api/a11oy/v1/alloy/demo — LIVE local demo: runs the bundled CPU GGUF
|
| 695 |
+
POST /api/a11oy/v1/alloy/demo via _local_generate (force_tier=demo_cpu),
|
| 696 |
+
returns served_locally + backend=="llama.cpp"
|
| 697 |
+
when present, else an HONEST tower-side label.
|
| 698 |
+
GET uses a default prompt so an uptime /
|
| 699 |
+
smoke check can hit it with no body.
|
| 700 |
"""
|
| 701 |
from fastapi.responses import JSONResponse
|
| 702 |
from fastapi import Request
|
|
|
|
| 769 |
body.get("prompt", ""), body.get("task_hint", "code"),
|
| 770 |
float(body.get("lambda", body.get("lam", 0.9))),
|
| 771 |
body.get("calib_scores"), bool(body.get("do_consensus", False)),
|
| 772 |
+
sign_fn=sign_fn, max_tokens=int(body.get("max_tokens", 256)),
|
| 773 |
+
force_tier=(body.get("tier") or body.get("force_tier"))))
|
| 774 |
+
|
| 775 |
+
async def _demo(request: "Request"):
|
| 776 |
+
# LIVE local demo: pin the router to the CPU GGUF demo tier so the bundled
|
| 777 |
+
# llama.cpp model is ACTUALLY exercised over HTTP (not the tower tier).
|
| 778 |
+
# GET (no body) uses a default prompt so uptime/smoke checks can hit it
|
| 779 |
+
# bare; POST accepts {"prompt", "max_tokens", ...}. NEVER fakes output —
|
| 780 |
+
# if no GGUF is mounted it returns served_locally=False + an honest label.
|
| 781 |
+
if request.method == "POST":
|
| 782 |
+
try:
|
| 783 |
+
body = await request.json()
|
| 784 |
+
except Exception:
|
| 785 |
+
body = {}
|
| 786 |
+
else:
|
| 787 |
+
body = dict(request.query_params)
|
| 788 |
+
prompt = (body.get("prompt") or
|
| 789 |
+
"Write a Python function add(a, b) that returns their sum.")
|
| 790 |
+
try:
|
| 791 |
+
max_tokens = int(body.get("max_tokens", 128))
|
| 792 |
+
except (TypeError, ValueError):
|
| 793 |
+
max_tokens = 128
|
| 794 |
+
out = alloy_governed_suggest(
|
| 795 |
+
prompt, str(body.get("task_hint", "code")),
|
| 796 |
+
float(body.get("lambda", body.get("lam", 0.9))),
|
| 797 |
+
body.get("calib_scores"), bool(body.get("do_consensus", False)),
|
| 798 |
+
sign_fn=sign_fn, max_tokens=max_tokens, force_tier="demo_cpu")
|
| 799 |
+
gen = out.get("generation", {}) if isinstance(out, dict) else {}
|
| 800 |
+
# Surface the proof fields at the TOP level so an uptime / smoke check can
|
| 801 |
+
# assert on them directly (backend == "llama.cpp", served_locally == True).
|
| 802 |
+
out["tier_forced"] = "demo_cpu"
|
| 803 |
+
out["backend"] = gen.get("backend")
|
| 804 |
+
out["served_locally"] = bool(gen.get("served_locally"))
|
| 805 |
+
out["live_demo"] = out["served_locally"]
|
| 806 |
+
if not out["served_locally"]:
|
| 807 |
+
out["honest_label"] = (gen.get("honest_label") or
|
| 808 |
+
"local CPU backend unavailable — honest tower-side label "
|
| 809 |
+
"(mount /app/models/*.gguf or set A11OY_ALLOY_GGUF).")
|
| 810 |
+
return JSONResponse(out)
|
| 811 |
|
| 812 |
for p in (base + "/roster", stripped + "/roster"):
|
| 813 |
_routes_first(p, _roster, ["GET"])
|
|
|
|
| 821 |
_routes_first(p, _consensus, ["POST"])
|
| 822 |
for p in (base + "/suggest", stripped + "/suggest"):
|
| 823 |
_routes_first(p, _suggest, ["POST"])
|
| 824 |
+
for p in (base + "/demo", stripped + "/demo"):
|
| 825 |
+
_routes_first(p, _demo, ["GET", "POST"])
|
| 826 |
|
| 827 |
+
return {"ns": ns,
|
| 828 |
+
"endpoints": ["roster", "health", "route", "conformal", "consensus", "suggest", "demo"],
|
| 829 |
"alloy_models": len(ALLOY_ROSTER), "unification": unify,
|
| 830 |
"backend_available": backend_available()}
|