Ligeng-Zhu
commited on
Upload upload2hf.py with huggingface_hub
Browse files- upload2hf.py +178 -0
upload2hf.py
ADDED
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os, os.path as osp
|
2 |
+
import time
|
3 |
+
import argparse
|
4 |
+
from hashlib import sha1, sha256
|
5 |
+
|
6 |
+
from termcolor import colored
|
7 |
+
|
8 |
+
from huggingface_hub import HfApi
|
9 |
+
from huggingface_hub.hf_api import CommitOperationAdd
|
10 |
+
|
11 |
+
MAX_UPLOAD_FILES_PER_COMMIT = 64
|
12 |
+
MAX_UPLOAD_SIZE_PER_COMMIT = 64 * 1024 * 1024 * 1024 # 64 GiB
|
13 |
+
|
14 |
+
|
15 |
+
def compute_git_hash(filename):
|
16 |
+
with open(filename, "rb") as f:
|
17 |
+
data = f.read()
|
18 |
+
s = "blob %u\0" % len(data)
|
19 |
+
h = sha1()
|
20 |
+
h.update(s.encode("utf-8"))
|
21 |
+
h.update(data)
|
22 |
+
return h.hexdigest()
|
23 |
+
|
24 |
+
|
25 |
+
def compute_lfs_hash(filename):
|
26 |
+
with open(filename, "rb") as f:
|
27 |
+
data = f.read()
|
28 |
+
h = sha256()
|
29 |
+
h.update(data)
|
30 |
+
return h.hexdigest()
|
31 |
+
|
32 |
+
|
33 |
+
if __name__ == "__main__":
|
34 |
+
import os
|
35 |
+
|
36 |
+
os.environ["CURL_CA_BUNDLE"] = ""
|
37 |
+
|
38 |
+
parser = argparse.ArgumentParser()
|
39 |
+
parser.add_argument(
|
40 |
+
"local_folder",
|
41 |
+
type=str,
|
42 |
+
)
|
43 |
+
parser.add_argument("--model-name", type=str, default=None)
|
44 |
+
|
45 |
+
parser.add_argument("--repo-type", type=str, choices=["model", "dataset"])
|
46 |
+
parser.add_argument("--repo-org", type=str, default="Efficient-Large-Model")
|
47 |
+
|
48 |
+
parser.add_argument("--fast-check", action="store_true")
|
49 |
+
parser.add_argument("--sleep-on-error", action="store_true")
|
50 |
+
|
51 |
+
args = parser.parse_args()
|
52 |
+
|
53 |
+
api = HfApi()
|
54 |
+
|
55 |
+
repo_type = args.repo_type
|
56 |
+
local_folder = args.local_folder
|
57 |
+
if local_folder[-1] == "/":
|
58 |
+
local_folder = local_folder[:-1]
|
59 |
+
|
60 |
+
if args.model_name is None:
|
61 |
+
model_name = osp.basename(local_folder).replace("+", "-")
|
62 |
+
repo = osp.join(args.repo_org, model_name)
|
63 |
+
|
64 |
+
local_folder = os.path.expanduser(local_folder)
|
65 |
+
if not api.repo_exists(repo, repo_type=repo_type):
|
66 |
+
api.create_repo(
|
67 |
+
repo_id=repo,
|
68 |
+
private=True,
|
69 |
+
repo_type=repo_type,
|
70 |
+
)
|
71 |
+
|
72 |
+
BASE_URL = "https://hf.co"
|
73 |
+
if args.repo_type == "dataset":
|
74 |
+
BASE_URL = "https://hf.co/datasets"
|
75 |
+
print(colored(f"Uploading {osp.join(BASE_URL, repo)}", "green"))
|
76 |
+
|
77 |
+
ops = []
|
78 |
+
commit_description = ""
|
79 |
+
commit_size = 0
|
80 |
+
for root, dirs, files in os.walk(local_folder, topdown=True):
|
81 |
+
dirs.sort()
|
82 |
+
for name in files:
|
83 |
+
fpath = osp.join(root, name)
|
84 |
+
rpath = osp.relpath(fpath, local_folder)
|
85 |
+
# print(rpath)
|
86 |
+
if "checkpoint-" in rpath:
|
87 |
+
print(colored(f"Checkpoint detected: {rpath}, skipping", "yellow"))
|
88 |
+
continue
|
89 |
+
|
90 |
+
if api.file_exists(repo_id=repo, filename=rpath, repo_type=repo_type):
|
91 |
+
if args.fast_check:
|
92 |
+
print(
|
93 |
+
colored(
|
94 |
+
f"Already uploaded {rpath}, fast check pass, skipping",
|
95 |
+
"green",
|
96 |
+
)
|
97 |
+
)
|
98 |
+
continue
|
99 |
+
else:
|
100 |
+
hf_meta = list(
|
101 |
+
api.list_files_info(
|
102 |
+
repo_id=repo, paths=rpath, repo_type=repo_type
|
103 |
+
)
|
104 |
+
)[0]
|
105 |
+
|
106 |
+
if hf_meta.lfs is not None:
|
107 |
+
hash_type = "lfs-sha256"
|
108 |
+
hf_hash = hf_meta.lfs["sha256"]
|
109 |
+
git_hash = compute_lfs_hash(fpath)
|
110 |
+
else:
|
111 |
+
hash_type = "git-sha1"
|
112 |
+
hf_hash = hf_meta.blob_id
|
113 |
+
git_hash = compute_git_hash(fpath)
|
114 |
+
|
115 |
+
if hf_hash == git_hash:
|
116 |
+
print(
|
117 |
+
colored(
|
118 |
+
f"Already uploaded {rpath}, hash check pass, skipping",
|
119 |
+
"green",
|
120 |
+
)
|
121 |
+
)
|
122 |
+
continue
|
123 |
+
else:
|
124 |
+
print(
|
125 |
+
colored(
|
126 |
+
f"{rpath} is not same as local version, re-uploading...",
|
127 |
+
"red",
|
128 |
+
)
|
129 |
+
)
|
130 |
+
|
131 |
+
operation = CommitOperationAdd(
|
132 |
+
path_or_fileobj=fpath,
|
133 |
+
path_in_repo=rpath,
|
134 |
+
)
|
135 |
+
print(colored(f"Commiting {rpath}", "green"))
|
136 |
+
ops.append(operation)
|
137 |
+
commit_size += operation.upload_info.size
|
138 |
+
commit_description += f"Upload {rpath}\n"
|
139 |
+
if len(ops) <= MAX_UPLOAD_FILES_PER_COMMIT and commit_size <= MAX_UPLOAD_SIZE_PER_COMMIT:
|
140 |
+
continue
|
141 |
+
|
142 |
+
commit_message = "Upload files with huggingface_hub"
|
143 |
+
|
144 |
+
result = None
|
145 |
+
while result is None:
|
146 |
+
try:
|
147 |
+
commit_info = api.create_commit(
|
148 |
+
repo_id=repo,
|
149 |
+
repo_type=repo_type,
|
150 |
+
operations=ops,
|
151 |
+
commit_message=commit_message,
|
152 |
+
commit_description=commit_description,
|
153 |
+
)
|
154 |
+
except RuntimeError as e:
|
155 |
+
print(e)
|
156 |
+
if not args.sleep_on_error:
|
157 |
+
raise e
|
158 |
+
else:
|
159 |
+
print("sleeping for one hour then re-try")
|
160 |
+
time.sleep(3600)
|
161 |
+
continue
|
162 |
+
result = "success"
|
163 |
+
|
164 |
+
commit_description = ""
|
165 |
+
ops = []
|
166 |
+
commit_size = 0
|
167 |
+
|
168 |
+
print(colored(f"Finish {commit_info}", "yellow"))
|
169 |
+
|
170 |
+
# upload residuals
|
171 |
+
commit_message = "Upload files with huggingface_hub"
|
172 |
+
commit_info = api.create_commit(
|
173 |
+
repo_id=repo,
|
174 |
+
repo_type=repo_type,
|
175 |
+
operations=ops,
|
176 |
+
commit_message=commit_message,
|
177 |
+
commit_description=commit_description,
|
178 |
+
)
|