Add training-kit/upload_ppocr_checkpoint.py
Browse files
training-kit/upload_ppocr_checkpoint.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Upload a PP-OCR checkpoint prefix and its training metadata to Hugging Face."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import argparse
|
| 7 |
+
import json
|
| 8 |
+
import os
|
| 9 |
+
from datetime import datetime, timezone
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
from huggingface_hub import HfApi
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def main() -> None:
|
| 16 |
+
parser = argparse.ArgumentParser()
|
| 17 |
+
parser.add_argument("--repo-id", required=True)
|
| 18 |
+
parser.add_argument("--checkpoint-prefix", type=Path, required=True)
|
| 19 |
+
parser.add_argument("--tag", required=True)
|
| 20 |
+
parser.add_argument("--metric", type=float, required=True)
|
| 21 |
+
parser.add_argument("--config", type=Path, required=True)
|
| 22 |
+
parser.add_argument("--dictionary", type=Path, required=True)
|
| 23 |
+
parser.add_argument("--token-file", type=Path, default=Path("/root/.hf_token"))
|
| 24 |
+
args = parser.parse_args()
|
| 25 |
+
|
| 26 |
+
token = args.token_file.read_text().strip()
|
| 27 |
+
api = HfApi(token=token)
|
| 28 |
+
api.create_repo(args.repo_id, repo_type="model", exist_ok=True)
|
| 29 |
+
destination = f"checkpoints/{args.tag}"
|
| 30 |
+
|
| 31 |
+
uploaded = []
|
| 32 |
+
for suffix in (".pdparams", ".states"):
|
| 33 |
+
source = Path(str(args.checkpoint_prefix) + suffix)
|
| 34 |
+
if source.exists():
|
| 35 |
+
api.upload_file(
|
| 36 |
+
path_or_fileobj=source,
|
| 37 |
+
path_in_repo=f"{destination}/{source.name}",
|
| 38 |
+
repo_id=args.repo_id,
|
| 39 |
+
repo_type="model",
|
| 40 |
+
)
|
| 41 |
+
uploaded.append(source.name)
|
| 42 |
+
for source in (args.config, args.dictionary):
|
| 43 |
+
api.upload_file(
|
| 44 |
+
path_or_fileobj=source,
|
| 45 |
+
path_in_repo=f"{destination}/{source.name}",
|
| 46 |
+
repo_id=args.repo_id,
|
| 47 |
+
repo_type="model",
|
| 48 |
+
)
|
| 49 |
+
uploaded.append(source.name)
|
| 50 |
+
|
| 51 |
+
receipt = {
|
| 52 |
+
"tag": args.tag,
|
| 53 |
+
"metric": args.metric,
|
| 54 |
+
"checkpoint_prefix": args.checkpoint_prefix.name,
|
| 55 |
+
"uploaded": uploaded,
|
| 56 |
+
"uploaded_at": datetime.now(timezone.utc).isoformat(),
|
| 57 |
+
}
|
| 58 |
+
api.upload_file(
|
| 59 |
+
path_or_fileobj=json.dumps(receipt, indent=2).encode(),
|
| 60 |
+
path_in_repo="latest-checkpoint.json",
|
| 61 |
+
repo_id=args.repo_id,
|
| 62 |
+
repo_type="model",
|
| 63 |
+
)
|
| 64 |
+
print(json.dumps(receipt))
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
main()
|