Skip to content

Commit 5d4a256

Browse files
committed
Add default value for properties
1 parent ee4784d commit 5d4a256

File tree

3 files changed

+107
-85
lines changed

3 files changed

+107
-85
lines changed

docs/docs/integrations/document_loaders/pymupdf.ipynb

Lines changed: 91 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
{
4343
"metadata": {
4444
"ExecuteTime": {
45-
"end_time": "2025-01-16T07:13:01.204685Z",
46-
"start_time": "2025-01-16T07:13:01.202180Z"
45+
"end_time": "2025-01-16T09:48:32.087401Z",
46+
"start_time": "2025-01-16T09:48:32.084843Z"
4747
}
4848
},
4949
"cell_type": "code",
@@ -67,8 +67,8 @@
6767
"cell_type": "code",
6868
"metadata": {
6969
"ExecuteTime": {
70-
"end_time": "2025-01-16T07:13:04.314631Z",
71-
"start_time": "2025-01-16T07:13:02.540177Z"
70+
"end_time": "2025-01-16T09:48:34.720803Z",
71+
"start_time": "2025-01-16T09:48:33.057015Z"
7272
}
7373
},
7474
"source": "%pip install -qU langchain_community pymupdf",
@@ -96,8 +96,8 @@
9696
"cell_type": "code",
9797
"metadata": {
9898
"ExecuteTime": {
99-
"end_time": "2025-01-16T07:13:06.238762Z",
100-
"start_time": "2025-01-16T07:13:05.543744Z"
99+
"end_time": "2025-01-16T09:48:36.618850Z",
100+
"start_time": "2025-01-16T09:48:35.787958Z"
101101
}
102102
},
103103
"source": [
@@ -120,8 +120,8 @@
120120
"cell_type": "code",
121121
"metadata": {
122122
"ExecuteTime": {
123-
"end_time": "2025-01-16T07:13:07.522535Z",
124-
"start_time": "2025-01-16T07:13:07.378088Z"
123+
"end_time": "2025-01-16T09:48:37.650774Z",
124+
"start_time": "2025-01-16T09:48:37.492137Z"
125125
}
126126
},
127127
"source": [
@@ -132,7 +132,7 @@
132132
{
133133
"data": {
134134
"text/plain": [
135-
"Document(metadata={'source': './example_data/layout-parser-paper.pdf', 'file_path': './example_data/layout-parser-paper.pdf', 'total_pages': 16, 'format': 'PDF 1.5', 'title': '', 'author': '', 'subject': '', 'keywords': '', 'creator': 'LaTeX with hyperref', 'producer': 'pdfTeX-1.40.21', 'creationdate': '2021-06-22T01:27:10+00:00', 'moddate': '2021-06-22T01:27:10+00:00', 'trapped': '', 'page': 0}, page_content='LayoutParser: A Unified Toolkit for Deep\\nLearning Based Document Image Analysis\\nZejiang Shen1 (\\x00), Ruochen Zhang2, Melissa Dell3, Benjamin Charles Germain\\nLee4, Jacob Carlson3, and Weining Li5\\n1 Allen Institute for AI\\[email protected]\\n2 Brown University\\nruochen [email protected]\\n3 Harvard University\\n{melissadell,jacob carlson}@fas.harvard.edu\\n4 University of Washington\\[email protected]\\n5 University of Waterloo\\[email protected]\\nAbstract. Recent advances in document image analysis (DIA) have been\\nprimarily driven by the application of neural networks. Ideally, research\\noutcomes could be easily deployed in production and extended for further\\ninvestigation. However, various factors like loosely organized codebases\\nand sophisticated model configurations complicate the easy reuse of im-\\nportant innovations by a wide audience. Though there have been on-going\\nefforts to improve reusability and simplify deep learning (DL) model\\ndevelopment in disciplines like natural language processing and computer\\nvision, none of them are optimized for challenges in the domain of DIA.\\nThis represents a major gap in the existing toolkit, as DIA is central to\\nacademic research across a wide range of disciplines in the social sciences\\nand humanities. This paper introduces LayoutParser, an open-source\\nlibrary for streamlining the usage of DL in DIA research and applica-\\ntions. The core LayoutParser library comes with a set of simple and\\nintuitive interfaces for applying and customizing DL models for layout de-\\ntection, character recognition, and many other document processing tasks.\\nTo promote extensibility, LayoutParser also incorporates a community\\nplatform for sharing both pre-trained models and full document digiti-\\nzation pipelines. We demonstrate that LayoutParser is helpful for both\\nlightweight and large-scale digitization pipelines in real-word use cases.\\nThe library is publicly available at https://layout-parser.github.io.\\nKeywords: Document Image Analysis · Deep Learning · Layout Analysis\\n· Character Recognition · Open Source library · Toolkit.\\n1\\nIntroduction\\nDeep Learning(DL)-based approaches are the state-of-the-art for a wide range of\\ndocument image analysis (DIA) tasks including document image classification [11,\\narXiv:2103.15348v2 [cs.CV] 21 Jun 2021')"
135+
"Document(metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2021-06-22T01:27:10+00:00', 'source': './example_data/layout-parser-paper.pdf', 'file_path': './example_data/layout-parser-paper.pdf', 'total_pages': 16, 'format': 'PDF 1.5', 'title': '', 'author': '', 'subject': '', 'keywords': '', 'moddate': '2021-06-22T01:27:10+00:00', 'trapped': '', 'page': 0}, page_content='LayoutParser: A Unified Toolkit for Deep\\nLearning Based Document Image Analysis\\nZejiang Shen1 (\\x00), Ruochen Zhang2, Melissa Dell3, Benjamin Charles Germain\\nLee4, Jacob Carlson3, and Weining Li5\\n1 Allen Institute for AI\\[email protected]\\n2 Brown University\\nruochen [email protected]\\n3 Harvard University\\n{melissadell,jacob carlson}@fas.harvard.edu\\n4 University of Washington\\[email protected]\\n5 University of Waterloo\\[email protected]\\nAbstract. Recent advances in document image analysis (DIA) have been\\nprimarily driven by the application of neural networks. Ideally, research\\noutcomes could be easily deployed in production and extended for further\\ninvestigation. However, various factors like loosely organized codebases\\nand sophisticated model configurations complicate the easy reuse of im-\\nportant innovations by a wide audience. Though there have been on-going\\nefforts to improve reusability and simplify deep learning (DL) model\\ndevelopment in disciplines like natural language processing and computer\\nvision, none of them are optimized for challenges in the domain of DIA.\\nThis represents a major gap in the existing toolkit, as DIA is central to\\nacademic research across a wide range of disciplines in the social sciences\\nand humanities. This paper introduces LayoutParser, an open-source\\nlibrary for streamlining the usage of DL in DIA research and applica-\\ntions. The core LayoutParser library comes with a set of simple and\\nintuitive interfaces for applying and customizing DL models for layout de-\\ntection, character recognition, and many other document processing tasks.\\nTo promote extensibility, LayoutParser also incorporates a community\\nplatform for sharing both pre-trained models and full document digiti-\\nzation pipelines. We demonstrate that LayoutParser is helpful for both\\nlightweight and large-scale digitization pipelines in real-word use cases.\\nThe library is publicly available at https://layout-parser.github.io.\\nKeywords: Document Image Analysis · Deep Learning · Layout Analysis\\n· Character Recognition · Open Source library · Toolkit.\\n1\\nIntroduction\\nDeep Learning(DL)-based approaches are the state-of-the-art for a wide range of\\ndocument image analysis (DIA) tasks including document image classification [11,\\narXiv:2103.15348v2 [cs.CV] 21 Jun 2021')"
136136
]
137137
},
138138
"execution_count": 4,
@@ -146,8 +146,8 @@
146146
"cell_type": "code",
147147
"metadata": {
148148
"ExecuteTime": {
149-
"end_time": "2025-01-16T07:13:08.814226Z",
150-
"start_time": "2025-01-16T07:13:08.810836Z"
149+
"end_time": "2025-01-16T09:48:38.072178Z",
150+
"start_time": "2025-01-16T09:48:38.069508Z"
151151
}
152152
},
153153
"source": [
@@ -160,17 +160,17 @@
160160
"name": "stdout",
161161
"output_type": "stream",
162162
"text": [
163-
"{'source': './example_data/layout-parser-paper.pdf',\n",
163+
"{'producer': 'pdfTeX-1.40.21',\n",
164+
" 'creator': 'LaTeX with hyperref',\n",
165+
" 'creationdate': '2021-06-22T01:27:10+00:00',\n",
166+
" 'source': './example_data/layout-parser-paper.pdf',\n",
164167
" 'file_path': './example_data/layout-parser-paper.pdf',\n",
165168
" 'total_pages': 16,\n",
166169
" 'format': 'PDF 1.5',\n",
167170
" 'title': '',\n",
168171
" 'author': '',\n",
169172
" 'subject': '',\n",
170173
" 'keywords': '',\n",
171-
" 'creator': 'LaTeX with hyperref',\n",
172-
" 'producer': 'pdfTeX-1.40.21',\n",
173-
" 'creationdate': '2021-06-22T01:27:10+00:00',\n",
174174
" 'moddate': '2021-06-22T01:27:10+00:00',\n",
175175
" 'trapped': '',\n",
176176
" 'page': 0}\n"
@@ -190,8 +190,8 @@
190190
"cell_type": "code",
191191
"metadata": {
192192
"ExecuteTime": {
193-
"end_time": "2025-01-16T07:13:09.863650Z",
194-
"start_time": "2025-01-16T07:13:09.813260Z"
193+
"end_time": "2025-01-16T09:48:39.349546Z",
194+
"start_time": "2025-01-16T09:48:39.295384Z"
195195
}
196196
},
197197
"source": [
@@ -223,8 +223,8 @@
223223
"cell_type": "code",
224224
"metadata": {
225225
"ExecuteTime": {
226-
"end_time": "2025-01-16T07:13:10.355177Z",
227-
"start_time": "2025-01-16T07:13:10.351604Z"
226+
"end_time": "2025-01-16T09:48:39.991257Z",
227+
"start_time": "2025-01-16T09:48:39.987732Z"
228228
}
229229
},
230230
"source": [
@@ -239,17 +239,17 @@
239239
"LayoutParser: A Unified Toolkit for DL-Based DIA\n",
240240
"11\n",
241241
"focuses on precision, efficiency, and robustness. T\n",
242-
"{'source': './example_data/layout-parser-paper.pdf',\n",
242+
"{'producer': 'pdfTeX-1.40.21',\n",
243+
" 'creator': 'LaTeX with hyperref',\n",
244+
" 'creationdate': '2021-06-22T01:27:10+00:00',\n",
245+
" 'source': './example_data/layout-parser-paper.pdf',\n",
243246
" 'file_path': './example_data/layout-parser-paper.pdf',\n",
244247
" 'total_pages': 16,\n",
245248
" 'format': 'PDF 1.5',\n",
246249
" 'title': '',\n",
247250
" 'author': '',\n",
248251
" 'subject': '',\n",
249252
" 'keywords': '',\n",
250-
" 'creator': 'LaTeX with hyperref',\n",
251-
" 'producer': 'pdfTeX-1.40.21',\n",
252-
" 'creationdate': '2021-06-22T01:27:10+00:00',\n",
253253
" 'moddate': '2021-06-22T01:27:10+00:00',\n",
254254
" 'trapped': '',\n",
255255
" 'page': 10}\n"
@@ -303,8 +303,8 @@
303303
"cell_type": "code",
304304
"metadata": {
305305
"ExecuteTime": {
306-
"end_time": "2025-01-16T07:13:13.015263Z",
307-
"start_time": "2025-01-16T07:13:12.962686Z"
306+
"end_time": "2025-01-16T09:48:43.180738Z",
307+
"start_time": "2025-01-16T09:48:43.132909Z"
308308
}
309309
},
310310
"source": [
@@ -322,17 +322,17 @@
322322
"output_type": "stream",
323323
"text": [
324324
"16\n",
325-
"{'source': './example_data/layout-parser-paper.pdf',\n",
325+
"{'producer': 'pdfTeX-1.40.21',\n",
326+
" 'creator': 'LaTeX with hyperref',\n",
327+
" 'creationdate': '2021-06-22T01:27:10+00:00',\n",
328+
" 'source': './example_data/layout-parser-paper.pdf',\n",
326329
" 'file_path': './example_data/layout-parser-paper.pdf',\n",
327330
" 'total_pages': 16,\n",
328331
" 'format': 'PDF 1.5',\n",
329332
" 'title': '',\n",
330333
" 'author': '',\n",
331334
" 'subject': '',\n",
332335
" 'keywords': '',\n",
333-
" 'creator': 'LaTeX with hyperref',\n",
334-
" 'producer': 'pdfTeX-1.40.21',\n",
335-
" 'creationdate': '2021-06-22T01:27:10+00:00',\n",
336336
" 'moddate': '2021-06-22T01:27:10+00:00',\n",
337337
" 'trapped': '',\n",
338338
" 'page': 0}\n"
@@ -359,8 +359,8 @@
359359
"cell_type": "code",
360360
"metadata": {
361361
"ExecuteTime": {
362-
"end_time": "2025-01-16T07:13:15.907276Z",
363-
"start_time": "2025-01-16T07:13:15.852748Z"
362+
"end_time": "2025-01-16T09:48:45.358999Z",
363+
"start_time": "2025-01-16T09:48:45.305168Z"
364364
}
365365
},
366366
"source": [
@@ -378,17 +378,17 @@
378378
"output_type": "stream",
379379
"text": [
380380
"1\n",
381-
"{'source': './example_data/layout-parser-paper.pdf',\n",
381+
"{'producer': 'pdfTeX-1.40.21',\n",
382+
" 'creator': 'LaTeX with hyperref',\n",
383+
" 'creationdate': '2021-06-22T01:27:10+00:00',\n",
384+
" 'source': './example_data/layout-parser-paper.pdf',\n",
382385
" 'file_path': './example_data/layout-parser-paper.pdf',\n",
383386
" 'total_pages': 16,\n",
384387
" 'format': 'PDF 1.5',\n",
385388
" 'title': '',\n",
386389
" 'author': '',\n",
387390
" 'subject': '',\n",
388391
" 'keywords': '',\n",
389-
" 'creator': 'LaTeX with hyperref',\n",
390-
" 'producer': 'pdfTeX-1.40.21',\n",
391-
" 'creationdate': '2021-06-22T01:27:10+00:00',\n",
392392
" 'moddate': '2021-06-22T01:27:10+00:00',\n",
393393
" 'trapped': ''}\n"
394394
]
@@ -414,8 +414,8 @@
414414
"cell_type": "code",
415415
"metadata": {
416416
"ExecuteTime": {
417-
"end_time": "2025-01-16T07:13:18.026765Z",
418-
"start_time": "2025-01-16T07:13:17.976302Z"
417+
"end_time": "2025-01-16T09:48:47.435722Z",
418+
"start_time": "2025-01-16T09:48:47.389Z"
419419
}
420420
},
421421
"source": [
@@ -571,8 +571,8 @@
571571
"cell_type": "code",
572572
"metadata": {
573573
"ExecuteTime": {
574-
"end_time": "2025-01-16T07:13:22.172739Z",
575-
"start_time": "2025-01-16T07:13:20.671450Z"
574+
"end_time": "2025-01-16T09:48:52.381641Z",
575+
"start_time": "2025-01-16T09:48:50.979344Z"
576576
}
577577
},
578578
"source": [
@@ -592,8 +592,8 @@
592592
{
593593
"metadata": {
594594
"ExecuteTime": {
595-
"end_time": "2025-01-16T07:13:46.802768Z",
596-
"start_time": "2025-01-16T07:13:46.743696Z"
595+
"end_time": "2025-01-16T09:49:19.155886Z",
596+
"start_time": "2025-01-16T09:48:56.454277Z"
597597
}
598598
},
599599
"cell_type": "code",
@@ -607,45 +607,65 @@
607607
")\n",
608608
"docs = loader.load()\n",
609609
"\n",
610-
"print(docs[5].page_content)"
610+
"print(docs[6].page_content)"
611611
],
612612
"outputs": [
613613
{
614614
"name": "stdout",
615615
"output_type": "stream",
616616
"text": [
617-
"6\n",
618-
"Z. Shen et al.\n",
619-
"Fig. 2: The relationship between the three types of layout data structures.\n",
620-
"Coordinate supports three kinds of variation; TextBlock consists of the co-\n",
621-
"ordinate information and extra features like block text, types, and reading orders;\n",
622-
"a Layout object is a list of all possible layout elements, including other Layout\n",
623-
"objects. They all support the same set of transformation and operation APIs for\n",
624-
"maximum flexibility.\n",
625-
"Shown in Table 1, LayoutParser currently hosts 9 pre-trained models trained\n",
626-
"on 5 different datasets. Description of the training dataset is provided alongside\n",
627-
"with the trained models such that users can quickly identify the most suitable\n",
628-
"models for their tasks. Additionally, when such a model is not readily available,\n",
629-
"LayoutParser also supports training customized layout models and community\n",
630-
"sharing of the models (detailed in Section 3.5).\n",
631-
"3.2\n",
632-
"Layout Data Structures\n",
633-
"A critical feature of LayoutParser is the implementation of a series of data\n",
634-
"structures and operations that can be used to efficiently process and manipulate\n",
635-
"the layout elements. In document image analysis pipelines, various post-processing\n",
636-
"on the layout analysis model outputs is usually required to obtain the final\n",
637-
"outputs. Traditionally, this requires exporting DL model outputs and then loading\n",
638-
"the results into other pipelines. All model outputs from LayoutParser will be\n",
639-
"stored in carefully engineered data types optimized for further processing, which\n",
640-
"makes it possible to build an end-to-end document digitization pipeline within\n",
641-
"LayoutParser. There are three key components in the data structure, namely\n",
642-
"the Coordinate system, the TextBlock, and the Layout. They provide different\n",
643-
"levels of abstraction for the layout data, and a set of APIs are supported for\n",
644-
"transformations or operations on these classes.\n"
617+
"LayoutParser: A Unified Toolkit for DL-Based DIA\n",
618+
"7\n",
619+
"Coordinates are the cornerstones for storing layout information. Currently,\n",
620+
"three types of Coordinate data structures are provided in LayoutParser, shown\n",
621+
"in Figure 2. Interval and Rectangle are the most common data types and\n",
622+
"support specifying 1D or 2D regions within a document. They are parameterized\n",
623+
"with 2 and 4 parameters. A Quadrilateral class is also implemented to support\n",
624+
"a more generalized representation of rectangular regions when the document\n",
625+
"is skewed or distorted, where the 4 corner points can be specified and a total\n",
626+
"of 8 degrees of freedom are supported. A wide collection of transformations\n",
627+
"like shift, pad, and scale, and operations like intersect, union, and is_in,\n",
628+
"are supported for these classes. Notably, it is common to separate a segment\n",
629+
"of the image and analyze it individually. LayoutParser provides full support\n",
630+
"for this scenario via image cropping operations crop_image and coordinate\n",
631+
"transformations like relative_to and condition_on that transform coordinates\n",
632+
"to and from their relative representations. We refer readers to Table 2 for a more\n",
633+
"detailed description of these operations13.\n",
634+
"Based on Coordinates, we implement the TextBlock class that stores both\n",
635+
"the positional and extra features of individual layout elements. It also supports\n",
636+
"specifying the reading orders via setting the parent field to the index of the parent\n",
637+
"object. A Layout class is built that takes in a list of TextBlocks and supports\n",
638+
"processing the elements in batch. Layout can also be nested to support hierarchical\n",
639+
"layout structures. They support the same operations and transformations as the\n",
640+
"Coordinate classes, minimizing both learning and deployment effort.\n",
641+
"3.3\n",
642+
"OCR\n",
643+
"LayoutParser provides a unified interface for existing OCR tools. Though there\n",
644+
"are many OCR tools available, they are usually configured differently with distinct\n",
645+
"APIs or protocols for using them. It can be inefficient to add new OCR tools into\n",
646+
"an existing pipeline, and difficult to make direct comparisons among the available\n",
647+
"tools to find the best option for a particular project. To this end, LayoutParser\n",
648+
"builds a series of wrappers among existing OCR engines, and provides nearly\n",
649+
"the same syntax for using them. It supports a plug-and-play style of using OCR\n",
650+
"engines, making it effortless to switch, evaluate, and compare different OCR\n",
651+
"modules:\n",
652+
"1 ocr_agent = lp.TesseractAgent ()\n",
653+
"2 # Can be easily\n",
654+
"switched to other OCR\n",
655+
"software\n",
656+
"3 tokens = ocr_agent.detect(image)\n",
657+
"The OCR outputs will also be stored in the aforementioned layout data\n",
658+
"structures and can be seamlessly incorporated into the digitization pipeline.\n",
659+
"Currently LayoutParser supports the Tesseract and Google Cloud Vision OCR\n",
660+
"engines.\n",
661+
"LayoutParser also comes with a DL-based CNN-RNN OCR model [6] trained\n",
662+
"with the Connectionist Temporal Classification (CTC) loss [10]. It can be used\n",
663+
"like the other OCR modules, and can be easily trained on customized datasets.\n",
664+
"13 This is also available in the LayoutParser documentation pages.\n"
645665
]
646666
}
647667
],
648-
"execution_count": 13
668+
"execution_count": 12
649669
},
650670
{
651671
"cell_type": "markdown",

libs/community/langchain_community/document_loaders/parsers/pdf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,9 @@ def _extract_metadata(self, doc: pymupdf.Document, blob: Blob) -> dict:
655655
return _purge_metadata(
656656
dict(
657657
{
658+
"producer": "PyMuPDF",
659+
"creator": "PyMuPDF",
660+
"creationdate": "",
658661
"source": blob.source, # type: ignore[attr-defined]
659662
"file_path": blob.source, # type: ignore[attr-defined]
660663
"total_pages": len(doc),
@@ -679,7 +682,7 @@ def _extract_images_from_page(
679682
Returns:
680683
str: The extracted text from the images on the page.
681684
"""
682-
if not self.extract_images:
685+
if not self.images_parser:
683686
return ""
684687
import pymupdf
685688

0 commit comments

Comments
 (0)