Open-source inference server and production cluster for all the models your agent needs.
Python bgecolbertdata-pipelinedeep-learningembeddingsinferenceinference-serverinformation-retrievalllmmlmlopsnatural-language-processingnlppythonrerankingretrievalretrieval-augmented-generationsemantic-searchspladevector-search
from __future__ import annotations
import base64
import hashlib
import io
from collections.abc import Callable
from pathlib import Path
import pytest
pytest.importorskip("docling")
from sie_server.adapters.docling.adapter import DoclingAdapter
from sie_server.types.inputs import Item
@pytest.fixture(scope="module")
def loaded_adapter() -> DoclingAdapter:
adapter = DoclingAdapter()
adapter.load("cpu")
return adapter
def _make_pdf_bytes() -> bytes:
reportlab = pytest.importorskip("reportlab")
from reportlab.pdfgen import canvas # ty: ignore[unresolved-import]
_ = reportlab
buf = io.BytesIO()
pdf = canvas.Canvas(buf)
pdf.drawString(100, 750, "Smoke test heading")
pdf.drawString(100, 720, "Hello from reportlab.")
pdf.save()
return buf.getvalue()
def _make_docx_bytes() -> bytes:
docx = pytest.importorskip("docx")
document = docx.Document()
document.add_heading("Smoke test heading", level=1)
document.add_paragraph("Hello from python-docx.")
buf = io.BytesIO()
document.save(buf)
return buf.getvalue()
def _make_html_bytes() -> bytes:
return b"<html><body><h1>Smoke test heading</h1><p>Hello from HTML.</p></body></html>"
@pytest.mark.parametrize(
("format_hint", "maker"),
[
pytest.param("pdf", _make_pdf_bytes, marks=pytest.mark.model),
("docx", _make_docx_bytes),
("html", _make_html_bytes),
],
)
def test_extract_real_document(loaded_adapter: DoclingAdapter, format_hint: str, maker: Callable[[], bytes]) -> None:
data = maker()
out = loaded_adapter.extract([Item(document={"data": data, "format": format_hint})])
assert out.batch_size == 1
assert out.data is not None
item = out.data[0]
assert "error" not in item, f"adapter reported error: {item.get('error')}"
assert "Smoke test heading" in item["text"] or "Smoke test heading" in item["markdown"]
assert "document" in item
def _make_glyph_name_pdf_bytes(
text: bytes = b"Net sales $209,586, up 5%", differences: bytes = b"36 /dollarsign /percentsign"
) -> bytes:
"""One page of ``text`` whose font renames character codes per the encoding ``differences``.
The defaults map ``$`` and ``%`` to the non-standard glyph names ``/dollarsign`` and ``/percentsign``.
"""
content = b"BT /F1 12 Tf 72 700 Td (%s) Tj ET" % text
objects = [
b"<< /Type /Catalog /Pages 2 0 R >>",
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> >> "
b"/Contents 5 0 R >>",
b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding << /Type /Encoding "
b"/BaseEncoding /WinAnsiEncoding /Differences [%s] >> >>" % differences,
b"<< /Length %d >>\nstream\n%s\nendstream" % (len(content), content),
]
out = bytearray(b"%PDF-1.4\n")
offsets = []
for number, body in enumerate(objects, start=1):
offsets.append(len(out))
out += b"%d 0 obj\n%s\nendobj\n" % (number, body)
xref = len(out)
out += b"xref\n0 %d\n0000000000 65535 f \n" % (len(objects) + 1)
out += b"".join(b"%010d 00000 n \n" % offset for offset in offsets)
out += b"trailer\n<< /Size %d /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n" % (len(objects) + 1, xref)
return bytes(out)
@pytest.mark.model
def test_extract_pdf_restores_unmapped_glyph_names(loaded_adapter: DoclingAdapter) -> None:
out = loaded_adapter.extract([Item(document={"data": _make_glyph_name_pdf_bytes(), "format": "pdf"})])
assert out.errors is None
assert out.data is not None
for output_format in ("text", "markdown"):
rendered = out.data[0][output_format]
assert "Net sales $209,586, up 5%" in rendered
assert "sign" not in rendered
@pytest.mark.model
def test_extract_pdf_restores_unicode_glyph_names(loaded_adapter: DoclingAdapter) -> None:
pdf = _make_glyph_name_pdf_bytes(text=b"let x2 = y", differences=b"120 /u1D465 /u1D466")
out = loaded_adapter.extract([Item(document={"data": pdf, "format": "pdf"})])
assert out.errors is None
assert out.data is not None
for output_format in ("text", "markdown"):
rendered = out.data[0][output_format]
assert "let \U0001d4652 = \U0001d466" in rendered
assert "/u" not in rendered
def test_extract_html_keeps_literal_glyph_name_text(loaded_adapter: DoclingAdapter) -> None:
html = b"<html><body><p>Static files live under /dollarsign-assets.</p></body></html>"
out = loaded_adapter.extract([Item(document={"data": html, "format": "html"})])
assert out.errors is None
assert out.data is not None
for output_format in ("text", "markdown"):
assert "Static files live under /dollarsign-assets." in out.data[0][output_format]
_PROSE = (
"Alice was beginning to get very tired of sitting by her sister "
"on the bank and of having nothing to do once or twice she had "
"peeped into the book her sister was reading"
)
# Byte-identical to the catalog acceptance fixture. This compact,
# wide line caught the English-only PP-OCRv3 detector dropping every word after
# "SIE" while the larger prose fixture below still passed.
_COMPACT_OCR_PNG_B64 = (
"iVBORw0KGgoAAAANSUhEUgAAAwAAAAB4CAAAAABgc8pjAAAFgElEQVR4nO3dXailVRkH8N+YH6QOzYUlRnlpTGmaH8lMil8I"
"KaIEWUqCYxiJmoqgEyFZGFkqipAko4zmlUWhRhEZhkIieBGj4gfqRUiFFFIQhfmVF4uFa3jPu9njGXSm5/+7OAxrr73ed+0z"
"//PCw1prr/mfiLr2eL9vIOL9lABEaQlAlJYARGkJQJSWAERpCUCUlgBEaQlAlJYARGkJQJSWAERpCUCUlgBEaQlAlJYARGkJ"
"QJSWAERpCUCUlgBEaQlAlJYARGkJQJSWAERpCUCUlgBEaQlAlJYARGkJQJS259wLfwTfBK8PXbeCj4N14J/Du/YFn52Mdia4"
"ctJ+B/gxWAv2B7cPVxndCS4BL4EDwX3g1qHnH8BxQ8ul4IsLx2nWYft5Td0FfgT2Bq+By8D5Q88twxX3HMZv7R+bGX/8JNsJ"
"3v8Ct4ATJn1G00978XzHcdYMc/ka2ASeAV8GT2D7v51Hg7vBoStNZ5eVJ0CUlgBEaQlAlJYARGkJQJS2Zu47wo4Av0KvVPwC"
"/Az8FCtVS6Ytc34Hrge/Bh8EvwE3gYcm7zoLHAI+CS6YGX/xnSweZ/F7fwuuQ/98xv5ngO+gV29uBr8Ee4Efgt8Po01N7+Ep"
"8BXw5BL3OdrR+f4bvZp0ETgbnDuMdg767+4e9P8Vu5c8A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GkJQJSWAERpCUCUlgBEaQlAlJYARGkJQJSWAERpCUCUlgBEaQlAlJYA"
"RGkJQJSWAERpCUCUlgBEaQlAlJYARGkJQJSWAERpCUCUlgBEaQlAlJYARGkJQJSWAERpCUCUlgBEaQlAlJYARGkJQJSWAERp"
"CUCU9jZ9MtgcKlsBIwAAAABJRU5ErkJggg=="
)
_COMPACT_OCR_PNG = base64.b64decode(_COMPACT_OCR_PNG_B64, validate=True)
def _make_prose_image_bytes() -> bytes:
pil = pytest.importorskip("PIL")
from PIL import Image, ImageDraw, ImageFont # ty: ignore[unresolved-import]
_ = pil
image = Image.new("RGB", (1400, 400), "white")
draw = ImageDraw.Draw(image)
font = ImageFont.load_default(size=40)
words = _PROSE.split()
for row in range(0, len(words), 8):
draw.text((40, 40 + (row // 8) * 70), " ".join(words[row : row + 8]), fill="black", font=font)
buf = io.BytesIO()
image.save(buf, format="PNG")
return buf.getvalue()
@pytest.fixture(scope="module")
def artifact_backed_adapter() -> DoclingAdapter:
yaml = pytest.importorskip("yaml")
model_yaml = Path(__file__).resolve().parents[2] / "models" / "docling.yaml"
pinned = yaml.safe_load(model_yaml.read_text())
adapter = DoclingAdapter(
model_name_or_path=pinned["hf_id"],
revision=pinned["hf_revision"],
)
adapter.load("cpu")
return adapter
@pytest.mark.integration
def test_served_ocr_recognizes_compact_catalog_fixture(artifact_backed_adapter: DoclingAdapter) -> None:
assert hashlib.sha256(_COMPACT_OCR_PNG).hexdigest() == (
"a9c82ded9169cdb69bbf3a2096e89a662cf93cb03dfd2cc01acf44acfc42cb9a"
)
out = artifact_backed_adapter.extract(
[Item(images=[{"data": _COMPACT_OCR_PNG, "format": "png"}])],
options={"ocr": True},
)
assert out.errors is None
assert out.data is not None
item = out.data[0]
assert "error" not in item, f"adapter reported error: {item.get('error')}"
words = {
"".join(character for character in word if character.isalnum()) for word in item["markdown"].casefold().split()
}
assert {"sie", "catalog", "ready"} <= words, item["markdown"]
assert out.pages == [1]
@pytest.mark.integration
def test_served_ocr_output_keeps_word_boundaries(artifact_backed_adapter: DoclingAdapter) -> None:
"""End-to-end sanity of the artifact-backed served OCR path.
This exercises the real pinned artifact revision end to end: it proves the
revision resolves a usable RapidOCR set on the served path and returns
segmented text, which a bare adapter on docling's own cache cannot show.
SCOPE — measured, not assumed. This floor does NOT discriminate the
recogniser language. Mutating the adapter to ``lang=["ch"]`` provably
reaches a different recogniser (``ch_PP-OCRv4_rec_mobile.onnx`` instead of
``en_PP-OCRv4_rec_mobile.onnx``) yet yields BYTE-IDENTICAL text here
(density 0.2741 both ways): the Chinese set reads clean, large, synthetic
renders of Latin script perfectly. Degrading the fixture does not rescue
the separation — across six render variants the two languages are
non-monotonic, and on a gray/noisy variant the Chinese set scored HIGHER
(0.2296 vs 0.1290). Threshold-fitting a synthetic image would buy a flaky
test, not a guard.
The unit cases in ``test_docling.py::TestDoclingMakeConverter`` hold the
language pin by asserting ``ocr_options.lang == ["en"]``. This served-path
smoke only checks that the pinned artifact returns segmented text.
So: keep this as a served-path smoke over the pinned revision. Do not read
it as evidence that the language is correct.
"""
out = artifact_backed_adapter.extract(
[Item(images=[{"data": _make_prose_image_bytes(), "format": "png"}])],
options={"ocr": True},
)
assert out.data is not None
assert out.errors is None, f"adapter reported errors: {out.errors}"
item = out.data[0]
text = item["text"]
alpha = sum(1 for ch in text if ch.isalpha())
whitespace = sum(1 for ch in text if ch.isspace())
assert alpha > 40, f"OCR recognised too little text to judge: {text!r}"
density = whitespace / alpha
assert density > 0.10, (
f"served OCR output is word-joined (whitespace/alpha = {density:.3f}); "
f"the pinned artifact revision resolves no recogniser able to segment "
f"this page at all. This is a catastrophic-failure floor, not a "
f"language check — see the docstring: {text!r}"
)