"""Assemble the folder that is uploaded to Hugging Face: the weights, the exact question, the scores, the code, the licences and the card. Only what `laya.load` reads is copied from the run, plus what a user needs to ask the question the way the model was trained, to check its numbers, and to credit its sources. The folder is checked for local paths before it is finished, because Laya's training config can record where the data lived, and a home folder in a public file names the person who built it. Run it with `python -m banking77.package seed0 --repo /laya-banking77`. """ from __future__ import annotations import argparse import hashlib import json import re import shutil from pathlib import Path from typing import List from banking77.data import QUESTION_ID, data_dir, home, question REPO_ROOT = Path(__file__).resolve().parent.parent PLACEHOLDER = "OWNER/laya-banking77" MODEL_FILES = ("rl_agent_config.json", "model.safetensors", "questions.json") MODEL_DIRS = ("tokenizer", "encoder") CODE_FILES = ("README.md", "LICENSE", "NOTICE", "requirements.txt", ".gitignore") CODE_DIRS = ("banking77", "tests") TEXT_SUFFIXES = (".json", ".md", ".txt", ".py", "") def check_repo_id(repo: str) -> str: """A Hub model id, `owner/name`, with the characters the Hub allows.""" if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*", repo) or repo == PLACEHOLDER: raise ValueError("--repo must be a Hub id like someone/laya-banking77, got %r" % (repo,)) return repo def looks_local(value: str) -> bool: """An absolute local path (`C:\\...`, `/home/...`, `~/...`, `\\\\server\\...`), not a Hub ID like `org/model`.""" return bool(re.match(r"^([A-Za-z]:[\\/]|/|~|\\\\)", value)) def local_path_leaks(folder: Path, needles: List[str]) -> List[str]: """Files under `folder` that mention any of `needles`, compared case-insensitively and with either slash.""" hits = [] norm = [n.lower().replace("\\", "/") for n in needles if n] for path in folder.rglob("*"): # Configs, cards and code are small. The tokenizer's vocabulary is not, and short names can match a word piece. if not path.is_file() or path.suffix not in TEXT_SUFFIXES or path.stat().st_size > 1_000_000: continue text = path.read_text(encoding="utf-8", errors="ignore").lower().replace("\\\\", "/").replace("\\", "/") if any(n in text for n in norm): hits.append(str(path.relative_to(folder))) return hits def scrub_config(path: Path) -> None: """Drop the training block's record of local file paths from the saved Laya config, keep everything else.""" with open(path, encoding="utf-8") as f: cfg = json.load(f) training = cfg.get("training") if isinstance(training, dict): for key in list(training): if isinstance(training[key], str) and looks_local(training[key]): training.pop(key) with open(path, "w", encoding="utf-8") as f: json.dump(cfg, f, indent=2) def fill_repo(path: Path, repo: str) -> None: text = path.read_text(encoding="utf-8") path.write_text(text.replace(PLACEHOLDER, repo), encoding="utf-8", newline="\n") def sha256sums(folder: Path) -> str: lines = [] for path in sorted(p for p in folder.rglob("*") if p.is_file() and p.name != "SHA256SUMS"): h = hashlib.sha256() with open(path, "rb") as f: for block in iter(lambda: f.read(1 << 20), b""): h.update(block) lines.append("%s %s" % (h.hexdigest(), path.relative_to(folder).as_posix())) return "\n".join(lines) + "\n" def copy_scores(run_dir: Path, out: Path) -> None: """The run's scores, without the field that records where the model was loaded from.""" for name in ("eval.json", "offtopic.json"): with open(run_dir / name, encoding="utf-8") as f: result = json.load(f) result.pop("model", None) with open(out / name, "w", encoding="utf-8") as f: json.dump(result, f, indent=2) def copy_code(out: Path) -> None: code = out / "code" code.mkdir() for name in CODE_FILES: shutil.copy2(REPO_ROOT / name, code / name) for name in CODE_DIRS: shutil.copytree(REPO_ROOT / name, code / name, ignore=shutil.ignore_patterns("__pycache__", "*.pyc")) def build(run: str, repo: str) -> Path: repo = check_repo_id(repo) run_dir = home() / "runs" / run src = run_dir / "model" out = run_dir / "release" if out.exists(): shutil.rmtree(out) out.mkdir(parents=True) for name in MODEL_FILES: shutil.copy2(src / name, out / name) for name in MODEL_DIRS: shutil.copytree(src / name, out / name) scrub_config(out / "rl_agent_config.json") # Laya's fine-tune writes the question it trained on to questions.json. It must be the one this repo defines, # or the card would tell users to ask a question the model never saw. with open(data_dir() / "labels.json", encoding="utf-8") as f: names = json.load(f)["names"] with open(out / "questions.json", encoding="utf-8") as f: if json.load(f) != {QUESTION_ID: question(names)}: raise RuntimeError("the run's questions.json is not the question banking77.data defines") with open(out / "labels.json", "w", encoding="utf-8") as f: json.dump({"banking77_label_names": names}, f, indent=2) copy_scores(run_dir, out) shutil.copy2(REPO_ROOT / "LICENSE", out / "LICENSE") shutil.copy2(REPO_ROOT / "NOTICE", out / "NOTICE") shutil.copy2(REPO_ROOT / "MODEL_CARD.md", out / "README.md") copy_code(out) for card in (out / "README.md", out / "code" / "README.md"): fill_repo(card, repo) leaks = local_path_leaks(out, [str(Path.home()), Path.home().name, str(home())]) if leaks: raise RuntimeError("these release files mention a local path or user name: %s" % ", ".join(leaks)) left = [str(p.relative_to(out)) for p in out.rglob("*.md") if PLACEHOLDER in p.read_text(encoding="utf-8")] if left: raise RuntimeError("these files still name the placeholder repo: %s" % ", ".join(left)) (out / "SHA256SUMS").write_text(sha256sums(out), encoding="utf-8", newline="\n") return out def main() -> None: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("run", help="the run to package, e.g. seed0") parser.add_argument("--repo", required=True, help="the Hugging Face model id it will be uploaded as") args = parser.parse_args() print(build(args.run, args.repo)) if __name__ == "__main__": main()