Package: unstructured-inference
Open-Source Pre-Processing Tools for Unstructured DataTheunstructured-inferencerepo contains hosted model inference code for layout parsing models.
These models are invoked via API as part of the partitioning bricks in theunstructuredpackage.InstallationPackageRunpip install unstructured-inference.Detectron2Detectron2is required for using models from thelayoutparser model zoobut is not automatically installed with this package.
For MacOS and Linux, build from source with:pipinstall'git+https://github.com/facebookresearch/detectron2.git@57bdb21249d5418c130d54e2ebdc94dda7a4c01a'Other install options can be found in theDetectron2 installation guide.Windows is not officially supported by Detectron2, but some users are able to install it anyway.
See discussionherefor
tips on installing Detectron2 on Windows.RepositoryTo install the repository for development, clone the repo and runmake installto install dependencies.
Runmake helpfor a full list of install options.Getting StartedTo get started with the layout parsing model, use the following commands:fromunstructured_inference.inference.layoutimportDocumentLayoutlayout=DocumentLayout.from_file("sample-docs/loremipsum.pdf")print(layout.pages[0].elements)Once the model has detected the layout and OCR'd the document, the text extracted from the first
page of the sample document will be displayed.
You can convert a given element to adictby running the.to_dict()method.ModelsThe inference pipeline operates by finding text elements in a document page using a detection model, then extracting the contents of the elements using direct extraction (if available), OCR, and optionally table inference models.We offer several detection models includingDetectron2andYOLOX.Using a non-default modelWhen doing inference, an alternate model can be used by passing the model object to the ingestion method via themodelparameter. Theget_modelfunction can be used to construct one of our out-of-the-box models from a keyword, e.g.:fromunstructured_inference.models.baseimportget_modelfromunstructured_inference.inference.layoutimportDocumentLayoutmodel=get_model("yolox")layout=DocumentLayout.from_file("sample-docs/layout-parser-paper.pdf",detection_model=model)Using models from the layoutparser model zooTheUnstructuredDetectronModelclass inunstructured_inference.modelts.detectron2uses thefaster_rcnn_R_50_FPN_3xmodel pretrained on DocLayNet, but by using different construction parameters, any model in thelayoutparsermodel zoocan be used.UnstructuredDetectronModelis a light wrapper around thelayoutparserDetectron2LayoutModelobject, and accepts the same arguments. Seelayoutparser documentationfor details.Using your own modelAny detection model can be used for in theunstructured_inferencepipeline by wrapping the model in theUnstructuredObjectDetectionModelclass. To integrate with theDocumentLayoutclass, a subclass ofUnstructuredObjectDetectionModelmust have apredictmethod that accepts aPIL.Image.Imageand returns a list ofLayoutElements, and aninitializemethod, which loads the model and prepares it for inference.Security PolicySee oursecurity policyfor
information on how to report security vulnerabilities.Learn moreSectionDescriptionUnstructured Community GithubInformation about Unstructured.io community projectsUnstructured GithubUnstructured.io open source repositoriesCompany WebsiteUnstructured.io product and company info