# Build a speech dataset

> **About this page**
>
> - Kind: Example
> - Summary: Transcribes recordings with whisper.cpp, cuts them into one clip per transcribed segment, and packages clips and text as a Parquet speech-recognition dataset.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/speech-dataset
> - 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/speech-dataset`](https://github.com/swaleio/examples/tree/main/workflows/speech-dataset) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/speech-dataset/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/speech-dataset/workflow.yaml).

Turns long recordings into a speech-recognition dataset of short clips, each
with its transcription. whisper.cpp transcribes every recording into segments
with start and end times; FFmpeg cuts one clip per segment; and DuckDB packages
the clips and their text as Parquet, with the audio in the shape of a Hugging
Face `Audio` column, ready for training or for the Hugging Face Hub.

| Step | Task definition | Compute |
|------|-----------------|---------|
| Fetch the whisper.cpp model | [`swaleio/hf-download`](https://hub.swale.io/swaleio/hf-download) | `cpu-4` |
| For each recording: convert it to 16 kHz mono WAV | [`swaleio/ffmpeg`](https://hub.swale.io/swaleio/ffmpeg) | `cpu-4` |
| For each recording: transcribe it into timed segments | [`swaleio/whisper-cpp`](https://hub.swale.io/swaleio/whisper-cpp) | `a100-1` |
| List the segments, keep those within the length range | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Cut one clip per kept segment | [`swaleio/ffmpeg`](https://hub.swale.io/swaleio/ffmpeg) | `cpu-4` |
| Package clips and text into `speech.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 step is its own task.

## Inputs

| Input | Default | Description |
|-------|---------|-------------|
| `media_urls` | three poems read for LibriVox | The recordings, as a JSON array of URLs or workspace paths. |
| `whisper_model` | `ggml-large-v3-turbo-q5_0.bin` | Model file in `ggerganov/whisper.cpp`. |
| `language` | `en` | Two-letter language code, or `auto`. |
| `min_seconds` | `1` | Shortest clip kept. |
| `max_seconds` | `30` | Longest clip kept. |
| `hub_repo` | empty | Hugging Face dataset repo to upload the dataset to, as `data/train.parquet`. |

The upload reads the secret `hf_token`.

## Output

`speech.parquet` on the workspace, with `audio` (`bytes` and `path`),
`transcription` and `seconds` for each clip.

The transcriptions come from a model, not from people. Check a sample before
training on them.

## 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: Build a speech dataset
description: Transcribes recordings with whisper.cpp, cuts them into one clip per transcribed segment, and packages clips and text as a Parquet speech-recognition dataset.
compute_type: cpu-4

inputs:
  media_urls:
    description: The recordings, as a JSON array of URLs or workspace paths; anything FFmpeg reads.
    default: '["https://archive.org/download/spc140_1502_librivox/spc140_blackvulture_wt_64kb.mp3", "https://archive.org/download/spc140_1502_librivox/spc140_gethsemane_db_64kb.mp3", "https://archive.org/download/spc140_1502_librivox/spc140_fromonewhostays_ss_64kb.mp3"]'
  whisper_model:
    description: File name of the whisper.cpp model in ggerganov/whisper.cpp on the Hugging Face Hub.
    default: ggml-large-v3-turbo-q5_0.bin
  language:
    description: Spoken language as a two-letter code, or auto.
    default: en
  min_seconds:
    description: Shortest clip to keep.
    default: "1"
  max_seconds:
    description: Longest clip to keep.
    default: "30"
  hub_repo:
    description: Hugging Face dataset repo to upload the dataset to, as namespace/name. Leave empty to keep it on the workspace only.
    default: ""

entry_point: main

