|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Download models or datasets from HuggingFace Hub or ModelScope to a user-defined folder. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + # Download model from HuggingFace |
| 7 | + python download.py \ |
| 8 | + --repo_id lerobot/pi0_base \ |
| 9 | + --output_dir ~/models \ |
| 10 | + --source huggingface |
| 11 | + # Downloads to: ~/models/lerobot/pi0_base |
| 12 | +
|
| 13 | + # Download dataset from HuggingFace |
| 14 | + python download.py \ |
| 15 | + --repo_id lerobot/aloha_mobile_cabinet \ |
| 16 | + --output_dir ~/datasets \ |
| 17 | + --repo_type dataset \ |
| 18 | + --source huggingface |
| 19 | + # Downloads to: ~/datasets/lerobot/aloha_mobile_cabinet |
| 20 | +""" |
| 21 | + |
| 22 | +import argparse |
| 23 | +import sys |
| 24 | + |
| 25 | +from pathlib import Path |
| 26 | +from typing import Optional |
| 27 | + |
| 28 | + |
| 29 | +def _prepare_download(repo_id: str, output_dir: Path, repo_type: str, source_name: str) -> Path: |
| 30 | + """Prepare download directory and print info. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + Final output directory path |
| 34 | + """ |
| 35 | + final_output_dir = output_dir / repo_id |
| 36 | + print(f"Downloading {repo_type} {repo_id} from {source_name}...") |
| 37 | + print(f"Output directory: {final_output_dir}") |
| 38 | + final_output_dir.mkdir(parents=True, exist_ok=True) |
| 39 | + return final_output_dir |
| 40 | + |
| 41 | + |
| 42 | +def _handle_download_error(e: Exception, repo_id: str, source: str) -> None: |
| 43 | + """Handle download errors with helpful tips.""" |
| 44 | + print(f"✗ Error downloading from {source}: {e}") |
| 45 | + if "401" in str(e) or "authentication" in str(e).lower(): |
| 46 | + if source == "HuggingFace": |
| 47 | + print("\nTip: You may need to set a HuggingFace token:") |
| 48 | + print(" export HF_TOKEN=your_token_here") |
| 49 | + print(" or run: huggingface-cli login") |
| 50 | + else: |
| 51 | + print("\nTip: You may need to set ModelScope credentials:") |
| 52 | + print(" export MODELSCOPE_API_TOKEN=your_token_here") |
| 53 | + elif "404" in str(e) or "not found" in str(e).lower(): |
| 54 | + print(f"\nTip: Repository '{repo_id}' not found. Check the repo ID.") |
| 55 | + sys.exit(1) |
| 56 | + |
| 57 | + |
| 58 | +def download_from_huggingface( |
| 59 | + repo_id: str, |
| 60 | + output_dir: Path, |
| 61 | + repo_type: str = "model", |
| 62 | + revision: Optional[str] = None, |
| 63 | + token: Optional[str] = None, |
| 64 | +) -> Path: |
| 65 | + """Download model or dataset from HuggingFace Hub. |
| 66 | +
|
| 67 | + Args: |
| 68 | + repo_id: HuggingFace repository ID (e.g., "lerobot/pi0_base") |
| 69 | + output_dir: Base directory to save the repository |
| 70 | + (will be saved to output_dir/repo_id) |
| 71 | + repo_type: Type of repository - "model" or "dataset" (default: "model") |
| 72 | + revision: Git revision (branch, tag, or commit hash). Defaults to "main" |
| 73 | + token: HuggingFace token for private repos. If None, uses cached token |
| 74 | +
|
| 75 | + Returns: |
| 76 | + Path to downloaded repository directory |
| 77 | + """ |
| 78 | + try: |
| 79 | + from huggingface_hub import snapshot_download |
| 80 | + except ImportError: |
| 81 | + print("Error: huggingface_hub is not installed.") |
| 82 | + print("Install it with: pip install huggingface_hub") |
| 83 | + sys.exit(1) |
| 84 | + |
| 85 | + final_output_dir = _prepare_download(repo_id, output_dir, repo_type, "HuggingFace Hub") |
| 86 | + |
| 87 | + try: |
| 88 | + downloaded_path = snapshot_download( |
| 89 | + repo_id=repo_id, |
| 90 | + repo_type=repo_type, |
| 91 | + revision=revision, |
| 92 | + local_dir=str(final_output_dir), |
| 93 | + local_dir_use_symlinks=False, |
| 94 | + token=token, |
| 95 | + ) |
| 96 | + downloaded_path = Path(downloaded_path) |
| 97 | + print(f"✓ Successfully downloaded to: {downloaded_path}") |
| 98 | + return downloaded_path |
| 99 | + except Exception as e: |
| 100 | + _handle_download_error(e, repo_id, "HuggingFace") |
| 101 | + return Path() # Never reached, but satisfies type checker |
| 102 | + |
| 103 | + |
| 104 | +def download_from_modelscope( |
| 105 | + repo_id: str, output_dir: Path, repo_type: str = "model", revision: Optional[str] = None |
| 106 | +) -> Path: |
| 107 | + """Download model or dataset from ModelScope. |
| 108 | +
|
| 109 | + Args: |
| 110 | + repo_id: ModelScope repository ID (e.g., "lerobot/pi0_base") |
| 111 | + output_dir: Base directory to save the repository |
| 112 | + (will be saved to output_dir/repo_id) |
| 113 | + repo_type: Type of repository - "model" or "dataset" (default: "model") |
| 114 | + revision: Git revision (branch, tag, or commit hash). Defaults to "master" |
| 115 | +
|
| 116 | + Returns: |
| 117 | + Path to downloaded repository directory |
| 118 | + """ |
| 119 | + try: |
| 120 | + from modelscope.hub.snapshot_download import snapshot_download as ms_snapshot_download |
| 121 | + except ImportError: |
| 122 | + try: |
| 123 | + from modelscope import snapshot_download as ms_snapshot_download |
| 124 | + except ImportError: |
| 125 | + print("Error: modelscope is not installed.") |
| 126 | + print("Install it with: pip install modelscope") |
| 127 | + sys.exit(1) |
| 128 | + |
| 129 | + final_output_dir = _prepare_download(repo_id, output_dir, repo_type, "ModelScope") |
| 130 | + |
| 131 | + try: |
| 132 | + downloaded_path = ms_snapshot_download( |
| 133 | + model_id=repo_id, |
| 134 | + repo_type=repo_type, |
| 135 | + local_dir=str(final_output_dir), |
| 136 | + revision=revision, |
| 137 | + ) |
| 138 | + downloaded_path = Path(downloaded_path) |
| 139 | + print(f"✓ Successfully downloaded to: {downloaded_path}") |
| 140 | + return downloaded_path |
| 141 | + except Exception as e: |
| 142 | + _handle_download_error(e, repo_id, "ModelScope") |
| 143 | + return Path() # Never reached, but satisfies type checker |
| 144 | + |
| 145 | + |
| 146 | +def main(): |
| 147 | + parser = argparse.ArgumentParser( |
| 148 | + description="Download models or datasets from HuggingFace Hub or ModelScope", |
| 149 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 150 | + epilog=""" |
| 151 | +Examples: |
| 152 | + # Download model from HuggingFace (saves to ~/models/lerobot/pi0_base) |
| 153 | + python download.py --repo_id lerobot/pi0_base \\ |
| 154 | + --output_dir ~/models --source huggingface |
| 155 | +
|
| 156 | + # Download dataset from HuggingFace (saves to ~/datasets/lerobot/aloha_mobile_cabinet) |
| 157 | + python download.py --repo_id lerobot/aloha_mobile_cabinet \\ |
| 158 | + --output_dir ~/datasets --repo_type dataset --source huggingface |
| 159 | +
|
| 160 | + # Download from ModelScope (China users, saves to ~/models/lerobot/pi0_base) |
| 161 | + python download.py --repo_id lerobot/pi0_base \\ |
| 162 | + --output_dir ~/models --source modelscope |
| 163 | +
|
| 164 | + # Download tokenizer (saves to ~/models/google/paligemma-3b-pt-224) |
| 165 | + python download.py --repo_id google/paligemma-3b-pt-224 \\ |
| 166 | + --output_dir ~/models --source huggingface |
| 167 | +
|
| 168 | +Note: For private repositories, set HF_TOKEN environment variable: |
| 169 | + export HF_TOKEN=your_token_here |
| 170 | + """, |
| 171 | + ) |
| 172 | + |
| 173 | + parser.add_argument( |
| 174 | + "--repo_id", |
| 175 | + type=str, |
| 176 | + required=True, |
| 177 | + help="Repository ID (e.g., 'lerobot/pi0_base' or 'lerobot/aloha_mobile_cabinet')", |
| 178 | + ) |
| 179 | + |
| 180 | + parser.add_argument( |
| 181 | + "--output_dir", |
| 182 | + type=str, |
| 183 | + required=True, |
| 184 | + help=( |
| 185 | + "Base output directory (repository will be saved to output_dir/repo_id, " |
| 186 | + "e.g., '~/models' -> '~/models/lerobot/pi0_base')" |
| 187 | + ), |
| 188 | + ) |
| 189 | + |
| 190 | + parser.add_argument( |
| 191 | + "--repo_type", |
| 192 | + type=str, |
| 193 | + choices=["model", "dataset"], |
| 194 | + default="model", |
| 195 | + help="Type of repository: 'model' or 'dataset' (default: model)", |
| 196 | + ) |
| 197 | + |
| 198 | + parser.add_argument( |
| 199 | + "--source", |
| 200 | + type=str, |
| 201 | + choices=["huggingface", "modelscope"], |
| 202 | + default="huggingface", |
| 203 | + help="Source to download from: 'huggingface' or 'modelscope' (default: huggingface)", |
| 204 | + ) |
| 205 | + |
| 206 | + args = parser.parse_args() |
| 207 | + |
| 208 | + output_dir = Path(args.output_dir).expanduser().resolve() |
| 209 | + |
| 210 | + if args.source == "huggingface": |
| 211 | + downloaded_path = download_from_huggingface( |
| 212 | + repo_id=args.repo_id, |
| 213 | + output_dir=output_dir, |
| 214 | + repo_type=args.repo_type, |
| 215 | + revision=None, |
| 216 | + token=None, |
| 217 | + ) |
| 218 | + elif args.source == "modelscope": |
| 219 | + downloaded_path = download_from_modelscope( |
| 220 | + repo_id=args.repo_id, output_dir=output_dir, repo_type=args.repo_type, revision=None |
| 221 | + ) |
| 222 | + else: |
| 223 | + raise ValueError(f"Unknown source: {args.source}") |
| 224 | + |
| 225 | + repo_type_name = "Dataset" if args.repo_type == "dataset" else "Model" |
| 226 | + print(f"\n{repo_type_name} downloaded successfully to: {downloaded_path}") |
| 227 | + print("You can now use this path in your config file:") |
| 228 | + if args.repo_type == "dataset": |
| 229 | + print(f" data_path: {downloaded_path}") |
| 230 | + else: |
| 231 | + print(f" checkpoint_dir: {downloaded_path}") |
| 232 | + |
| 233 | + |
| 234 | +if __name__ == "__main__": |
| 235 | + main() |
0 commit comments