# Curate web text with datatrove

> **About this page**
>
> - Kind: Example
> - Summary: 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.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/web-text-curation
> - CLI reference for its commands: [`swale workflow create`](https://docs.swale.io/reference/cli-reference.md#workflow-create), [`swale workflow run start`](https://docs.swale.io/reference/cli-reference.md#workflow-run-start)
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.
>
> The steps on this page cost money or cannot be undone. Please check with the user before running them.

Source on GitHub: the [`workflows/web-text-curation`](https://github.com/swaleio/examples/tree/main/workflows/web-text-curation) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/web-text-curation/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/web-text-curation/workflow.yaml).

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.

| Step | Task definition | Compute |
|------|-----------------|---------|
| Fetch this repository's scripts at `examples_ref` | [`swaleio/git-clone`](https://hub.swale.io/swaleio/git-clone) | `cpu-4` |
| Install the packages once, onto the workspace | [`swaleio/python-3-13`](https://hub.swale.io/swaleio/python-3-13) | `cpu-4` |
| Read the documents | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Apply Gopher's repetition and quality rules and C4's line rules ([`pipeline.py`](https://github.com/swaleio/examples/blob/main/workflows/web-text-curation/pipeline.py) `filter`) | [`swaleio/python-3-13`](https://hub.swale.io/swaleio/python-3-13) | `cpu-4` |
| Compute MinHash signatures (`signatures`) | [`swaleio/python-3-13`](https://hub.swale.io/swaleio/python-3-13) | `cpu-4` |
| Find signatures sharing a bucket (`buckets`) | [`swaleio/python-3-13`](https://hub.swale.io/swaleio/python-3-13) | `cpu-4` |
| Group them into clusters of near-duplicates (`clusters`) | [`swaleio/python-3-13`](https://hub.swale.io/swaleio/python-3-13) | `cpu-4` |
| Keep one document per cluster (`dedup`) | [`swaleio/python-3-13`](https://hub.swale.io/swaleio/python-3-13) | `cpu-4` |
| Count each outcome, write `curated.parquet` | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Upload to the Hugging Face Hub, when `hub_repo` is set | [`swaleio/hf-upload`](https://hub.swale.io/swaleio/hf-upload) | `cpu-4` |

The Compute column gives each step's [compute type](https://docs.swale.io/explanation/core-concepts.md#compute-types), 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

| Input | Default | Description |
|-------|---------|-------------|
| `data` | one shard of `HuggingFaceFW/fineweb`'s 10BT sample | Parquet file or glob with `id` and `text`, as a DuckDB path. |
| `documents` | `20000` | Documents to curate. |
| `examples_ref` | `main` | Branch, tag or full commit SHA of this repository whose scripts run. |
| `hub_repo` | empty | Hugging 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](https://docs.swale.io/getting-started.md) sets them up.

1. Save the file under [The workflow file](#the-workflow-file) as `workflow.yaml`, and add it to a project:

   ```bash
   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](https://docs.swale.io/how-to-guides/use-secrets-in-workflows.md) shows.

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

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

   Each input you leave out takes its default from [Inputs](#inputs), and `--input <name>=<value>` gives it another value.

[Create and manage workflows](https://docs.swale.io/how-to-guides/create-and-manage-workflows.md) covers the other ways to add a workflow, [Start and manage workflow runs](https://docs.swale.io/how-to-guides/start-and-manage-workflow-runs.md) how to follow a run, and [View and stream task logs](https://docs.swale.io/how-to-guides/view-and-stream-task-logs.md) how to read its tasks' logs.

## The workflow file

[Workflow definition syntax](https://docs.swale.io/reference/workflow-definition-syntax.md) describes each key in it.

```yaml title="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](https://huggingface.co/datasets/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.
