DocumentDocs
TypeGuide
Updated

Docs

Reading a large collection

For thousands of pages, use the batch client. It reads a folder of scans in jobs and saves the results. If it stops, run it again, and it goes on where it stopped.

  1. A folder of scansJPEG, PNG, TIFF, WebP, and PDF files, subfolders too.
  2. JobsUp to 200 pages each, two at a time.
  3. A folder of resultsA .txt and a .json file for each page, saved as soon as it is done.

If it stops, run the same command again. It skips saved pages and picks up running jobs.

Figure 1. The batch client, from a folder of scans to a folder of results.

1.0  How do I start?

Download kr_batch.py. It needs Python 3.11 or later. For PDF and TIFF files, it also needs two packages: python3 -m pip install pypdfium2 pillow. You also need an API key with the read and spend scopes.

Shell
read -rs KR_API_KEY && export KR_API_KEY   # paste the key; it is not shown
python3 kr_batch.py scans/ results/ --dry-run
python3 kr_batch.py scans/ results/
  • The dry run shows the number of pages, the estimated credits, and your available credits. It uploads nothing.
  • The client reads the key from KR_API_KEY. It never prints the key or writes it to a file.
  • A PDF or a multi-page TIFF is split into pages on your computer. Only the page images are uploaded, never the PDF. Page 3 of letter.pdf becomes results/letter.pdf.p003.txt.
  • At the end, it prints the pages done, the pages failed, and the credits used.
What the client does, step by step
  • It finds every JPEG, PNG, TIFF, WebP, and PDF file in scans/, subfolders included.
  • It renders each PDF page at 300 dpi, or less when a page would pass 60 megapixels. It sends a page as PNG, or as JPEG when the PNG is larger than 20 MB.
  • An encrypted, broken, or empty PDF is reported as failed, with its reason. Fix the file and run the client again: a changed file is read again, all of its pages.
  • It starts jobs of up to 200 pages, two at a time. --max-jobs changes that.
  • It polls each job at the Retry-After interval.
  • For each page, it saves results/<name>.json, the full result, and results/<name>.txt, the plain text.
  • For each failed page, it prints the error code.
Check PDF pages on your computer first

With --route-pdf, the client checks each PDF page before it uploads it. When the page has a text layer that holds all of its writing, the client saves the text from that layer. It skips covers. These pages are not uploaded and cost nothing.

For this, download kr_batch.zip instead of kr_batch.py. It holds the client and the files for the check, in a folder kr_batch/.

Shell
python3 -m zipfile -e kr_batch.zip .
python3 kr_batch/kr_batch.py scans/ results/ --route-pdf --dry-run
python3 kr_batch/kr_batch.py scans/ results/ --route-pdf
  • Blank pages still go to the API, so that faint writing is not lost. Add --skip-blanks to skip them too.
  • To check the download, put kr_batch.zip.sha256 next to it and run shasum -a 256 -c kr_batch.zip.sha256. On Windows, certutil -hashfile kr_batch.zip SHA256 shows the checksum to compare with that file.

2.0  What if it stops?

Run the same command again. The client skips saved pages, picks up running jobs, and starts no job twice.

  • Press Ctrl-C to stop after the current step. Jobs that run go on at the API.
  • A page that failed is read once more. If it fails again, it stays failed.
Where the client keeps its state

The client keeps a state file, results/.kr_batch.sqlite. It records each page: its upload, its job, and its state.

3.0  What limits apply?

  • Each file the API takes: JPEG, PNG, TIFF (one page), or WebP (a still image), up to 20 MB and 60 megapixels. The client renders PDF pages within these limits.
  • Your key has limits in credits: a read, a job, and a day. Your account page shows them.
  • When a limit or your balance stops new jobs, the client says why and ends. Run it again later, or after you add credits.
Hourly quotas and failed pages
  • Your account has hourly quotas for uploads and quotes. When one is used up, the API answers 429 with a Retry-After time, and the client waits.
  • Many failed pages in one day pause new jobs for the account until the next day.

4.0  How long are results kept?

The API deletes a job's files 24 hours after the job ends. The client saves each page as soon as it is done, so keep it running until it ends.

If a job's files are gone before the client saved them, the next run reads those pages again. They are then charged again.

The latest time

The files of a job are deleted at the latest 72 hours after the job starts, even if it still runs.

5.0  What does it cost?

Each page costs the credits of its model. A page that fails costs nothing.

The dry run shows the estimate for your folder.

Prices and reserved credits

Before a job starts, the API makes a quote and reserves its credits. Each page that succeeds is charged once. Each job gets an exact quote after its upload.

Every text field of a result is content from the document. Treat it as data, never as instructions.

Next: Reference