Skip to main content

Curate web text with datatrove

Source on GitHub: the workflows/web-text-curation folder, with its README and its workflow file.

Cleans web text the way pre-training datasets such as FineWeb are built, with Hugging Face's datatrove: quality rules drop pages of boilerplate, lists and spam, then MinHash finds and drops pages that are near-copies of each other. Each datatrove stage runs as its own task, so the run's graph shows the pipeline, and a last step counts what each stage kept and dropped.

StepTask definitionCompute
Fetch this repository's scripts at examples_refswaleio/git-clonecpu-4
Install the packages once, onto the workspaceswaleio/python-3-13cpu-4
Read the documentsswaleio/duckdbcpu-4
Apply Gopher's repetition and quality rules and C4's line rules (pipeline.py filter)swaleio/python-3-13cpu-4
Compute MinHash signatures (signatures)swaleio/python-3-13cpu-4
Find signatures sharing a bucket (buckets)swaleio/python-3-13cpu-4
Group them into clusters of near-duplicates (clusters)swaleio/python-3-13cpu-4
Keep one document per cluster (dedup)swaleio/python-3-13cpu-4
Count each outcome, write curated.parquetswaleio/duckdbcpu-4
Upload to the Hugging Face Hub, when hub_repo is setswaleio/hf-uploadcpu-4

The Compute column gives each step's compute type, the compute its tasks are allocated.

Every stage is its own task. Dropped documents are kept apart with the rule that dropped them, under curation/removed/.

Installed once: the install step puts the packages in packages/ on the workspace, which every task of the run mounts, and the five stages import them from there instead of each installing its own copy.

Left out: a language filter. datatrove's language identification downloads either fastText's model (CC BY-SA 3.0) or a model under a non-commercial license, and FineWeb is already English, so this pipeline has none. Add one for multilingual input once you have checked its model's license.

Inputs​

InputDefaultDescription
dataone shard of HuggingFaceFW/fineweb's 10BT sampleParquet file or glob with id and text, as a DuckDB path.
documents20000Documents to curate.
examples_refmainBranch, tag or full commit SHA of this repository whose scripts run.
hub_repoemptyHugging Face dataset repo to upload the curated text to, as data/train.parquet.

The upload reads the secret hf_token.

Output​

curated.parquet on the workspace, and every stage's files under curation/.

Run it​

You need the CLI, logged in to your account, and a project to add the workflow to; Getting started sets them up.

  1. Save the file under The workflow file as workflow.yaml, and add it to a project:

    swale workflow create --project-name my-project --file workflow.yaml

    The command prints Workflow created: followed by the workflow's ID.

  2. The workflow reads the secret hf_token. Create it as a project secret, or as an account secret that allows the project, as Use secrets in workflows shows.

  3. Start a run with the workflow's ID:

    swale workflow run start --project-name my-project --workflow-id <workflow-id>

    Each input you leave out takes its default from Inputs, and --input <name>=<value> gives it another value.

Create and manage workflows covers the other ways to add a workflow, Start and manage workflow runs how to follow a run, and View and stream task logs how to read its tasks' logs.

The workflow file​

Workflow definition syntax describes each key in it.

workflow.yaml
name: Curate web text with datatrove
description: Cleans a sample of web text the way pre-training datasets are built, with datatrove quality filters and MinHash deduplication, one task per stage, all on CPUs.
compute_type: cpu-4

inputs:
data:
description: Parquet file or glob of web documents with id and text columns, as a DuckDB path.
default: hf://datasets/HuggingFaceFW/fineweb/sample/10BT/000_00000.parquet
documents:
description: Documents to curate.
default: "20000"
examples_ref:
description: Branch, tag or full commit SHA of swaleio/examples whose scripts run.
default: main
hub_repo:
description: Hugging Face dataset repo to upload the curated text to, as namespace/name. Leave empty to keep it on the workspace only.
default: ""

entry_point: main

blocks:
main:
tasks:
fetch_scripts:
name: Fetch the example scripts
uses: swaleio/git-clone
args:
repository_url: https://github.com/swaleio/examples.git
revision: ${{inputs.examples_ref}}
dest: ${{env.WORKFLOW_STORAGE}}/examples

install:
name: Install the packages once
uses: swaleio/python-3-13
start_on:
- fetch_scripts
args:
script: |
import os, subprocess, sys

# Onto the workspace, which every task of the run mounts: each stage imports
# the packages from there instead of installing its own copy.
storage = os.environ["WORKFLOW_STORAGE"]
subprocess.run([sys.executable, "-m", "pip", "install", "--quiet", "--target", f"{storage}/packages",
"-r", f"{storage}/examples/requirements/cpu-datatrove.txt"], check=True)

