sparknlp.annotator.cv.dolphin_for_document_parsing#
Contains classes concerning DolphinForDocumentParsing.
Module Contents#
Classes#
Document image parsing with ByteDance Dolphin. |
- class DolphinForDocumentParsing(classname='com.johnsnowlabs.nlp.annotators.cv.DolphinForDocumentParsing', java_model=None)[source]#
Document image parsing with ByteDance Dolphin.
Dolphin follows an analyze-then-parse design: stage 1 generates the page’s layout elements in reading order, then each element is cropped and parsed independently in stage 2, batched by type. Tables come back as HTML, formulas as LaTeX, text as markdown.
Dolphin 1.5 and 1.0 load the same way. The release is recognised from its stage-1 output, which decides the box geometry and whether formulas and code get their own prompts (1.5 only).
Pretrained models can be loaded with
pretrained()of the companion object:>>> dolphin = DolphinForDocumentParsing.pretrained() \ ... .setInputCols(["image_assembler"]) \ ... .setOutputCol("elements")
The default model is
"dolphin_1_5".Input Annotation types
Output Annotation type
IMAGEDOCUMENT- Parameters:
- parsingMode
What the input image is.
page(default) runs both stages on a whole page,layoutruns stage 1 only, andtable,text,formulaorcoderun stage 2 on an already cropped region.formulaandcodeare Dolphin 1.5 prompts.- outputFormat
elements(default) emits one annotation per document element carryingdolphinLabel,elementType,readingOrder,readingGroupandbboxNormalizedmetadata, plusbboxin pixels inpageandlayoutmodes.bboxNormalizedis that box as fractions of the image’s width and height.markdownemits a single assembled document per page, withelementCountandlayoutRaw. An image that fails to parse yields one empty annotation with anexceptionkey.- elementBatchSize
Element crops decoded per forward pass, default 4.
- maxOutputLength
Maximum tokens generated per element, default 4096. Generation always stops at the decoder’s 4096-position window, prompt included.
- layoutMaxOutputLength
Maximum tokens generated for the layout pass, default 4096.
- repetitionPenalty
Repetition penalty, default 1.0 (off) as in Dolphin 1.5’s reference pipeline; Dolphin 1.0’s used 1.1.
- customPrompt
Overrides the prompt in the
table,text,formulaandcodemodes.- adjustBoxEdges
Dolphin 1.0 only: widen its boxes so their edges do not cut through ink, default True. 1.5 boxes are used as generated.
- minElementSize
Page mode drops elements whose crop width or height is this many pixels or fewer, default 3, as the reference does.
Notes
The task comes from
parsingMode, not from the image annotation’stextfield.Reader2Imagedefaults itspromptTemplatetoqwen2vl-chat, and that markup would otherwise be fed to Dolphin as a task prompt.Examples
>>> import sparknlp >>> from sparknlp.base import * >>> from sparknlp.annotator import * >>> from pyspark.ml import Pipeline >>> imageDF = spark.read.format("image").load(path="pages/") >>> imageAssembler = ImageAssembler() \ ... .setInputCol("image") \ ... .setOutputCol("image_assembler") >>> dolphin = DolphinForDocumentParsing.pretrained() \ ... .setInputCols("image_assembler") \ ... .setOutputCol("elements") \ ... .setParsingMode("page") >>> pipeline = Pipeline().setStages([imageAssembler, dolphin]) >>> result = pipeline.fit(imageDF).transform(imageDF) >>> result.selectExpr("explode(elements) as e") \ ... .selectExpr("e.metadata['dolphinLabel'] as label", "e.result") \ ... .show(truncate=60)
- static loadSavedModel(folder, spark_session)[source]#
Loads a locally saved model.
- Parameters:
- folderstr
Folder of the saved model
- spark_sessionpyspark.sql.SparkSession
The current SparkSession
- Returns:
- DolphinForDocumentParsing
The restored model
- static pretrained(name='dolphin_1_5', lang='en', remote_loc=None)[source]#
Downloads and loads a pretrained model.
- Parameters:
- namestr, optional
Name of the pretrained model, by default “dolphin_1_5”
- langstr, optional
Language of the pretrained model, by default “en”
- remote_locstr, optional
Optional remote address of the resource, by default None. Will use Spark NLPs repositories otherwise.
- Returns:
- DolphinForDocumentParsing
The restored model