Skip to main content

Label a text dataset with an LLM

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

Labels every text in a dataset with one of a fixed set of labels, using a chat model through vLLM's offline batch inference. vLLM's structured outputs hold each answer to one of the labels, so no answer needs cleaning. When the texts already carry labels, a last step measures how often the model agrees with them and prints a confusion table.

StepTask definitionCompute
Fetch the modelswaleio/hf-downloadcpu-4
Write one labeling request per textswaleio/duckdbcpu-4
Labelswaleio/vllm-batcha100-1
Join labels to texts, count each label, write Parquetswaleio/duckdbcpu-4
Measure agreement with existing labels, when gold_column is setswaleio/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; only labeling holds a GPU.

Inputs​

InputDefaultDescription
modelQwen/Qwen2.5-1.5B-InstructChat model on the Hugging Face Hub.
textsthe test split of google-research-datasets/poem_sentimentParquet file or glob, as a DuckDB path.
text_columnverse_textColumn holding each text.
labelsnegative,positive,no_impact,mixedLabels the model chooses from, separated by commas.
instructiona sentiment instructionWhat the model is told before each text.
gold_columnlabelColumn of existing labels, given as each label's position in labels from 0; empty skips the comparison.
limit1000Number of texts to label.
hub_repoemptyHugging Face dataset repo to upload labels.parquet to.

The model download and the upload read the secret hf_token.

Output​

labels.parquet on the workspace: every column of the texts, a custom_id and the model's llm_label.

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: Label a text dataset with an LLM
description: Labels every text in a dataset with one of a fixed set of labels, using a chat model through vLLM batch inference, and measures agreement with existing labels.
compute_type: cpu-4

inputs:
model:
description: Hugging Face repo id of the chat model that labels.
default: Qwen/Qwen2.5-1.5B-Instruct
texts:
description: Parquet file or glob holding the texts, as a DuckDB path such as hf://datasets/<owner>/<dataset>/<file>.
default: hf://datasets/google-research-datasets/poem_sentiment/data/test-00000-of-00001.parquet
text_column:
description: Column holding each text.
default: verse_text
labels:
description: The labels to choose from, separated by commas. The model can answer only with one of them.
default: negative,positive,no_impact,mixed
instruction:
description: What the model is told before each text.
default: Classify the sentiment of this line of poetry as negative, positive, no_impact (neither) or mixed. Answer with the label only.
gold_column:
description: Column holding existing labels, as each label's position in labels counting from 0, to measure agreement against. Leave empty when the texts have none.
default: label
limit:
description: Number of texts to label.
default: "1000"
hub_repo:
description: Hugging Face dataset repo to upload the labels to, as namespace/name. Leave empty to keep them on the workspace only.
default: ""

entry_point: main

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

requests:
name: Write requests
uses: swaleio/duckdb
args:
sql: |
CREATE TABLE texts AS
SELECT 'row-' || lpad((row_number() OVER () - 1)::VARCHAR, 6, '0') AS custom_id, *
FROM (SELECT * FROM '${{inputs.texts}}' LIMIT ${{inputs.limit}});
COPY texts TO '${{env.WORKFLOW_STORAGE}}/texts.parquet' (FORMAT parquet);
COPY (
SELECT custom_id,
'POST' AS method,
'/v1/chat/completions' AS url,
{'model': '${{env.WORKFLOW_STORAGE}}/model',
'messages': [{'role': 'system', 'content': $$${{inputs.instruction}}$$},
{'role': 'user', 'content': "${{inputs.text_column}}"}],
'max_tokens': 16,
'temperature': 0,
'structured_outputs': {'choice': string_split($$${{inputs.labels}}$$, ',')}} AS body
FROM texts
) TO '${{env.WORKFLOW_STORAGE}}/requests.jsonl' (FORMAT json);
SELECT count(*) AS requests FROM texts;

label:
name: Label
uses: swaleio/vllm-batch
compute_type: a100-1
start_on:
- model
- requests
args:
model: ${{env.WORKFLOW_STORAGE}}/model
input_file: ${{env.WORKFLOW_STORAGE}}/requests.jsonl
output_file: ${{env.WORKFLOW_STORAGE}}/results.jsonl

collect:
name: Collect labels
uses: swaleio/duckdb
start_on:
- label
args:
sql: |
CREATE TABLE labeled AS
SELECT t.*, trim(r.response.body.choices[1].message.content) AS llm_label
FROM '${{env.WORKFLOW_STORAGE}}/texts.parquet' t
JOIN read_json('${{env.WORKFLOW_STORAGE}}/results.jsonl') r USING (custom_id)
ORDER BY custom_id;
SELECT llm_label, count(*) AS texts FROM labeled GROUP BY ALL ORDER BY texts DESC;
COPY labeled TO '${{env.WORKFLOW_STORAGE}}/labels.parquet' (FORMAT parquet);

compare:
name: Compare when existing labels are given
start_on:
- collect
if:
condition: ${{inputs.gold_column}}
true: agreement

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

agreement:
tasks:
measure:
name: Measure agreement
uses: swaleio/duckdb
args:
sql: |
CREATE TABLE pairs AS
SELECT string_split($$${{inputs.labels}}$$, ',')["${{inputs.gold_column}}" + 1] AS gold, llm_label
FROM '${{env.WORKFLOW_STORAGE}}/labels.parquet';
SELECT count(*) AS texts,
round(avg((gold = llm_label)::INT), 3) AS agreement
FROM pairs;
PIVOT pairs ON llm_label USING count(*) GROUP BY gold ORDER BY gold;

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

Licenses of the defaults​