File size: 5,393 Bytes
362a075 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import io
from pathlib import Path
from typing import Any, Dict, List, Optional, Protocol, Union
from haystack import Document, component, default_from_dict, default_to_dict, logging
from haystack.components.converters.utils import get_bytestream_from_source, normalize_metadata
from haystack.dataclasses import ByteStream
from haystack.lazy_imports import LazyImport
from haystack.utils.base_serialization import deserialize_class_instance, serialize_class_instance
with LazyImport("Run 'pip install pypdf'") as pypdf_import:
from pypdf import PdfReader
logger = logging.getLogger(__name__)
class PyPDFConverter(Protocol):
"""
A protocol that defines a converter which takes a PdfReader object and converts it into a Document object.
"""
def convert(self, reader: "PdfReader") -> Document: # noqa: D102
...
def to_dict(self): # noqa: D102
...
@classmethod
def from_dict(cls, data): # noqa: D102
...
@component
class PyPDFToDocument:
"""
Converts PDF files to documents your pipeline can query.
This component uses converters compatible with the PyPDF library.
If no converter is provided, uses a default text extraction converter.
You can attach metadata to the resulting documents.
### Usage example
```python
from haystack.components.converters.pypdf import PyPDFToDocument
converter = PyPDFToDocument()
results = converter.run(sources=["sample.pdf"], meta={"date_added": datetime.now().isoformat()})
documents = results["documents"]
print(documents[0].content)
# 'This is a text from the PDF file.'
```
"""
def __init__(self, converter: Optional[PyPDFConverter] = None):
"""
Create an PyPDFToDocument component.
:param converter:
An instance of a PyPDFConverter compatible class.
"""
pypdf_import.check()
self.converter = converter
def to_dict(self):
"""
Serializes the component to a dictionary.
:returns:
Dictionary with serialized data.
"""
return default_to_dict(
self, converter=(serialize_class_instance(self.converter) if self.converter is not None else None)
)
@classmethod
def from_dict(cls, data):
"""
Deserializes the component from a dictionary.
:param data:
Dictionary with serialized data.
:returns:
Deserialized component.
"""
init_parameters = data.get("init_parameters", {})
custom_converter_data = init_parameters.get("converter")
if custom_converter_data is not None:
data["init_parameters"]["converter"] = deserialize_class_instance(custom_converter_data)
return default_from_dict(cls, data)
def _default_convert(self, reader: "PdfReader") -> Document:
text = "\f".join(page.extract_text() for page in reader.pages)
return Document(content=text)
@component.output_types(documents=List[Document])
def run(
self,
sources: List[Union[str, Path, ByteStream]],
meta: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None,
):
"""
Converts PDF files to documents.
:param sources:
List of file paths or ByteStream objects to convert.
:param meta:
Optional metadata to attach to the documents.
This value can be a list of dictionaries or a single dictionary.
If it's a single dictionary, its content is added to the metadata of all produced documents.
If it's a list, its length must match the number of sources, as they are zipped together.
For ByteStream objects, their `meta` is added to the output documents.
:returns:
A dictionary with the following keys:
- `documents`: A list of converted documents.
"""
documents = []
meta_list = normalize_metadata(meta, sources_count=len(sources))
for source, metadata in zip(sources, meta_list):
try:
bytestream = get_bytestream_from_source(source)
except Exception as e:
logger.warning("Could not read {source}. Skipping it. Error: {error}", source=source, error=e)
continue
try:
pdf_reader = PdfReader(io.BytesIO(bytestream.data))
document = (
self._default_convert(pdf_reader) if self.converter is None else self.converter.convert(pdf_reader)
)
except Exception as e:
logger.warning(
"Could not read {source} and convert it to Document, skipping. {error}", source=source, error=e
)
continue
if document.content is None or document.content.strip() == "":
logger.warning(
"PyPDFToDocument could not extract text from the file {source}. Returning an empty document.",
source=source,
)
merged_metadata = {**bytestream.meta, **metadata}
document.meta = merged_metadata
documents.append(document)
return {"documents": documents}
|