Extract named entities with GLiNER
Source on GitHub: the workflows/entity-extraction folder, with its README and its workflow file.
Finds named entities of the types you name, such as people, organizations, places and dates, in a collection of texts, with GLiNER. GLiNER takes the types as plain words when it runs, so there is nothing to train. The texts are split into shards that several CPU tasks read at the same time, and SQL gathers what they found.
| Step | Task definition | Compute |
|---|---|---|
Fetch this repository's scripts at examples_ref | swaleio/git-clone | cpu-4 |
| Install the packages once, onto the workspace | swaleio/python-3-13 | cpu-4 |
| Fetch the model | swaleio/hf-download | cpu-4 |
Number the texts and cut each to max_characters | swaleio/duckdb | cpu-4 |
For each shard: extract the entities (extract.py) | swaleio/python-3-13 | cpu-4 each |
| Count mentions per type and list the most frequent entities | swaleio/duckdb | cpu-4 |
Upload to the Hugging Face Hub, when hub_repo is set | swaleio/hf-upload | cpu-4 |
The Compute column gives each step's compute type, the compute its tasks are allocated.
What stays one script: the extraction pass over a shard, one model call per text.
Why it runs on CPUs: GLiNER 0.2.30, its latest release, accepts only
Transformers below 5.23, while the GPU image runs 5.24. So this workflow
installs its own copy of the stack, with the CPU build of PyTorch. A small
GLiNER model reads a few texts a second on cpu-4; add shards to go faster.
Once GLiNER accepts current Transformers, the extraction can move to the
transformers task on a GPU.
Installed once: the install step puts the packages in packages/ on the
workspace, which every task of the run mounts, and each extraction task imports
them from there instead of installing its own copy. Adding shards adds no
installs.
Inputs
| Input | Default | Description |
|---|---|---|
model | urchade/gliner_small-v2.1 | GLiNER model on the Hugging Face Hub. |
texts | the test split of abisee/cnn_dailymail 3.0.0 | Parquet file or glob, as a DuckDB path. |
text_column | article | Column holding each text. |
limit | 200 | Texts to read. |
max_characters | 1500 | Characters of each text that are read. |
labels | person,organization,location,date | Entity types, as plain words. |
shards | ["0", "1", "2", "3"] | One extraction task per element. |
examples_ref | main | Branch, tag or full commit SHA of this repository whose scripts run. |
hub_repo | empty | Hugging Face dataset repo to upload the entities to, as data/train.parquet. |
The download and the upload read the secret hf_token.
Output
entities.parquet on the workspace, one row per entity with the text it was
found in, its words, type, score and character offsets.
Run it
You need the CLI, logged in to your account, and a project to add the workflow to; Getting started sets them up.
-
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.yamlThe command prints
Workflow created:followed by the workflow's ID. -
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. -
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.
name: Extract named entities with GLiNER
description: Extracts named entities of types you name from a collection of texts with GLiNER, with no training, in several parallel CPU tasks, and aggregates them with SQL.
compute_type: cpu-4
inputs:
model:
description: Hugging Face repo id of the GLiNER model.
default: urchade/gliner_small-v2.1
texts:
description: Parquet file or glob of the texts, as a DuckDB path.
default: hf://datasets/abisee/cnn_dailymail/3.0.0/test-00000-of-00001.parquet
text_column:
description: Column holding each text.
default: article
limit:
description: Texts to read.
default: "200"
max_characters:
description: Characters of each text that are read; GLiNER reads a few hundred words at a time.
default: "1500"
labels:
description: Entity types to find, as plain words, separated by commas.
default: person,organization,location,date
shards:
description: Extraction shards, as a JSON array; one CPU task runs per element.
default: '["0", "1", "2", "3"]'
examples_ref:
description: Branch, tag or full commit SHA of swaleio/examples whose scripts run.
default: main
hub_repo:
description: Hugging Face dataset repo to upload the entities to, as namespace/name. Leave empty to keep them on the workspace only.
default: ""
entry_point: main
blocks:
main:
tasks:
fetch_scripts:
name: Fetch the example scripts
uses: swaleio/git-clone
args:
repository_url: https://github.com/swaleio/examples.git
revision: ${{inputs.examples_ref}}
dest: ${{env.WORKFLOW_STORAGE}}/examples
install:
name: Install the packages once
uses: swaleio/python-3-13
start_on:
- fetch_scripts
args:
script: |
import os, subprocess, sys
# Onto the workspace, which every task of the run mounts: each extraction task
# imports the packages from there instead of installing its own copy.
storage = os.environ["WORKFLOW_STORAGE"]
subprocess.run([sys.executable, "-m", "pip", "install", "--quiet", "--target", f"{storage}/packages",
"-r", f"{storage}/examples/requirements/cpu-gliner.txt"], check=True)
fetch_model:
name: Fetch the model
uses: swaleio/hf-download
args:
repo: ${{inputs.model}}
dest: ${{env.WORKFLOW_STORAGE}}/model
token: ${{secrets.hf_token}}
prepare:
name: Prepare the texts
uses: swaleio/duckdb
args:
sql: |
COPY (SELECT row_number() OVER () - 1 AS id, left("${{inputs.text_column}}", ${{inputs.max_characters}}) AS text
FROM (SELECT * FROM '${{inputs.texts}}' LIMIT ${{inputs.limit}}))
TO '${{env.WORKFLOW_STORAGE}}/texts.parquet' (FORMAT parquet);
SELECT count(*) AS texts FROM '${{env.WORKFLOW_STORAGE}}/texts.parquet';
each_shard:
name: Extract from each shard
start_on:
- install
- fetch_model
- prepare
for_each:
items: ${{inputs.shards}}
block: extract_shard
aggregate:
name: Aggregate the entities
uses: swaleio/duckdb
start_on:
- each_shard
args:
sql: |
CREATE TABLE entities AS
SELECT * FROM read_json('${{env.WORKFLOW_STORAGE}}/entities/*.jsonl',
columns = {'id': 'BIGINT', 'text': 'VARCHAR', 'label': 'VARCHAR',
'score': 'DOUBLE', 'start': 'BIGINT', 'end': 'BIGINT'});
COPY entities TO '${{env.WORKFLOW_STORAGE}}/entities.parquet' (FORMAT parquet);
SELECT label, count(*) AS mentions, count(DISTINCT text) AS distinct_entities FROM entities GROUP BY label ORDER BY mentions DESC;
SELECT label, text, mentions, texts FROM (
SELECT label, text, count(*) AS mentions, count(DISTINCT id) AS texts
FROM entities GROUP BY label, text)
QUALIFY row_number() OVER (PARTITION BY label ORDER BY mentions DESC) <= 5
ORDER BY label, mentions DESC;
publish:
name: Upload when asked
start_on:
- aggregate
if:
condition: ${{inputs.hub_repo}}
true: upload
extract_shard:
tasks:
extract:
name: Extract entities
uses: swaleio/python-3-13
args:
script: |
import json, os, subprocess, sys
storage = os.environ["WORKFLOW_STORAGE"]
examples = f"{storage}/examples"
shards = len(json.loads("""${{inputs.shards}}"""))
os.makedirs(f"{storage}/entities", exist_ok=True)
subprocess.run([sys.executable, f"{examples}/workflows/entity-extraction/extract.py",
"--model", f"{storage}/model", "--texts", f"{storage}/texts.parquet",
"--shard", "${{tasks.each_shard.for_each.index}}", "--shards", str(shards),
"--labels", "${{inputs.labels}}",
"--output", f"{storage}/entities/${{tasks.each_shard.for_each.index}}.jsonl"],
env={**os.environ, "PYTHONPATH": f"{storage}/packages"}, check=True)
upload:
tasks:
upload_entities:
name: Upload to the Hugging Face Hub
uses: swaleio/hf-upload
args:
repo: ${{inputs.hub_repo}}
repo_type: dataset
source: ${{env.WORKFLOW_STORAGE}}/entities.parquet
path_in_repo: data/train.parquet
token: ${{secrets.hf_token}}
Licenses
- Model: urchade/gliner_small-v2.1, Apache-2.0; it loads the tokenizer of microsoft/deberta-v3-small, MIT, from the Hugging Face Hub.
- Dataset: abisee/cnn_dailymail, Apache-2.0.
- Packages the install step installs: GLiNER, Transformers and PyArrow (Apache-2.0), PyTorch (BSD-3-Clause), protobuf (BSD-3-Clause), with their dependencies under permissive licenses.