Perception

Classify

client.documents.classify(source, *, classes, **options)

ParameterTypeRequiredDescription
sourcestr | UrlSource | WorkspaceFileSourceYesThe document to classify. Doesn't take a parse_result source.
classeslist[dict]YesYour labels. See classes.
granularity"document" / "page"NoScore the whole file once, or once per page. Default: "document".
page_rangeslist[dict]NoOne-based, inclusive ranges to read and classify. Not for workbooks. Default: all pages.
unknown_policy"allow" / "force_best"Noallow (default) may mark a unit unknown; force_best commits an ambiguous unit to one of your classes.
outputdictNoHow many labels, and whether each has a reason. See output.
parse_mode"low" / "medium" / "high"NoEffort of the parse Classify runs first. Default: "low".

Each dict option takes these keys:

classes

One entry per label, each a dict:

KeyTypeDefaultDescription
classes[].idstrRequiredThe label's id, returned on the results.
classes[].labelstrRequiredThe label's name.
classes[].descriptionstr""What belongs in the class.
classes[].criterialist[str]NoneExtra rules to apply when scoring this class.
classes[].subclasseslist[dict]NoneFiner labels under this class, each with subclasses[].id and subclasses[].label (both required), and an optional subclasses[].description and subclasses[].criteria.

output

KeyTypeDefaultDescription
output.max_alternativesint3The ranked labels to return per unit, from 1 to 10.
output.include_reasonboolTrueAdd a short reason to each label.
classify.py
from nace_sdk import NaceClient

with NaceClient() as client:
    job = client.documents.classify(
        "https://example.com/invoice.pdf",
        classes=[
            {"id": "invoice", "label": "Invoice", "description": "A supplier invoice"},
            {"id": "contract", "label": "Contract", "description": "A signed agreement between two parties"},
        ],
        granularity="page",
        wait_seconds=60,
    )
    if job.status != "succeeded":
        job = client.jobs.wait(job.job_id)

for unit in job.result["units"]:
    print(unit["page_range"], unit["labels"][0]["class_id"])

Class ids must be unique and can't contain /, and other, blank_page, unreadable_content and __ndi_unclassified are reserved. See Classify.

On this page