Quick start

Give it a PDF and the classes you want, and get back the class of the document, or of each of its pages.

1. Set up

Create an API key in the PageIndex Console and set it:

export PAGEINDEX_API_KEY="your-pageindex-key"

Install the SDK:

pip install -U pageindex

2. Classify a document

Name each class, with a description where the name alone may not be enough, then call classify(). It uploads the PDF, classifies it and waits for the result, usually a few seconds.

from pageindex import PageClassifyClient

client = PageClassifyClient()  # reads PAGEINDEX_API_KEY

classes = {
    "invoice": "A bill asking for payment, with an invoice number and an amount due",
    "receipt": "Proof of a purchase already paid",
    "bank_statement": "A bank's statement of an account's transactions and balance",
    "utility_bill": None,  # the name alone is clear enough
}

job = client.classify("scan.pdf", classes)
print(job["class"], job["confidence"])  # invoice 0.98

A document is classified on its first 5 pages, unless you name others with pages, such as pages="2-6". A document that is none of your classes comes back with class None.

The PDF is kept in your "Page Classify" collection (collection names another). To classify a document you already uploaded, pass its ID in place of the file.

3. Or classify each page

To get a class for each page, pass granularity="page":

job = client.classify("mixed.pdf", classes, granularity="page")
for page in job["pages"]:
    print(page["page"], page["class"], page["confidence"])
1 invoice 0.98
2 invoice 0.97
3 receipt 0.95
4 None 0.88
5 bank_statement 0.93

A page that is none of your classes, such as a fax cover sheet, is None.

Every page is read unless you name fewer with pages, such as pages 3 to 9 of a long scan:

job = client.classify("scan.pdf", classes, granularity="page", pages="3-9")

Price

Classifying costs $0.001 a page read ($1 per 1,000 pages), and you only pay when the job completes. Uploading the PDF with it is free. See Pricing.

Jobs

classify() waits for the job. With wait=False it returns the job pending, and wait_classify() or get_classify() reads it later by its ID. A job that fails isn't charged; a PDF sent with it is left as a failed document. A job that could not read some pages completes without them, listed in failed_pages and not charged: classify() and wait_classify() raise for it, naming the pages, and get_classify() returns it as it is.

Next