CheezOne's picture
Release
e374d55 verified
Raw History Blame Contribute Delete
3.28 kB
"""Publish a trained model directory to the Hugging Face Hub (model repo + training scripts).
Usage: HF_TOKEN=... python publish.py <model_dir> <repo_id> [--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/")
ap.add_argument("--scripts", nargs="*", default=None, help="scripts/ files to ship (default: PII pipeline)")
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 (a.scripts if a.scripts is not None else 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)))
secrets = [os.environ[k].encode() for k in ("HF_TOKEN", "CNB_TOKEN") if os.environ.get(k)]
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 not any(x in txt for x in secrets) 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()