davanstrien HF Staff Claude commited on
Commit
db90693
·
1 Parent(s): b0a45e9

Add automatic dataset sampling to reduce disk usage

Browse files

When --sample is specified, pre-sample dataset using streaming to avoid
storing full dataset on disk. Reduces space usage from GBs to MBs.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>

Files changed (1) hide show
  1. atlas-export.py +66 -7
atlas-export.py CHANGED
@@ -3,6 +3,7 @@
3
  # dependencies = [
4
  # "huggingface-hub[hf_transfer]",
5
  # "torch", # For GPU detection
 
6
  # ]
7
  # ///
8
 
@@ -71,8 +72,58 @@ def check_gpu_available() -> bool:
71
  return False
72
 
73
 
74
- def build_atlas_command(args) -> list:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  """Build the embedding-atlas command with all parameters."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  # Use uvx to run embedding-atlas with required dependencies
77
  # Include hf-transfer for faster downloads when HF_HUB_ENABLE_HF_TRANSFER is set
78
  cmd = [
@@ -82,7 +133,7 @@ def build_atlas_command(args) -> list:
82
  "--with",
83
  "hf-transfer",
84
  "embedding-atlas",
85
- args.dataset_id,
86
  ]
87
  # Add all optional parameters
88
  if args.model:
@@ -95,11 +146,12 @@ def build_atlas_command(args) -> list:
95
  if args.image_column:
96
  cmd.extend(["--image", args.image_column])
97
 
98
- if args.split:
99
  cmd.extend(["--split", args.split])
100
 
101
- if args.sample:
102
- cmd.extend(["--sample", str(args.sample)])
 
103
 
104
  if args.trust_remote_code:
105
  cmd.append("--trust-remote-code")
@@ -120,7 +172,7 @@ def build_atlas_command(args) -> list:
120
  export_path = "atlas_export.zip"
121
  cmd.extend(["--export-application", export_path])
122
 
123
- return cmd, export_path
124
 
125
 
126
  def create_space_readme(args) -> str:
@@ -399,9 +451,11 @@ def main():
399
  output_dir = Path(temp_dir)
400
  logger.info(f"Using temporary directory: {output_dir}")
401
 
 
 
402
  try:
403
  # Build and run embedding-atlas command
404
- cmd, export_path = build_atlas_command(args)
405
  logger.info(f"Running command: {' '.join(cmd)}")
406
 
407
  # Run the command
@@ -445,6 +499,11 @@ def main():
445
  if temp_dir and not args.local_only:
446
  shutil.rmtree(temp_dir)
447
  logger.info("Cleaned up temporary files")
 
 
 
 
 
448
 
449
 
450
  if __name__ == "__main__":
 
3
  # dependencies = [
4
  # "huggingface-hub[hf_transfer]",
5
  # "torch", # For GPU detection
6
+ # "datasets", # For dataset sampling
7
  # ]
8
  # ///
9
 
 
72
  return False
73
 
74
 
75
+ def sample_dataset_to_parquet(
76
+ dataset_id: str,
77
+ sample_size: int,
78
+ split: str = "train",
79
+ trust_remote_code: bool = False
80
+ ) -> tuple[Path, Path]:
81
+ """Sample dataset and save to local parquet file."""
82
+ from datasets import load_dataset
83
+
84
+ logger.info(f"Pre-sampling {sample_size} examples from {dataset_id}...")
85
+
86
+ # Load with streaming to avoid loading entire dataset
87
+ ds = load_dataset(
88
+ dataset_id,
89
+ streaming=True,
90
+ split=split,
91
+ trust_remote_code=trust_remote_code
92
+ )
93
+
94
+ # Shuffle and sample
95
+ ds = ds.shuffle(seed=42)
96
+ sampled_ds = ds.take(sample_size)
97
+
98
+ # Create temporary directory and parquet file
99
+ temp_dir = Path(tempfile.mkdtemp(prefix="atlas_data_"))
100
+ parquet_path = temp_dir / "data.parquet"
101
+
102
+ logger.info(f"Saving sampled data to temporary file...")
103
+ sampled_ds.to_parquet(str(parquet_path))
104
+
105
+ file_size = parquet_path.stat().st_size / (1024 * 1024) # MB
106
+ logger.info(f"Created {file_size:.1f}MB parquet file with {sample_size} samples")
107
+
108
+ return parquet_path, temp_dir
109
+
110
+
111
+ def build_atlas_command(args) -> tuple[list, str, Optional[Path]]:
112
  """Build the embedding-atlas command with all parameters."""
113
+ temp_data_dir = None
114
+
115
+ # If sampling is requested, pre-sample the dataset
116
+ if args.sample:
117
+ parquet_path, temp_data_dir = sample_dataset_to_parquet(
118
+ args.dataset_id,
119
+ args.sample,
120
+ args.split,
121
+ args.trust_remote_code
122
+ )
123
+ dataset_input = str(parquet_path)
124
+ else:
125
+ dataset_input = args.dataset_id
126
+
127
  # Use uvx to run embedding-atlas with required dependencies
128
  # Include hf-transfer for faster downloads when HF_HUB_ENABLE_HF_TRANSFER is set
129
  cmd = [
 
133
  "--with",
134
  "hf-transfer",
135
  "embedding-atlas",
136
+ dataset_input,
137
  ]
138
  # Add all optional parameters
139
  if args.model:
 
146
  if args.image_column:
147
  cmd.extend(["--image", args.image_column])
148
 
149
+ if args.split and not args.sample: # Don't add split if we pre-sampled
150
  cmd.extend(["--split", args.split])
151
 
152
+ # Remove the --sample parameter since we've already sampled
153
+ # if args.sample:
154
+ # cmd.extend(["--sample", str(args.sample)])
155
 
156
  if args.trust_remote_code:
157
  cmd.append("--trust-remote-code")
 
172
  export_path = "atlas_export.zip"
173
  cmd.extend(["--export-application", export_path])
174
 
175
+ return cmd, export_path, temp_data_dir
176
 
177
 
178
  def create_space_readme(args) -> str:
 
451
  output_dir = Path(temp_dir)
452
  logger.info(f"Using temporary directory: {output_dir}")
453
 
454
+ temp_data_dir = None # Initialize to avoid UnboundLocalError
455
+
456
  try:
457
  # Build and run embedding-atlas command
458
+ cmd, export_path, temp_data_dir = build_atlas_command(args)
459
  logger.info(f"Running command: {' '.join(cmd)}")
460
 
461
  # Run the command
 
499
  if temp_dir and not args.local_only:
500
  shutil.rmtree(temp_dir)
501
  logger.info("Cleaned up temporary files")
502
+
503
+ # Also clean up the data sampling directory
504
+ if temp_data_dir and temp_data_dir.exists():
505
+ shutil.rmtree(temp_data_dir)
506
+ logger.info("Cleaned up sampled data")
507
 
508
 
509
  if __name__ == "__main__":