
* propagated changes for new CodeItem class Signed-off-by: Matteo Omenetti <omenetti.matteo@gmail.com> * Rebased branch on latest main. changes for CodeItem Signed-off-by: Matteo Omenetti <omenetti.matteo@gmail.com> * removed unused files Signed-off-by: Matteo Omenetti <omenetti.matteo@gmail.com> * chore: update lockfile Signed-off-by: Christoph Auer <cau@zurich.ibm.com> * pin latest docling-core Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * update docling-core pinning Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * pin docling-core Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * use new add_code in backends and update typing in MD backend Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * added if statement for backend Signed-off-by: Matteo Omenetti <omenetti.matteo@gmail.com> * removed unused import Signed-off-by: Matteo Omenetti <omenetti.matteo@gmail.com> * removed print statements Signed-off-by: Matteo Omenetti <omenetti.matteo@gmail.com> * gt for new pdf Signed-off-by: Matteo Omenetti <omenetti.matteo@gmail.com> * Update docling/pipeline/standard_pdf_pipeline.py Co-authored-by: Michele Dolfi <97102151+dolfim-ibm@users.noreply.github.com> Signed-off-by: Matteo <43417658+Matteo-Omenetti@users.noreply.github.com> * fixed doc comment of __call__ function of code_formula_model Signed-off-by: Matteo Omenetti <omenetti.matteo@gmail.com> * fix artifacts_path type Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * move imports Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * move expansion_factor to base class Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> --------- Signed-off-by: Matteo Omenetti <omenetti.matteo@gmail.com> Signed-off-by: Christoph Auer <cau@zurich.ibm.com> Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> Signed-off-by: Matteo <43417658+Matteo-Omenetti@users.noreply.github.com> Co-authored-by: Christoph Auer <cau@zurich.ibm.com> Co-authored-by: Michele Dolfi <dol@zurich.ibm.com> Co-authored-by: Michele Dolfi <97102151+dolfim-ibm@users.noreply.github.com>
85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
from abc import ABC, abstractmethod
|
|
from typing import Any, Generic, Iterable, Optional
|
|
|
|
from docling_core.types.doc import BoundingBox, DoclingDocument, NodeItem, TextItem
|
|
from typing_extensions import TypeVar
|
|
|
|
from docling.datamodel.base_models import ItemAndImageEnrichmentElement, Page
|
|
from docling.datamodel.document import ConversionResult
|
|
|
|
|
|
class BasePageModel(ABC):
|
|
@abstractmethod
|
|
def __call__(
|
|
self, conv_res: ConversionResult, page_batch: Iterable[Page]
|
|
) -> Iterable[Page]:
|
|
pass
|
|
|
|
|
|
EnrichElementT = TypeVar("EnrichElementT", default=NodeItem)
|
|
|
|
|
|
class GenericEnrichmentModel(ABC, Generic[EnrichElementT]):
|
|
|
|
@abstractmethod
|
|
def is_processable(self, doc: DoclingDocument, element: NodeItem) -> bool:
|
|
pass
|
|
|
|
@abstractmethod
|
|
def prepare_element(
|
|
self, conv_res: ConversionResult, element: NodeItem
|
|
) -> Optional[EnrichElementT]:
|
|
pass
|
|
|
|
@abstractmethod
|
|
def __call__(
|
|
self, doc: DoclingDocument, element_batch: Iterable[EnrichElementT]
|
|
) -> Iterable[NodeItem]:
|
|
pass
|
|
|
|
|
|
class BaseEnrichmentModel(GenericEnrichmentModel[NodeItem]):
|
|
|
|
def prepare_element(
|
|
self, conv_res: ConversionResult, element: NodeItem
|
|
) -> Optional[NodeItem]:
|
|
if self.is_processable(doc=conv_res.document, element=element):
|
|
return element
|
|
return None
|
|
|
|
|
|
class BaseItemAndImageEnrichmentModel(
|
|
GenericEnrichmentModel[ItemAndImageEnrichmentElement]
|
|
):
|
|
|
|
images_scale: float
|
|
expansion_factor: float = 0.0
|
|
|
|
def prepare_element(
|
|
self, conv_res: ConversionResult, element: NodeItem
|
|
) -> Optional[ItemAndImageEnrichmentElement]:
|
|
if not self.is_processable(doc=conv_res.document, element=element):
|
|
return None
|
|
|
|
assert isinstance(element, TextItem)
|
|
element_prov = element.prov[0]
|
|
|
|
bbox = element_prov.bbox
|
|
width = bbox.r - bbox.l
|
|
height = bbox.t - bbox.b
|
|
|
|
# TODO: move to a utility in the BoundingBox class
|
|
expanded_bbox = BoundingBox(
|
|
l=bbox.l - width * self.expansion_factor,
|
|
t=bbox.t + height * self.expansion_factor,
|
|
r=bbox.r + width * self.expansion_factor,
|
|
b=bbox.b - height * self.expansion_factor,
|
|
coord_origin=bbox.coord_origin,
|
|
)
|
|
|
|
page_ix = element_prov.page_no - 1
|
|
cropped_image = conv_res.pages[page_ix].get_image(
|
|
scale=self.images_scale, cropbox=expanded_bbox
|
|
)
|
|
return ItemAndImageEnrichmentElement(item=element, image=cropped_image)
|