#!/usr/bin/env python3 """Validate and publish this folder as a public Hugging Face model repository.""" from __future__ import annotations import argparse import subprocess import sys from pathlib import Path from huggingface_hub import HfApi ROOT = Path(__file__).resolve().parents[1] EXPECTED_REPOSITORY_NAME = "kokoro-kmp-models" def run_validation(skip_web: bool) -> None: subprocess.run([sys.executable, ROOT / "scripts/build_catalog.py"], check=True, cwd=ROOT) subprocess.run([sys.executable, ROOT / "scripts/validate_package.py"], check=True, cwd=ROOT) if not skip_web: subprocess.run(["npm", "run", "validate:web"], check=True, cwd=ROOT) def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument("repo_id", help="Hugging Face repo, e.g. username/kokoro-kmp-models") parser.add_argument("--tag", default="v2.1.1") parser.add_argument("--skip-web-validation", action="store_true") return parser.parse_args() def main() -> None: args = parse_args() parts = args.repo_id.split("/") if len(parts) != 2 or parts[1] != EXPECTED_REPOSITORY_NAME: raise SystemExit( f"repo_id must be /{EXPECTED_REPOSITORY_NAME}; got {args.repo_id!r}" ) run_validation(args.skip_web_validation) api = HfApi() identity = api.whoami() writable_namespaces = {identity["name"]} writable_namespaces.update( org["name"] for org in identity.get("orgs", []) if org.get("roleInOrg") in {"admin", "write"} ) if parts[0] not in writable_namespaces: raise SystemExit(f"The authenticated account cannot publish to namespace {parts[0]!r}") api.create_repo(args.repo_id, repo_type="model", private=False, exist_ok=True) commit = api.upload_folder( repo_id=args.repo_id, repo_type="model", folder_path=ROOT, commit_message=f"Publish complete Kokoro multilingual catalog {args.tag}", ignore_patterns=[ ".git/**", "node_modules/**", "**/__pycache__/**", "**/*.pyc", ":memory:.ses", ], ) tags = {tag.ref: tag.target_commit for tag in api.list_repo_refs(args.repo_id).tags} tag_ref = f"refs/tags/{args.tag}" existing_target = tags.get(tag_ref) if existing_target and existing_target != commit.oid: raise SystemExit( f"Refusing to move immutable tag {args.tag}: it already points to {existing_target}" ) if not existing_target: api.create_tag( args.repo_id, repo_type="model", tag=args.tag, revision=commit.oid, tag_message=f"Complete Kokoro multilingual catalog {args.tag}", ) print(f"https://huggingface.co/{args.repo_id}/tree/{args.tag}") if __name__ == "__main__": main()