Add --glob flag for HF parquet shard patterns
Browse files- atlas-export-remote.py +64 -1
atlas-export-remote.py
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
# dependencies = [
|
| 4 |
# "huggingface-hub",
|
| 5 |
# "torch", # For GPU detection
|
|
|
|
| 6 |
# ]
|
| 7 |
# ///
|
| 8 |
|
|
@@ -50,6 +51,7 @@ import argparse
|
|
| 50 |
import json
|
| 51 |
import logging
|
| 52 |
import os
|
|
|
|
| 53 |
import shutil
|
| 54 |
import subprocess
|
| 55 |
import sys
|
|
@@ -60,6 +62,7 @@ from typing import Optional, Tuple
|
|
| 60 |
|
| 61 |
from huggingface_hub import (
|
| 62 |
HfApi,
|
|
|
|
| 63 |
create_repo,
|
| 64 |
get_token,
|
| 65 |
login,
|
|
@@ -100,6 +103,50 @@ def build_parquet_url(data_repo_id: str) -> str:
|
|
| 100 |
return f"https://huggingface.co/datasets/{data_repo_id}/resolve/main/dataset.parquet"
|
| 101 |
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def build_atlas_command(args, data_repo_id: str) -> Tuple[list, str]:
|
| 104 |
"""Build the embedding-atlas command with all parameters."""
|
| 105 |
cmd = [
|
|
@@ -392,6 +439,16 @@ def main():
|
|
| 392 |
type=str,
|
| 393 |
help="Use existing atlas_export.zip file",
|
| 394 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
|
| 396 |
# Required Space and data repo configuration
|
| 397 |
parser.add_argument(
|
|
@@ -504,10 +561,16 @@ def main():
|
|
| 504 |
|
| 505 |
args = parser.parse_args()
|
| 506 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
# Validate input
|
| 508 |
if not args.from_export and not args.inputs:
|
| 509 |
parser.error(
|
| 510 |
-
"Either provide
|
| 511 |
)
|
| 512 |
if args.from_export and args.inputs:
|
| 513 |
parser.error("Cannot use --from-export together with positional inputs")
|
|
|
|
| 3 |
# dependencies = [
|
| 4 |
# "huggingface-hub",
|
| 5 |
# "torch", # For GPU detection
|
| 6 |
+
# "fsspec", # For HfFileSystem glob
|
| 7 |
# ]
|
| 8 |
# ///
|
| 9 |
|
|
|
|
| 51 |
import json
|
| 52 |
import logging
|
| 53 |
import os
|
| 54 |
+
import random
|
| 55 |
import shutil
|
| 56 |
import subprocess
|
| 57 |
import sys
|
|
|
|
| 62 |
|
| 63 |
from huggingface_hub import (
|
| 64 |
HfApi,
|
| 65 |
+
HfFileSystem,
|
| 66 |
create_repo,
|
| 67 |
get_token,
|
| 68 |
login,
|
|
|
|
| 103 |
return f"https://huggingface.co/datasets/{data_repo_id}/resolve/main/dataset.parquet"
|
| 104 |
|
| 105 |
|
| 106 |
+
def resolve_glob_pattern(
|
| 107 |
+
pattern: str, max_shards: Optional[int] = None, seed: int = 42
|
| 108 |
+
) -> list[str]:
|
| 109 |
+
"""Resolve an HF glob pattern to a list of resolve URLs.
|
| 110 |
+
|
| 111 |
+
Pattern format: hf://datasets/<repo_id>/<path>/*.parquet
|
| 112 |
+
or just: datasets/<repo_id>/<path>/*.parquet
|
| 113 |
+
"""
|
| 114 |
+
# Normalize: strip hf:// prefix if present
|
| 115 |
+
fs_pattern = pattern.removeprefix("hf://")
|
| 116 |
+
|
| 117 |
+
fs = HfFileSystem()
|
| 118 |
+
matches = fs.glob(fs_pattern)
|
| 119 |
+
|
| 120 |
+
if not matches:
|
| 121 |
+
raise ValueError(f"No files matched glob pattern: {pattern}")
|
| 122 |
+
|
| 123 |
+
logger.info(f"Glob matched {len(matches)} files")
|
| 124 |
+
|
| 125 |
+
if max_shards and max_shards < len(matches):
|
| 126 |
+
random.seed(seed)
|
| 127 |
+
matches = random.sample(matches, max_shards)
|
| 128 |
+
logger.info(f"Sampled {max_shards} shards (seed={seed})")
|
| 129 |
+
|
| 130 |
+
# Convert HfFileSystem paths to resolve URLs
|
| 131 |
+
# HfFileSystem paths look like: datasets/org/repo/path/file.parquet
|
| 132 |
+
urls = [f"https://huggingface.co/{m}/resolve/main" for m in matches]
|
| 133 |
+
# Actually, HfFileSystem paths include the revision already in their structure
|
| 134 |
+
# The correct URL format is: https://huggingface.co/datasets/<repo>/resolve/main/<path>
|
| 135 |
+
urls = []
|
| 136 |
+
for m in matches:
|
| 137 |
+
# m = "datasets/org/repo/path/to/file.parquet"
|
| 138 |
+
# Split into repo_type/org/repo and the rest
|
| 139 |
+
parts = m.split("/")
|
| 140 |
+
repo_type = parts[0] # "datasets"
|
| 141 |
+
repo_id = f"{parts[1]}/{parts[2]}"
|
| 142 |
+
file_path = "/".join(parts[3:])
|
| 143 |
+
urls.append(
|
| 144 |
+
f"https://huggingface.co/{repo_type}/{repo_id}/resolve/main/{file_path}"
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
return urls
|
| 148 |
+
|
| 149 |
+
|
| 150 |
def build_atlas_command(args, data_repo_id: str) -> Tuple[list, str]:
|
| 151 |
"""Build the embedding-atlas command with all parameters."""
|
| 152 |
cmd = [
|
|
|
|
| 439 |
type=str,
|
| 440 |
help="Use existing atlas_export.zip file",
|
| 441 |
)
|
| 442 |
+
parser.add_argument(
|
| 443 |
+
"--glob",
|
| 444 |
+
type=str,
|
| 445 |
+
help="HF glob pattern for parquet shards, e.g. 'hf://datasets/org/repo/faq/*.parquet'",
|
| 446 |
+
)
|
| 447 |
+
parser.add_argument(
|
| 448 |
+
"--glob-max-shards",
|
| 449 |
+
type=int,
|
| 450 |
+
help="Max number of shards to randomly sample from glob matches",
|
| 451 |
+
)
|
| 452 |
|
| 453 |
# Required Space and data repo configuration
|
| 454 |
parser.add_argument(
|
|
|
|
| 561 |
|
| 562 |
args = parser.parse_args()
|
| 563 |
|
| 564 |
+
# Resolve glob pattern into inputs
|
| 565 |
+
if args.glob:
|
| 566 |
+
if args.inputs:
|
| 567 |
+
parser.error("Cannot use --glob together with positional inputs")
|
| 568 |
+
args.inputs = resolve_glob_pattern(args.glob, args.glob_max_shards)
|
| 569 |
+
|
| 570 |
# Validate input
|
| 571 |
if not args.from_export and not args.inputs:
|
| 572 |
parser.error(
|
| 573 |
+
"Either provide inputs, --glob, or --from-export"
|
| 574 |
)
|
| 575 |
if args.from_export and args.inputs:
|
| 576 |
parser.error("Cannot use --from-export together with positional inputs")
|