Skip to main content

Convert PDFs to a Markdown dataset

Source on GitHub: the workflows/pdfs-to-markdown folder, with its README and its workflow file.

Turns a list of PDFs into a text dataset. Each PDF is fetched and converted to Markdown by Docling in its own pair of tasks, and all the pairs run at the same time; a last step gathers the Markdown files into one Parquet file with each document's source URL.

StepTask definitionCompute
For each PDF: fetch itswaleio/http-requestcpu-4
For each PDF: convert it to Markdown, pictures marked by placeholdersswaleio/doclingcpu-4
Gather the Markdown into documents.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. Docling converts a 10-page paper in under a minute on a CPU compute type; give the convert task a GPU compute type for long documents.

Inputs​

InputDefaultDescription
pdf_urlsthree papers from arXivThe PDFs, as a JSON array of URLs.
hub_repoemptyHugging Face dataset repo to upload the dataset to, as data/train.parquet.

The upload reads the secret hf_token.

Output​

documents.parquet on the workspace, with url, markdown and characters for each PDF, and the Markdown files in markdown/.

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: Convert PDFs to a Markdown dataset
description: Converts a list of PDFs to Markdown with Docling, one task per document, and collects the results into one Parquet dataset.
compute_type: cpu-4

inputs:
pdf_urls:
description: The PDFs to convert, as a JSON array of URLs.
default: '["https://arxiv.org/pdf/2408.09869v5", "https://arxiv.org/pdf/2501.17887v1", "https://arxiv.org/pdf/2305.14314v1"]'
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:
each_pdf:
name: Convert each PDF
for_each:
items: ${{inputs.pdf_urls}}
block: convert_one

collect:
name: Collect into a dataset
uses: swaleio/duckdb
start_on:
- each_pdf
args:
sql: |
CREATE TABLE documents AS
WITH urls AS (
SELECT unnest(from_json($$${{inputs.pdf_urls}}$$, '["VARCHAR"]')) AS url,
generate_subscripts(from_json($$${{inputs.pdf_urls}}$$, '["VARCHAR"]'), 1) - 1 AS item)
SELECT u.url, t.content AS markdown, length(t.content) AS characters
FROM read_text('${{env.WORKFLOW_STORAGE}}/markdown/*.md') t
JOIN urls u ON regexp_extract(t.filename, '(\d+)\.md$', 1)::INT = u.item
ORDER BY u.item;
SELECT url, characters FROM documents;
COPY documents TO '${{env.WORKFLOW_STORAGE}}/documents.parquet' (FORMAT parquet);

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

convert_one:
tasks:
fetch:
name: Fetch PDF
uses: swaleio/http-request
args:
url: ${{tasks.each_pdf.for_each.item}}
expected_status: "200"
response_path: ${{env.WORKFLOW_STORAGE}}/pdf/${{tasks.each_pdf.for_each.index}}.pdf
convert:
name: Convert to Markdown
uses: swaleio/docling
start_on:
- fetch
args:
source: ${{env.WORKFLOW_STORAGE}}/pdf/${{tasks.each_pdf.for_each.index}}.pdf
output_dir: ${{env.WORKFLOW_STORAGE}}/markdown
image_export_mode: placeholder

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}}/documents.parquet
path_in_repo: data/train.parquet
token: ${{secrets.hf_token}}

Licenses of the defaults​

The three default PDFs are licensed CC BY 4.0 by their authors: Docling Technical Report, Docling: An Efficient Open-Source Toolkit for AI-driven Document Conversion and QLoRA: Efficient Finetuning of Quantized LLMs. A dataset made from them keeps their attribution.