#!/usr/bin/env python3 """Download, verify, and run the released Scotty checkpoint.""" from __future__ import annotations import argparse from contextlib import contextmanager import hashlib import json import os from pathlib import Path import shutil import tempfile EXPECTED_SHA256 = "e79b758aea590bcc4504e6b622c206080a7d1fedc49d572d3a185321b8e2eeda" DEFAULT_REPO = "DarwinGuru/scotty" PUBLIC_CONFIG = "scotty_config.json" LEGACY_CONFIG = "rl_agent_config.json" def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as stream: for chunk in iter(lambda: stream.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def checkpoint_path(checkpoint: Path | None, repo: str, revision: str | None) -> Path: if checkpoint is not None: return checkpoint.expanduser().resolve() from huggingface_hub import snapshot_download return Path( snapshot_download( repo_id=repo, revision=revision, allow_patterns=( "model.safetensors", PUBLIC_CONFIG, LEGACY_CONFIG, "encoder/*", "tokenizer/*", ), ) ) def checkpoint_config(checkpoint: Path) -> Path: for name in (PUBLIC_CONFIG, LEGACY_CONFIG): path = checkpoint / name if path.is_file(): return path raise FileNotFoundError( f"Checkpoint is missing {PUBLIC_CONFIG} (or legacy {LEGACY_CONFIG})" ) @contextmanager def laya_checkpoint_view(checkpoint: Path): """Expose Scotty's neutral config name through Laya's legacy file layout.""" legacy = checkpoint / LEGACY_CONFIG if legacy.is_file(): yield checkpoint return public = checkpoint_config(checkpoint) with tempfile.TemporaryDirectory(prefix="scotty-laya-") as directory: view = Path(directory) shutil.copy2(public, view / LEGACY_CONFIG) model_source = checkpoint / "model.safetensors" model_target = view / "model.safetensors" try: os.link(model_source, model_target) except OSError: try: os.symlink(model_source, model_target) except OSError: shutil.copy2(model_source, model_target) for name in ("encoder", "tokenizer"): source = checkpoint / name target = view / name try: os.symlink(source, target, target_is_directory=True) except OSError: shutil.copytree(source, target) yield view def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--checkpoint", type=Path, help="Local model directory; otherwise download from the Hub") parser.add_argument("--repo", default=DEFAULT_REPO) parser.add_argument("--revision", help="Optional immutable Hugging Face commit") parser.add_argument("--verify-only", action="store_true") parser.add_argument("--device", default="cpu", help="cpu, cuda, or mps") parser.add_argument("--state", help="Situation text to score") parser.add_argument("--instruction", help="Decision question") parser.add_argument("--option", action="append", default=[], help="Candidate answer; repeat as needed") args = parser.parse_args() checkpoint = checkpoint_path(args.checkpoint, args.repo, args.revision) config = checkpoint_config(checkpoint) required = ( checkpoint / "model.safetensors", checkpoint / "encoder/config.json", checkpoint / "tokenizer/tokenizer.json", checkpoint / "tokenizer/tokenizer_config.json", ) missing = [str(path.relative_to(checkpoint)) for path in required if not path.is_file()] if missing: raise FileNotFoundError(f"Checkpoint is missing: {', '.join(missing)}") actual = sha256(checkpoint / "model.safetensors") if actual != EXPECTED_SHA256: raise ValueError(f"model.safetensors SHA-256 mismatch: expected {EXPECTED_SHA256}, got {actual}") if args.verify_only: print(json.dumps({"checkpoint": str(checkpoint), "config": config.name, "model_safetensors_sha256": actual, "status": "verified"}, indent=2)) return 0 if not args.state or not args.instruction or len(args.option) < 2: parser.error("inference needs --state, --instruction, and at least two --option values") if len(set(args.option)) != len(args.option): parser.error("--option values must be unique") import laya with laya_checkpoint_view(checkpoint) as laya_checkpoint: agent = laya.load( str(laya_checkpoint), device=args.device, expected_sha256={"model.safetensors": EXPECTED_SHA256}, ) question = { "decision": { "type": "choice", "instructions": args.instruction, "criteria": args.option, } } result = agent.predict(args.state, question) result["model"] = "Scotty" print(json.dumps(result, indent=2, sort_keys=True)) return 0 if __name__ == "__main__": raise SystemExit(main())