"""Publish a trained model directory to the Hugging Face Hub (model repo + training scripts). Usage: HF_TOKEN=... python publish.py [--private] Uploads: weights, tokenizer, config, onnx/, README.md, eval JSONs, and scripts/ (for reproducibility). Never uploads checkpoints, data, or anything containing tokens. """ import argparse, os, re, shutil, tempfile from huggingface_hub import HfApi SCRIPTS = ["common.py", "prep_openpii.py", "gen_zh_realistic.py", "train.py", "export_onnx.py", "quant_sweep.py", "bench_redaction.py", "error_analysis.py", "make_card.py", "publish.py"] def main(): ap = argparse.ArgumentParser() ap.add_argument("model_dir"); ap.add_argument("repo_id"); ap.add_argument("--private", action="store_true") ap.add_argument("--extra", nargs="*", default=[], help="extra files to put under eval/") a = ap.parse_args() api = HfApi(token=os.environ["HF_TOKEN"]) api.create_repo(a.repo_id, repo_type="model", private=a.private, exist_ok=True) here = os.path.dirname(os.path.abspath(__file__)) with tempfile.TemporaryDirectory() as tmp: for f in os.listdir(a.model_dir): src = os.path.join(a.model_dir, f) if f in ("ckpt", "train_log.jsonl.bak") or f.startswith("."): continue if os.path.isdir(src): if f == "onnx": os.makedirs(os.path.join(tmp, "onnx")) for o in ("model.onnx", "model_quantized.onnx"): if os.path.exists(os.path.join(src, o)): shutil.copy(os.path.join(src, o), os.path.join(tmp, "onnx", o)) continue dst = os.path.join(tmp, "eval", f) if f.endswith(".json") and f.startswith(("eval", "onnx_report")) else os.path.join(tmp, f) os.makedirs(os.path.dirname(dst), exist_ok=True); shutil.copy(src, dst) os.makedirs(os.path.join(tmp, "scripts"), exist_ok=True) for s in SCRIPTS: if os.path.exists(os.path.join(here, s)): shutil.copy(os.path.join(here, s), os.path.join(tmp, "scripts", s)) for e in a.extra: os.makedirs(os.path.join(tmp, "eval"), exist_ok=True); shutil.copy(e, os.path.join(tmp, "eval", os.path.basename(e))) secret = os.environ["HF_TOKEN"].encode() for root, _, files in os.walk(tmp): # safety: refuse to upload anything that looks like a token for f in files: p = os.path.join(root, f) if os.path.getsize(p) < 5_000_000: txt = open(p, "rb").read() assert secret not in txt and not re.search(rb"hf_[A-Za-z0-9]{30,}", txt), f"token-like string in {p}" print("uploading:", sorted(os.path.relpath(os.path.join(r, f), tmp) for r, _, fs in os.walk(tmp) for f in fs)) api.upload_folder(folder_path=tmp, repo_id=a.repo_id, repo_type="model", commit_message="Release") print("https://huggingface.co/" + a.repo_id) if __name__ == "__main__": main()