blocks:
  main:
    tasks:
      fetch_model:
        name: Fetch whisper model
        uses: swaleio/hf-download
        args:
          repo: ggerganov/whisper.cpp
          include: ${{inputs.whisper_model}}
          dest: ${{env.WORKFLOW_STORAGE}}/whisper

      each_recording:
        name: Transcribe each recording
        start_on:
          - fetch_model
        for_each:
          items: ${{inputs.media_urls}}
          block: transcribe_one

      cut_list:
        name: List the segments to cut
        uses: swaleio/duckdb
        start_on:
          - each_recording
        args:
          sql: |
            -- whisper.cpp's JSON has one entry per segment, with offsets in milliseconds.
            CREATE TABLE segments AS
              SELECT regexp_extract(filename, '(\d+)\.json$', 1)::INT AS recording,
                     generate_subscripts(transcription, 1) - 1 AS segment,
                     unnest(transcription).offsets."from" / 1000.0 AS start_s,
                     unnest(transcription).offsets."to" / 1000.0 AS end_s,
                     trim(unnest(transcription).text) AS text
              FROM read_json('${{env.WORKFLOW_STORAGE}}/transcripts/*.json', filename = true);
            CREATE TABLE kept AS
              SELECT *, end_s - start_s AS seconds,
                     recording || '-' || lpad(segment::VARCHAR, 4, '0') AS clip
              FROM segments
              WHERE end_s - start_s BETWEEN ${{inputs.min_seconds}} AND ${{inputs.max_seconds}} AND text <> '';
            COPY kept TO '${{env.WORKFLOW_STORAGE}}/segments.parquet' (FORMAT parquet);
            COPY (SELECT recording, start_s, seconds, clip FROM kept ORDER BY clip)
              TO '${{env.WORKFLOW_STORAGE}}/cuts.tsv' (FORMAT csv, DELIMITER '\t', HEADER false);
            SELECT count(*) AS segments, count(*) FILTER (seconds BETWEEN ${{inputs.min_seconds}} AND ${{inputs.max_seconds}}) AS kept
              FROM (SELECT end_s - start_s AS seconds FROM segments);

      cut:
        name: Cut the clips
        uses: swaleio/ffmpeg
        start_on:
          - cut_list
        args:
          script: |
            set -euo pipefail
            mkdir -p "$WORKFLOW_STORAGE/clips"
            while IFS=$'\t' read -r recording start seconds clip; do
              ffmpeg -hide_banner -loglevel error -nostdin -ss "$start" -t "$seconds" \
                -i "$WORKFLOW_STORAGE/wav/$recording.wav" -c:a pcm_s16le "$WORKFLOW_STORAGE/clips/$clip.wav"
            done < "$WORKFLOW_STORAGE/cuts.tsv"
            ls "$WORKFLOW_STORAGE/clips" | wc -l

      package:
        name: Package the dataset
        uses: swaleio/duckdb
        start_on:
          - cut
        args:
          sql: |
            -- The audio column has the shape of a Hugging Face Audio feature: the encoded file and its name.
            COPY (
              SELECT {'bytes': b.content, 'path': s.clip || '.wav'} AS audio,
                     s.text AS transcription, round(s.seconds, 2) AS seconds
              FROM '${{env.WORKFLOW_STORAGE}}/segments.parquet' s
              JOIN read_blob('${{env.WORKFLOW_STORAGE}}/clips/*.wav') b
                ON b.filename LIKE '%/' || s.clip || '.wav'
              ORDER BY s.clip
            ) TO '${{env.WORKFLOW_STORAGE}}/speech.parquet' (FORMAT parquet);
            SELECT count(*) AS clips, round(sum(seconds) / 60, 1) AS minutes,
                   round(avg(seconds), 1) AS mean_seconds
              FROM '${{env.WORKFLOW_STORAGE}}/speech.parquet';

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

  transcribe_one:
    tasks:
      to_wav:
        name: Convert to 16 kHz WAV
        uses: swaleio/ffmpeg
        args:
          script: |
            set -euo pipefail
            mkdir -p "$WORKFLOW_STORAGE/wav" "$WORKFLOW_STORAGE/transcripts"
            ffmpeg -hide_banner -loglevel error -i "${{tasks.each_recording.for_each.item}}" \
              -vn -ac 1 -ar 16000 -c:a pcm_s16le "$WORKFLOW_STORAGE/wav/${{tasks.each_recording.for_each.index}}.wav"
      transcribe:
        name: Transcribe
        uses: swaleio/whisper-cpp
        compute_type: a100-1
        start_on:
          - to_wav
        args:
          model: ${{env.WORKFLOW_STORAGE}}/whisper/${{inputs.whisper_model}}
          audio: ${{env.WORKFLOW_STORAGE}}/wav/${{tasks.each_recording.for_each.index}}.wav
          output_prefix: ${{env.WORKFLOW_STORAGE}}/transcripts/${{tasks.each_recording.for_each.index}}
          language: ${{inputs.language}}

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

## Licenses of the defaults

- Model: [ggerganov/whisper.cpp](https://huggingface.co/ggerganov/whisper.cpp), MIT.
- Recordings: from [LibriVox Short Poetry Collection 140](https://archive.org/details/spc140_1502_librivox), public domain.
