| import os |
| from huggingface_hub import HfApi, login |
| from pathlib import Path |
|
|
| |
| login() |
|
|
| |
| api = HfApi() |
|
|
| |
| repo_id = "lihong-cs/rhosckpt" |
| local_ckpt_path = "/home/lixingyu/workspace_lxy2/workspace/robotpolicy/src/ckpt" |
| repo_type = "model" |
|
|
| |
| def upload_single_file(): |
| ckpt_file = "model.pth" |
| |
| api.upload_file( |
| path_or_fileobj=os.path.join(local_ckpt_path, ckpt_file), |
| path_in_repo=ckpt_file, |
| repo_id=repo_id, |
| repo_type=repo_type, |
| commit_message=f"Upload {ckpt_file}" |
| ) |
| print(f"文件 {ckpt_file} 上传完成!") |
|
|
| |
| def upload_folder(): |
| """上传整个检查点文件夹""" |
| api.upload_folder( |
| folder_path=local_ckpt_path, |
| repo_id=repo_id, |
| repo_type=repo_type, |
| commit_message="Upload checkpoint folder" |
| ) |
| print(f"文件夹 {local_ckpt_path} 上传完成!") |
|
|
| |
| def create_and_upload(): |
| """创建仓库并上传检查点""" |
| try: |
| |
| api.create_repo( |
| repo_id=repo_id, |
| repo_type=repo_type, |
| exist_ok=True, |
| private=False |
| ) |
| print(f"仓库 {repo_id} 创建成功或已存在") |
| |
| |
| upload_folder() |
| |
| except Exception as e: |
| print(f"上传过程中出错: {e}") |
|
|
| |
| def upload_with_progress(): |
| """带进度显示的上传""" |
| from tqdm import tqdm |
| |
| |
| files_to_upload = [] |
| for root, dirs, files in os.walk(local_ckpt_path): |
| for file in files: |
| file_path = os.path.join(root, file) |
| relative_path = os.path.relpath(file_path, local_ckpt_path) |
| files_to_upload.append((file_path, relative_path)) |
| |
| |
| for file_path, relative_path in tqdm(files_to_upload, desc="上传文件"): |
| try: |
| api.upload_file( |
| path_or_fileobj=file_path, |
| path_in_repo=relative_path, |
| repo_id=repo_id, |
| repo_type=repo_type, |
| commit_message=f"Upload {relative_path}" |
| ) |
| except Exception as e: |
| print(f"上传 {relative_path} 时出错: {e}") |
|
|
| if __name__ == "__main__": |
| |
| if not os.path.exists(local_ckpt_path): |
| print(f"错误: 本地路径 {local_ckpt_path} 不存在") |
| exit(1) |
| |
| |
| print("请选择上传方式:") |
| print("1. 上传单个文件") |
| print("2. 上传整个文件夹") |
| print("3. 创建仓库并上传") |
| print("4. 带进度条上传") |
| |
| choice = input("请输入选择 (1-4): ") |
| |
| if choice == "1": |
| upload_single_file() |
| elif choice == "2": |
| upload_folder() |
| elif choice == "3": |
| create_and_upload() |
| elif choice == "4": |
| upload_with_progress() |
| else: |
| print("无效选择") |