steer_test_lerobot / upload_to_hf.py
TreeePlanter's picture
Upload LeRobot dataset
5ca698b verified
#!/usr/bin/env python3
"""
上传 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: 是否设为私有仓库
"""
# 初始化 HF API
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 目录下的文件
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 目录下的所有 parquet 文件
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.md
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" # 设置为您的 HF 用户名,或留空自动检测
PRIVATE = False # 设为 True 创建私有仓库
# 检查数据集路径
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❌ 上传失败,请检查错误信息")