fix(classifier): make image coverage diagnostic-only, not an OCR routing trigger
The tier-0 classifier escalated any page with raster-image coverage >=0.80 to the OCR tier regardless of its text layer. On OHR-Bench this drove ~45% of all OCR escalations: clean born-digital pages dominated by a figure, and scanned pages that already carry a usable OCR text layer -- re-OCR adds nothing for either, but each one was routed to the paid tier-3 OCR. Route on the text signals only (near-empty or junk-quality layer). Image coverage is still computed and still raises the `image_heavy` diagnostic flag, but no longer routes. True scans with no/garbage text continue to escalate via the empty-text and quality signals, so genuine OCR needs are unaffected. Trade-off: image-only content on an otherwise-clean page (handwriting, stamps, text inside figures) is no longer force-routed to OCR. This was previously intentional; the escalation cost outweighed the benefit for RAG indexing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
c79389a224
commit
906a5805ef
@@ -4,17 +4,24 @@ Decides which extraction tier a PDF should escalate to, from cheap signals:
|
|||||||
* text_quality -- is the text layer usable, or mashed/space-less junk? (the
|
* text_quality -- is the text layer usable, or mashed/space-less junk? (the
|
||||||
"Student 147" lesson: a text layer can exist yet be unusable, e.g.
|
"Student 147" lesson: a text layer can exist yet be unusable, e.g.
|
||||||
"01322234567mobile")
|
"01322234567mobile")
|
||||||
* image_coverage -- a page that is mostly a raster image is a scan/photo whose
|
|
||||||
content isn't fully in any text layer.
|
|
||||||
* no text layer -- the strongest OCR signal available from text alone.
|
* no text layer -- the strongest OCR signal available from text alone.
|
||||||
|
|
||||||
|
Routing is on the TEXT signals only: a page escalates to OCR when its text is
|
||||||
|
near-empty or junk-quality. ``image_coverage`` is computed but is a DIAGNOSTIC
|
||||||
|
signal (the ``image_heavy`` flag), NOT a routing trigger: a mostly-raster page
|
||||||
|
whose embedded text is already usable (a scan carrying a clean OCR layer, or a
|
||||||
|
digital page dominated by a figure) gains nothing from re-OCR, so escalating it
|
||||||
|
to the paid OCR tier was wasteful -- on OHR-Bench the coverage trigger drove
|
||||||
|
~45% of escalations. The trade-off: image-only content on an otherwise-clean
|
||||||
|
page (handwriting, stamps, figure text) is no longer force-routed to OCR.
|
||||||
|
|
||||||
Two entry points:
|
Two entry points:
|
||||||
* ``classify_from_text(text, page_boundaries, ...)`` -- the HOT PATH. Routes on
|
* ``classify_from_text(text, page_boundaries, ...)`` -- the HOT PATH. Routes on
|
||||||
text-quality + near-empty pages derived from the tier-1 extraction (~no
|
text-quality + near-empty pages derived from the tier-1 extraction (~no
|
||||||
cost). When OCR + scan-detection are enabled the registry also passes
|
cost). When OCR + scan-detection are enabled the registry also passes
|
||||||
per-page ``image_coverage`` (from ``image_coverage_per_page``) so scans are
|
per-page ``image_coverage`` (from ``image_coverage_per_page``) for the
|
||||||
caught too; that image pass is the only added cost and only OCR-opted-in
|
``image_heavy`` diagnostic flag; that image pass is the only added cost and
|
||||||
tenants pay it. Thresholds come from per-tenant settings.
|
only OCR-opted-in tenants pay it. Thresholds come from per-tenant settings.
|
||||||
* ``classify_pdf(content)`` -- a standalone/diagnostic pass that re-opens the
|
* ``classify_pdf(content)`` -- a standalone/diagnostic pass that re-opens the
|
||||||
PDF and does image-coverage analysis inline. Off the hot path.
|
PDF and does image-coverage analysis inline. Off the hot path.
|
||||||
|
|
||||||
@@ -152,15 +159,15 @@ def classify_pdf(content: bytes) -> DocClassification:
|
|||||||
text = page.get_text("text")
|
text = page.get_text("text")
|
||||||
quality = _text_quality(text)
|
quality = _text_quality(text)
|
||||||
coverage = _page_image_coverage(page)
|
coverage = _page_image_coverage(page)
|
||||||
# OCR-worthy on the same three signals as classify_from_text (kept in
|
# OCR-worthy on TEXT signals only (kept in sync with
|
||||||
# sync so an operator reproducing routing offline gets the pipeline's
|
# classify_from_text): a junk/low-quality text layer (the
|
||||||
# answer): a mostly-raster scan, a junk/low-quality text layer (the
|
# word-merging case) or an effectively empty one. Image coverage is
|
||||||
# word-merging case), or an effectively empty text layer.
|
# deliberately NOT a routing trigger -- a mostly-raster page whose
|
||||||
needs_ocr = (
|
# embedded text is already usable (a scan with a clean OCR layer, or
|
||||||
coverage >= IMAGE_COVERAGE_SCANNED
|
# a digital page dominated by a figure) gains nothing from re-OCR, so
|
||||||
or quality < MIN_TEXT_QUALITY
|
# routing it to the paid OCR tier was wasteful. High coverage still
|
||||||
or len(text.strip()) < MIN_PAGE_CHARS
|
# raises the diagnostic image_heavy flag below.
|
||||||
)
|
needs_ocr = quality < MIN_TEXT_QUALITY or len(text.strip()) < MIN_PAGE_CHARS
|
||||||
pages.append(
|
pages.append(
|
||||||
PageSignals(n, len(text), round(coverage, 3), quality, needs_ocr)
|
PageSignals(n, len(text), round(coverage, 3), quality, needs_ocr)
|
||||||
)
|
)
|
||||||
@@ -206,16 +213,13 @@ def classify_pdf(content: bytes) -> DocClassification:
|
|||||||
def image_coverage_per_page(content: bytes) -> list[float]:
|
def image_coverage_per_page(content: bytes) -> list[float]:
|
||||||
"""Raster-image coverage in ``[0, 1]`` for every page (document order).
|
"""Raster-image coverage in ``[0, 1]`` for every page (document order).
|
||||||
|
|
||||||
Lets the hot path flag scanned pages whose embedded text layer is junk but
|
Feeds the ``image_heavy`` DIAGNOSTIC flag only (coverage no longer routes --
|
||||||
statistically clean-looking. Re-opens the PDF, so the registry calls it only
|
see module docstring). Re-opens the PDF, so the registry calls it only when
|
||||||
when OCR + scan detection are enabled (the cost is borne by OCR-opted-in
|
OCR + scan detection are enabled (the cost is borne by OCR-opted-in tenants).
|
||||||
tenants). Returned list is aligned by index with the leading page boundaries.
|
Returned list is aligned by index with the leading page boundaries.
|
||||||
|
|
||||||
Bounded to the first ``MAX_SAMPLED_PAGES`` pages -- the image pass is the
|
Bounded to the first ``MAX_SAMPLED_PAGES`` pages -- the image pass is the
|
||||||
costly part, so a 200-page scan isn't fully rasterised on the hot path. Pages
|
costly part, so a 200-page scan isn't fully rasterised on the hot path.
|
||||||
beyond the cap fall back to the text-quality signal in ``classify_from_text``
|
|
||||||
(a scanned tail has junk text too), and ``page_fraction`` still gates over
|
|
||||||
every page.
|
|
||||||
"""
|
"""
|
||||||
import pymupdf # noqa: PLC0415 -- keep the heavy import lazy
|
import pymupdf # noqa: PLC0415 -- keep the heavy import lazy
|
||||||
|
|
||||||
@@ -238,18 +242,18 @@ def classify_from_text(
|
|||||||
"""Classify from text already extracted by tier-1 -- no PDF re-open by default.
|
"""Classify from text already extracted by tier-1 -- no PDF re-open by default.
|
||||||
|
|
||||||
The hot-path classifier. A page is OCR-worthy when its text is near-empty
|
The hot-path classifier. A page is OCR-worthy when its text is near-empty
|
||||||
(``< min_page_chars``), its text-quality is junk (``< min_text_quality`` --
|
(``< min_page_chars``) or its text-quality is junk (``< min_text_quality`` --
|
||||||
the word-merging signal), OR (when ``image_coverage`` is supplied, i.e. OCR +
|
the word-merging signal). The doc recommends ``ocr`` once
|
||||||
scan-detection are on) the page is mostly a raster image. The doc recommends
|
``ocr_frac >= page_fraction``. Thresholds are passed in by the registry from
|
||||||
``ocr`` once ``ocr_frac >= page_fraction``. Thresholds are passed in by the
|
per-tenant settings. ``image_coverage`` (when supplied) only feeds the
|
||||||
registry from per-tenant settings.
|
``image_heavy`` diagnostic flag -- it does NOT route (see module docstring).
|
||||||
|
|
||||||
``page_boundaries`` are ``{page, start_offset, end_offset}`` indexing into
|
``page_boundaries`` are ``{page, start_offset, end_offset}`` indexing into
|
||||||
``full_text``; ``image_coverage[i]`` (if given) aligns with the i-th boundary.
|
``full_text``; ``image_coverage[i]`` (if given) aligns with the i-th boundary.
|
||||||
|
|
||||||
Note: the ``image_heavy`` flag (and the image-coverage trigger) are only set
|
Note: the ``image_heavy`` flag is only set when ``image_coverage`` is
|
||||||
when ``image_coverage`` is supplied, so for tenants with scan detection off
|
supplied, so for tenants with scan detection off that flag is always zero.
|
||||||
that flag is always zero -- the text-quality/empty signals still route.
|
Routing is unaffected either way -- it is on the text signals alone.
|
||||||
"""
|
"""
|
||||||
# image_coverage is expected to be one entry per page, capped at
|
# image_coverage is expected to be one entry per page, capped at
|
||||||
# MAX_SAMPLED_PAGES (see image_coverage_per_page). Any other length means the
|
# MAX_SAMPLED_PAGES (see image_coverage_per_page). Any other length means the
|
||||||
@@ -278,11 +282,14 @@ def classify_from_text(
|
|||||||
if image_coverage is not None and idx < len(image_coverage)
|
if image_coverage is not None and idx < len(image_coverage)
|
||||||
else 0.0
|
else 0.0
|
||||||
)
|
)
|
||||||
needs_ocr = (
|
# Routing is on TEXT signals only: a near-empty layer or a junk/
|
||||||
len(seg.strip()) < min_page_chars
|
# space-mangled one. Image coverage (``cov``) is intentionally not a
|
||||||
or quality < min_text_quality
|
# routing trigger -- a mostly-raster page with an already-usable text
|
||||||
or cov >= IMAGE_COVERAGE_SCANNED
|
# layer does not benefit from re-OCR, so escalating it to the paid OCR
|
||||||
)
|
# tier was wasteful (on OHR-Bench this drove ~45% of escalations: clean
|
||||||
|
# digital figure-pages and scans that already carry a good OCR layer).
|
||||||
|
# ``cov`` still feeds the diagnostic ``image_heavy`` flag below.
|
||||||
|
needs_ocr = len(seg.strip()) < min_page_chars or quality < min_text_quality
|
||||||
pages.append(
|
pages.append(
|
||||||
PageSignals(b["page"], len(seg), round(cov, 3), quality, needs_ocr)
|
PageSignals(b["page"], len(seg), round(cov, 3), quality, needs_ocr)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,8 +3,10 @@
|
|||||||
Pins the routing decisions and the text-quality heuristic that drive which
|
Pins the routing decisions and the text-quality heuristic that drive which
|
||||||
extraction tier a PDF starts in:
|
extraction tier a PDF starts in:
|
||||||
* a clean born-digital PDF (text, no full-page images) -> ``fast`` (tier 1);
|
* a clean born-digital PDF (text, no full-page images) -> ``fast`` (tier 1);
|
||||||
* a full-page-image scan -> ``ocr`` (tier 3), since handwriting/stamps aren't
|
* a full-page-image scan with no usable text layer -> ``ocr`` (tier 3);
|
||||||
in any text layer;
|
* routing is on TEXT signals only -- image coverage feeds the ``image_heavy``
|
||||||
|
diagnostic flag but does not route (a mostly-raster page with a clean text
|
||||||
|
layer stays ``fast``);
|
||||||
* the text-quality score distinguishes clean prose from mashed/space-less junk.
|
* the text-quality score distinguishes clean prose from mashed/space-less junk.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -236,12 +238,16 @@ def test_quality_floor_override_disables_trigger():
|
|||||||
assert c.recommended_tier == "fast"
|
assert c.recommended_tier == "fast"
|
||||||
|
|
||||||
|
|
||||||
def test_scan_signal_routes_ocr_even_with_clean_text():
|
def test_image_heavy_clean_text_stays_fast():
|
||||||
# clean text but every page is a raster scan -> OCR (the Student-147 case)
|
# Image coverage is diagnostic, not routing: fully-raster pages whose text
|
||||||
|
# layer is already clean (a scan carrying a good OCR layer, or a figure-heavy
|
||||||
|
# digital page) carry the image_heavy flag but stay on the fast tier --
|
||||||
|
# re-OCR adds nothing. This was the ~45% over-escalation on OHR-Bench.
|
||||||
full, bounds = _two_page(_CLEAN, _CLEAN)
|
full, bounds = _two_page(_CLEAN, _CLEAN)
|
||||||
c = clf.classify_from_text(full, bounds, image_coverage=[1.0, 1.0])
|
c = clf.classify_from_text(full, bounds, image_coverage=[1.0, 1.0])
|
||||||
assert c.recommended_tier == "ocr"
|
assert c.recommended_tier == "fast"
|
||||||
assert "image_heavy" in c.flags
|
assert "image_heavy" in c.flags
|
||||||
|
assert all(p.needs_ocr is False for p in c.pages)
|
||||||
|
|
||||||
|
|
||||||
def test_scan_signal_ignored_when_coverage_low():
|
def test_scan_signal_ignored_when_coverage_low():
|
||||||
@@ -270,9 +276,10 @@ def test_image_coverage_per_page():
|
|||||||
assert len(digital) == 2 and all(c < 0.1 for c in digital)
|
assert len(digital) == 2 and all(c < 0.1 for c in digital)
|
||||||
|
|
||||||
|
|
||||||
def test_scan_coverage_shorter_than_pages_falls_back_to_text():
|
def test_scan_coverage_shorter_than_pages_aligns_without_crash():
|
||||||
# image_coverage shorter than the boundaries (the MAX_SAMPLED_PAGES cap):
|
# image_coverage shorter than the boundaries (the MAX_SAMPLED_PAGES cap):
|
||||||
# page 0 is flagged scanned; later pages fall back to the text-quality signal.
|
# the single entry aligns to page 0; later pages get no coverage entry. With
|
||||||
|
# clean text everywhere, routing is on text only, so nothing escalates.
|
||||||
n = len(_CLEAN)
|
n = len(_CLEAN)
|
||||||
full = _CLEAN * 3
|
full = _CLEAN * 3
|
||||||
bounds = [
|
bounds = [
|
||||||
@@ -281,6 +288,8 @@ def test_scan_coverage_shorter_than_pages_falls_back_to_text():
|
|||||||
{"page": 3, "start_offset": 2 * n, "end_offset": 3 * n},
|
{"page": 3, "start_offset": 2 * n, "end_offset": 3 * n},
|
||||||
]
|
]
|
||||||
c = clf.classify_from_text(full, bounds, image_coverage=[1.0])
|
c = clf.classify_from_text(full, bounds, image_coverage=[1.0])
|
||||||
assert c.pages[0].needs_ocr is True # scanned (coverage)
|
assert c.pages[0].image_coverage == pytest.approx(1.0) # entry aligned
|
||||||
assert c.pages[1].needs_ocr is False # clean text, no coverage entry
|
assert c.pages[1].image_coverage == pytest.approx(0.0) # no entry -> 0
|
||||||
assert c.recommended_tier == "fast" # only 1/3 pages bad
|
assert all(p.needs_ocr is False for p in c.pages) # coverage no longer routes
|
||||||
|
assert "image_heavy" in c.flags # but page 0 still flags image_heavy
|
||||||
|
assert c.recommended_tier == "fast"
|
||||||
|
|||||||
Reference in New Issue
Block a user