VTdevelops/bond-text-extraction
0
1from __future__ import annotations2 3from pathlib import Path4from typing import Iterable, Sequence5 6from .llm_client import BondInfoExtractor7from .models import BondRecord8from .pdf_loader import extract_text_from_pdf9from .xml_builder import build_bond_xml10 11 12class ExtractionPipeline:13 """High-level orchestration for PDF ingestion to XML output."""14 15 def __init__(16 self,17 model: str = "gpt-4.1-mini",18 extractor: BondInfoExtractor | None = None,19 ) -> None:20 self.extractor = extractor or BondInfoExtractor(model=model)21 22 def run(23 self,24 pdf_paths: Sequence[Path | str],25 *,26 extra_instructions: str | None = None,27 output_path: Path | str | None = None,28 ) -> tuple[list[BondRecord], str]:29 """Extract records into XML; optionally write XML to *output_path*."""30 31 normalised_paths = [Path(path) for path in pdf_paths]32 records: list[BondRecord] = []33 34 for path in normalised_paths:35 document_text = extract_text_from_pdf(path)36 # Query the LLM per document so each PDF generates its own bond payload.37 per_doc_records = self.extractor.extract_bonds(38 [document_text],39 extra_instructions,40 )41 records.extend(per_doc_records)42 xml_doc = build_bond_xml(records)43 44 if output_path is not None:45 Path(output_path).write_text(xml_doc, encoding="utf-8")46 47 return records, xml_doc48 