|
| 1 | +# |
| 2 | +# CLOUDERA APPLIED MACHINE LEARNING PROTOTYPE (AMP) |
| 3 | +# (C) Cloudera, Inc. 2025 |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Applicable Open Source License: Apache 2.0 |
| 7 | +# |
| 8 | +# NOTE: Cloudera open source products are modular software products |
| 9 | +# made up of hundreds of individual components, each of which was |
| 10 | +# individually copyrighted. Each Cloudera open source product is a |
| 11 | +# collective work under U.S. Copyright Law. Your license to use the |
| 12 | +# collective work is as provided in your written agreement with |
| 13 | +# Cloudera. Used apart from the collective work, this file is |
| 14 | +# licensed for your use pursuant to the open source license |
| 15 | +# identified above. |
| 16 | +# |
| 17 | +# This code is provided to you pursuant a written agreement with |
| 18 | +# (i) Cloudera, Inc. or (ii) a third-party authorized to distribute |
| 19 | +# this code. If you do not have a written agreement with Cloudera nor |
| 20 | +# with an authorized and properly licensed third party, you do not |
| 21 | +# have any rights to access nor to use this code. |
| 22 | +# |
| 23 | +# Absent a written agreement with Cloudera, Inc. ("Cloudera") to the |
| 24 | +# contrary, A) CLOUDERA PROVIDES THIS CODE TO YOU WITHOUT WARRANTIES OF ANY |
| 25 | +# KIND; (B) CLOUDERA DISCLAIMS ANY AND ALL EXPRESS AND IMPLIED |
| 26 | +# WARRANTIES WITH RESPECT TO THIS CODE, INCLUDING BUT NOT LIMITED TO |
| 27 | +# IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND |
| 28 | +# FITNESS FOR A PARTICULAR PURPOSE; (C) CLOUDERA IS NOT LIABLE TO YOU, |
| 29 | +# AND WILL NOT DEFEND, INDEMNIFY, NOR HOLD YOU HARMLESS FOR ANY CLAIMS |
| 30 | +# ARISING FROM OR RELATED TO THE CODE; AND (D)WITH RESPECT TO YOUR EXERCISE |
| 31 | +# OF ANY RIGHTS GRANTED TO YOU FOR THE CODE, CLOUDERA IS NOT LIABLE FOR ANY |
| 32 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR |
| 33 | +# CONSEQUENTIAL DAMAGES INCLUDING, BUT NOT LIMITED TO, DAMAGES |
| 34 | +# RELATED TO LOST REVENUE, LOST PROFITS, LOSS OF INCOME, LOSS OF |
| 35 | +# BUSINESS ADVANTAGE OR UNAVAILABILITY, OR LOSS OR CORRUPTION OF |
| 36 | +# DATA. |
| 37 | +# |
| 38 | + |
| 39 | +import logging |
| 40 | +from pathlib import Path |
| 41 | +from typing import List, Any |
| 42 | + |
| 43 | +from docling.datamodel.document import ConversionResult |
| 44 | +from docling.document_converter import DocumentConverter |
| 45 | +from docling_core.transforms.chunker.hierarchical_chunker import HierarchicalChunker |
| 46 | +from docling_core.transforms.chunker.base import BaseChunk |
| 47 | +from docling_core.transforms.serializer.base import SerializationResult |
| 48 | +from docling_core.transforms.serializer.markdown import MarkdownDocSerializer |
| 49 | +from llama_index.core.schema import Document, TextNode, NodeRelationship |
| 50 | + |
| 51 | +from .base_reader import BaseReader |
| 52 | +from .base_reader import ChunksResult |
| 53 | +from .pdf import MarkdownSerializerProvider |
| 54 | + |
| 55 | +logger = logging.getLogger(__name__) |
| 56 | + |
| 57 | +class DoclingReader(BaseReader): |
| 58 | + def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 59 | + super().__init__(*args, **kwargs) |
| 60 | + |
| 61 | + def load_chunks(self, file_path: Path) -> ChunksResult: |
| 62 | + document = Document() |
| 63 | + document.id_ = self.document_id |
| 64 | + self._add_document_metadata(document, file_path) |
| 65 | + parent = document.as_related_node_info() |
| 66 | + |
| 67 | + converted_chunks: List[TextNode] = [] |
| 68 | + logger.debug(f"{file_path=}") |
| 69 | + docling_doc: ConversionResult = DocumentConverter().convert(file_path) |
| 70 | + chunky_chunks = HierarchicalChunker(serializer_provider=MarkdownSerializerProvider()).chunk(docling_doc.document) |
| 71 | + chunky_chunk: BaseChunk |
| 72 | + serializer = MarkdownDocSerializer(doc=docling_doc.document) |
| 73 | + for i, chunky_chunk in enumerate(chunky_chunks): |
| 74 | + text = "" |
| 75 | + page_number: int = 0 |
| 76 | + if not hasattr(chunky_chunk.meta, "doc_items"): |
| 77 | + logger.warning(f"Chunk {i} is empty, skipping") |
| 78 | + continue |
| 79 | + for item in chunky_chunk.meta.doc_items: |
| 80 | + page_number= item.prov[0].page_no if item.prov else None |
| 81 | + item_ser: SerializationResult = serializer.serialize(item=item) |
| 82 | + text += item_ser.text |
| 83 | + node = TextNode(text=text) |
| 84 | + if page_number: |
| 85 | + node.metadata["page_number"] = page_number |
| 86 | + node.metadata["file_name"] = document.metadata["file_name"] |
| 87 | + node.metadata["document_id"] = document.metadata["document_id"] |
| 88 | + node.metadata["data_source_id"] = document.metadata["data_source_id"] |
| 89 | + node.metadata["chunk_number"] = i |
| 90 | + node.metadata["chunk_format"] = "markdown" |
| 91 | + node.relationships.update( |
| 92 | + {NodeRelationship.SOURCE: parent} |
| 93 | + ) |
| 94 | + converted_chunks.append(node) |
| 95 | + return ChunksResult(converted_chunks) |
0 commit comments