upload
Browse files- upload_file_to_hf.py +136 -0
upload_file_to_hf.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import os
|
3 |
+
import argparse
|
4 |
+
from tqdm import tqdm
|
5 |
+
from huggingface_hub import HfApi, CommitOperationAdd
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
def get_all_file(directory: str) -> list:
|
9 |
+
logging.warning(f"获取 {directory} 中的所有文件绝对路径中")
|
10 |
+
file_list = []
|
11 |
+
for dirname, _, filenames in os.walk(directory):
|
12 |
+
for filename in filenames:
|
13 |
+
file_list.append(Path(os.path.join(dirname, filename)).as_posix())
|
14 |
+
logging.warning(f"{directory} 已有的文件数量: {len(file_list)}")
|
15 |
+
return file_list
|
16 |
+
|
17 |
+
|
18 |
+
def filter_file(file_list: list, root_path) -> list:
|
19 |
+
logging.warning("过滤文件中")
|
20 |
+
file_list_ = []
|
21 |
+
for file_path in file_list:
|
22 |
+
rel_path = Path(os.path.relpath(file_path, root_path)).as_posix()
|
23 |
+
if rel_path.startswith(".git") or rel_path.startswith(".huggingface"):
|
24 |
+
continue
|
25 |
+
file_list_.append(file_path)
|
26 |
+
logging.warning(f"过滤后的文件数量: {len(file_list_)}")
|
27 |
+
return file_list_
|
28 |
+
|
29 |
+
|
30 |
+
def get_hf_repo_file_list(hf_access_token, repo, repo_type):
|
31 |
+
logging.warning(f"获取 {repo} 中的所有文件路径中")
|
32 |
+
api = HfApi()
|
33 |
+
model_list = api.list_repo_files(
|
34 |
+
repo_id = repo,
|
35 |
+
repo_type = repo_type,
|
36 |
+
token = hf_access_token
|
37 |
+
)
|
38 |
+
logging.warning(f"{repo} 已有的文件数量: {len(model_list)}")
|
39 |
+
return model_list
|
40 |
+
|
41 |
+
|
42 |
+
def get_upload_list(file_list: list, hf_file_list: list, root_path) -> list:
|
43 |
+
logging.warning("计算需要上传的文件中")
|
44 |
+
upload_list = []
|
45 |
+
for file_path in file_list:
|
46 |
+
file_rel_path = Path(os.path.relpath(file_path, root_path)).as_posix()
|
47 |
+
if file_rel_path not in hf_file_list:
|
48 |
+
upload_list.append(file_path)
|
49 |
+
|
50 |
+
logging.warning(f"需要上传的文件数量: {len(upload_list)}")
|
51 |
+
return upload_list
|
52 |
+
|
53 |
+
|
54 |
+
def upload_file_to_hf(hf_access_token: str, repo: str, repo_type: str, upload_file_list: list, root_path) -> None:
|
55 |
+
# 验证 HF Token
|
56 |
+
api = HfApi()
|
57 |
+
try:
|
58 |
+
api.whoami(token = hf_access_token)
|
59 |
+
logging.warning("HuggingFace Token 验证成功")
|
60 |
+
except Exception as e:
|
61 |
+
logging.warning("HuggingFace Token 验证失败: ", e)
|
62 |
+
|
63 |
+
# 添加文件操作
|
64 |
+
operations = []
|
65 |
+
for local_file_path in tqdm(upload_file_list, desc="添加文件"):
|
66 |
+
hf_file_path = Path(os.path.relpath(local_file_path, root_path)).as_posix() # 计算在 HuggingFace 仓库中的相对路径
|
67 |
+
|
68 |
+
operations.append(CommitOperationAdd(path_in_repo = hf_file_path, path_or_fileobj = local_file_path))
|
69 |
+
|
70 |
+
api.create_commit(
|
71 |
+
repo_id = repo,
|
72 |
+
operations = operations,
|
73 |
+
commit_message = f"upload",
|
74 |
+
repo_type = repo_type,
|
75 |
+
token = hf_access_token
|
76 |
+
)
|
77 |
+
|
78 |
+
logging.warning("上传文件结束")
|
79 |
+
|
80 |
+
|
81 |
+
def get_args():
|
82 |
+
parser = argparse.ArgumentParser()
|
83 |
+
normalized_filepath = lambda filepath: str(Path(filepath).absolute().as_posix())
|
84 |
+
|
85 |
+
parser.add_argument("--hf-token", type = str, default = None, help = "huggingFace Token")
|
86 |
+
parser.add_argument("--repo", type = str, default = None, help = "仓库的名称")
|
87 |
+
parser.add_argument("--repo-type", type = str, default = None, help = "HuggingFace 仓库的种类, 如: model, dataset")
|
88 |
+
parser.add_argument("--upload-path", type = normalized_filepath, default = None, help = "本地要上传文件的路径")
|
89 |
+
|
90 |
+
return parser.parse_args()
|
91 |
+
|
92 |
+
|
93 |
+
def main():
|
94 |
+
args = get_args()
|
95 |
+
|
96 |
+
if args.hf_token is None:
|
97 |
+
logging.error("缺失 HF Token")
|
98 |
+
return
|
99 |
+
else:
|
100 |
+
hf_token = args.hf_token
|
101 |
+
|
102 |
+
if args.repo is None:
|
103 |
+
logging.error("未填写仓库名称")
|
104 |
+
return
|
105 |
+
else:
|
106 |
+
repo = args.repo
|
107 |
+
|
108 |
+
if args.repo_type is None:
|
109 |
+
logging.error("缺少仓库种类")
|
110 |
+
return
|
111 |
+
else:
|
112 |
+
repo_type = args.repo_type
|
113 |
+
|
114 |
+
if args.upload_path is None:
|
115 |
+
root_path = Path(os.path.abspath(__file__)).parent.as_posix()
|
116 |
+
else:
|
117 |
+
root_path = args.upload_path
|
118 |
+
|
119 |
+
logging.warning(f"repo: {repo}")
|
120 |
+
logging.warning(f"repo_type: {repo_type}")
|
121 |
+
logging.warning(f"root_path: {root_path}")
|
122 |
+
|
123 |
+
all_file_list = get_all_file(root_path) # 本地所有文件的绝对路径
|
124 |
+
filtered_file_list = filter_file(all_file_list, root_path) # 除去不需要上传的文件
|
125 |
+
hf_file_list = get_hf_repo_file_list(hf_access_token = hf_token, repo = repo, repo_type = repo_type) # 获取 HuggingFace 中的文件列表
|
126 |
+
upload_file_list = get_upload_list(filtered_file_list, hf_file_list, root_path) # 获取需要上传的文件列表
|
127 |
+
|
128 |
+
upload_file_to_hf(
|
129 |
+
hf_access_token = hf_token,
|
130 |
+
repo = repo,
|
131 |
+
repo_type = repo_type,
|
132 |
+
upload_file_list = upload_file_list,
|
133 |
+
root_path = root_path)
|
134 |
+
|
135 |
+
|
136 |
+
main()
|