Python library for document processing
Project description
Inkwell
Quickstart on Colab
Overview
Inkwell is a modular Python library for extracting information from PDF documents documents with state of the art Vision Language Models. We make use of layout understanding models to improve accuracy of Vision Language models.
Inkwell uses the following models, with more integrations in the work
- Layout Detection: Faster RCNN, LayoutLMv3, Paddle
- Table Detection: Table Transformer
- Table Data Extraction: Phi3.5-Vision, Qwen2 VL 2B, Table Transformer, OpenAI GPT4o Mini
- OCR: Tesseract, PaddleOCR, Phi3.5-Vision, Qwen2 VL 2B
Installation
pip install py-inkwell[inference]
In addition, install detectron2
pip install git+https://github.com/facebookresearch/detectron2.git
Install Tesseract
For Ubuntu -
sudo apt install tesseract-ocr
sudo apt install libtesseract-dev
and, Mac OS
brew install tesseract
For GPUs, install flash attention and vllm for faster inference.
pip install flash-attn --no-build-isolation
pip install vllm
Basic Usage
Parse Pages
from inkwell.pipeline import Pipeline
pipeline = Pipeline()
document = pipeline.process("/path/to/file.pdf")
Extract Page Elements
pages = document.pages
Every Page has the following fragment objects -
- Figures
- Tables
- Text
Figures
Each figure fragment's content has the following attributes -
- bbox - The bounding box of the figure
- text - The text in the figure, extracted using OCR
- image - The cropped image of the figure
figures = page.figure_fragments()
for figure in figures:
figure_image = figure.content.image
figure_bbox = figure.content.bbox
figure_text = figure.content.text
Table
Each table fragment's content has the following attributes -
- data - The data in the table, extracted using Table Extractor
- bbox - The bounding box of the table
- image - The image of the table, extracted using OCR
tables = page.table_fragments()
for table in tables:
table_data = table.content.data
table_bbox = table.content.bbox
table_image = table.content.image
Text
Each text fragment's content has the following attributes -
- text - The text in the text block
- bbox - The bounding box of the text block
- image - The image of the text block
text_blocks = page.text_fragments()
for text_block in text_blocks:
text_block_text = text_block.content.text
text_block_bbox = text_block.content.bbox
text_block_image = text_block.content.image
Complete Example
We will take the following PDF and extract text, tables and images from this separtely.
from inkwell.pipeline import Pipeline
pipeline = Pipeline()
document = pipeline.process("/path/to/file.pdf")
pages = document.pages
for page in pages:
figures = page.figure_fragments()
tables = page.table_fragments()
text_blocks = page.text_fragments()
# Check the content of the image fragments
for figure in figures:
figure_image = figure.content.image
figure_text = figure.content.text
# Check the content of the table fragments
for table in tables:
table_image = table.content.image
table_data = table.content.data
# Check the content of the text blocks
for text_block in text_blocks:
text_block_image = text_block.content.image
text_block_text = text_block.content.text
Using Qwen2/Phi3.5/OpenAI Vision Models
We have defined a default config class here. You can add vision-language models to the config to use them instead of the default models.
from inkwell.pipeline import DefaultPipelineConfig, Pipeline
from inkwell.ocr import OCRType
from inkwell.table_extractor import TableExtractorType
# using Qwen2 2B Vision OCR anf Table Extractor
config = DefaultPipelineConfig(
ocr_detector=OCRType.QWEN2_2B_VISION,
table_extractor=TableExtractorType.QWEN2_2B_VISION
)
# using Phi3.5 Vision OCR and Table Extractor
config = DefaultPipelineConfig(
ocr_detector=OCRType.PHI3_VISION,
table_extractor=TableExtractorType.PHI3_VISION
)
# using OpenAI GPT4o Mini OCR and Table Extractor (Requires API Key)
config = DefaultPipelineConfig(
ocr_detector=OCRType.OPENAI_GPT4O_MINI,
table_extractor=TableExtractorType.OPENAI_GPT4O_MINI
)
pipeline = Pipeline(config=config)
Advanced Customizations
You can add custom detectors and other components to the pipeline yourself - follow the instructions in the Custom Components notebook
Acknowledgements
We derived inspiration from several open-source libraries in our implementation, like Layout Parser and Deepdoctection. We would like to thank the contributors to these libraries for their work.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file py_inkwell-0.0.38.tar.gz
.
File metadata
- Download URL: py_inkwell-0.0.38.tar.gz
- Upload date:
- Size: 19.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a4fa4a5c0ac2f90a1f5bfdc8c0e92e111d78fe0a90d9cd61885f03273385f5d |
|
MD5 | ed3fc7f4bde05224b918cf70e48de182 |
|
BLAKE2b-256 | 4eb88cb81313f481aea1926cc2f60b9ce8f8935c8a54a54974220a575313e41c |
Provenance
The following attestation bundles were made for py_inkwell-0.0.38.tar.gz
:
Publisher:
publish_inkwell_pypi.yaml
on tensorlakeai/inkwell
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
py_inkwell-0.0.38.tar.gz
- Subject digest:
5a4fa4a5c0ac2f90a1f5bfdc8c0e92e111d78fe0a90d9cd61885f03273385f5d
- Sigstore transparency entry: 145671268
- Sigstore integration time:
- Predicate type:
File details
Details for the file py_inkwell-0.0.38-py3-none-any.whl
.
File metadata
- Download URL: py_inkwell-0.0.38-py3-none-any.whl
- Upload date:
- Size: 19.2 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b7e8f8f5952c505cdffd1a1c738fc4321536831d0666cc61d2ff1fe774d8e52 |
|
MD5 | 57566a4e19ae5764bda31544e01c6426 |
|
BLAKE2b-256 | cbd1d74631fc3d585258c184b2a99be3351149f4736dfa1881bac44743f47185 |
Provenance
The following attestation bundles were made for py_inkwell-0.0.38-py3-none-any.whl
:
Publisher:
publish_inkwell_pypi.yaml
on tensorlakeai/inkwell
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
py_inkwell-0.0.38-py3-none-any.whl
- Subject digest:
4b7e8f8f5952c505cdffd1a1c738fc4321536831d0666cc61d2ff1fe774d8e52
- Sigstore transparency entry: 145671271
- Sigstore integration time:
- Predicate type: