-
Notifications
You must be signed in to change notification settings - Fork 15
added docling plugin and tests #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vwe-ibm
wants to merge
1
commit into
main
Choose a base branch
from
vwe/docling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| from docling_core.types.doc import ( | ||
| PictureItem, | ||
| ) | ||
| from docling.datamodel.base_models import InputFormat | ||
| from docling.document_converter import DocumentConverter, PdfFormatOption | ||
|
|
||
| from molgrapher.docling.picture_molecule_annotator_pipeline import PictureMoleculeAnnotatorPipelineOptions, PictureMoleculeAnnotatorPipeline | ||
|
|
||
| def main(): | ||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
| data_folder = Path(__file__).parent / "../../vw-tests/docling/" | ||
| input_doc_path = data_folder / "CN119912385A-5.pdf" | ||
|
|
||
| pipeline_options = PictureMoleculeAnnotatorPipelineOptions() | ||
| pipeline_options.images_scale = 2.0 | ||
| pipeline_options.generate_picture_images = True | ||
|
|
||
| doc_converter = DocumentConverter( | ||
| format_options={ | ||
| InputFormat.PDF: PdfFormatOption( | ||
| pipeline_cls=PictureMoleculeAnnotatorPipeline, | ||
| pipeline_options=pipeline_options, | ||
| ) | ||
| } | ||
| ) | ||
| result = doc_converter.convert(input_doc_path) | ||
|
|
||
| for element, _level in result.document.iterate_items(): | ||
| if isinstance(element, PictureItem): | ||
| print( | ||
| f"The model populated the `data` portion of picture {element.self_ref}:\n{element.meta}" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| from collections.abc import Iterable | ||
| from pathlib import Path | ||
| from typing import List, Literal, Optional, Union | ||
|
|
||
| import numpy as np | ||
| from docling_core.types.doc import ( | ||
| DoclingDocument, | ||
| NodeItem, | ||
| PictureItem, | ||
| PictureMeta, | ||
| MoleculeMetaField, | ||
| ) | ||
| from PIL import Image | ||
| from pydantic import BaseModel | ||
|
|
||
| from docling.datamodel.accelerator_options import AcceleratorOptions | ||
| from docling.datamodel.base_models import ItemAndImageEnrichmentElement | ||
| from docling.models.base_model import BaseItemAndImageEnrichmentModel | ||
| from docling.models.utils.hf_model_download import download_hf_model | ||
| from docling.utils.accelerator_utils import decide_device | ||
|
|
||
| from molgrapher.models.molgrapher_model import MolgrapherModel | ||
|
|
||
|
|
||
| class PictureMoleculeAnnotatorOptions(BaseModel): | ||
| """ | ||
| Options for configuring the PictureMoleculeAnnotator. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| kind : Literal["picture_molecule_annotator"] | ||
| Identifier for the annotator. | ||
| """ | ||
|
|
||
| kind: Literal["picture_molecule_annotator"] = "picture_molecule_annotator" | ||
|
|
||
|
|
||
| class PictureMoleculeAnnotator(BaseItemAndImageEnrichmentModel): | ||
| """ | ||
| A model for annotating molecule pictures in documents. | ||
|
|
||
| This class enriches document pictures with predicted annotations | ||
| based on a predefined set of classes. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| enabled : bool | ||
| Whether the annotator is enabled for use. | ||
| options : PictureMoleculeAnnotatorOptions | ||
| Configuration options for the annotator. | ||
|
|
||
| Methods | ||
| ------- | ||
| __init__(enabled, options, accelerator_options) | ||
| Initializes the annotator with specified configurations. | ||
| is_processable(doc, element) | ||
| Checks if the given element can be processed by the annotator. | ||
| __call__(doc, element_batch) | ||
| Processes a batch of elements and adds molecule annotations. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| enabled: bool, | ||
| options: PictureMoleculeAnnotatorOptions, | ||
| accelerator_options: AcceleratorOptions, | ||
| ): | ||
| """ | ||
| Initializes the PictureMoleculeAnnotator. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| enabled : bool | ||
| Indicates whether the annotator is enabled. | ||
| options : PictureMoleculeAnnotatorOptions | ||
| Configuration options for the annotator. | ||
| accelerator_options : AcceleratorOptions | ||
| Options for configuring the device and parallelism. | ||
| """ | ||
| self.enabled = enabled | ||
| self.options = options | ||
|
|
||
| if self.enabled: | ||
| device = decide_device(accelerator_options.device) | ||
|
|
||
| # enforce only cpu for the moment | ||
| assert device == "cpu" | ||
|
|
||
| self.picture_molecule_annotator = MolgrapherModel() | ||
|
|
||
| def is_processable(self, doc: DoclingDocument, element: NodeItem) -> bool: | ||
| """ | ||
| Determines if the given element can be processed by the annotator. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| doc : DoclingDocument | ||
| The document containing the element. | ||
| element : NodeItem | ||
| The element to be checked. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| True if the element is a PictureItem and processing is enabled; False otherwise. | ||
| """ | ||
| return self.enabled and isinstance(element, PictureItem) | ||
|
|
||
| def __call__( | ||
| self, | ||
| doc: DoclingDocument, | ||
| element_batch: Iterable[ItemAndImageEnrichmentElement], | ||
| ) -> Iterable[NodeItem]: | ||
| """ | ||
| Processes a batch of elements and enriches them with molecule annotation predictions. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| doc : DoclingDocument | ||
| The document containing the elements to be processed. | ||
| element_batch : Iterable[ItemAndImageEnrichmentElement] | ||
| A batch of pictures to annotate. | ||
|
|
||
| Returns | ||
| ------- | ||
| Iterable[NodeItem] | ||
| An iterable of NodeItem objects after processing. The field | ||
| 'meta.molecule' is added containing the annotation for each picture. | ||
| """ | ||
| if not self.enabled: | ||
| for element in element_batch: | ||
| yield element.item | ||
| return | ||
|
|
||
| images: List[Union[Image.Image, np.ndarray]] = [] | ||
| elements: List[PictureItem] = [] | ||
| for el in element_batch: | ||
| assert isinstance(el.item, PictureItem) | ||
| elements.append(el.item) | ||
| images.append(el.image) | ||
|
|
||
| outputs = self.picture_molecule_annotator.predict(images) | ||
|
|
||
| for item, output in zip(elements, outputs): | ||
| if output.get("smi", "") == "" or \ | ||
| output.get("smi", "") == "C": | ||
| continue | ||
| if item.meta is None: | ||
| item.meta = PictureMeta() | ||
| # | ||
| item.meta.molecule = MoleculeMetaField( | ||
| smi=output.get("smi", ""), | ||
| confidence=output.get("conf", 0.0), | ||
| created_by=output.get("annotator", {}).get("program", "") | ||
| ) | ||
|
|
||
| yield item |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| from docling_core.types.doc import ( | ||
| PictureItem, | ||
| ) | ||
| from docling.datamodel.base_models import InputFormat | ||
| from docling.datamodel.pipeline_options import PdfPipelineOptions | ||
| from docling.document_converter import DocumentConverter, PdfFormatOption | ||
| from docling.models.base_model import BaseEnrichmentModel | ||
| from docling.pipeline.standard_pdf_pipeline import StandardPdfPipeline | ||
| from docling.datamodel.accelerator_options import AcceleratorDevice, AcceleratorOptions | ||
|
|
||
| from molgrapher.docling.picture_molecule_annotator import PictureMoleculeAnnotator, PictureMoleculeAnnotatorOptions | ||
|
|
||
|
|
||
| class PictureMoleculeAnnotatorPipelineOptions(PdfPipelineOptions): | ||
| do_picture_annotator: bool = True | ||
|
|
||
|
|
||
| class PictureMoleculeAnnotatorPipeline(StandardPdfPipeline): | ||
| def __init__(self, pipeline_options: PictureMoleculeAnnotatorPipelineOptions): | ||
| super().__init__(pipeline_options) | ||
| self.pipeline_options: PictureMoleculeAnnotatorPipeline | ||
|
|
||
| self.enrichment_pipe = [ | ||
| PictureMoleculeAnnotator( | ||
| enabled=pipeline_options.do_picture_annotator, | ||
| options=PictureMoleculeAnnotatorOptions(), | ||
| accelerator_options=AcceleratorOptions(device=AcceleratorDevice.CPU) | ||
| ) | ||
| ] | ||
|
|
||
| @classmethod | ||
| def get_default_options(cls) -> PictureMoleculeAnnotatorPipelineOptions: | ||
| return PictureMoleculeAnnotatorPipelineOptions() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [aliases] | ||
| test=pytest | ||
| [tool:pytest] | ||
| addopts = -q | ||
| testpaths = tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """Test methods in module molgrapher.""" | ||
|
|
||
| import unittest | ||
|
|
||
| from PIL import Image | ||
| from pathlib import Path | ||
| try: | ||
| from docling.datamodel.base_models import InputFormat | ||
| from docling.document_converter import DocumentConverter, PdfFormatOption | ||
| from docling_core.types.doc import ( | ||
| PictureItem, | ||
| ) | ||
| from molgrapher.docling.picture_molecule_annotator_pipeline import PictureMoleculeAnnotatorPipelineOptions, PictureMoleculeAnnotatorPipeline | ||
| has_docling = True | ||
| except ImportError: | ||
| has_docling = False | ||
|
|
||
|
|
||
| @unittest.skipIf(not has_docling, "docling package not installed") | ||
| class TestDocling(unittest.TestCase): | ||
| """Test the methods in molgrapher.""" | ||
|
|
||
| def test_docl_1(self): | ||
|
|
||
| input_doc_path = Path(__file__).parent / "../images/CN119912385A-5.pdf" | ||
|
|
||
| pipeline_options = PictureMoleculeAnnotatorPipelineOptions() | ||
| pipeline_options.images_scale = 2.0 | ||
| pipeline_options.generate_picture_images = True | ||
|
|
||
| doc_converter = DocumentConverter( | ||
| format_options={ | ||
| InputFormat.PDF: PdfFormatOption( | ||
| pipeline_cls=PictureMoleculeAnnotatorPipeline, | ||
| pipeline_options=pipeline_options, | ||
| ) | ||
| } | ||
| ) | ||
| result = doc_converter.convert(input_doc_path) | ||
|
|
||
| smis = [] | ||
| for element, _level in result.document.iterate_items(): | ||
| if isinstance(element, PictureItem): | ||
| print( | ||
| f"The model populated the `data` portion of picture {element.self_ref}:\n{element.meta}" | ||
| ) | ||
| if element.meta is not None: | ||
| smis.append(element.meta.molecule.smi) | ||
|
|
||
| refs = ["[H]C1=CC=C(C=CC(=O)C2=CC=C3C(=C2)C2=C(C=CC(C(=O)C=CC4=CC=C(F)C=C4)=C2)N3CC)C=C1", "CC(=O)C1=CC=N2=[I-](=C1)C1=C(C=CC(C(C)=O)=C1)N21CC1"] | ||
| for smi, ref in zip(smis, refs): | ||
| print(smi) | ||
| self.assertEqual(smi, ref) | ||
|
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """Test methods in module molgrapher.""" | ||
|
|
||
| import unittest | ||
| import json | ||
|
|
||
| from molgrapher.models.molgrapher_model import MolgrapherModel | ||
| from PIL import Image | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| class TestMolG(unittest.TestCase): | ||
| """Test the methods in molgrapher.""" | ||
|
|
||
| def test_molg_1(self): | ||
| model = MolgrapherModel() | ||
| image_paths = [ | ||
| Path(__file__).parent /"../images/CN119912385A-5-1.png", | ||
| Path(__file__).parent /"../images/CN119912385A-5-2.png" | ||
| ] | ||
| batch = [Image.open(image_path) for image_path in image_paths] | ||
| annos = model.predict(batch) | ||
| self.assertEqual(annos[0].get("smi"), "C") | ||
| self.assertEqual(annos[1].get("smi"), "C=CC(=CC1=CN2(CC2)N2=CC=C(C(C)=O)C=C=21)C(C)=O") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how are these docling integrations differing from the existing onese in https://github.com/DS4SD/MolGrapher/tree/main/molgrapher/scripts/annotate/docling ?