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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| def get_all_file(directory): import os file_list = [] for dirname, _, filenames in os.walk(directory): for filename in filenames: file_list.append(os.path.join(dirname, filename)) return file_list
def is_file_exists_in_ms_repo(upload_file, work_path, repo): import os repo_file = os.path.join(work_path, repo.split("/").pop(), upload_file) if os.path.exists(repo_file): return True else: return False
def clone_modelscope_without_lfs(ms_access_token, repo, work_path): import os os.environ["GIT_LFS_SKIP_SMUDGE"] = "1" !git lfs uninstall
repo_name = repo.split("/").pop() path = os.path.join(work_path, repo_name) if os.path.exists(path): !rm -rf "{path}"
repo_url = f"https://oauth2:{ms_access_token}@www.modelscope.cn/{repo}.git" !git clone "{repo_url}" "{path}" os.environ["GIT_LFS_SKIP_SMUDGE"] = "0" !git lfs install
def push_file_to_modelscope(ms_access_token, repo, work_path, upload_path): import os from modelscope.hub.api import HubApi if repo.split("/").pop() == upload_path.split("/").pop(): raise Exception("本地要上传的仓库名称与要上传到的仓库的名称相同, 这将导致本地要上传的仓库被自动删除")
api = HubApi() try: ms_access_token = api.login(ms_access_token)[0] print(":: ModelScope Token 验证成功") except Exception as e: print(":: ModelScope Token 验证失败: ", e) return
os.chdir(work_path) count = 0 upload_file_lists = get_all_file(upload_path)
print(f"将文件上传至 ModelScope:: {upload_path} -> {os.path.join(work_path, repo.split('/').pop())}") count = 0 sum = len(upload_file_lists) for upload_file in upload_file_lists: count += 1
print(f"[{count}/{sum}]:: 克隆仓库到 {work_path}") clone_modelscope_without_lfs(ms_access_token, repo, work_path) rel_upload_file = os.path.relpath(upload_file, upload_path) repo_path = os.path.join(work_path, repo.split("/").pop()) print(f"[{count}/{sum}]:: 要上传的文件的相对路径: {rel_upload_file}") print(f"[{count}/{sum}]:: 绝对路径: {upload_file}") print(f"[{count}/{sum}]:: 仓库地址: {repo_path}")
if is_file_exists_in_ms_repo(rel_upload_file, work_path, repo): !rm -rf "{repo_path}" print(f"[{count}/{sum}]:: {os.path.basename(upload_file)} 已存在于仓库中") else: p_path = os.path.dirname(os.path.join(work_path, repo.split("/").pop(), rel_upload_file)) if not os.path.exists(p_path): print(f"[{count}/{sum}]:: 创建文件对应的父文件夹: {p_path}") os.makedirs(p_path, exist_ok=True)
print(f"[{count}/{sum}]:: {upload_file} -> {repo_path}") !cp "{upload_file}" "{p_path}"
os.chdir(repo_path) file_name = rel_upload_file.split("/").pop() print(f"[{count}/{sum}]:: 添加文件: {rel_upload_file}") !git add "{rel_upload_file}" print(f"[{count}/{sum}]:: 提交信息: \"upload {file_name}\"") !git commit -m "upload {file_name}" !git config lfs.https://oauth2:{ms_access_token}@www.modelscope.cn/{repo}.git/info/lfs.locksverify true print(f"[{count}/{sum}]:: 上传 {file_name} 到 {repo} 中") !git push
os.chdir(work_path) !rm -rf "{repo_path}" print(f"[{count}/{sum}]:: 上传 {file_name} 完成")
print(f"[{count}/{sum}]:: {repo} 仓库上传完成")
def get_hf_repo_file_list(hf_access_token, repo, repo_type): from huggingface_hub import HfApi api = HfApi() model_list = api.list_repo_files( repo_id = repo, repo_type = repo_type, token = hf_access_token ) return model_list
def push_file_to_huggingface(hf_access_token, repo, repo_type, work_path, upload_path): import os from pathlib import Path from huggingface_hub import HfApi, CommitOperationAdd api = HfApi()
try: api.whoami(token = hf_access_token) print(":: HuggingFace Token 验证成功") except Exception as e: print(":: HuggingFace Token 验证失败: ", e)
os.chdir(work_path) count = 0 upload_file_lists = get_all_file(upload_path)
print(f"将文件上传至 HuggingFace:: {upload_path} -> {os.path.join(work_path, repo.split('/').pop())}") count = 0 sum = len(upload_file_lists) hf_repo_flie_list = get_hf_repo_file_list(hf_access_token, repo, repo_type) for upload_file in upload_file_lists: count += 1
print(f"[{count}/{sum}]:: 克隆仓库到 {work_path}") rel_upload_file = os.path.relpath(upload_file, upload_path) repo_path = os.path.join(work_path, repo.split("/").pop()) print(f"[{count}/{sum}]:: 要上传的文件的相对路径: {rel_upload_file}") print(f"[{count}/{sum}]:: 绝对路径: {upload_file}") print(f"[{count}/{sum}]:: 仓库地址: {repo_path}")
if rel_upload_file in hf_repo_flie_list: print(f"[{count}/{sum}]:: {os.path.basename(upload_file)} 已存在于仓库中") else: file_name = rel_upload_file.split("/").pop() hf_path_in_repo = Path(rel_upload_file).as_posix() local_file_obj = Path(upload_file).as_posix() print(f"[{count}/{sum}]:: {local_file_obj} -> {repo}/{hf_path_in_repo}") operations = [ CommitOperationAdd(path_in_repo = hf_path_in_repo, path_or_fileobj = local_file_obj) ] print(f"[{count}/{sum}]:: 上传 {file_name} 到 {repo} 中") print(f"repo: {repo}, repo_type: {repo_type}") api.create_commit( repo_id = repo, operations = operations, commit_message = f"Upload {file_name}", repo_type = repo_type, token = hf_access_token ) print(f"[{count}/{sum}]:: 上传 {file_name} 完成")
print(f"[{count}/{sum}]:: {repo} 仓库上传完成")
push_file_to_modelscope( ms_access_token = "xxxxxxxxxxxxx", repo = "licyks/test", work_path = "D:/Downloads", upload_path = "D:/Downloads/BaiduNetdiskWorkspace" )
push_file_to_huggingface( hf_access_token = "xxxxxxxxxxxx", repo = "licyk/test", repo_type = "model", work_path = "D:/Downloads", upload_path = "D:/Downloads/BaiduNetdiskWorkspace" )
|