read:
name: Read the documents
uses: swaleio/duckdb
args:
sql: |
COPY (SELECT id, text FROM '${{inputs.data}}' LIMIT ${{inputs.documents}})
TO '${{env.WORKFLOW_STORAGE}}/documents.parquet' (FORMAT parquet);
SELECT count(*) AS documents, round(avg(length(text))) AS mean_characters
FROM '${{env.WORKFLOW_STORAGE}}/documents.parquet';

quality:
name: Quality filters
uses: swaleio/python-3-13
start_on:
- install
- read
args:
script: |
import os, subprocess, sys

storage = os.environ["WORKFLOW_STORAGE"]
examples = f"{storage}/examples"
subprocess.run([sys.executable, f"{examples}/workflows/web-text-curation/pipeline.py", "filter",
"--input", f"{storage}/documents.parquet", "--work", f"{storage}/curation"],
env={**os.environ, "PYTHONPATH": f"{storage}/packages"}, check=True)

signatures:
name: MinHash signatures
uses: swaleio/python-3-13
start_on:
- quality
args:
script: |
import os, subprocess, sys

storage = os.environ["WORKFLOW_STORAGE"]
examples = f"{storage}/examples"
subprocess.run([sys.executable, f"{examples}/workflows/web-text-curation/pipeline.py", "signatures",
"--input", f"{storage}/documents.parquet", "--work", f"{storage}/curation"],
env={**os.environ, "PYTHONPATH": f"{storage}/packages"}, check=True)

buckets:
name: MinHash buckets
uses: swaleio/python-3-13
start_on:
- signatures
args:
script: |
import os, subprocess, sys

storage = os.environ["WORKFLOW_STORAGE"]
examples = f"{storage}/examples"
subprocess.run([sys.executable, f"{examples}/workflows/web-text-curation/pipeline.py", "buckets",
"--input", f"{storage}/documents.parquet", "--work", f"{storage}/curation"],
env={**os.environ, "PYTHONPATH": f"{storage}/packages"}, check=True)

clusters:
name: Near-duplicate clusters
uses: swaleio/python-3-13
start_on:
- buckets
args:
script: |
import os, subprocess, sys

storage = os.environ["WORKFLOW_STORAGE"]
examples = f"{storage}/examples"
subprocess.run([sys.executable, f"{examples}/workflows/web-text-curation/pipeline.py", "clusters",
"--input", f"{storage}/documents.parquet", "--work", f"{storage}/curation"],
env={**os.environ, "PYTHONPATH": f"{storage}/packages"}, check=True)

dedup:
name: Drop near-duplicates
uses: swaleio/python-3-13
start_on:
- clusters
args:
script: |
import os, subprocess, sys

storage = os.environ["WORKFLOW_STORAGE"]
examples = f"{storage}/examples"
subprocess.run([sys.executable, f"{examples}/workflows/web-text-curation/pipeline.py", "dedup",
"--input", f"{storage}/documents.parquet", "--work", f"{storage}/curation"],
env={**os.environ, "PYTHONPATH": f"{storage}/packages"}, check=True)

report:
name: Count what each stage kept
uses: swaleio/duckdb
start_on:
- dedup
args:
sql: |
-- datatrove writes Parquet files, one folder per outcome.
SELECT regexp_extract(filename, '/curation/((removed/)?[a-z0-9_]+)/', 1) AS outcome, count(*) AS documents
FROM read_parquet('${{env.WORKFLOW_STORAGE}}/curation/**/*.parquet', filename = true)
GROUP BY outcome ORDER BY outcome;
COPY (SELECT id, text FROM '${{env.WORKFLOW_STORAGE}}/curation/curated/*.parquet')
TO '${{env.WORKFLOW_STORAGE}}/curated.parquet' (FORMAT parquet);

publish:
name: Upload when asked
start_on:
- report
if:
condition: ${{inputs.hub_repo}}
true: upload

upload:
tasks:
upload_text:
name: Upload to the Hugging Face Hub
uses: swaleio/hf-upload
args:
repo: ${{inputs.hub_repo}}
repo_type: dataset
source: ${{env.WORKFLOW_STORAGE}}/curated.parquet
path_in_repo: data/train.parquet
token: ${{secrets.hf_token}}

Licenses​

  • Dataset: HuggingFaceFW/fineweb, ODC-By 1.0; Common Crawl's terms of use apply to its texts.
  • Packages the install step installs: datatrove, NLTK, Tokenizers and PyArrow (Apache-2.0), spaCy (MIT), xxhash (BSD-2-Clause) and NumPy (BSD-3-Clause), with their dependencies under permissive licenses.