mirror of
https://github.com/DS4SD/docling.git
synced 2025-07-27 20:44:16 +00:00
Fixes to preserve page image and demo export to html
Signed-off-by: Maksym Lysak <mly@zurich.ibm.com>
This commit is contained in:
parent
3c4c647615
commit
1b968e4984
@ -23,7 +23,6 @@ from docling.datamodel.pipeline_options import AcceleratorDevice, AcceleratorOpt
|
|||||||
from docling.datamodel.settings import settings
|
from docling.datamodel.settings import settings
|
||||||
from docling.models.base_model import BasePageModel
|
from docling.models.base_model import BasePageModel
|
||||||
from docling.utils.accelerator_utils import decide_device
|
from docling.utils.accelerator_utils import decide_device
|
||||||
from docling.utils.layout_postprocessor import LayoutPostprocessor
|
|
||||||
from docling.utils.profiling import TimeRecorder
|
from docling.utils.profiling import TimeRecorder
|
||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
@ -44,7 +43,6 @@ class SmolDoclingModel(BasePageModel):
|
|||||||
)
|
)
|
||||||
self.param_quantized = False
|
self.param_quantized = False
|
||||||
|
|
||||||
# self.your_vlm_predictor(..., device) = None # TODO
|
|
||||||
self.processor = AutoProcessor.from_pretrained(artifacts_path)
|
self.processor = AutoProcessor.from_pretrained(artifacts_path)
|
||||||
if not self.param_quantized:
|
if not self.param_quantized:
|
||||||
self.vlm_model = Idefics3ForConditionalGeneration.from_pretrained(
|
self.vlm_model = Idefics3ForConditionalGeneration.from_pretrained(
|
||||||
|
@ -200,13 +200,12 @@ class PaginatedPipeline(BasePipeline): # TODO this is a bad name.
|
|||||||
return conv_res
|
return conv_res
|
||||||
|
|
||||||
def _unload(self, conv_res: ConversionResult) -> ConversionResult:
|
def _unload(self, conv_res: ConversionResult) -> ConversionResult:
|
||||||
for page in conv_res.pages:
|
# for page in conv_res.pages:
|
||||||
if page._backend is not None:
|
# if page._backend is not None:
|
||||||
page._backend.unload()
|
# page._backend.unload()
|
||||||
|
|
||||||
if conv_res.input._backend:
|
|
||||||
conv_res.input._backend.unload()
|
|
||||||
|
|
||||||
|
# if conv_res.input._backend:
|
||||||
|
# conv_res.input._backend.unload()
|
||||||
return conv_res
|
return conv_res
|
||||||
|
|
||||||
def _determine_status(self, conv_res: ConversionResult) -> ConversionStatus:
|
def _determine_status(self, conv_res: ConversionResult) -> ConversionStatus:
|
||||||
|
@ -100,7 +100,7 @@ class VlmPipeline(PaginatedPipeline):
|
|||||||
if page.predictions.doctags is not None:
|
if page.predictions.doctags is not None:
|
||||||
document_tags += page.predictions.doctags.tag_string
|
document_tags += page.predictions.doctags.tag_string
|
||||||
|
|
||||||
conv_res.document = self._turn_tags_into_doc(document_tags, None)
|
conv_res.document = self._turn_tags_into_doc(document_tags, page.image)
|
||||||
"""
|
"""
|
||||||
image_bytes = BytesIO()
|
image_bytes = BytesIO()
|
||||||
if page.image:
|
if page.image:
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from docling.backend.docling_parse_backend import DoclingParseDocumentBackend
|
from docling.backend.docling_parse_backend import DoclingParseDocumentBackend
|
||||||
from docling.datamodel.base_models import InputFormat
|
from docling.datamodel.base_models import InputFormat
|
||||||
from docling.datamodel.pipeline_options import PdfPipelineOptions
|
from docling.datamodel.pipeline_options import PdfPipelineOptions
|
||||||
@ -9,19 +11,21 @@ from docling.pipeline.vlm_pipeline import VlmPipeline
|
|||||||
source = "tests/data/2305.03393v1-pg9.pdf"
|
source = "tests/data/2305.03393v1-pg9.pdf"
|
||||||
|
|
||||||
pipeline_options = PdfPipelineOptions()
|
pipeline_options = PdfPipelineOptions()
|
||||||
|
pipeline_options.generate_page_images = True
|
||||||
pipeline_options.artifacts_path = "model_artifacts"
|
pipeline_options.artifacts_path = "model_artifacts"
|
||||||
|
|
||||||
|
from docling_core.types.doc import DocItemLabel, ImageRefMode
|
||||||
|
from docling_core.types.doc.document import DEFAULT_EXPORT_LABELS
|
||||||
|
|
||||||
converter = DocumentConverter(
|
converter = DocumentConverter(
|
||||||
format_options={
|
format_options={
|
||||||
InputFormat.PDF: PdfFormatOption(
|
InputFormat.PDF: PdfFormatOption(
|
||||||
pipeline_cls=VlmPipeline,
|
pipeline_cls=VlmPipeline,
|
||||||
pipeline_options=pipeline_options,
|
pipeline_options=pipeline_options,
|
||||||
backend=DoclingParseDocumentBackend,
|
|
||||||
),
|
),
|
||||||
InputFormat.IMAGE: PdfFormatOption(
|
InputFormat.IMAGE: PdfFormatOption(
|
||||||
pipeline_cls=VlmPipeline,
|
pipeline_cls=VlmPipeline,
|
||||||
pipeline_options=pipeline_options,
|
pipeline_options=pipeline_options,
|
||||||
backend=DoclingParseDocumentBackend,
|
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -33,11 +37,11 @@ print("")
|
|||||||
|
|
||||||
result = converter.convert(source)
|
result = converter.convert(source)
|
||||||
|
|
||||||
print("------------")
|
# print("------------")
|
||||||
print("result:")
|
# print("result:")
|
||||||
print("------------")
|
# print("------------")
|
||||||
print("")
|
# print("")
|
||||||
print(result)
|
# print(result)
|
||||||
|
|
||||||
print("------------")
|
print("------------")
|
||||||
print("MD:")
|
print("MD:")
|
||||||
@ -45,6 +49,13 @@ print("------------")
|
|||||||
print("")
|
print("")
|
||||||
print(result.document.export_to_markdown())
|
print(result.document.export_to_markdown())
|
||||||
|
|
||||||
|
Path("scratch").mkdir(parents=True, exist_ok=True)
|
||||||
|
result.document.save_as_html(
|
||||||
|
filename=Path("scratch/smol_export.html"),
|
||||||
|
image_mode=ImageRefMode.REFERENCED,
|
||||||
|
labels=[*DEFAULT_EXPORT_LABELS, DocItemLabel.FOOTNOTE],
|
||||||
|
)
|
||||||
|
|
||||||
print("")
|
print("")
|
||||||
print("============")
|
print("============")
|
||||||
print("done!")
|
print("done!")
|
||||||
|
Loading…
Reference in New Issue
Block a user