Data processing
Part of uv-scripts — self-contained UV scripts you run on Hugging Face Jobs in one command.
General data processing recipes: convert, clean and prepare data files.
| Script | What it does |
|---|---|
optimize-parquet.py |
Converts CSV, JSON and Parquet files uploaded to a bucket into optimized Parquet, triggered by a bucket webhook |
optimize-parquet.py: optimized Parquet from bucket uploads
Upload a CSV, JSON or Parquet file to a Storage Bucket and get an optimized Parquet version in a second bucket, automatically. A bucket webhook starts a Job for each upload, and the Job converts only the files that changed.
input bucket ──upload──▶ webhook ──▶ Job (optimize-parquet.py) ──▶ output bucket
data.csv data.csv/data/train-00000-of-00001.parquet
The output is written by datasets, so it gets the same optimizations as push_to_hub: content-defined chunking for Xet deduplication, a page index for fast filtering and random access, and row groups of at most 100 MB.
Setup
You need two buckets: one you upload to, and one for the output. The Job writes to a different bucket so that its own output does not trigger it again.
hf buckets create my-raw-files --private
hf buckets create my-parquet --private
1. Create a base Job for the webhook to re-run. With no webhook payload, this first run exits straight away:
hf jobs run --flavor cpu-upgrade --timeout 2h -e OUTPUT_BUCKET=<user>/my-parquet \
ghcr.io/astral-sh/uv:python3.12-bookworm \
uv run https://huggingface.co/datasets/uv-scripts/data-processing/raw/main/optimize-parquet.py
Use hf jobs run ... uv run <url> here, not hf jobs uv run <url>. hf jobs uv run uploads the script as a volume, and webhook runs don't keep volumes.
2. Create a webhook on the input bucket that re-runs this Job:
from huggingface_hub import create_webhook
create_webhook(
job_id="<job id from step 1>",
watched=[{"type": "bucket", "name": "<user>/my-raw-files"}],
domains=["repo"],
secret="<fine-grained token>",
)
The Job uses the webhook secret as its token to read and write the buckets. Use a fine-grained token, not your main one.
3. Upload a file:
hf buckets cp data.csv hf://buckets/<user>/my-raw-files/data.csv
After about a minute, the output is in <user>/my-parquet/data.csv/: the Parquet file(s) under data/, plus a README written by datasets.
Options
| Environment variable | Default | Meaning |
|---|---|---|
OUTPUT_BUCKET |
required | Bucket to write the Parquet files to. Must differ from the watched bucket. |
STREAM_ABOVE_BYTES |
1/3 of free disk | Files larger than this are streamed instead of loaded to disk. |
Files that fit on the Job's disk are loaded in full. Larger files are streamed, so they don't need to fit on the disk (50 GB on cpu-upgrade); raise --timeout for very large files. Supported inputs: .csv, .json, .jsonl, .parquet. Other files are skipped, and deleted files are ignored.
Notes
- Cost: a small file takes about 20 seconds on
cpu-upgrade($0.03/hour). In testing, onehf buckets syncof several files sent one webhook event, so it started one Job. - Pin the script: each webhook run downloads the script again. To stop changes to this recipe from reaching your webhook, replace
mainin the URL with a commit hash. - Limits: a webhook can trigger at most 1,000 times per 24 hours. Above 10,000 changed files in one event, the payload list is truncated; those files are not converted.
- Downloads last month
- -