Batch mirror uploads to respect commit limits
Browse files- mirror_worker.py +46 -12
mirror_worker.py
CHANGED
|
@@ -12,7 +12,7 @@ import time
|
|
| 12 |
import json
|
| 13 |
from pathlib import Path
|
| 14 |
|
| 15 |
-
from huggingface_hub import HfApi, hf_hub_download
|
| 16 |
from modelscope_hub import HubApi
|
| 17 |
|
| 18 |
SOURCE_REPO = "daimonrobotics/Daimon-Infinity"
|
|
@@ -34,6 +34,31 @@ def retry(action, label: str, attempts: int = 6):
|
|
| 34 |
time.sleep(min(180, 2 ** attempt * 5))
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def paginated_manifest(source: HubApi) -> list[dict]:
|
| 38 |
"""List the source tree page-by-page, reauthenticating on transient 400s."""
|
| 39 |
items: list[dict] = []
|
|
@@ -88,6 +113,18 @@ def main() -> None:
|
|
| 88 |
and (item.get("Type") or item.get("type")) != "tree"
|
| 89 |
]
|
| 90 |
print(f"worker {WORKER_INDEX}/{WORKER_COUNT}: {len(selected)} files")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
for number, item in enumerate(selected, start=1):
|
| 93 |
path = item.get("Path") or item.get("path") or item.get("Name")
|
|
@@ -111,20 +148,16 @@ def main() -> None:
|
|
| 111 |
),
|
| 112 |
f"download {path}",
|
| 113 |
)
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
path_or_fileobj=str(local),
|
| 117 |
-
path_in_repo=path,
|
| 118 |
-
repo_id=DEST_REPO,
|
| 119 |
-
repo_type="dataset",
|
| 120 |
-
commit_message=f"Mirror {path}",
|
| 121 |
-
),
|
| 122 |
-
f"upload {path}",
|
| 123 |
-
)
|
| 124 |
uploaded.add(path)
|
|
|
|
|
|
|
| 125 |
print(f"done {number}/{len(selected)} {path}", flush=True)
|
| 126 |
finally:
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
# Remove any empty nested directories left by this file.
|
| 129 |
parent = local.parent
|
| 130 |
while parent != WORKDIR:
|
|
@@ -134,6 +167,7 @@ def main() -> None:
|
|
| 134 |
break
|
| 135 |
parent = parent.parent
|
| 136 |
|
|
|
|
| 137 |
shutil.rmtree(WORKDIR, ignore_errors=True)
|
| 138 |
|
| 139 |
|
|
|
|
| 12 |
import json
|
| 13 |
from pathlib import Path
|
| 14 |
|
| 15 |
+
from huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download
|
| 16 |
from modelscope_hub import HubApi
|
| 17 |
|
| 18 |
SOURCE_REPO = "daimonrobotics/Daimon-Infinity"
|
|
|
|
| 34 |
time.sleep(min(180, 2 ** attempt * 5))
|
| 35 |
|
| 36 |
|
| 37 |
+
def commit_batch(target: HfApi, batch: list[tuple[str, Path]]) -> None:
|
| 38 |
+
"""Upload many staged files in one Hub commit, respecting 429 cooldowns."""
|
| 39 |
+
operations = [
|
| 40 |
+
CommitOperationAdd(path_in_repo=path, path_or_fileobj=str(local))
|
| 41 |
+
for path, local in batch
|
| 42 |
+
]
|
| 43 |
+
for attempt in range(8):
|
| 44 |
+
try:
|
| 45 |
+
target.create_commit(
|
| 46 |
+
repo_id=DEST_REPO,
|
| 47 |
+
repo_type="dataset",
|
| 48 |
+
operations=operations,
|
| 49 |
+
commit_message=f"Mirror batch: {len(batch)} files",
|
| 50 |
+
)
|
| 51 |
+
return
|
| 52 |
+
except Exception as exc:
|
| 53 |
+
if "429" in str(exc):
|
| 54 |
+
# HF reports an hour-long repository commit cooldown.
|
| 55 |
+
time.sleep(3700)
|
| 56 |
+
elif attempt == 7:
|
| 57 |
+
raise
|
| 58 |
+
else:
|
| 59 |
+
time.sleep(min(300, 2 ** attempt * 10))
|
| 60 |
+
|
| 61 |
+
|
| 62 |
def paginated_manifest(source: HubApi) -> list[dict]:
|
| 63 |
"""List the source tree page-by-page, reauthenticating on transient 400s."""
|
| 64 |
items: list[dict] = []
|
|
|
|
| 113 |
and (item.get("Type") or item.get("type")) != "tree"
|
| 114 |
]
|
| 115 |
print(f"worker {WORKER_INDEX}/{WORKER_COUNT}: {len(selected)} files")
|
| 116 |
+
pending: list[tuple[str, Path]] = []
|
| 117 |
+
pending_bytes = 0
|
| 118 |
+
|
| 119 |
+
def flush() -> None:
|
| 120 |
+
nonlocal pending, pending_bytes
|
| 121 |
+
if not pending:
|
| 122 |
+
return
|
| 123 |
+
commit_batch(target, pending)
|
| 124 |
+
for _, local_file in pending:
|
| 125 |
+
local_file.unlink(missing_ok=True)
|
| 126 |
+
pending = []
|
| 127 |
+
pending_bytes = 0
|
| 128 |
|
| 129 |
for number, item in enumerate(selected, start=1):
|
| 130 |
path = item.get("Path") or item.get("path") or item.get("Name")
|
|
|
|
| 148 |
),
|
| 149 |
f"download {path}",
|
| 150 |
)
|
| 151 |
+
pending.append((path, local))
|
| 152 |
+
pending_bytes += local.stat().st_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
uploaded.add(path)
|
| 154 |
+
if len(pending) >= 200 or pending_bytes >= 20_000_000_000:
|
| 155 |
+
flush()
|
| 156 |
print(f"done {number}/{len(selected)} {path}", flush=True)
|
| 157 |
finally:
|
| 158 |
+
# Staged files remain until their batch commit succeeds.
|
| 159 |
+
if path not in uploaded:
|
| 160 |
+
local.unlink(missing_ok=True)
|
| 161 |
# Remove any empty nested directories left by this file.
|
| 162 |
parent = local.parent
|
| 163 |
while parent != WORKDIR:
|
|
|
|
| 167 |
break
|
| 168 |
parent = parent.parent
|
| 169 |
|
| 170 |
+
flush()
|
| 171 |
shutil.rmtree(WORKDIR, ignore_errors=True)
|
| 172 |
|
| 173 |
|