Upload hf_job_add_dimensions.py with huggingface_hub
Browse files- hf_job_add_dimensions.py +132 -0
hf_job_add_dimensions.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Add height/width columns to nastol-images-full dataset
|
| 4 |
+
"""
|
| 5 |
+
import argparse
|
| 6 |
+
import os
|
| 7 |
+
import logging
|
| 8 |
+
import sys
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
logging.basicConfig(
|
| 12 |
+
level=logging.INFO,
|
| 13 |
+
format='[%(asctime)s] %(levelname)s: %(message)s',
|
| 14 |
+
datefmt='%Y-%m-%d %H:%M:%S',
|
| 15 |
+
stream=sys.stdout,
|
| 16 |
+
force=True
|
| 17 |
+
)
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
|
| 20 |
+
from datasets import load_dataset
|
| 21 |
+
from huggingface_hub import HfApi
|
| 22 |
+
import pyarrow as pa
|
| 23 |
+
import pyarrow.parquet as pq
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
ap = argparse.ArgumentParser()
|
| 28 |
+
ap.add_argument('--input-dataset', type=str, default='vlordier/nastol-images-full')
|
| 29 |
+
ap.add_argument('--output-dataset', type=str, default='vlordier/nastol-images-full')
|
| 30 |
+
ap.add_argument('--split', type=str, default='train')
|
| 31 |
+
ap.add_argument('--shard-index', type=int, default=0)
|
| 32 |
+
ap.add_argument('--num-shards', type=int, default=1)
|
| 33 |
+
ap.add_argument('--batch-size', type=int, default=1000)
|
| 34 |
+
args = ap.parse_args()
|
| 35 |
+
|
| 36 |
+
logger.info("="*60)
|
| 37 |
+
logger.info("Add Height/Width Columns to Dataset")
|
| 38 |
+
logger.info("="*60)
|
| 39 |
+
logger.info(f"Arguments: {vars(args)}")
|
| 40 |
+
|
| 41 |
+
token = os.environ.get('HF_TOKEN')
|
| 42 |
+
api = HfApi(token=token)
|
| 43 |
+
|
| 44 |
+
# Load dataset
|
| 45 |
+
logger.info(f"Loading {args.input_dataset}...")
|
| 46 |
+
ds = load_dataset(args.input_dataset, split=args.split, streaming=True)
|
| 47 |
+
|
| 48 |
+
if args.num_shards > 1:
|
| 49 |
+
ds = ds.shard(num_shards=args.num_shards, index=args.shard_index)
|
| 50 |
+
logger.info(f"Processing shard {args.shard_index+1}/{args.num_shards}")
|
| 51 |
+
|
| 52 |
+
# Process in batches
|
| 53 |
+
buffer = []
|
| 54 |
+
batch_count = 0
|
| 55 |
+
upload_count = 0
|
| 56 |
+
|
| 57 |
+
def flush_buffer():
|
| 58 |
+
nonlocal buffer, upload_count
|
| 59 |
+
if not buffer:
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
# Build columns
|
| 63 |
+
image_paths = [b['image_path'] for b in buffer]
|
| 64 |
+
images_bytes = [b['image'] for b in buffer]
|
| 65 |
+
heights = [b['height'] for b in buffer]
|
| 66 |
+
widths = [b['width'] for b in buffer]
|
| 67 |
+
|
| 68 |
+
table = pa.table({
|
| 69 |
+
'image_path': image_paths,
|
| 70 |
+
'image': images_bytes,
|
| 71 |
+
'height': heights,
|
| 72 |
+
'width': widths
|
| 73 |
+
})
|
| 74 |
+
|
| 75 |
+
# Write parquet
|
| 76 |
+
local_dir = Path('dimension_batches')
|
| 77 |
+
local_dir.mkdir(parents=True, exist_ok=True)
|
| 78 |
+
file_name = f"shard-{args.shard_index:03d}-batch-{upload_count:04d}.parquet"
|
| 79 |
+
local_path = local_dir / file_name
|
| 80 |
+
pq.write_table(table, local_path)
|
| 81 |
+
|
| 82 |
+
# Upload
|
| 83 |
+
path_in_repo = f"data/{file_name}"
|
| 84 |
+
logger.info(f"Uploading batch {upload_count} with {len(buffer)} images -> {path_in_repo}")
|
| 85 |
+
try:
|
| 86 |
+
api.upload_file(
|
| 87 |
+
path_or_fileobj=str(local_path),
|
| 88 |
+
path_in_repo=path_in_repo,
|
| 89 |
+
repo_id=args.output_dataset,
|
| 90 |
+
repo_type='dataset',
|
| 91 |
+
token=token
|
| 92 |
+
)
|
| 93 |
+
logger.info("✓ Uploaded")
|
| 94 |
+
except Exception as e:
|
| 95 |
+
logger.error(f"Upload failed: {e}")
|
| 96 |
+
|
| 97 |
+
buffer.clear()
|
| 98 |
+
upload_count += 1
|
| 99 |
+
|
| 100 |
+
logger.info("Processing images...")
|
| 101 |
+
for idx, sample in enumerate(ds):
|
| 102 |
+
image = sample['image']
|
| 103 |
+
image_path = sample.get('image_path', f'img_{idx:06d}')
|
| 104 |
+
|
| 105 |
+
# Get dimensions
|
| 106 |
+
width, height = image.size
|
| 107 |
+
|
| 108 |
+
# Store original image bytes
|
| 109 |
+
import io
|
| 110 |
+
buf = io.BytesIO()
|
| 111 |
+
image.save(buf, format='PNG')
|
| 112 |
+
image_bytes = buf.getvalue()
|
| 113 |
+
|
| 114 |
+
buffer.append({
|
| 115 |
+
'image_path': image_path,
|
| 116 |
+
'image': image_bytes,
|
| 117 |
+
'height': height,
|
| 118 |
+
'width': width
|
| 119 |
+
})
|
| 120 |
+
|
| 121 |
+
if len(buffer) >= args.batch_size:
|
| 122 |
+
flush_buffer()
|
| 123 |
+
batch_count += 1
|
| 124 |
+
logger.info(f"Processed {batch_count * args.batch_size} images")
|
| 125 |
+
|
| 126 |
+
# Final flush
|
| 127 |
+
flush_buffer()
|
| 128 |
+
logger.info(f"✓ Completed shard {args.shard_index}: {batch_count * args.batch_size + len(buffer)} images")
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
if __name__ == '__main__':
|
| 132 |
+
main()
|