#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Script to upload robot trajectory dataset to Hugging Face Hub """ import os import sys from pathlib import Path from huggingface_hub import HfApi, create_repo, upload_folder from huggingface_hub.utils import HfHubHTTPError def upload_dataset_to_hf( local_folder_path=".", repo_id=None, token=None, private=False, repo_type="dataset" ): """ Upload a local folder to Hugging Face as a dataset. Args: local_folder_path: Path to the local folder to upload (default: current directory) repo_id: HuggingFace repo ID (format: 'username/dataset-name') token: HuggingFace API token private: Whether to make the repo private (default: False) repo_type: Type of repo (default: 'dataset') """ # Initialize the API api = HfApi(token=token) # Get the absolute path local_folder_path = Path(local_folder_path).absolute() print(f"šŸ“ Preparing to upload from: {local_folder_path}") # Count files to upload (optional, for progress indication) total_files = sum(1 for _ in local_folder_path.rglob("*") if _.is_file()) print(f"šŸ“Š Found {total_files} files to upload") try: # Create the repository if it doesn't exist print(f"šŸ”§ Creating repository: {repo_id}") create_repo( repo_id=repo_id, token=token, private=private, repo_type=repo_type, exist_ok=True # Don't error if repo already exists ) print(f"āœ… Repository ready: {repo_id}") # Upload the entire folder print(f"šŸ“¤ Starting upload...") print(f" This may take a while depending on the size of your dataset...") # Upload folder with all its contents upload_folder( folder_path=str(local_folder_path), repo_id=repo_id, repo_type=repo_type, token=token, ignore_patterns=["*.pyc", "__pycache__", ".git", ".DS_Store"], # Ignore common unwanted files commit_message="Upload robot trajectory dataset" ) print(f"āœ… Successfully uploaded dataset to: https://huggingface.co/datasets/{repo_id}") except HfHubHTTPError as e: print(f"āŒ HTTP Error occurred: {e}") sys.exit(1) except Exception as e: print(f"āŒ An error occurred: {e}") sys.exit(1) def main(): """Main function to run the upload script.""" # Configuration - MODIFY THESE VALUES # ==================================== # Your Hugging Face username and desired dataset name REPO_ID = "ASethi04/robot-trajectories-dataset" # <- CHANGE THIS # Your Hugging Face token (get it from https://huggingface.co/settings/tokens) # You can also set this as an environment variable: HF_TOKEN HF_TOKEN = os.getenv("HF_TOKEN", None) # <- SET THIS or use environment variable # Path to your local dataset folder (. for current directory) LOCAL_FOLDER = "." # Whether to make the dataset private PRIVATE = False # Set to True if you want a private dataset # ==================================== # Validate inputs if not HF_TOKEN: print("āŒ Error: Hugging Face token not provided!") print("Please either:") print(" 1. Set the HF_TOKEN environment variable: export HF_TOKEN='your-token-here'") print(" 2. Modify the HF_TOKEN variable in this script") print("\nGet your token from: https://huggingface.co/settings/tokens") sys.exit(1) if REPO_ID == "your-username/robot-trajectories-dataset": print("āŒ Error: Please update the REPO_ID with your Hugging Face username and desired dataset name!") print("Format: 'username/dataset-name'") sys.exit(1) # Confirm before uploading print("=" * 50) print("šŸ“‹ Upload Configuration:") print(f" Repository: {REPO_ID}") print(f" Local Path: {Path(LOCAL_FOLDER).absolute()}") print(f" Private: {PRIVATE}") print("=" * 50) response = input("\nāš ļø Do you want to proceed with the upload? (yes/no): ").lower().strip() if response not in ['yes', 'y']: print("āŒ Upload cancelled.") sys.exit(0) # Run the upload upload_dataset_to_hf( local_folder_path=LOCAL_FOLDER, repo_id=REPO_ID, token=HF_TOKEN, private=PRIVATE ) if __name__ == "__main__": main()