| |
| """ |
| 上传 LeRobot 数据集到 Hugging Face Hub |
| """ |
|
|
| import os |
| from huggingface_hub import HfApi, create_repo |
| from pathlib import Path |
|
|
| def upload_dataset_to_hf( |
| dataset_path: str, |
| repo_name: str, |
| username: str = None, |
| private: bool = False |
| ): |
| """ |
| 上传数据集到 Hugging Face Hub |
| |
| Args: |
| dataset_path: 数据集本地路径 |
| repo_name: 仓库名称 |
| username: Hugging Face 用户名 (可选) |
| private: 是否设为私有仓库 |
| """ |
| |
| |
| api = HfApi() |
| |
| |
| if username: |
| full_repo_name = f"{username}/{repo_name}" |
| else: |
| |
| user_info = api.whoami() |
| username = user_info["name"] |
| full_repo_name = f"{username}/{repo_name}" |
| |
| print(f"准备上传到: {full_repo_name}") |
| |
| |
| try: |
| create_repo( |
| repo_id=full_repo_name, |
| repo_type="dataset", |
| private=private, |
| exist_ok=True |
| ) |
| print(f"✅ 仓库 {full_repo_name} 已创建或已存在") |
| except Exception as e: |
| print(f"⚠️ 创建仓库时出现问题: {e}") |
| |
| |
| dataset_path = Path(dataset_path) |
| |
| |
| files_to_upload = [] |
| |
| |
| meta_dir = dataset_path / "meta" |
| if meta_dir.exists(): |
| for file_path in meta_dir.iterdir(): |
| if file_path.is_file(): |
| files_to_upload.append(str(file_path)) |
| |
| |
| data_dir = dataset_path / "data" |
| if data_dir.exists(): |
| for chunk_dir in data_dir.iterdir(): |
| if chunk_dir.is_dir(): |
| for file_path in chunk_dir.iterdir(): |
| if file_path.is_file() and file_path.suffix == ".parquet": |
| files_to_upload.append(str(file_path)) |
| |
| |
| readme_path = dataset_path / "README.md" |
| if readme_path.exists(): |
| files_to_upload.append(str(readme_path)) |
| |
| print(f"📁 找到 {len(files_to_upload)} 个文件需要上传") |
| |
| |
| try: |
| api.upload_folder( |
| folder_path=dataset_path, |
| repo_id=full_repo_name, |
| repo_type="dataset", |
| path_in_repo="", |
| commit_message="Upload LeRobot dataset" |
| ) |
| print(f"✅ 数据集已成功上传到 {full_repo_name}") |
| print(f"🔗 访问链接: https://huggingface.co/datasets/{full_repo_name}") |
| |
| except Exception as e: |
| print(f"❌ 上传失败: {e}") |
| return False |
| |
| return True |
|
|
| if __name__ == "__main__": |
| |
| DATASET_PATH = "/home/shuo/research/datasets/steer_test_lerobot" |
| REPO_NAME = "steer_test_lerobot" |
| USERNAME = "TreeePlanter" |
| PRIVATE = False |
| |
| |
| if not os.path.exists(DATASET_PATH): |
| print(f"❌ 数据集路径不存在: {DATASET_PATH}") |
| exit(1) |
| |
| print("🚀 开始上传数据集到 Hugging Face Hub...") |
| print(f"📂 数据集路径: {DATASET_PATH}") |
| print(f"📦 仓库名称: {REPO_NAME}") |
| |
| success = upload_dataset_to_hf( |
| dataset_path=DATASET_PATH, |
| repo_name=REPO_NAME, |
| username=USERNAME, |
| private=PRIVATE |
| ) |
| |
| if success: |
| print("\n🎉 上传完成!") |
| print("\n📋 后续步骤:") |
| print("1. 访问您的数据集页面") |
| print("2. 检查数据集是否正确显示") |
| print("3. 添加标签和描述") |
| print("4. 测试数据集加载:") |
| print(" from datasets import load_dataset") |
| print(f" dataset = load_dataset('{USERNAME or 'your-username'}/{REPO_NAME}')") |
| else: |
| print("\n❌ 上传失败,请检查错误信息") |
|
|