MiniMax-H3-Image-mlx / scripts /publish_hf_stats_v041.py
devin-lai's picture
v0.4.1: enable Hub download statistics
c138412 verified
Raw History Blame Contribute Delete
5.86 kB
"""Publish the v0.4.1 Hub download-statistics metadata patch.
This changes only small metadata/documentation files. It keeps the v0.4.0
commits and tags intact and records each new immutable revision locally.
"""
from __future__ import annotations
import hashlib
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download # noqa: E402
from h3_mlx.hub import read_json # noqa: E402
from scripts.prepare_hf_release import component_config # noqa: E402
from scripts.publish_hf_release import verify_remote # noqa: E402
PROJECT = "devin-lai/MiniMax-H3-Image-mlx"
RELEASE = "v0.4.1"
COMPONENTS = ("shared", "bf16", "packed12", "q8", "q4", "examples")
MAIN_FILES = (
"README.md",
"README.zh-CN.md",
"USAGE.md",
"MODEL_INDEX.md",
"RELEASE.md",
"docs/VALIDATION.md",
"pyproject.toml",
"tests/test_hub.py",
"scripts/prepare_hf_release.py",
"scripts/publish_hf_stats_v041.py",
"h3_mlx/model_index.json",
"config.json",
)
def json_bytes(value):
return (json.dumps(value, indent=2, ensure_ascii=False) + "\n").encode()
def add_to_manifest(manifest, name, content):
manifest["files"][name] = {
"bytes": len(content),
"sha256": hashlib.sha256(content).hexdigest(),
}
def save_state(path, state):
path.write_bytes(json_bytes(state))
def publish_patch(api, repo_id, old_revision, changes):
current = api.model_info(repo_id)
if current.sha != old_revision:
raise RuntimeError(f"Repository changed since v0.4.0: {repo_id}")
manifest = read_json(hf_hub_download(repo_id, "files.json", revision=old_revision))
if manifest.get("format") != "h3-mlx-files-v1":
raise ValueError(f"Unexpected manifest format: {repo_id}")
for name, content in changes.items():
add_to_manifest(manifest, name, content)
changes = {**changes, "files.json": json_bytes(manifest)}
operations = [
CommitOperationAdd(path_in_repo=name, path_or_fileobj=content)
for name, content in changes.items()
]
commit = api.create_commit(
repo_id=repo_id,
operations=operations,
parent_commit=old_revision,
commit_message=f"{RELEASE}: enable Hub download statistics",
)
revision = verify_remote(api, repo_id, manifest)
if revision != commit.oid:
raise RuntimeError(f"Repository changed during verification: {repo_id}")
api.create_tag(repo_id, tag=RELEASE, revision=revision, exist_ok=True)
if api.model_info(repo_id, revision=RELEASE).sha != revision:
raise RuntimeError(f"Release tag points to a different commit: {repo_id}")
if api.model_info(repo_id, revision="v0.4.0").sha != old_revision:
raise RuntimeError(f"Original v0.4.0 tag changed: {repo_id}")
return {"repo_id": repo_id, "old_revision": old_revision, "revision": revision}
def main():
api = HfApi()
if api.whoami()["name"] != "devin-lai":
raise RuntimeError("Expected the devin-lai publishing account")
old_state = read_json(ROOT / ".release/publication.json")
state_path = ROOT / ".release/stats-v0.4.1.json"
state = read_json(state_path) if state_path.exists() else {}
catalog = read_json(ROOT / "h3_mlx/model_index.json")
# Check every untouched repository before making the first remote change.
for name in (*COMPONENTS, "main"):
if name in state:
continue
info = api.model_info(old_state[name]["repo_id"])
if info.sha != old_state[name]["revision"]:
raise RuntimeError(f"Repository changed since v0.4.0: {info.id}")
if name != "main" and (info.private or info.gated != "manual"):
raise RuntimeError(f"Unexpected access settings: {info.id}")
if name == "main" and (info.private or info.gated):
raise RuntimeError(f"Unexpected access settings: {info.id}")
if any(tag.name == RELEASE for tag in api.list_repo_refs(info.id).tags):
raise RuntimeError(f"Release tag already exists: {info.id}")
for name in COMPONENTS:
if name in state:
continue
entry = old_state[name]
config = json_bytes(component_config(name, catalog))
changes = {"config.json": config}
if name != "examples":
old_card = Path(
hf_hub_download(entry["repo_id"], "README.md", revision=entry["revision"])
).read_text()
new_card = old_card.replace(
"**Release:** v0.4.0.",
"**Release:** v0.4.1 (v0.4.0 weights with download-count metadata).",
)
if new_card == old_card:
raise RuntimeError(f"Unexpected model card: {entry['repo_id']}")
changes["README.md"] = new_card.encode()
state[name] = publish_patch(api, entry["repo_id"], entry["revision"], changes)
save_state(state_path, state)
print(f"Verified {name}: {state[name]['revision']}", flush=True)
catalog["release"] = RELEASE
catalog["shared"]["revision"] = state["shared"]["revision"]
for name in catalog["variants"]:
catalog["variants"][name]["revision"] = state[name]["revision"]
(ROOT / "h3_mlx/model_index.json").write_bytes(json_bytes(catalog))
(ROOT / "config.json").write_bytes(json_bytes(component_config("runtime", catalog)))
if "main" not in state:
changes = {name: (ROOT / name).read_bytes() for name in MAIN_FILES}
entry = old_state["main"]
state["main"] = publish_patch(api, entry["repo_id"], entry["revision"], changes)
save_state(state_path, state)
print(f"Verified main: {state['main']['revision']}", flush=True)
print(json.dumps(state, indent=2))
if __name__ == "__main__":
main()