|
| 1 | +from docling.chunking import HierarchicalChunker |
| 2 | +from docling.datamodel.base_models import InputFormat |
| 3 | +from docling.datamodel.pipeline_options import AcceleratorOptions, EasyOcrOptions, PdfPipelineOptions, PipelineOptions |
| 4 | +from docling.document_converter import ( |
| 5 | + DocumentConverter, |
| 6 | + ExcelFormatOption, |
| 7 | + HTMLFormatOption, |
| 8 | + MarkdownFormatOption, |
| 9 | + PdfFormatOption, |
| 10 | + PowerpointFormatOption, |
| 11 | + WordFormatOption, |
| 12 | +) |
| 13 | +from docling_core.types.doc import DocItem, DoclingDocument |
| 14 | + |
| 15 | +from ragbits.document_search.documents.document import Document, DocumentType |
| 16 | +from ragbits.document_search.documents.element import Element, ElementLocation, ImageElement, TextElement |
| 17 | +from ragbits.document_search.ingestion.parsers import DocumentParser |
| 18 | + |
| 19 | + |
| 20 | +class DoclingDocumentParser(DocumentParser): |
| 21 | + """ |
| 22 | + Parser that uses the Docling to process the documents. |
| 23 | + """ |
| 24 | + |
| 25 | + supported_document_types = { |
| 26 | + DocumentType.DOCX, |
| 27 | + DocumentType.PPTX, |
| 28 | + DocumentType.XLSX, |
| 29 | + DocumentType.MD, |
| 30 | + DocumentType.PNG, |
| 31 | + DocumentType.JPG, |
| 32 | + DocumentType.HTML, |
| 33 | + DocumentType.TXT, |
| 34 | + DocumentType.PDF, |
| 35 | + } |
| 36 | + |
| 37 | + def __init__(self, ignore_images: bool = False, num_threads: int = 1) -> None: |
| 38 | + """ |
| 39 | + Initialize the DoclingDocumentParser instance. |
| 40 | +
|
| 41 | + Args: |
| 42 | + ignore_images: If True images will be skipped. |
| 43 | + num_threads: The number of threads for parsing parallelism on CPU. |
| 44 | + """ |
| 45 | + self.ignore_images = ignore_images |
| 46 | + self.num_threads = num_threads |
| 47 | + |
| 48 | + async def parse(self, document: Document) -> list[Element]: |
| 49 | + """ |
| 50 | + Parse the document using the Docling API. |
| 51 | +
|
| 52 | + Args: |
| 53 | + document: The document to parse. |
| 54 | +
|
| 55 | + Returns: |
| 56 | + The list of elements extracted from the document. |
| 57 | + """ |
| 58 | + self.validate_document_type(document.metadata.document_type) |
| 59 | + partitioned_document = await self._partition(document) |
| 60 | + return self._chunk(partitioned_document, document) |
| 61 | + |
| 62 | + async def _partition(self, document: Document) -> DoclingDocument: |
| 63 | + """ |
| 64 | + Partition the document. |
| 65 | +
|
| 66 | + Args: |
| 67 | + document: The document to parse. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + The docling document. |
| 71 | +
|
| 72 | + Raises: |
| 73 | + ConversionError: If converting the document to the Docling format fails. |
| 74 | + """ |
| 75 | + accelerator_options = AcceleratorOptions(num_threads=self.num_threads) |
| 76 | + pipeline_options = PipelineOptions(accelerator_options=accelerator_options) |
| 77 | + pdf_pipeline_options = PdfPipelineOptions( |
| 78 | + images_scale=2, |
| 79 | + generate_page_images=True, |
| 80 | + ocr_options=EasyOcrOptions(), |
| 81 | + accelerator_options=accelerator_options, |
| 82 | + ) |
| 83 | + converter = DocumentConverter( |
| 84 | + format_options={ |
| 85 | + InputFormat.XLSX: ExcelFormatOption(pipeline_options=pipeline_options), |
| 86 | + InputFormat.DOCX: WordFormatOption(pipeline_options=pipeline_options), |
| 87 | + InputFormat.PPTX: PowerpointFormatOption(pipeline_options=pipeline_options), |
| 88 | + InputFormat.HTML: HTMLFormatOption(pipeline_options=pipeline_options), |
| 89 | + InputFormat.MD: MarkdownFormatOption(pipeline_options=pipeline_options), |
| 90 | + InputFormat.IMAGE: PdfFormatOption(pipeline_options=pdf_pipeline_options), |
| 91 | + InputFormat.PDF: PdfFormatOption(pipeline_options=pdf_pipeline_options), |
| 92 | + }, |
| 93 | + ) |
| 94 | + # For txt files, temporarily rename to .md extension. Docling doesn't support text files natively. |
| 95 | + if document.metadata.document_type == DocumentType.TXT: |
| 96 | + original_suffix = document.local_path.suffix |
| 97 | + document.local_path = document.local_path.rename(document.local_path.with_suffix(".md")) |
| 98 | + |
| 99 | + partitioned_document = converter.convert(document.local_path).document |
| 100 | + |
| 101 | + # Convert back to the original file. |
| 102 | + if document.metadata.document_type == DocumentType.TXT: |
| 103 | + document.local_path = document.local_path.rename(document.local_path.with_suffix(original_suffix)) |
| 104 | + |
| 105 | + return partitioned_document |
| 106 | + |
| 107 | + def _chunk(self, partitioned_document: DoclingDocument, document: Document) -> list[Element]: |
| 108 | + """ |
| 109 | + Chunk the partitioned document. |
| 110 | +
|
| 111 | + Args: |
| 112 | + partitioned_document: The partitioned document by Docling. |
| 113 | + document: The document to parse. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + The list of chunked elements. |
| 117 | + """ |
| 118 | + chunker = HierarchicalChunker() |
| 119 | + text_elements: list[Element] = [ |
| 120 | + TextElement( |
| 121 | + document_meta=document.metadata, |
| 122 | + location=self._extract_element_location(chunk.meta.doc_items[0]), # type: ignore |
| 123 | + content=chunk.text, |
| 124 | + ) |
| 125 | + for chunk in chunker.chunk(partitioned_document) |
| 126 | + ] |
| 127 | + |
| 128 | + if self.ignore_images: |
| 129 | + return text_elements |
| 130 | + |
| 131 | + return text_elements + [ |
| 132 | + ImageElement( |
| 133 | + document_meta=document.metadata, |
| 134 | + location=self._extract_element_location(element), |
| 135 | + image_bytes=image_bytes, |
| 136 | + ocr_extracted_text=element.caption_text(partitioned_document), |
| 137 | + ) |
| 138 | + for element in partitioned_document.pictures |
| 139 | + if (image := element.get_image(partitioned_document)) and (image_bytes := image._repr_jpeg_()) |
| 140 | + ] |
| 141 | + |
| 142 | + @staticmethod |
| 143 | + def _extract_element_location(element: DocItem) -> ElementLocation: |
| 144 | + """ |
| 145 | + Convert docling element to element location. |
| 146 | +
|
| 147 | + Args: |
| 148 | + element: The element from docling. |
| 149 | +
|
| 150 | + Returns: |
| 151 | + The element location. |
| 152 | + """ |
| 153 | + metadata = element.prov[0].model_dump() if element.prov else {} |
| 154 | + return ElementLocation( |
| 155 | + page_number=metadata.get("page_no"), |
| 156 | + ) |
0 commit comments