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