licyk commited on
Commit
e7cfd37
1 Parent(s): b947989
Files changed (1) hide show
  1. get_hf_file_to_local.py +60 -0
get_hf_file_to_local.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import os
3
+ import argparse
4
+ from pathlib import Path
5
+ from huggingface_hub import snapshot_download
6
+
7
+
8
+ def get_args():
9
+ parser = argparse.ArgumentParser()
10
+ normalized_filepath = lambda filepath: str(Path(filepath).absolute().as_posix())
11
+
12
+ parser.add_argument("--hf-token", type = str, default = None, help = "huggingFace Token")
13
+ parser.add_argument("--repo", type = str, default = None, help = "仓库的名称")
14
+ parser.add_argument("--repo-type", type = str, default = None, help = "HuggingFace 仓库的种类, 如: model, dataset")
15
+ parser.add_argument("--upload-path", type = normalized_filepath, default = None, help = "本地要上传文件的路径")
16
+
17
+ return parser.parse_args()
18
+
19
+
20
+ def main():
21
+ args = get_args()
22
+
23
+ if args.hf_token is None:
24
+ logging.error("缺失 HF Token")
25
+ return
26
+ else:
27
+ hf_token = args.hf_token
28
+
29
+ if args.repo is None:
30
+ logging.error("未填写仓库名称")
31
+ return
32
+ else:
33
+ repo = args.repo
34
+
35
+ if args.repo_type is None:
36
+ logging.error("缺少仓库种类")
37
+ return
38
+ else:
39
+ repo_type = args.repo_type
40
+
41
+ if args.upload_path is None:
42
+ root_path = Path(os.path.abspath(__file__)).parent.as_posix()
43
+ else:
44
+ root_path = args.upload_path
45
+
46
+ logging.warning(f"repo: {repo}")
47
+ logging.warning(f"repo_type: {repo_type}")
48
+ logging.warning(f"root_path: {root_path}")
49
+ logging.warning(f"拉取 {repo} 中的内容到本地中")
50
+ snapshot_download(
51
+ repo_id = repo,
52
+ repo_type = repo_type,
53
+ local_dir = root_path,
54
+ token = hf_token,
55
+ local_dir_use_symlinks = False
56
+ )
57
+ logging.warning("拉取文件完成")
58
+
59
+
60
+ main()