Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions extract-core/extract_core/docling_.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
TableStructureOptions,
ThreadedPdfPipelineOptions,
)
from docling.datamodel.settings import (
BatchConcurrencySettings,
DebugSettings,
InferenceSettings,
)
from icij_common.pydantic_utils import (
merge_configs,
safe_copy,
tagged_union,
to_lower_snake_case,
)
Expand Down Expand Up @@ -258,12 +264,19 @@ def _default_format_opts() -> dict[InputFormat, DoclingFormatOption]:
}


class DoclingSettings(BaseModel):
perf: BatchConcurrencySettings = Field(default_factory=BatchConcurrencySettings)
debug: DebugSettings = Field(default_factory=DebugSettings)
inference: InferenceSettings = Field(default_factory=InferenceSettings)


class DoclingPipelineConfig(BasePipelineConfig):
pipeline: ClassVar[PipelineType] = Field(frozen=True, default=PipelineType.DOCLING)

format_options: dict[InputFormat, DoclingFormatOption] = Field(
default_factory=_default_format_opts
)
settings: DoclingSettings = Field(default_factory=DoclingSettings)

@classmethod
@cache
Expand All @@ -276,3 +289,7 @@ def supported_exts(cls) -> set[SupportedExt]:
for ext in FormatToExtensions[f]:
supported.add(SupportedExt(f".{ext.lower()}"))
return supported

def with_setting(self, settings: DoclingSettings) -> "DoclingPipelineConfig":
update = {"settings": settings}
return safe_copy(self, update=update)
1 change: 1 addition & 0 deletions extract-python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ô
28 changes: 18 additions & 10 deletions extract-python/extract_python/docling_.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from docling.datamodel.document import ConversionResult
from docling.datamodel.pipeline_options import PipelineOptions
from docling.datamodel.settings import scoped
from docling.document_converter import DocumentConverter, FormatOption

# TODO: this is long to load improve it
Expand Down Expand Up @@ -62,16 +63,23 @@ def __init__(self, config: DoclingPipelineConfig):
async def extract_content(
self, docs: Iterable[InputDoc], output_format: OutputFormat, output_path: Path
) -> AsyncGenerator[Result, None]:
docs, path_or_streams = map_and_preserve(_to_docling, docs)
outputs = self._converter.convert_all(path_or_streams, raises_on_error=False)

sentinel = object()
while True:
res = await asyncio.to_thread(next, outputs, sentinel)
if res is sentinel:
return
doc = next(docs)
yield _to_result(res, doc, output_format, output_path=output_path)
settings = self._config.settings
logger.info("starting extraction with settings: %s", settings)
with scoped(
perf=settings.perf, debug=settings.debug, inference=settings.inference
):
docs, path_or_streams = map_and_preserve(_to_docling, docs)
outputs = self._converter.convert_all(
path_or_streams, raises_on_error=False
)

sentinel = object()
while True:
res = await asyncio.to_thread(next, outputs, sentinel)
if res is sentinel:
return
doc = next(docs)
yield _to_result(res, doc, output_format, output_path=output_path)


def _to_docling(docs: Iterable[InputDoc]) -> Iterator["Path | DocumentStream"]:
Expand Down