Skip to main content

OCR pages with a vision-language model

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

Transcribes a PDF page by page with a vision-language model: each page is rendered to an image, the model writes out the page as Markdown, and the pages are joined into one document. This suits scans and layouts a text extractor cannot read. For born-digital PDFs, swaleio/docling is faster and keeps the document's structure; see the PDFs to Markdown workflow.

StepTask definitionCompute
Fetch the modelswaleio/hf-downloadcpu-4
Fetch the PDFswaleio/http-requestcpu-4
Render each page to PNGswaleio/python-3-13cpu-4
Write one request per page, with the page inlineswaleio/duckdbcpu-4
Transcribeswaleio/vllm-batcha100-1
Join the pages into one Markdown documentswaleio/duckdbcpu-4

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

Every step is its own task; only transcription holds a GPU.

The last step prints each page's finish_reason. length means the page ran into the token limit. With the default 2B model that happens on dense pages, such as a list of references, where the model starts repeating itself. A larger model, such as Qwen/Qwen3-VL-8B-Instruct, reads those pages better.

Inputs​

InputDefaultDescription
pdf_urlthe Docling technical report on arXivURL of the PDF.
modelQwen/Qwen3-VL-2B-InstructVision-language model on the Hugging Face Hub that vLLM serves.
dpi110Resolution the pages are rendered at.

The download reads the secret hf_token.

Output​

document.md on the workspace, with the pages separated by ---, and pages.parquet with each page's Markdown and finish_reason.

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: OCR pages with a vision-language model
description: Renders each page of a PDF to an image and has a vision-language model transcribe every page to Markdown, then joins the pages into one document.
compute_type: cpu-4

inputs:
pdf_url:
description: URL of the PDF to transcribe.
default: https://arxiv.org/pdf/2408.09869v5
model:
description: Hugging Face repo id of the vision-language model that reads the pages.
default: Qwen/Qwen3-VL-2B-Instruct
dpi:
description: Resolution the pages are rendered at.
default: "110"

entry_point: main

blocks:
main:
tasks:
fetch_model:
name: Fetch model
uses: swaleio/hf-download
args:
repo: ${{inputs.model}}
dest: ${{env.WORKFLOW_STORAGE}}/model
token: ${{secrets.hf_token}}

fetch_pdf:
name: Fetch PDF
uses: swaleio/http-request
args:
url: ${{inputs.pdf_url}}
expected_status: "200"
response_path: ${{env.WORKFLOW_STORAGE}}/document.pdf

render:
name: Render pages
uses: swaleio/python-3-13
start_on:
- fetch_pdf
args:
script: |
import os, subprocess, sys
subprocess.run([sys.executable, "-m", "pip", "install", "--quiet", "--target", "/tmp/deps",
"pypdfium2==5.15.2", "pillow==13.0.0"], check=True)
sys.path.insert(0, "/tmp/deps")
import pypdfium2 as pdfium

storage = os.environ["WORKFLOW_STORAGE"]
out = os.path.join(storage, "pages")
os.makedirs(out, exist_ok=True)
pdf = pdfium.PdfDocument(os.path.join(storage, "document.pdf"))
for number, page in enumerate(pdf, 1):
page.render(scale=${{inputs.dpi}} / 72).to_pil().save(os.path.join(out, f"{number:04d}.png"))
print(f"rendered {len(pdf)} pages")

requests:
name: Write page requests
uses: swaleio/duckdb
start_on:
- render
args:
sql: |
COPY (
SELECT 'page-' || regexp_extract(filename, '(\d+)\.png$', 1) AS custom_id,
'POST' AS method, '/v1/chat/completions' AS url,
{'model': '${{env.WORKFLOW_STORAGE}}/model',
'messages': [{'role': 'user', 'content': [
{'type': 'image_url', 'image_url': {'url': 'data:image/png;base64,' || base64(content)}},
{'type': 'text', 'text': 'Read this page and write out all of its text as plain Markdown, in reading order, with its headings, lists and tables. Do not use LaTeX. Write nothing else.'}]}],
'max_tokens': 4096, 'temperature': 0, 'repetition_penalty': 1.05} AS body
FROM read_blob('${{env.WORKFLOW_STORAGE}}/pages/*.png')
) TO '${{env.WORKFLOW_STORAGE}}/requests.jsonl' (FORMAT json);
SELECT count(*) AS pages FROM read_blob('${{env.WORKFLOW_STORAGE}}/pages/*.png');

transcribe:
name: Transcribe pages
uses: swaleio/vllm-batch
compute_type: a100-1
start_on:
- fetch_model
- requests
args:
model: ${{env.WORKFLOW_STORAGE}}/model
input_file: ${{env.WORKFLOW_STORAGE}}/requests.jsonl
output_file: ${{env.WORKFLOW_STORAGE}}/pages.jsonl

assemble:
name: Assemble document
uses: swaleio/duckdb
start_on:
- transcribe
args:
sql: |
CREATE TABLE pages AS
SELECT custom_id AS page,
response.body.choices[1].message.content AS markdown,
response.body.choices[1].finish_reason AS finish_reason
FROM read_json('${{env.WORKFLOW_STORAGE}}/pages.jsonl') ORDER BY page;
COPY pages TO '${{env.WORKFLOW_STORAGE}}/pages.parquet' (FORMAT parquet);
COPY (SELECT string_agg(markdown, chr(10) || chr(10) || '---' || chr(10) || chr(10) ORDER BY page) FROM pages)
TO '${{env.WORKFLOW_STORAGE}}/document.md' (FORMAT csv, HEADER false, QUOTE '', ESCAPE '');
SELECT page, finish_reason, length(markdown) AS characters FROM pages;

Licenses of the defaults​