Skip to main content

Build a speech dataset

Source on GitHub: the workflows/speech-dataset folder, with its README and its workflow file.

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.

StepTask definitionCompute
Fetch the whisper.cpp modelswaleio/hf-downloadcpu-4
For each recording: convert it to 16 kHz mono WAVswaleio/ffmpegcpu-4
For each recording: transcribe it into timed segmentsswaleio/whisper-cppa100-1
List the segments, keep those within the length rangeswaleio/duckdbcpu-4
Cut one clip per kept segmentswaleio/ffmpegcpu-4
Package clips and text into speech.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 step is its own task.

Inputs​

InputDefaultDescription
media_urlsthree poems read for LibriVoxThe recordings, as a JSON array of URLs or workspace paths.
whisper_modelggml-large-v3-turbo-q5_0.binModel file in ggerganov/whisper.cpp.
languageenTwo-letter language code, or auto.
min_seconds1Shortest clip kept.
max_seconds30Longest clip kept.
hub_repoemptyHugging 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 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: 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​