mrfakename commited on
Commit
0c8d13f
·
verified ·
1 Parent(s): 5230b4d

Add resumable mirror worker

Browse files
Files changed (1) hide show
  1. mirror_worker.py +96 -0
mirror_worker.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = ["huggingface_hub>=1.0.0", "modelscope-hub>=0.2.0"]
4
+ # ///
5
+ """Resumable, bounded-storage ModelScope -> HF Dataset mirror worker."""
6
+
7
+ from __future__ import annotations
8
+
9
+ import os
10
+ import shutil
11
+ import time
12
+ from pathlib import Path
13
+
14
+ from huggingface_hub import HfApi
15
+ from modelscope_hub import HubApi
16
+
17
+ SOURCE_REPO = "daimonrobotics/Daimon-Infinity"
18
+ DEST_REPO = "mrfakename/Daimon-Infinity"
19
+ WORKER_INDEX = int(os.environ["WORKER_INDEX"])
20
+ WORKER_COUNT = int(os.environ["WORKER_COUNT"])
21
+ MODELSCOPE_TOKEN = os.environ["MODELSCOPE_TOKEN"]
22
+ HF_TOKEN = os.environ["HF_TOKEN"]
23
+ WORKDIR = Path("/tmp/daimon-infinity")
24
+
25
+
26
+ def retry(action, label: str, attempts: int = 6):
27
+ for attempt in range(attempts):
28
+ try:
29
+ return action()
30
+ except Exception:
31
+ if attempt == attempts - 1:
32
+ raise
33
+ time.sleep(min(180, 2 ** attempt * 5))
34
+
35
+
36
+ def main() -> None:
37
+ WORKDIR.mkdir(parents=True, exist_ok=True)
38
+ source = HubApi(token=MODELSCOPE_TOKEN)
39
+ # Authenticate explicitly so the gated dataset access applies to downloads.
40
+ source.login(MODELSCOPE_TOKEN)
41
+ target = HfApi(token=HF_TOKEN)
42
+ uploaded = set(target.list_repo_files(DEST_REPO, repo_type="dataset"))
43
+ files = source.list_repo_files(SOURCE_REPO, "dataset", recursive=True)
44
+ selected = [
45
+ item for number, item in enumerate(files)
46
+ if number % WORKER_COUNT == WORKER_INDEX and item.type != "tree"
47
+ ]
48
+ print(f"worker {WORKER_INDEX}/{WORKER_COUNT}: {len(selected)} files")
49
+
50
+ for number, item in enumerate(selected, start=1):
51
+ path = item.path
52
+ if path in uploaded:
53
+ print(f"skip {number}/{len(selected)} {path}", flush=True)
54
+ continue
55
+ local = WORKDIR / path
56
+ local.parent.mkdir(parents=True, exist_ok=True)
57
+ try:
58
+ retry(
59
+ lambda: source.download_file(
60
+ SOURCE_REPO,
61
+ "dataset",
62
+ path,
63
+ local_dir=WORKDIR,
64
+ force=True,
65
+ expected_sha256=item.blob_id,
66
+ ),
67
+ f"download {path}",
68
+ )
69
+ retry(
70
+ lambda: target.upload_file(
71
+ path_or_fileobj=str(local),
72
+ path_in_repo=path,
73
+ repo_id=DEST_REPO,
74
+ repo_type="dataset",
75
+ commit_message=f"Mirror {path}",
76
+ ),
77
+ f"upload {path}",
78
+ )
79
+ uploaded.add(path)
80
+ print(f"done {number}/{len(selected)} {path}", flush=True)
81
+ finally:
82
+ local.unlink(missing_ok=True)
83
+ # Remove any empty nested directories left by this file.
84
+ parent = local.parent
85
+ while parent != WORKDIR:
86
+ try:
87
+ parent.rmdir()
88
+ except OSError:
89
+ break
90
+ parent = parent.parent
91
+
92
+ shutil.rmtree(WORKDIR, ignore_errors=True)
93
+
94
+
95
+ if __name__ == "__main__":
96
+ main()