portable-ai-runner / scripts /make_zip.py
Johnny5b's picture
make_zip: self-verify zip entry paths after build
dc8f676 verified
Raw History Blame Contribute Delete
3.85 kB
#!/usr/bin/env python3
"""Build a self-contained CORTEX drive zip: app + engine + weights.
Run AT HOME (online) AFTER `huggingface-cli download ... --local-dir CORTEX`
(weights are included in the repo download; setup.py only re-verifies them).
The result runs cold on any machine with Python 3 - no internet, no pip installs
(serve.py needs only the standard library). Copy the zip to a USB stick.
"""
import hashlib
import json
import os
import time
import zipfile
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PINNED = {
"Ornith-1.5-9B-Q4_K_M.gguf": "70c112196e0b7023803c9762752e46d29e612a92c83f995bc3ba1ceb07e8fab6",
"Qwen3-0.6B-Q8_0.gguf": "9465e63a22add5354d9bb4b99e90117043c7124007664907259bd16d043bb031",
}
def sha256(p):
h = hashlib.sha256()
with open(p, "rb") as f:
for c in iter(lambda: f.read(1 << 22), b""):
h.update(c)
return h.hexdigest()
def main():
mj = json.load(open(os.path.join(ROOT, "models", "models.json")))
ggufs = []
for m in mj["models"]:
fname = m.get("file") or os.path.basename(m.get("url") or "")
assert fname.endswith(".gguf"), "cannot resolve gguf filename for entry: " + json.dumps(m)
p = os.path.join(ROOT, "models", "gguf", fname)
assert os.path.exists(p), "missing " + fname + " - run setup.py or re-download"
got = sha256(p)
want = PINNED.get(fname) or m.get("sha256")
if want:
assert got == want, "sha256 MISMATCH for " + fname + " - redownload"
print("verified ", fname, got[:16], "...")
else:
print("WARNING: no pinned sha for", fname, "- hashing only:", got[:16], "...")
ggufs.append((p, fname, got))
wasm = os.path.join(ROOT, "vendor", "wllama", "esm", "wasm", "wllama.wasm")
assert os.path.exists(wasm), "vendored engine missing - run setup.py first"
out = os.path.join(os.path.dirname(ROOT), "CORTEX-DRIVE-" + time.strftime("%Y%m%d") + ".zip")
skip_dirs = {"__pycache__", ".git", ".cache"}
files = []
for dp, dn, fn in os.walk(ROOT):
dn[:] = [d for d in dn if d not in skip_dirs]
for f in fn:
if f.endswith(".pyc"):
continue
full = os.path.join(dp, f)
arc = os.path.relpath(full, ROOT).replace(os.sep, "/")
if arc.startswith("models/gguf"):
continue
files.append((full, arc))
with zipfile.ZipFile(out, "w", zipfile.ZIP_STORED) as z:
for full, arc in files:
comp = zipfile.ZIP_STORED if arc.startswith("vendor") else zipfile.ZIP_DEFLATED
z.write(full, arc, compress_type=comp)
for p, arc, got in ggufs:
z.write(p, "models/gguf/" + arc, compress_type=zipfile.ZIP_STORED)
man = {"built": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"files": {arc: {"sha256": sha256(full), "bytes": os.path.getsize(full)}
for full, arc in files},
"weights": {arc: {"sha256": got, "bytes": os.path.getsize(p)} for p, arc, got in ggufs}}
z.writestr("CORTEX-ZIP-MANIFEST.json", json.dumps(man, indent=2))
with zipfile.ZipFile(out) as z:
names = set(z.namelist())
for p, arc, got in ggufs:
assert "models/gguf/" + arc in names, "zip entry missing: models/gguf/" + arc
assert "index.html" in names and "serve.py" in names
assert "vendor/wllama/esm/wasm/wllama.wasm" in names
assert "CORTEX-ZIP-MANIFEST.json" in names
print("zip:", out)
print("zip bytes:", os.path.getsize(out))
print("zip sha256:", sha256(out))
print("weights inside:", ", ".join(g[1] + " (" + g[2][:16] + "...)" for g in ggufs))
print("BLIND-RUN READY - copy this zip to the USB stick.")
if __name__ == "__main__":
main()