How faizancyber007/document-parser works
Purpose and Entry Points
pak_legal_parser converts Pakistani bilingual legal PDFs (Urdu Nastaliq/English) into provenance-rich structured JSON: physical blocks, OCR text, legal sections, metadata, corrections, and citation links. The primary single-document entry point is main.py; batch execution is run_batch.py. Inspection/reprocessing CLIs are run_triage.py, run_segmentation.py, and run_self_correction.py.
An optional HTTP façade accepts asynchronous parse jobs and reports/pushes results: api/app.py, api/jobs.py, and api/webhooks.py.
Pipeline Orchestration and Data Contract
main.py coordinates phase outputs through shared Pydantic schemas, chiefly schemas/data_models.py and schemas/profile_models.py. Configuration and secrets are centralized in config.py and config.yaml.
The core durability boundary is between immutable-ish physical extraction (raw_blocks) and derived semantic results (sections, metadata, citation graph). Semantic failures should not discard OCR/layout output. Blocks carry IDs, page coordinates, script, OCR source/confidence, errors, and optional inline Markdown.
Phase 0–1.5: Profile, Layout, Segment
Triage classifies document type, page script mix, scan status, and quality before expensive work: modules/triage.py. This selects a taxonomy/profile rather than assuming every input is a judgment; label sets live in schemas/taxonomies.py.
Layout extraction is split by page type in modules/layout_engine.py: native/digital PDFs use PyMuPDF text, tables, coordinates, and font-span formatting; scanned pages are rasterized and use lazily loaded DocLayout-YOLO. This is the primary licensing-sensitive boundary because both dependencies are AGPL/commercial.
modules/segmentation.py assigns structural roles—titles, lists, tables, headers/footers, footnotes—and establishes reading order and OCR routing intent. It prevents repeating boilerplate and footnotes from contaminating legal semantic tagging.
Phase 2: OCR Routing and Provider Resilience
modules/ocr_router.py executes the routing plan. Native digital text can remain untouched; non-native blocks first use local Tesseract, then escalate Urdu, tables, and low-confidence results to vision providers.
Provider abstraction, ordered fallbacks, and optional local GPU vision are in utils/api_clients.py. External services include Gemini, OpenRouter, Groq, and Hugging Face inference; local engines include Tesseract and EasyOCR (utils/easyocr_client.py). Image crop/preprocessing policy resides at utils/image_utils.py.
Cloud reliability is a cross-cutting boundary: utils/rate_limiter.py enforces per-minute limits and utils/usage_tracker.py persists usage and proactively skips daily-exhausted providers.
Phase 3–3.7: Legal Enrichment
modules/semantic_merger.py sends eligible blocks through the text-provider fallback chain, classifies them into document-specific legal sections, merges same-page geometry, renders Markdown, and creates retrieval chunks where needed. It references source block IDs rather than rewriting raw OCR.
Independent enrichments operate over blocks: modules/metadata_extractor.py produces confidence-scored case details; modules/self_correction.py runs deterministic defect detectors, permits LLM repair only when the original detector clears, and otherwise emits human-review work; modules/citation_graph.py uses regex/deterministic matching for case law, statutes, and footnote-reference edges.
Chart and formula paths are architecturally available for specialized blocks in modules/chart_extractor.py and modules/formula_extractor.py.
Batch and API Execution Models
modules/batch_orchestrator.py supports bounded-concurrent batches, content-hash caching, resumable checkpoints, per-document error isolation, and usage reporting. Persistence shapes are defined in schemas/batch_models.py.
The API intentionally uses process-local execution: api/jobs.py maintains an in-memory job store and one background thread per job; no Redis, broker, or distributed worker is present. api/webhooks.py provides best-effort HMAC-SHA256 signed callbacks, while polling remains the durable result access method within the running process.
Runtime and Artifacts
The target runtime is Python 3.10+ on a CPU-only laptop, with optional CUDA/local-GPU vision. Required local infrastructure includes the Tesseract binary and language packs. Runtime outputs default under output/; real judgment inputs and benchmark artifacts are in Judgements/, LlamaParse_Files/, and reference_baselines/.
Main entry points: main.py Single-document parser · jobs.py In-memory jobs · api_clients.py Provider fallbacks · batch_orchestrator.py Batch orchestrator
Components 22
Execution
- Legal PDF inputs
- Configuration · Centralizes settings and secrets.
config.py - Single-document parser · Coordinates phase outputs without losing physical extraction.
main.py - Shared data models · Defines blocks, profiles, and derived result contracts.
schemas/data_models.py - Batch orchestrator · Provides bounded concurrency, caching, checkpoints, and error isolation.
modules/batch_orchestrator.py - Batch models
schemas/batch_models.py - Output artifacts · Default location for parse results and checkpoints.
Physical extraction
- Document triage · Classifies type, scripts, scan state, and quality.
modules/triage.py - Layout extraction · Uses native PDF extraction or scanned-page layout detection.
modules/layout_engine.py - Raw blocks · Immutable-ish OCR, coordinates, script, confidence, and errors.
- Structural segmentation · Assigns roles and OCR routing intent.
modules/segmentation.py - OCR router · Keeps native text or escalates difficult blocks.
modules/ocr_router.py - Provider fallbacks · Routes across local and cloud OCR/vision providers.
utils/api_clients.py - Rate limits · Enforces provider per-minute limits.
utils/rate_limiter.py
Legal enrichment
- Semantic merger · Builds legal sections, Markdown, and retrieval chunks from source block IDs.
modules/semantic_merger.py - Metadata extraction · Produces confidence-scored case metadata.
modules/metadata_extractor.py - Self-correction · Uses deterministic detectors and emits human-review work when needed.
modules/self_correction.py - Citation graph · Finds case law, statute, and footnote-reference links.
modules/citation_graph.py - Derived legal results · Sections, metadata, corrections, and citation links remain separate from raw blocks.
HTTP façade
- HTTP API
api/app.py - In-memory jobs · Uses one background thread per job; no broker or distributed workers.
api/jobs.py - Signed webhooks · Pushes HMAC-SHA256 signed completion callbacks.
api/webhooks.py
Connections 26
- Legal PDF inputs → Single-document parser: parse
- Configuration → Single-document parser: configures
- Shared data models → Single-document parser: contracts
- Single-document parser → Document triage: phase 0
- Document triage → Layout extraction: page strategy
- Layout extraction → Raw blocks: physical extraction
- Raw blocks → Structural segmentation: blocks
- Structural segmentation → OCR router: routing plan
- OCR router → Provider fallbacks: escalates
- Rate limits → Provider fallbacks: throttles
- Provider fallbacks → Raw blocks: OCR text and confidence
- Raw blocks → Semantic merger: source block IDs
- Raw blocks → Metadata extraction: block evidence
- Raw blocks → Self-correction: detect defects
- Raw blocks → Citation graph: deterministic matching
- Semantic merger → Derived legal results: sections and chunks
- Metadata extraction → Derived legal results: case details
- Self-correction → Derived legal results: repairs and review work
- Citation graph → Derived legal results: citation links
- Single-document parser → Output artifacts: writes results
- Derived legal results → Output artifacts: serialize
- Batch orchestrator → Single-document parser: per document
- Batch models → Batch orchestrator: persistence shapes
- HTTP API → In-memory jobs: submits async parse
- In-memory jobs → Single-document parser: background thread
- In-memory jobs → Signed webhooks: completion callback