papertrail/python/document_ai/docling_extract.py
caelum0x 695a365ae4 feat: PaperTrail — enterprise evidence-verification platform
Multi-tenant platform (182 pages, 228 API routes, ~30 modules) built on a
deterministic verification core: clinical-trial claims checked against
ClinicalTrials.gov registered results and raw event counts (ARR/NNT/RR+CI
recomputed in code, oracle-tested), code-enforced exact-span grounding, and
multi-source cross-verification. Auth/RBAC/multi-tenancy, document processing
at scale (unpdf + Docling), agentic workflows, Claude Science connector,
compliance/audit, billing, public API, and more.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 07:21:22 +03:00

62 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""Docling-based scholarly PDF extraction (MIT-licensed Docling, merged in-repo).
Converts a research paper PDF into structured Markdown + plain text + per-section
content, emitted as JSON on stdout. Invoked DIRECTLY as a subprocess by the Node
app (lib/ingestion/docling.ts) — no HTTP service. Docling recovers reading order,
sections, tables, and figures far better than raw pdf.js text, which matters when
processing hundreds of pages of papers and locating exact claim/finding spans.
Usage: python3 docling_extract.py <pdf_path>
Requires: pip install -r python/requirements.txt
"""
import json
import sys
def extract(pdf_path: str) -> dict:
from docling.document_converter import DocumentConverter
converter = DocumentConverter()
result = converter.convert(pdf_path)
doc = result.document
markdown = doc.export_to_markdown()
try:
text = doc.export_to_text()
except Exception:
text = markdown
# Per-section text when the document model exposes it (best-effort).
sections = []
for item, _level in getattr(doc, "iterate_items", lambda: [])():
label = getattr(item, "label", None)
content = getattr(item, "text", None)
if content:
sections.append({"label": str(label) if label else None, "text": content})
return {
"ok": True,
"markdown": markdown,
"text": text,
"sections": sections,
"num_pages": getattr(doc, "num_pages", None),
}
def main() -> int:
if len(sys.argv) < 2:
print(json.dumps({"ok": False, "error": "usage: docling_extract.py <pdf_path>"}))
return 2
try:
out = extract(sys.argv[1])
print(json.dumps(out))
return 0
except Exception as exc: # noqa: BLE001 — surface any failure to the Node caller as JSON
print(json.dumps({"ok": False, "error": f"{type(exc).__name__}: {exc}"}))
return 1
if __name__ == "__main__":
raise SystemExit(main())