ocr-software 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,71 @@
1
+ ocr-software: Python client and command line for the ocr-software.com OCR API (MIT, standard library only, Python 3.9 or newer).
2
+
3
+ Install:
4
+ pipx install ocr-software command line only, in its own environment
5
+ pip install ocr-software client library, inside a venv
6
+ uvx ocr-software --version run once without installing (uv)
7
+ First call (list the pipelines, then read one file):
8
+ ocr-software --list-pipelines
9
+ ocr-software scan.pdf --pipeline scan-ctc@2 -o scan.txt
10
+
11
+ Settings (environment; the command line flag wins):
12
+ OCR_SOFTWARE_API_KEY API key (starts with ocr_). OCR_KEY is read too, it is the name used in the API docs.
13
+ OCR_SOFTWARE_BASE_URL default https://api.ocr-software.com ; own server e.g. http://127.0.0.1:8080 (also --base-url). OCR_BASE_URL is read too.
14
+ OCR_SOFTWARE_ALLOWED_DIR optional: read documents only from below this folder (symlinks and .. do not escape)
15
+ The key is never printed and never part of an error message. Without --pipeline the default is used: the fastest and cheapest available pipeline (no previews). It is named on stderr; --list-pipelines marks it. In Python: client.ocr(path, client.default_pipeline()).
16
+
17
+ Command line:
18
+ ocr-software --list-pipelines available pipelines, one per line: name@version, tab, what it delivers and costs; the default one on stderr (no key needed)
19
+ ocr-software scan.pdf --pipeline NAME@VERSION --dry-run page count and price in euros, nothing runs, nothing is billed
20
+ ocr-software scan.pdf text to stdout, with the default pipeline (named on stderr)
21
+ ocr-software scan.pdf --pipeline NAME@VERSION text to stdout
22
+ ocr-software scan.pdf -o scan.md --pipeline NAME@VERSION --format md
23
+ ocr-software scan.pdf -o scan.pdf.txt --pipeline NAME@VERSION
24
+ ocr-software scan.pdf -o searchable.pdf --pipeline NAME@VERSION --format pdf searchable PDF, -o is required
25
+ ocr-software scan.pdf --pipeline NAME@VERSION --format json > scan.json
26
+ ocr-software scan.pdf --pipeline NAME@VERSION --base-url http://10.0.0.5:8080 your own server
27
+ python -m ocr_software ... the same command without the script on PATH
28
+ Options: --format text (default) | md | json | hocr | pdf ; --dpi 150|200|300 (PDF render resolution) ;
29
+ --timeout SECONDS (wait for the job, default 3600) ; --job-budget MILLICENTS (cap; ignored where billing is off) ; --dry-run ; --version
30
+ Without -o the result goes to stdout as UTF-8 (not for pdf). With -o the file appears whole or not at all, an existing
31
+ file is replaced, and the input file itself is refused as output.
32
+
33
+ Exit codes:
34
+ 0 done, every page read
35
+ 1 error from the service or the network (message on stderr with the API error code, for example 401 unauthorized);
36
+ also an address that does not answer: a typo and an outage look the same, so a script may retry this
37
+ 2 wrong call: unreadable or unsupported input, output not writable, no key, malformed address (not http/https)
38
+ 3 the job is done but a page failed or did not run, or the job stopped early: the result is written anyway,
39
+ stderr says what is missing and names the job; treat the file as incomplete
40
+ 4 the job did not finish within --timeout; it keeps running, stderr names the job
41
+
42
+ Python:
43
+ from ocr_software import Client, PagesUnread
44
+ client = Client() # key and address from the environment
45
+ text = client.ocr("scan.pdf", "NAME@VERSION", "md") # bytes; raises PagesUnread (with .data) if a page was not read
46
+ pdf = client.ocr("scan.pdf", "NAME@VERSION", "pdf") # searchable PDF
47
+ job = client.submit("scan.pdf", "NAME@VERSION") # step by step: submit, wait, result
48
+ client.wait(job["job_id"]); client.result(job["job_id"], "json")
49
+ client.quote("scan.pdf", "NAME@VERSION") # dry run: page count and price, nothing runs
50
+ from ocr_software import price_sentence; price_sentence(q) # the same sentence as --dry-run, in euros
51
+ Price: the API counts money in millicents, 100,000 millicents = 1 EUR (a page of the cheapest pipeline costs 100 = EUR 0.001).
52
+ Today nothing is charged ("you pay EUR 0.00 today"); the list price is shown for information. Free use has limits:
53
+ https://ocr-software.com/legal/fair-usage/ ; for more volume write to info@ocr-software.com.
54
+ Word boxes (json): box = [x0, y0, x1, y1] in pixels of the page image, origin top left; width, height and dpi of the page say which
55
+ image. In PDF points: pixels * 72 / dpi. Per word, verified is true only where a second reader confirmed it (scan-vlm-ctc@2);
56
+ safe_ok is always false today. Judge by confidence (0 to 1). Every field: https://ocr-software.com/docs/reference/
57
+ Retries follow the API rules: 429, 500, 502, 503, 504 and network errors up to 5 times (waits 1, 2, 4, 8, 16 s or Retry-After). An
58
+ upload the service cuts off (it does that beyond 2 uploads at once per key, instead of answering 429) counts like 429. If no connection
59
+ can be made at all (wrong address, host down) it gives up after 2 retries and names the address: a few seconds, up to about 10 s on
60
+ Windows, where a refused port itself takes 2 s per try (connect timeout 10 s). A Retry-After over 60 s (locked_out) is not waited for. A submit carries an Idempotency-Key, so a retry never creates a second job.
61
+ Every document is checked locally first: PDF, PNG, JPEG or TIFF by its first bytes, at most 100 MiB.
62
+
63
+ Paperless-ngx, pre-consume script (sketch, not tried against a real Paperless; a non-PDF input keeps its file name but becomes a PDF):
64
+ #!/bin/sh
65
+ ocr-software "$DOCUMENT_WORKING_PATH" --pipeline NAME@VERSION --format pdf -o "$DOCUMENT_WORKING_PATH.ocr" && mv "$DOCUMENT_WORKING_PATH.ocr" "$DOCUMENT_WORKING_PATH"
66
+ with OCR_SOFTWARE_API_KEY and OCR_SOFTWARE_BASE_URL set in the environment of the Paperless container.
67
+
68
+ Tests (fake local service, no network, no key):
69
+ python -m venv .venv
70
+ .venv/bin/pip install pytest (Windows: .venv\Scripts\pip install pytest)
71
+ .venv/bin/python -m pytest -p no:cacheprovider . (Windows: .venv\Scripts\python -m pytest -p no:cacheprovider .)
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ocr-software.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,88 @@
1
+ Metadata-Version: 2.4
2
+ Name: ocr-software
3
+ Version: 0.1.0
4
+ Summary: Client and command line for the ocr-software.com OCR API: text, word boxes, layout, searchable PDF.
5
+ License-Expression: MIT
6
+ Project-URL: Homepage, https://ocr-software.com/
7
+ Project-URL: Repository, https://github.com/ocr-software/ocr-software-python
8
+ Keywords: ocr,pdf,searchable-pdf,hocr,markdown,document-ai
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Topic :: Scientific/Engineering :: Image Recognition
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/plain
13
+ License-File: LICENSE
14
+ Provides-Extra: test
15
+ Requires-Dist: pytest==9.0.2; extra == "test"
16
+ Dynamic: license-file
17
+
18
+ ocr-software: Python client and command line for the ocr-software.com OCR API (MIT, standard library only, Python 3.9 or newer).
19
+
20
+ Install:
21
+ pipx install ocr-software command line only, in its own environment
22
+ pip install ocr-software client library, inside a venv
23
+ uvx ocr-software --version run once without installing (uv)
24
+ First call (list the pipelines, then read one file):
25
+ ocr-software --list-pipelines
26
+ ocr-software scan.pdf --pipeline scan-ctc@2 -o scan.txt
27
+
28
+ Settings (environment; the command line flag wins):
29
+ OCR_SOFTWARE_API_KEY API key (starts with ocr_). OCR_KEY is read too, it is the name used in the API docs.
30
+ OCR_SOFTWARE_BASE_URL default https://api.ocr-software.com ; own server e.g. http://127.0.0.1:8080 (also --base-url). OCR_BASE_URL is read too.
31
+ OCR_SOFTWARE_ALLOWED_DIR optional: read documents only from below this folder (symlinks and .. do not escape)
32
+ The key is never printed and never part of an error message. Without --pipeline the default is used: the fastest and cheapest available pipeline (no previews). It is named on stderr; --list-pipelines marks it. In Python: client.ocr(path, client.default_pipeline()).
33
+
34
+ Command line:
35
+ ocr-software --list-pipelines available pipelines, one per line: name@version, tab, what it delivers and costs; the default one on stderr (no key needed)
36
+ ocr-software scan.pdf --pipeline NAME@VERSION --dry-run page count and price in euros, nothing runs, nothing is billed
37
+ ocr-software scan.pdf text to stdout, with the default pipeline (named on stderr)
38
+ ocr-software scan.pdf --pipeline NAME@VERSION text to stdout
39
+ ocr-software scan.pdf -o scan.md --pipeline NAME@VERSION --format md
40
+ ocr-software scan.pdf -o scan.pdf.txt --pipeline NAME@VERSION
41
+ ocr-software scan.pdf -o searchable.pdf --pipeline NAME@VERSION --format pdf searchable PDF, -o is required
42
+ ocr-software scan.pdf --pipeline NAME@VERSION --format json > scan.json
43
+ ocr-software scan.pdf --pipeline NAME@VERSION --base-url http://10.0.0.5:8080 your own server
44
+ python -m ocr_software ... the same command without the script on PATH
45
+ Options: --format text (default) | md | json | hocr | pdf ; --dpi 150|200|300 (PDF render resolution) ;
46
+ --timeout SECONDS (wait for the job, default 3600) ; --job-budget MILLICENTS (cap; ignored where billing is off) ; --dry-run ; --version
47
+ Without -o the result goes to stdout as UTF-8 (not for pdf). With -o the file appears whole or not at all, an existing
48
+ file is replaced, and the input file itself is refused as output.
49
+
50
+ Exit codes:
51
+ 0 done, every page read
52
+ 1 error from the service or the network (message on stderr with the API error code, for example 401 unauthorized);
53
+ also an address that does not answer: a typo and an outage look the same, so a script may retry this
54
+ 2 wrong call: unreadable or unsupported input, output not writable, no key, malformed address (not http/https)
55
+ 3 the job is done but a page failed or did not run, or the job stopped early: the result is written anyway,
56
+ stderr says what is missing and names the job; treat the file as incomplete
57
+ 4 the job did not finish within --timeout; it keeps running, stderr names the job
58
+
59
+ Python:
60
+ from ocr_software import Client, PagesUnread
61
+ client = Client() # key and address from the environment
62
+ text = client.ocr("scan.pdf", "NAME@VERSION", "md") # bytes; raises PagesUnread (with .data) if a page was not read
63
+ pdf = client.ocr("scan.pdf", "NAME@VERSION", "pdf") # searchable PDF
64
+ job = client.submit("scan.pdf", "NAME@VERSION") # step by step: submit, wait, result
65
+ client.wait(job["job_id"]); client.result(job["job_id"], "json")
66
+ client.quote("scan.pdf", "NAME@VERSION") # dry run: page count and price, nothing runs
67
+ from ocr_software import price_sentence; price_sentence(q) # the same sentence as --dry-run, in euros
68
+ Price: the API counts money in millicents, 100,000 millicents = 1 EUR (a page of the cheapest pipeline costs 100 = EUR 0.001).
69
+ Today nothing is charged ("you pay EUR 0.00 today"); the list price is shown for information. Free use has limits:
70
+ https://ocr-software.com/legal/fair-usage/ ; for more volume write to info@ocr-software.com.
71
+ Word boxes (json): box = [x0, y0, x1, y1] in pixels of the page image, origin top left; width, height and dpi of the page say which
72
+ image. In PDF points: pixels * 72 / dpi. Per word, verified is true only where a second reader confirmed it (scan-vlm-ctc@2);
73
+ safe_ok is always false today. Judge by confidence (0 to 1). Every field: https://ocr-software.com/docs/reference/
74
+ Retries follow the API rules: 429, 500, 502, 503, 504 and network errors up to 5 times (waits 1, 2, 4, 8, 16 s or Retry-After). An
75
+ upload the service cuts off (it does that beyond 2 uploads at once per key, instead of answering 429) counts like 429. If no connection
76
+ can be made at all (wrong address, host down) it gives up after 2 retries and names the address: a few seconds, up to about 10 s on
77
+ Windows, where a refused port itself takes 2 s per try (connect timeout 10 s). A Retry-After over 60 s (locked_out) is not waited for. A submit carries an Idempotency-Key, so a retry never creates a second job.
78
+ Every document is checked locally first: PDF, PNG, JPEG or TIFF by its first bytes, at most 100 MiB.
79
+
80
+ Paperless-ngx, pre-consume script (sketch, not tried against a real Paperless; a non-PDF input keeps its file name but becomes a PDF):
81
+ #!/bin/sh
82
+ ocr-software "$DOCUMENT_WORKING_PATH" --pipeline NAME@VERSION --format pdf -o "$DOCUMENT_WORKING_PATH.ocr" && mv "$DOCUMENT_WORKING_PATH.ocr" "$DOCUMENT_WORKING_PATH"
83
+ with OCR_SOFTWARE_API_KEY and OCR_SOFTWARE_BASE_URL set in the environment of the Paperless container.
84
+
85
+ Tests (fake local service, no network, no key):
86
+ python -m venv .venv
87
+ .venv/bin/pip install pytest (Windows: .venv\Scripts\pip install pytest)
88
+ .venv/bin/python -m pytest -p no:cacheprovider . (Windows: .venv\Scripts\python -m pytest -p no:cacheprovider .)
@@ -0,0 +1,71 @@
1
+ ocr-software: Python client and command line for the ocr-software.com OCR API (MIT, standard library only, Python 3.9 or newer).
2
+
3
+ Install:
4
+ pipx install ocr-software command line only, in its own environment
5
+ pip install ocr-software client library, inside a venv
6
+ uvx ocr-software --version run once without installing (uv)
7
+ First call (list the pipelines, then read one file):
8
+ ocr-software --list-pipelines
9
+ ocr-software scan.pdf --pipeline scan-ctc@2 -o scan.txt
10
+
11
+ Settings (environment; the command line flag wins):
12
+ OCR_SOFTWARE_API_KEY API key (starts with ocr_). OCR_KEY is read too, it is the name used in the API docs.
13
+ OCR_SOFTWARE_BASE_URL default https://api.ocr-software.com ; own server e.g. http://127.0.0.1:8080 (also --base-url). OCR_BASE_URL is read too.
14
+ OCR_SOFTWARE_ALLOWED_DIR optional: read documents only from below this folder (symlinks and .. do not escape)
15
+ The key is never printed and never part of an error message. Without --pipeline the default is used: the fastest and cheapest available pipeline (no previews). It is named on stderr; --list-pipelines marks it. In Python: client.ocr(path, client.default_pipeline()).
16
+
17
+ Command line:
18
+ ocr-software --list-pipelines available pipelines, one per line: name@version, tab, what it delivers and costs; the default one on stderr (no key needed)
19
+ ocr-software scan.pdf --pipeline NAME@VERSION --dry-run page count and price in euros, nothing runs, nothing is billed
20
+ ocr-software scan.pdf text to stdout, with the default pipeline (named on stderr)
21
+ ocr-software scan.pdf --pipeline NAME@VERSION text to stdout
22
+ ocr-software scan.pdf -o scan.md --pipeline NAME@VERSION --format md
23
+ ocr-software scan.pdf -o scan.pdf.txt --pipeline NAME@VERSION
24
+ ocr-software scan.pdf -o searchable.pdf --pipeline NAME@VERSION --format pdf searchable PDF, -o is required
25
+ ocr-software scan.pdf --pipeline NAME@VERSION --format json > scan.json
26
+ ocr-software scan.pdf --pipeline NAME@VERSION --base-url http://10.0.0.5:8080 your own server
27
+ python -m ocr_software ... the same command without the script on PATH
28
+ Options: --format text (default) | md | json | hocr | pdf ; --dpi 150|200|300 (PDF render resolution) ;
29
+ --timeout SECONDS (wait for the job, default 3600) ; --job-budget MILLICENTS (cap; ignored where billing is off) ; --dry-run ; --version
30
+ Without -o the result goes to stdout as UTF-8 (not for pdf). With -o the file appears whole or not at all, an existing
31
+ file is replaced, and the input file itself is refused as output.
32
+
33
+ Exit codes:
34
+ 0 done, every page read
35
+ 1 error from the service or the network (message on stderr with the API error code, for example 401 unauthorized);
36
+ also an address that does not answer: a typo and an outage look the same, so a script may retry this
37
+ 2 wrong call: unreadable or unsupported input, output not writable, no key, malformed address (not http/https)
38
+ 3 the job is done but a page failed or did not run, or the job stopped early: the result is written anyway,
39
+ stderr says what is missing and names the job; treat the file as incomplete
40
+ 4 the job did not finish within --timeout; it keeps running, stderr names the job
41
+
42
+ Python:
43
+ from ocr_software import Client, PagesUnread
44
+ client = Client() # key and address from the environment
45
+ text = client.ocr("scan.pdf", "NAME@VERSION", "md") # bytes; raises PagesUnread (with .data) if a page was not read
46
+ pdf = client.ocr("scan.pdf", "NAME@VERSION", "pdf") # searchable PDF
47
+ job = client.submit("scan.pdf", "NAME@VERSION") # step by step: submit, wait, result
48
+ client.wait(job["job_id"]); client.result(job["job_id"], "json")
49
+ client.quote("scan.pdf", "NAME@VERSION") # dry run: page count and price, nothing runs
50
+ from ocr_software import price_sentence; price_sentence(q) # the same sentence as --dry-run, in euros
51
+ Price: the API counts money in millicents, 100,000 millicents = 1 EUR (a page of the cheapest pipeline costs 100 = EUR 0.001).
52
+ Today nothing is charged ("you pay EUR 0.00 today"); the list price is shown for information. Free use has limits:
53
+ https://ocr-software.com/legal/fair-usage/ ; for more volume write to info@ocr-software.com.
54
+ Word boxes (json): box = [x0, y0, x1, y1] in pixels of the page image, origin top left; width, height and dpi of the page say which
55
+ image. In PDF points: pixels * 72 / dpi. Per word, verified is true only where a second reader confirmed it (scan-vlm-ctc@2);
56
+ safe_ok is always false today. Judge by confidence (0 to 1). Every field: https://ocr-software.com/docs/reference/
57
+ Retries follow the API rules: 429, 500, 502, 503, 504 and network errors up to 5 times (waits 1, 2, 4, 8, 16 s or Retry-After). An
58
+ upload the service cuts off (it does that beyond 2 uploads at once per key, instead of answering 429) counts like 429. If no connection
59
+ can be made at all (wrong address, host down) it gives up after 2 retries and names the address: a few seconds, up to about 10 s on
60
+ Windows, where a refused port itself takes 2 s per try (connect timeout 10 s). A Retry-After over 60 s (locked_out) is not waited for. A submit carries an Idempotency-Key, so a retry never creates a second job.
61
+ Every document is checked locally first: PDF, PNG, JPEG or TIFF by its first bytes, at most 100 MiB.
62
+
63
+ Paperless-ngx, pre-consume script (sketch, not tried against a real Paperless; a non-PDF input keeps its file name but becomes a PDF):
64
+ #!/bin/sh
65
+ ocr-software "$DOCUMENT_WORKING_PATH" --pipeline NAME@VERSION --format pdf -o "$DOCUMENT_WORKING_PATH.ocr" && mv "$DOCUMENT_WORKING_PATH.ocr" "$DOCUMENT_WORKING_PATH"
66
+ with OCR_SOFTWARE_API_KEY and OCR_SOFTWARE_BASE_URL set in the environment of the Paperless container.
67
+
68
+ Tests (fake local service, no network, no key):
69
+ python -m venv .venv
70
+ .venv/bin/pip install pytest (Windows: .venv\Scripts\pip install pytest)
71
+ .venv/bin/python -m pytest -p no:cacheprovider . (Windows: .venv\Scripts\python -m pytest -p no:cacheprovider .)
@@ -0,0 +1,6 @@
1
+ """ocr-software.com: client and command line for the OCR API."""
2
+ from .advice import default_pipeline, price_sentence
3
+ from .client import Client, OcrError, PagesUnread, split_pages, unread_message
4
+
5
+ __version__ = "0.1.0"
6
+ __all__ = ["Client", "OcrError", "PagesUnread", "split_pages", "unread_message", "default_pipeline", "price_sentence", "__version__"]
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from .cli import main
4
+
5
+ sys.exit(main())
@@ -0,0 +1,86 @@
1
+ """Words for a human: price in euros, the default pipeline, a short pipeline line.
2
+
3
+ One copy for the command line and the MCP server, so both say the same. Nothing here calls the network.
4
+ Unit: 100,000 millicents = 1 EUR; factor 1 = 100 millicents = EUR 0.001 per page.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ # The price rate lives here and nowhere else: showing pages of the base pipeline instead of euros changes eur() and price_sentence().
9
+ MILLICENTS_PER_EUR = 100000
10
+ MILLICENTS_PER_FACTOR = 100 # factor 1 = the base price per page
11
+ FAIR_USE = "Free use has limits (https://ocr-software.com/legal/fair-usage/); for more volume write to info@ocr-software.com."
12
+ SWITCH_HINTS = {
13
+ "hard_pages": "for hard pages (poor scans, doubtful words) use %s",
14
+ "turned_pages": "for turned pages use %s",
15
+ "no_tables": "without tables %s is cheaper",
16
+ "clean_bulk": "for clean bulk text %s is cheaper",
17
+ }
18
+
19
+
20
+ def eur(millicents):
21
+ """1 EUR = 100000 millicents; at least two decimals, as many as needed up to four."""
22
+ text = ("%.4f" % (millicents / MILLICENTS_PER_EUR)).rstrip("0")
23
+ return "EUR " + text + "0" * (2 - len(text.partition(".")[2]))
24
+
25
+
26
+ def price_sentence(q, show_list_price=True):
27
+ """What the human reads before saying yes, from a dry run. Nothing is charged today (billing off or free pages): the price
28
+ is still shown, as information, with the fair-use link and the contact."""
29
+ pages, name = q["pages"], q["pipeline"]
30
+ price, charge, per_page = q.get("price_millicents") or 0, q.get("max_charge_millicents") or 0, q.get("price_per_page_millicents") or 0
31
+ start = "%d page%s with %s" % (pages, "" if pages == 1 else "s", name)
32
+ if not price:
33
+ text = "%s: nothing is charged (this service has no prices set)." % start
34
+ elif not charge:
35
+ text = "%s: you pay %s today." % (start, eur(0))
36
+ if show_list_price:
37
+ text += " List price %s in total (%s per page), shown for information only. %s" % (eur(price), eur(per_page), FAIR_USE)
38
+ else:
39
+ text = "%s: up to %s is charged (%s per page)." % (start, eur(charge), eur(per_page))
40
+ covered = q.get("pages_covered", pages)
41
+ if covered < pages:
42
+ text += " Only %d of %d pages would run." % (covered, pages)
43
+ return text
44
+
45
+
46
+ def cost(p):
47
+ """Price per page in millicents, comparable between pipelines: the catalog price, else factor * 100 (the catalog hides the
48
+ price while billing is off, the factor stays)."""
49
+ price = p.get("price_per_page_millicents")
50
+ if isinstance(price, (int, float)) and price > 0:
51
+ return price
52
+ factor = p.get("factor")
53
+ return factor * MILLICENTS_PER_FACTOR if isinstance(factor, (int, float)) else None
54
+
55
+
56
+ def preview(p):
57
+ """A preview (preview: true, or version @0) has no long-term promise: never a default."""
58
+ return bool(p.get("preview")) or str(p.get("pipeline", "")).endswith("@0")
59
+
60
+
61
+ def usable(p):
62
+ return p.get("available", True) and p.get("orderable", True) is not False and not preview(p)
63
+
64
+
65
+ def default_pipeline(pipelines):
66
+ """name@version of the pipeline used when the caller names none: the fastest and cheapest usable one (rule in ziel.md).
67
+ Not ready and preview pipelines are out. Order: price, then not autoregressive (faster), then the name, so a tie never
68
+ depends on the order of the catalog. None if nothing is usable. The caller must say which one was chosen."""
69
+ best = min(((cost(p), bool(p.get("autoregressive")), p.get("pipeline", "")) for p in pipelines if usable(p) and cost(p) is not None),
70
+ default=None)
71
+ return best[2] if best else None
72
+
73
+
74
+ def summary(p, default=None):
75
+ """One short line for a pipeline (without its name): what it delivers, what it costs, when to choose another one."""
76
+ feats = p.get("features") or []
77
+ parts = ["%s boxes" % p["level"] if p.get("level") else "", "tables" if "table_cells" in feats else "",
78
+ "layout" if "layout_classes" in feats else "", "slower" if p.get("autoregressive") else ""]
79
+ line = ", ".join(x for x in parts if x) or p.get("description", "")[:80]
80
+ price = cost(p)
81
+ if price is not None:
82
+ line += "; price %s per page" % eur(price)
83
+ if default and p.get("pipeline") == default:
84
+ line += "; the default (fastest and cheapest)"
85
+ hints = [SWITCH_HINTS[s["when"]] % s["pipeline"] for s in p.get("switch") or [] if s.get("when") in SWITCH_HINTS and s.get("pipeline")]
86
+ return line + ("; " + "; ".join(hints) if hints else "")
@@ -0,0 +1,122 @@
1
+ """Command line: ocr-software IN [-o OUT] --pipeline NAME@VERSION [--format md|text|json|hocr|pdf].
2
+
3
+ Exit codes: 0 done, every page read | 1 service or network error | 2 wrong call, unreadable input or output, no key
4
+ | 3 job done but a page failed or did not run (the result is still written, the message says what is missing)
5
+ | 4 the job did not finish within --timeout (it keeps running; the message names the job).
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import argparse
10
+ import os
11
+ import sys
12
+
13
+ from . import __version__
14
+ from .advice import default_pipeline, price_sentence, summary
15
+ from .client import DEFAULT_WAIT_TIMEOUT_S, FORMATS, Client, OcrError, PagesUnread
16
+
17
+ EXIT_ERROR, EXIT_USAGE, EXIT_UNREAD, EXIT_TIMEOUT = 1, 2, 3, 4
18
+ USAGE_CODES = ("bad_input", "no_key")
19
+
20
+
21
+ def _parser():
22
+ p = argparse.ArgumentParser(prog="ocr-software", description="Read a PDF, PNG, JPEG or TIFF with the ocr-software.com API.")
23
+ p.add_argument("input", nargs="?", help="the document")
24
+ p.add_argument("-o", "--output", help="output file; without it the result goes to stdout (not for pdf)")
25
+ p.add_argument("--pipeline", help="name@version (default: the fastest and cheapest available one, named on stderr); --list-pipelines shows the choices")
26
+ p.add_argument("--format", default="text", choices=FORMATS, help="default text")
27
+ p.add_argument("--dpi", type=int, choices=(150, 200, 300), help="render resolution of a PDF (default 150)")
28
+ p.add_argument("--job-budget", type=int, help="cap in millicents (100,000 = 1 EUR; one page of the cheapest pipeline = 100). Where billing is off it is ignored")
29
+ p.add_argument("--dry-run", action="store_true", help="show the page count and the price in euros, run nothing, cost nothing")
30
+ p.add_argument("--timeout", type=int, default=DEFAULT_WAIT_TIMEOUT_S, help="seconds to wait for the job (default %(default)s)")
31
+ p.add_argument("--base-url", help="address of the service (default OCR_SOFTWARE_BASE_URL, else the hosted API)")
32
+ p.add_argument("--list-pipelines", action="store_true", help="print the available pipelines (name, tab, what it delivers and costs) and the default one, then exit")
33
+ p.add_argument("--version", action="version", version="ocr-software " + __version__)
34
+ return p
35
+
36
+
37
+ def _write(path, data):
38
+ """Atomic: a half written output file never exists (a pre-consume hook may pick it up at once)."""
39
+ tmp = "%s.%d.part" % (path, os.getpid())
40
+ try:
41
+ with open(tmp, "xb") as f: # normal file mode, unlike mkstemp
42
+ f.write(data)
43
+ os.replace(tmp, path)
44
+ except BaseException:
45
+ if os.path.exists(tmp):
46
+ os.unlink(tmp)
47
+ raise
48
+
49
+
50
+ def _same_file(a, b):
51
+ try:
52
+ return os.path.exists(b) and os.path.samefile(a, b)
53
+ except OSError: # input missing: the client reports it with the right code
54
+ return False
55
+
56
+
57
+ def _emit(data, output):
58
+ if output:
59
+ _write(output, data)
60
+ else:
61
+ sys.stdout.buffer.write(data)
62
+ sys.stdout.buffer.flush()
63
+
64
+
65
+ def main(argv=None):
66
+ for stream in (sys.stderr, sys.stdout): # a Windows console must not fail on a file name
67
+ if hasattr(stream, "reconfigure"):
68
+ stream.reconfigure(errors="backslashreplace")
69
+ parser = _parser()
70
+ args = parser.parse_args(argv)
71
+ try:
72
+ client = Client(base_url=args.base_url)
73
+ if args.list_pipelines:
74
+ pipelines = client.catalog().get("pipelines", [])
75
+ default = default_pipeline(pipelines)
76
+ for p in pipelines:
77
+ if p.get("available"):
78
+ print("%s %s" % (p.get("pipeline"), summary(p, default)))
79
+ if default:
80
+ print("ocr-software: without --pipeline the default is %s (the fastest and cheapest available one)" % default, file=sys.stderr)
81
+ return 0
82
+ if not args.input:
83
+ parser.error("IN is required (--list-pipelines shows the pipelines)")
84
+ if args.format == "pdf" and not args.output:
85
+ parser.error("--format pdf needs -o OUT (a PDF is not written to the terminal)")
86
+ if args.output and _same_file(args.input, args.output):
87
+ parser.error("OUT is the input file; it would be overwritten")
88
+ if args.output and not os.path.isdir(os.path.dirname(os.path.abspath(args.output))):
89
+ parser.error("folder of %s does not exist; nothing was sent" % args.output) # before the job, not after it
90
+ pipeline = args.pipeline
91
+ if not pipeline: # the default is named, never silent (ziel.md: fastest and cheapest available; stdout stays the result)
92
+ pipeline = client.default_pipeline()
93
+ print("ocr-software: no --pipeline given, using %s (the fastest and cheapest available one; --list-pipelines shows the others)" % pipeline,
94
+ file=sys.stderr)
95
+ if args.dry_run:
96
+ q = client.quote(args.input, pipeline, args.dpi)
97
+ q.setdefault("pipeline", pipeline)
98
+ print(price_sentence(q))
99
+ return 0
100
+ data = client.ocr(args.input, pipeline, args.format, timeout=args.timeout, dpi=args.dpi, job_budget=args.job_budget)
101
+ _emit(data, args.output)
102
+ return 0
103
+ except PagesUnread as e:
104
+ try:
105
+ _emit(e.data, args.output)
106
+ except OSError as w:
107
+ print("ocr-software: cannot write output: %s" % w, file=sys.stderr)
108
+ print("ocr-software: %s; the result is incomplete" % e, file=sys.stderr)
109
+ return EXIT_UNREAD
110
+ except OcrError as e:
111
+ print("ocr-software: %s" % e, file=sys.stderr)
112
+ return EXIT_USAGE if e.code in USAGE_CODES else EXIT_TIMEOUT if e.code == "timeout" else EXIT_ERROR
113
+ except OSError as e:
114
+ print("ocr-software: cannot write output: %s" % e, file=sys.stderr)
115
+ return EXIT_USAGE
116
+ except KeyboardInterrupt: # Ctrl+C: no traceback; a job already submitted keeps running and is not billed twice on retry
117
+ print("ocr-software: interrupted; a job already submitted keeps running on the service", file=sys.stderr)
118
+ return 130
119
+
120
+
121
+ if __name__ == "__main__":
122
+ sys.exit(main())