|
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: |
|
|
|
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() |
|
|
|
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) |
|
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() |