
    =pj8                    *   S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJ	r	  S SK
JrJrJrJrJrJr  S SKrS SKJr  SSKJr  SS	KJr  S
SKJr  S
SKJr  S
SKJrJr  S
SK J!r!  SSK"J#r#  SSK$J%r%  SSK&J'r'  SSK(J)r)  SSK*J+r+  SSK,J-r-J.r.  SSK/J0r0J1r1J2r2  SSK3J4r4J5r5J6r6J7r7J8r8J9r9J:r:J;r;J<r<  / SQr=\!R|                   " S S\%5      5       r? " S S\#5      r@\" S5       " S S \@5      5       rA\" S5       " S! S"\@5      5       rB\" S5       " S# S$\@5      5       rCg)%    )annotationsN)ThreadPoolExecutor)chain)AnyDictListOptionalTupleUnion)Image   )logging)pipeline_requires_extra   )ImageBatchSampler)	ReadImage)	HPIConfigPaddlePredictorOption)	benchmark   )(AutoParallelImageSimpleInferencePipeline)BasePipeline)CropByBoxes)merge_tables_across_pages)assign_levels_to_parsing_res)construct_img_pathgather_imgs   )
BaseResultPaddleOCRVLBlockPaddleOCRVLResult)	convert_otsl_to_htmlcrop_marginfilter_overlap_boxesmerge_blockspost_process_for_spottingpre_process_for_spottingtokenize_figure_of_tabletruncate_repetitive_contentuntokenize_figure_of_table)imageheader_imagefooter_imagec                     ^  \ rS rSrSrSSSSSSSS.                 S%U 4S jjjrS r S&                   S'S	 jjrS(S
 jr\	R                  " SS9S 5       r\	R                  " SS9S 5       r\	R                  " SS9S 5       rS r\	R                  " SS9S 5       rS rS r\	R                  " SS9S 5       r\	R                  " SS9S 5       r\	R                  " SS9S 5       r\	R                  " SS9S 5       r      S)S jr                       S*                                                 S+S  jjrS,S! jr   S-       S.S" jjr   S-       S/S# jjrS$rU =r$ )0_PaddleOCRVLPipeline7   z_PaddleOCRVLPipeline PipelineNFT)deviceengineengine_config	pp_optionuse_hpip
hpi_configinitial_predictorc          
       > [         TU ]  " S(UUUUUUS.U	D6  U(       Ga  UR                  SS5      U l        U R                  (       a:  UR                  S0 5      R                  SSS05      n
U R	                  U
5      U l        UR                  SS5      U l        U R                  (       a  UR                  S	0 5      R                  S
SS05      nUR                  SS5      nUb  US;   d   S5       e0 nUR                  SS5      =nb  XS'   UR                  SS5      =n b  XS'   UR                  SS5      =n b  UUS'   UR                  SS5      =n b  UUS'   U R                  " U40 UD6U l        UR                  SS5      U l	        UR                  SS5      U l
        UR                  S	0 5      R                  SSS05      nU R                  U5      U l        UR                  SS5      U l        UR                  SS5      U l        [        UR                  SS5      S9U l        [!        SS 9U l        [%        5       U l        UR                  S!S5      U l        UR                  S"S5      U l        UR                  S#/ S$Q5      U l        UR                  S%S&5      n [/        S&[1        U5      5      U l        gg! [4        [6        4 a  n[7        S'5      UeSnAff = f))a  Initializes the PaddleOCR-VL pipeline.

Args:
    config (Dict): Configuration dictionary containing various settings.
    device (Optional[str], optional): The device to use for prediction. Defaults to `None`.
    engine (Optional[str], optional): Inference engine. Defaults to `None`.
    engine_config (Optional[Dict[str, Any]], optional): Engine-specific config. Defaults to `None`.
    pp_option (Optional[PaddlePredictorOption], optional): Paddle predictor options.
        Defaults to `None`.
    use_hpip (bool, optional): Whether to use HPIP. Defaults to `False`.
    hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional):
        HPIP configuration. Defaults to `None`.
    initial_predictor (bool, optional): Whether to initialize predictors.
        Defaults to `True`.
)r1   r2   r3   r4   r5   r6   use_doc_preprocessorTSubPipelinesDocPreprocessorpipeline_config_errorz+config error for doc_preprocessor_pipeline!use_layout_detection
SubModulesLayoutDetectionmodel_config_errorz"config error for layout_det_model!
model_nameN)zPP-DocLayoutV2zPP-DocLayoutV3z3model_name must be PP-DocLayoutV2 or PP-DocLayoutV3	threshold
layout_nmslayout_unclip_ratiolayout_merge_bboxes_modeuse_chart_recognitionFuse_seal_recognitionVLRecognitionzconfig error for vl_rec_model!format_block_contentuse_ocr_for_image_block
batch_sizer   )rK   BGR)format
use_queuesmerge_layout_blocksmarkdown_ignore_labels)numberfootnoteheaderr,   footerr-   
aside_textlayout_prep_cpu_workersr   zf`layout_prep_cpu_workers` must be a non-negative integer (0 disables parallel CPU layout preparation). )super__init__getr9   create_pipelinedoc_preprocessor_pipeliner=   create_modellayout_det_modelrF   rG   vl_rec_modelrI   rJ   r   batch_samplerr   
img_readerr   crop_by_boxesrN   rO   rP   maxintrV   	TypeError
ValueError)selfconfigr1   r2   r3   r4   r5   r6   r7   kwargsdoc_preprocessor_configlayout_det_configrA   layout_kwargsrB   rC   rD   rE   vl_rec_configlpexc	__class__s                        m/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/paddlex/inference/pipelines/paddleocr_vl/pipeline.pyrY   _PaddleOCRVLPipeline.__init__;   sP   8 	 	
'!	
 	
 (.

3I4(PD%((*0**^R*H*L*L%/1^+' 261E1E+2. )/

3I4(PD%(($*JJ|R$@$D$D%)+OP%! /22<F
!-* A 3 I II  !#!2!6!6{D!IIIV1:+."3"7"7d"KKJ 3=,/+<+@+@-t, ' 	
 <OM"780A0E0E2D1 , 	
 AYM"<=(,(9(9%))6)% *04KU)SD&(.

3I5(QD%"JJ|R8<<%'GHM
 !% 1 1- @D(.

3I5(QD%+1::6OQV+WD(!2!::lA6"D (u5DO!,D$jju=DO'-zz2G'ND$*0**(+D' 5q9B/21c"g,e f z*  D s   J; ;KKKc                \    [        U S5      (       a  U R                  R                  5         g g )Nr_   )hasattrr_   closerg   s    rq   ru   _PaddleOCRVLPipeline.close   s&    4((##% )    c
                8   Uc  Uc  U R                   n
OUSL d  USL a  Sn
OSn
Uc  U R                  nUc  U R                  nUc  U R                  nUc  U R                  nUc  U R
                  nUc  U R                  nU	c  U R                  n	[        U
UUUUUUU	S9$ )a  
Get the model settings based on the provided parameters or default values.

Args:
    use_doc_orientation_classify (Union[bool, None]): Enables document orientation classification if True. Defaults to system setting if None.
    use_doc_unwarping (Union[bool, None]): Enables document unwarping if True. Defaults to system setting if None.

Returns:
    dict: A dictionary containing the model settings.

TF)r9   r=   rF   rG   rJ   rI   rO   rP   )	r9   r=   rF   rG   rJ   rI   rO   rP   dict)rg   use_doc_orientation_classifyuse_doc_unwarpingr=   rF   rG   rJ   rI   rO   rP   r9   s              rq   get_model_settings'_PaddleOCRVLPipeline.get_model_settings   s    . (/4E4M#'#<#< +t37HD7P'+$',$'#'#<#<  ($($>$>!'#'#<#< "*&*&B&B#'#'#<#< &"&":":!)%)%@%@"!5!5"7!5$;!5 3#9	
 		
rx   c                h    US   (       a(  U R                   (       d  [        R                  " S5        gg)a  
Check if the input parameters are valid based on the initialized models.

Args:
    input_params (Dict): A dictionary containing input parameters.

Returns:
    bool: True if all required models are initialized according to input parameters, False otherwise.
r9   zRSet use_doc_preprocessor, but the models for doc preprocessor are not initialized.FT)r9   r   error)rg   input_paramss     rq   check_model_settings_valid/_PaddleOCRVLPipeline.check_model_settings_valid   s,     ./8Q8QMMd rx   !paddleocr_vl_filter_overlap_boxes)namec                    [        X5      $ N)r$   )rg   layout_det_reslayout_shape_modes      rq   "_paddleocr_vl_filter_overlap_boxes7_PaddleOCRVLPipeline._paddleocr_vl_filter_overlap_boxes  s    #NFFrx    paddleocr_vl_crop_layout_regionsc                &    U R                  XU5      $ r   )rb   )rg   r+   boxesr   s       rq   !_paddleocr_vl_crop_layout_regions6_PaddleOCRVLPipeline._paddleocr_vl_crop_layout_regions  s    !!%0ABBrx   "paddleocr_vl_merge_adjacent_blocksc                <    US   (       d  U$ [        UUS   S/-   S9$ )NrO   image_labelstablenon_merge_labels)r%   )rg   blocks_for_imglayout_prep_cfgs      rq   #_paddleocr_vl_merge_adjacent_blocks8_PaddleOCRVLPipeline._paddleocr_vl_merge_adjacent_blocks  s0    45!!,^<yH
 	
rx   c           	     j   / nSn[        5       n[        U5       GH  u  pU	S   n
U	S   nXS   ;  d  M  U
c  M  0 nSnUS   nUS   n/ nUS:X  a  S	n[        XS
   U5      u  pnUS   nUS   nOUS:X  a  US   (       a  SnUS   nUS   nOxSU;   a<  US:w  a6  Sn[        U
5      nUR                  u  nnnUS:  a  US:  a  Un
US   nUS   nO6US:X  a  SnSnSnSn[        U
5      n
OUS:X  a  US   (       a  SnUS    nUS!   nUR                  UUU
UX4U45        UR                  U5        GM     XVU4$ )"NFimglabelr   zOCR:ocr_min_pixelsocr_max_pixelsr   zTable Recognition:boxtable_min_pixelstable_max_pixelschartrF   zChart Recognition:chart_min_pixelschart_max_pixelsformulaformula_numberzFormula Recognition:r   formula_min_pixelsformula_max_pixelsspottingz	Spotting:T  i  sealrG   zSeal Recognition:seal_min_pixelsseal_max_pixels)set	enumerater(   r#   shaper'   appendupdate)rg   page_idxr   imgs_in_doc_for_imgr   page_vlm_entriespage_has_spottingpage_drop_figuresjblock	block_imgblock_labelfigure_token_maptext_promptblk_min_pixelsblk_max_pixelsdrop_figurescrop_imgwh_s                        rq   +_paddleocr_vl_collect_page_vlm_entries_core@_PaddleOCRVLPipeline._paddleocr_vl_collect_page_vlm_entries_core!  s    !E!.1HAeI.K>#BB)#% $!01A!B!01A!B!')"6K0%U|5H >I
 &55G%HN%45G%HN7*?V/W"6K%45G%HN%45G%HN+-+AQ2Q"8K*95H&nnGAq!1uQ$,	%45I%JN%45I%JN J."-K(,%%+N%,N 8 CI F*?U/V"5K%45F%GN%45F%GN '' !#'8(	 "((6q 2t  4EEErx   %paddleocr_vl_collect_block_vlm_inputsc                *    U R                  UUUU5      $ r   )r   )rg   r   r   r   r   s        rq   &_paddleocr_vl_collect_block_vlm_inputs;_PaddleOCRVLPipeline._paddleocr_vl_collect_block_vlm_inputsh  s#     ??	
 	
rx   c                    Uu  p#pEn[        XFS   5      nUS   nU R                  X7US   5      nUS   (       a  [        UUS   S/-   S9nU R                  X(XV5      u  n	n
nUUU	U
U4$ )uX   Filter → crop → merge → build VLM inputs (safe for thread pool; no nested timers).r   r   rO   r   r   r   )r$   rb   r%   r   rg   payloadir+   r   r   r   r   r   r   r   r   s               rq   _paddleocr_vl_prepare_page_core4_PaddleOCRVLPipeline._paddleocr_vl_prepare_page_corew  s    IPF.-,?@
 w'++/*=>
 01)!0!@G9!LN <<2
		

 
 	
rx   c                    Uu  nnnnnU R                  XFS   5      nUS   nU R                  X7US   5      nU R                  X5      nU R                  UUUU5      u  n	n
nUUU	U
U4$ )Nr   r   )r   r   r   r   r   s               rq   -_paddleocr_vl_prepare_page_serial_benchmarkedB_PaddleOCRVLPipeline._paddleocr_vl_prepare_page_serial_benchmarked  s     	
@@,?@
 w'??/*=>
 AA
 77	
		
 
 	
rx   !paddleocr_vl_layout_prep_parallelc                    [        US9 n[        UR                  U R                  U5      5      sS S S 5        $ ! , (       d  f       g = f)N)max_workers)r   listmapr   )rg   page_payloadsr   cpu_pools       rq   (_paddleocr_vl_layout_prep_parallel_pages=_PaddleOCRVLPipeline._paddleocr_vl_layout_prep_parallel_pages  s4    K8HTAA=Q 988s	   %9
A"paddleocr_vl_aggregate_vlm_batchesc                   / nSn[        5       n0 n0 nU H  u  nnn	n
nUR                  U5        U
(       a  SnUR                  U5        U	 H~  u  nnnnnnUU;  a  / / / / SS.UU'   UU   S   R                  U5        UU   S   R                  U5        UU   S   R                  U5        UU   S   R                  X45        UXlU4'   M     M     UUUUU4$ )	NFTr   )imagesqueriesfigure_token_mapsvlm_block_idscurr_vlm_block_idxr   r   r   r   )r   r   r   )rg   page_resultsblockshas_spottingdrop_figures_setbatch_dict_by_pixelid2pixel_key_mapr   r   r   r   r   r   r   r   r   	pixel_keyr   s                     rq   #_paddleocr_vl_aggregate_vlm_batches8_PaddleOCRVLPipeline._paddleocr_vl_aggregate_vlm_batches  s9   5  
MM.) ###$56 " $77"$#%-/)+./6'	2 $I.x8??	J#I.y9@@M#I./BCJJ$ $I.?FFvN+4 Q( " < 
 	
rx   'paddleocr_vl_run_vl_recognition_batchesc                H   Uc  0 nOUR                  SS 5      c  SUS'   U Hx  nUu  pVSUUS.UEnX   S   nX   S   n	[        U R                  R                  " [	        X5       V
Vs/ s H
  u  pU
US.PM     snn
4SU(       a  S	OS0UD65      nAA	XU   S
'   Mz     g s  snn
f )Nmax_new_tokensi   T)	use_cache
min_pixels
max_pixelsr   r   )r+   queryskip_special_tokensFvlm_results)rZ   r   r_   predictzip)rg   r   r   
vlm_kwargsr   min_pxmax_pxri   	pv_imagesr   r+   r   batch_resultss                rq   (_paddleocr_vl_run_vl_recognition_batches=_PaddleOCRVLPipeline._paddleocr_vl_run_vl_recognition_batches  s     J^^,d3;+/J'(,I&NF!$$ 	F ,6x@I)4Y?G !!)) -0	,C
 -DLE &+%* -D
 2>4
 
M 7<I	*=91 -s   'B%paddleocr_vl_assemble_parsing_resultsc                   / n/ n/ n0 n	/ n
[        U5       GH  u  p/ n/ n0 n[        U5       GHP  u  nnUS   nUS   nUS   nSn0 nUU4U;   Gak  X;U4   nUU   nUS   nU[        US   5      :  a  US   U   UU4:X  d   eUS   U   nUS   U   nUS	   U   nUS
-  nUUS'   UUS'   UR                  SS5      nUc  SnUS:X  a  SOSn[        UUS9nSU;   a  SU;   d  SU;   a  SU;   a  UR	                  SS5      nUR	                  SS5      R	                  SS5      R	                  SS5      R	                  SS5      R	                  SS5      R	                  SS5      nUS:X  a  UR	                  SS5      nUS:X  a  [        U5      nUS:w  a  UnUS:X  a"  UR                  S S u  nn [        UU U5      u  nnUn[        UUUUR                  SS 5      UR                  SS 5      S 9n!US:X  a  U
R                  UU!S!.5        UU;   ab  Ub_  [        US   US   5      n"U!U	U"'   U"U;  a?  S"S Kn#U#R                  UU#R                  5      nU"[        R                  " U5      S#.U!l        OGM=  UR                  U!5        A!AGMS     U
 H)  n$U$S$   nU$S%   n[#        UR$                  UU	5      Ul        M+     UR                  U5        UR                  U5        UR                  U5        AAAGM     XgU4$ )&Nr   r   r    r   r   r   r   r   r   r+   resultr   i  2   )	min_countz\(z\)z\[z\]$z $ z $z\[\[z\]\]z $$ r   r   r   group_idpolygon_points)r   bboxcontentr  r  )r   r   r   pathr   r   r   )r   lenrZ   r)   replacer"   r   r&   r    r   r   cv2cvtColorCOLOR_BGR2RGBr   	fromarrayr+   r*   r
  )%rg   r   r   r   r   vis_image_labelsparsing_res_liststable_res_listsspotting_res_listimage_path_to_obj_maptable_blocksr   r   parsing_res_listtable_res_listspotting_resr   r   r   
block_bboxr   block_contentr   r   
pixel_infor   vl_rec_resultblock_img4vl
result_strr  html_strr   r   
block_infoimg_pathr  blk_infos%                                        rq   &_paddleocr_vl_assemble_parsing_results;_PaddleOCRVLPipeline._paddleocr_vl_assemble_parsing_results  s     "!*6!2A!NL%n55!%L	"5\
#Gn "#% q6-- 0Q 8I!4Y!?J)34H)I&-"?31 $_56HIaQRVST T %/}$=>P$QM#-h#78J#KL'12E'F*($ '!+&7IJ34-9M'*!.!2!28R!@J!)%'
(3w(>BI!<"i"J +0C+0C%/%7%7R%@
 '..ue<$WUD1$WXu5$WXu5$WUF3$WUF3 # '*::)3););C)DJ"g-#7
#C#r>)1J"j0(r213L&140
L %/M-%#)"YYz48#(99-=t#D
 ') ''0@%/ "22y7L1%.%,OH6@)(3'77"$'LLC<M<M$N	$,#(??9#=,
(
 ! ''
3	g 6h ) )#+,>#?  :MM#35J! ) $$%56"">2$$\2 .,C "3F !3DDDrx   c
                   Uc  0 nUR                  SS 5      n
U
b  U
OSnUR                  SS 5      nUb  UOSn[        S/-   nU(       a  / O[        R                  5       nU(       d  US/-  nUS/-  nU(       d  US/-  nUR                  SU5      nUR                  SU5      nUR                  S	U5      nUR                  S
U5      nUR                  SU5      nUR                  SU5      nUR                  SU5      nUR                  SU5      nUR                  SU5      nUR                  SU5      nU	UUUUUUUUUUUUUUS.n[        U5      n[	        U5       Vs/ s H  nUUU   UU   UU   U4PM     nnUS:  a  [        U R                  U5      OSnUS:  a  US:  a  U R                  UU5      nO=US:  a"  U V s/ s H  n U R                  U 5      PM     nn OU R                  US   5      /nU R                  U5      u  n!n"n#n$n%AAAU R                  U$U"U5        U R                  U!U$U%U#U5      u  n&n'n(U&U'U(U4$ s  snf s  sn f )Nr   r   r   i P r   r   r   r   r   r   r   r   r   r   r   r   )r   rO   r   rF   rG   r   r   r   r   r   r   r   r   r   r   r   r   )popIMAGE_LABELScopyr  rangeminrV   r   r   r   r   r&  ))rg   r   layout_det_resultsimgs_in_docrF   rG   rJ   r   rO   r   r   default_min_pixelsr   default_max_pixelsr  r   r   r   r   r   r   r   r   r   r   r   r   	num_pagesr   r   r   r   pr   r   r   r   r   r  r  r  s)                                            rq   get_layout_parsing_results/_PaddleOCRVLPipeline.get_layout_parsing_results  s    J^^L$7
+5+AZv^^L$7
+5+AZw'6(24r,:K:K:M$WI%L	)#VH$L#(8:LM#(8:LM%>>*<>PQ%>>*<>PQ%>>*<>PQ%>>*<>PQ'^^,@BTU'^^,@BTU$..):<NO$..):<NO "3#6(%:$8,, 0 0 0 0"4"4..
$ K	 9%	
 & q	"1%A & 	 	
 =FMC,,i8q 	 q=[1_HH{L ] '&A BB1E&  L BB=QRCSTL 44\B	
 &55z	
 77
		
 	
 	
m	
&s   H? Ic              +  
  ^ ^^^^	^
^^^^^^^^^^^&^'^(^)^*^+^,^-^.^/#    T R                  TTUUUUUUU5	      m,TS:X  a  SOST,S'   T R                  T,5      (       d  SS0v   Uc  T R                  nTc  0 mT,S   (       d]  T(       a  TOS	mTR                  5       S
:X  a  ST,S'   OTR                  5       S:X  a  ST,S'   TR                  5       S;   d   ST S35       eS%UU	UUU
U,UU UU4
S jjm&UUUUU,UU UUU4
S jm'U(       Ga<  Sn[        R
                  " US9m.[        R
                  " US9m-[        R
                  " T R                  R                  U-  S9m/[        R                  " 5       m*[        R                  " 5       m)[        R                  " 5       m([        R                  " 5       m+U)U*U.U 4S jnU&U(U)U*U,U-U.U 4S jnU'U(U*U+U-U/U 4S jn[        R                  " UU4SS9nUR                  5         [        R                  " USS9nUR                  5         [        R                  " USS9n U R                  5          U(       a  T+R                  5       (       a  T/R                  5       (       dj   T/R                  SS9n!U!c  OU!S   (       d  [!        SU!S    SU!S     35      eU!S   v   T+R                  5       (       d  MS  T/R                  5       (       d  Mj  OdT R                  U5       HO  n"[#        T&" U"5      5      n#[%        U#5      S:X  d   [%        U#5      5       eU#S   n$T'" U$5       H  n%U%v   M	     A%A$A#A"MQ     U(       a  T*R'                  5         WR)                  S!S9  UR+                  5       (       a  [,        R.                  " S"5        WR)                  S!S9  UR+                  5       (       a  [,        R.                  " S#5        W R)                  S!S9  U R+                  5       (       a  [,        R.                  " S$5        ggg! [        R                   a    T+R                  5       (       a   M   GM  f = f! U(       a  T*R'                  5         WR)                  S!S9  UR+                  5       (       a  [,        R.                  " S"5        WR)                  S!S9  UR+                  5       (       a  [,        R.                  " S#5        W R)                  S!S9  U R+                  5       (       a  [,        R.                  " S$5        f f f = f7f)&aX  
Predicts the layout parsing result for the given input.

Args:
    input (Union[str, list[str], np.ndarray, list[np.ndarray]]): Input image path, list of image paths,
                                                                numpy array of an image, or list of numpy arrays.
    use_doc_orientation_classify (Optional[bool]): Whether to use document orientation classification.
    use_doc_unwarping (Optional[bool]): Whether to use document unwarping.
    use_layout_detection (Optional[bool]): Whether to use layout detection. Default is None.
    use_chart_recognition (Optional[bool]): Whether to use chart recognition. Default is None.
    use_seal_recognition (Optional[bool]): Whether to use seal recognition. Default is None.
    layout_threshold (Optional[float]): The threshold value to filter out low-confidence predictions. Default is None.
    layout_nms (Optional[bool], optional): Whether to use layout-aware NMS. Defaults to `False`.
    layout_unclip_ratio (Optional[Union[float, Tuple[float, float]]], optional): The ratio of unclipping the bounding box.
        Defaults to `None`.
        If it's a single number, then both width and height are used.
        If it's a tuple of two numbers, then they are used separately for width and height respectively.
        If it's None, then no unclipping will be performed.
    layout_merge_bboxes_mode (Optional[str], optional): The mode for merging bounding boxes. Defaults to `None`.
    layout_shape_mode (Optional[str], optional): The mode for layout shape. Defaults to "auto", [ "rect", "quad","poly", "auto"] are supported.
    use_queues (Optional[bool], optional): Whether to use queues. Defaults to `None`.
    prompt_label (Optional[Union[str, None]], optional): The label of the prompt in ['ocr', 'formula', 'table', 'chart']. Defaults to `None`.
    format_block_content (Optional[bool]): Whether to format the block content. Default is None.
    repetition_penalty (Optional[float]): The repetition penalty parameter used for VL model sampling. Default is None.
    temperature (Optional[float]): Temperature parameter used for VL model sampling. Default is None.
    top_p (Optional[float]): Top-p parameter used for VL model sampling. Default is None.
    min_pixels (Optional[int]): The minimum number of pixels allowed when the VL model preprocesses images. Default is None.
    max_pixels (Optional[int]): The maximum number of pixels allowed when the VL model preprocesses images. Default is None.
    max_new_tokens (Optional[int]): The maximum number of new tokens. Default is None.
    merge_layout_blocks (Optional[bool]): Whether to merge layout blocks. Default is None.
    markdown_ignore_labels (Optional[list[str]]): The list of ignored markdown labels. Default is None.
    **kwargs (Any): Additional settings to extend functionality.

Returns:
    PaddleOCRVLResult: The predicted layout parsing result.
rectFTreturn_layout_polygon_pointsr   z0the input params for model settings are invalid!Nr=   ocrr   rF   r   rG   )r9  r   r   r   r   r   zLayout detection is disabled (use_layout_detection=False). 'prompt_label' must be one of ['ocr', 'formula', 'table', 'chart'], but got 'z'.c              3  ^  >
#    U(       d  [        U 5      n[        S[        U 5      U5       GHf  nU R                  X"U-    nU R                  X"U-    nU R                  X"U-    nU R
                  X"U-    nTR                  U5      nTS   (       a  [        TR                  UTTS95      nOU V	s/ s H  n	SU	0PM	     nn	U V
s/ s H  oS   PM	     nn
TS   (       aJ  [        TR                  UTTTTTSS95      n[        X5       VVs/ s H  u  p[        XS   5      PM     nnnOd/ nU HJ  nUR                  S S STR                  5       S	SSUR                  S	   UR                  S   /S
./S.5        ML     U Vs/ s H  n/ PM     nnXEXkXU4v   GMi     g s  sn	f s  sn
f s  snnf s  snf 7f)Nr   r9   )r{   r|   
output_imgr=   F)rB   rC   rD   rE   r   r$   r   r   )cls_idr   score
coordinate)
input_path
page_indexr   )r  r,  	instancesinput_pathspage_indexespage_countsra   r   r\   r^   r   r   r   lowerr   )
batch_datanew_batch_sizeidxrA  rB  rC  rD  image_arraysdoc_preprocessor_resultsarritemdoc_preprocessor_imagesr.  
doc_pp_imgr   r/  doc_preprocessor_imager   rE   rC   r   layout_thresholdrD   model_settingsprompt_labelrg   r{   r|   s                     rq   _process_cv1_PaddleOCRVLPipeline.predict.<locals>._process_cvf  s:    !!$ZQJ@&00^7KL	(44S;OP)66s>=QR(44S;OP#y9!"89/366(9U.? 7 0, 8D07Cs+| - 0
 4L+3K4&3K ( + ""89)---3&6'10C5M.?16 . 
*& ;>3;#;6J $Jw0GH;   #K *,&2I.*11.2.2 341=1C1C1E12,-,-,B,H,H,K,B,H,H,K	7*	
%&*" 3J( 0B"B/A!2/AK"B!Wo  FQ  Q  QG A"0+ #8 #Cs7   B+F-.F<F-F8F-F""AF-<F(%F-c              3  <  >
#    U u  nnnnnnnTR                  UUUTS   TS   TS   TTTTTTS.TETS   TS9	u  nn	n
n[        UUUUUUU	UU
U5
       HD  u
  nnnnnnnnnnUUUUR                  S   UR                  S   UUUUUUTS	.n[        U5      v   MF     g 7f)
NrF   rG   rJ   )repetition_penaltytemperaturetop_pr   r   r   rO   )	r   r.  r/  rF   rG   rJ   r   rO   r   r   r   )r?  r@  
page_countwidthheightdoc_preprocessor_resr   r  r  r  r/  rQ  )r4  r   r   r!   ) 
results_cvrB  rC  rD  rM  rJ  r.  r/  r  r  r  r?  r@  rY  rO  r\  r   r  r  r  r   single_img_resr   r   r   r   rQ  rV  rg   rW  rX  vlm_extra_argss                          rq   _process_vlm2_PaddleOCRVLPipeline.predict.<locals>._process_vlm  s?     '(" //.#5'&45L&M%34J%K(67P(Q*<#."",",&4 % %33H$I"3# 0 !!@ '("!!&$ # #-",",399!<4::1=,@&4&4(8$0#6&4" (775s   BB@   )maxsizec                P  > TR                  U 5      nTR                  5       (       d7   [        U5      nTR                  SU45        ATR                  5       (       d  M7  TR                  5         g ! [         a     M  [
         a  nTR                  SSU45         S nAMA  S nAff = f)NTFinput)r`   is_setnextputStopIteration	Exceptionr   )input_all_batch_datarF  eevent_data_loading_doneevent_shutdownqueue_inputrg   s       rq   _worker_input3_PaddleOCRVLPipeline.predict.<locals>._worker_input  s    !%!3!3F!;(//11	'%).%9
 $z(:;& )//11 (++- ) $ #(;<s   A0 0
B%=	B%B  B%c                 Z  > TR                  5       (       d   T	R                  SS9n U S   (       d  TR                  U 5        g  T" U S   TS   (       a   T
R                  R                  R                  OS 5       H  nTR                  SU45        AM     A TR                  5       (       d  M  g g ! [        R                   a<    TR                  5       (       a#  TR	                  5         TR                  S 5         g  M  f = f! [         a  nTR                  SSU45         S nAg S nAff = f)	N      ?timeoutr   r   r=   TFcv)
rf  rZ   queueEmptyr   rh  r^   r`   rK   rj  )rL  r]  rm  rS  event_cv_processing_donern  ro  rQ  queue_cvrp  rg   s      rq   
_worker_cv0_PaddleOCRVLPipeline.predict.<locals>._worker_cv  s   (//11!*s;  7 T**5 G $22H#I !% 5 5 C C N N%)+J %LL$
);< *+ !/ )//11 !;; !299;;488:$LL.! !* %  eT1%56s+   B/ AD /A
C?>C?
D*D%%D*c                   > Sn TR                   R                  R                  nSnTR                  5       (       Gd  / n[        R                  " 5       nSnSn U [        R                  " 5       U-
  -
  nUS::  a  Oq TR                  US9nUc  SnO[US   (       d  TR                  U5        SnO=UR                  US   5        AUS   S    H  n	U[        U	S	   5      -  nM     Xa:  a  OM  U(       a  g U(       dA  U(       d  TR                  5       (       a"  TR                  5         TR                  S 5        g GM  [        U6  V
s/ s H"  n
[        [        R                  " U
5      5      PM$     nn
A T" U5       H  nTR                  SU45        AM     AU(       a    TR#                  5       nUb
  US   (       d  O`US   /n[        U6  V
s/ s H"  n
[        [        R                  " U
5      5      PM$     nn
 T" U5       H  nTR                  SU45        M     M  TR                  5         TR                  S 5        g TR                  5       (       d  GM  g g ! [        R                   a     GM  f = fs  sn
f ! [          a  nTR                  SS
U45         S nAg S nAff = f! [        R                   a     M  f = fs  sn
f ! [          a  nTR                  SS
U45         S nAM  S nAff = f)Nrt  Fr   Tru  r      r   vlm)r_   r`   rK   rf  timerZ   rx  ry  rh  r   r  r   r   r   r   from_iterablerj  
get_nowait)MAX_QUEUE_DELAY_SECSMAX_NUM_BOXEScv_doneresults_cv_list
start_timeshould_break	num_boxesremaining_timerL  reslistsmerged_results_cv
result_vlmrm  results_cv_list_finalmergedr`  rz  ro  event_vlm_processing_doner{  	queue_vlmrg   s                   rq   _worker_vlm1_PaddleOCRVLPipeline.predict.<locals>._worker_vlm2  s   '*$ $ 1 1 ? ? J J(//11&(O!%J#(L !I)= IIK*4* *Q.!"#+<<<#GD  <&*G!#Aw%MM$/+/L!'..tAw7 #22#6q#9C%S\)::I $:$5!/ 0 $*"&>&E&E&G&G599;%MM$/!  &)/%:)%:E U0078%: & ) (*67H*IJ%MM4*<= * +J . "&'/':':'<  $|47 %59!WI1 .12G-H&-HE !%U%8%8%? @-H # &&2>v2FJ$-MM42D$E 3G #$ 2557!d+Y )//11  %{{ "!"2) % !ueQ&78 $);; & %&
& $- & )ueQ.? @ %&s`   ;I )I52$I: J% )K 9"K I21I2:
J"JJ"%J=<J=
K.K))K.)targetargsdaemon)r  r  rt  ru  r   zException from the 'r   z
' worker: r   r  z&Input worker did not terminate in timez#CV worker did not terminate in timez$VLM worker did not terminate in timer   )r}   r   rN   rE  rx  Queuer`   rK   	threadingEventThreadstartrf  emptyrZ   ry  RuntimeErrorr   r  r   joinis_aliver   warning)0rg   re  r{   r|   r=   rF   rG   rJ   rP  rC   rD   rE   r   rN   rR  rI   rV  rW  rX  r   r   r   rO   rP   r_  ri   max_num_batches_in_processrq  r|  r  thread_input	thread_cv
thread_vlmrL  rF  r  r]  r  rS  r`  rz  rn  ro  r  rQ  r{  rp  r  s0   ` ``    ````` ` ``````  `             @@@@@@@@@@rq   r   _PaddleOCRVLPipeline.predict  s    @ 00( ! # "

 '&0Ed 	56 ..~>>NOOJ!N45+7<UL!!#w.:>67##%/9=56%%' ,  k Z  [g  Zh  hj  kk G	Q G	Q G	QRI	8 I	8V )+&++.HIK{{+EFH**558RRI '__.N&/oo&7#'0'8$(1(9%. . :Q Qf %++$E8EL  !((
5IIOO"))UKJ%	L4;;==)//BSBS!(}}S}9
 |7*247):d1gYO  #1g 5;;==)//BSBS" #'"4"4U";J&*;z+B&CO/14Jc/6JJ4!0!3J+J7!	  8Z* #< ""$!!!!,((**OO$LMq)%%''OO$IJ*&&((OO$JK ) - !;; !4;;==! !, ""$!!!!,((**OO$LMq)%%''OO$IJ*&&((OO$JK ) sR   H"T?1P; 1P  AP; P; A%P; ?CT)P80P; 7P88P; ;C	TTc                2    SnU H  nUSUS   -   -  nM     U$ )z
Concatenate Markdown content from multiple pages into a single document.

Args:
    markdown_list (list): A list containing Markdown data for each page.

Returns:
    tuple: A tuple containing the processed Markdown text.
r  z

markdown_textsrW   )rg   markdown_listr  r  s       rq   concatenate_markdown_pages/_PaddleOCRVLPipeline.concatenate_markdown_pages  s0      Cfs+;'<<<N ! rx   c                R    [         R                  " S5        U R                  XX45      $ )a  Concatenate layout parsing results from multiple pages.

Args:
    res_list: List of page parsing results
    merge_talble: Whether to merge tables across pages
    title_level: Whether to assign title levels
    merge_pages: Whether to concatenate pages using the new consolidate_pages() logic

Returns:
    PaddleOCRVLResult: Combined OCR-VL result after merge_table or title_level policy
zDeprecationWarning: `concatenate_pages()` is deprecated as of v3.3.14 and will be removed in v3.4.0. Please use `restructure_pages()` instead. It provides better support for table merging and title restructuring.)r   r  restructure_pages)rg   res_listmerge_tabletitle_levelmerge_pagess        rq   concatenate_pages&_PaddleOCRVLPipeline.concatenate_pages  s+    $ 	 c	
 %%h[VVrx   c              #    ^#    [        U5      S:X  a  / $ S mU4S jnSn/ nU H  n[        U[        5      (       d7  US   n/ US'   UR                  S/ 5      n	UR                  S0 5      n
U" X5      n	OUS   n	UR                  S0 5      n
/ nU	 H%  nXll        Xll        US-  nUR                  U5        M'     XS'   UR                  U5        M     UnU Vs/ s H  oS   PM	     nnU(       a  [        U5      nU(       a  [        U5      n/ nU(       a  / nU H$  nUR                  UR                  S/ 5      5        M&     UUS   S'   US   n/ n[        U5       H'  u  nnU H  nUUl        UR                  U5        M     M)     UUS'   S	US
'   [        U5      US'   W
S   (       a  U Vs/ s H  oS   PM	     snUS'   U
S   (       a  U Vs/ s H  oS   PM	     snUS'   UR                  [        U5      5        O7[        U5       H(  u  nnUU   US'   UR                  [        U5      5        M*     U S	h  vN   g	s  snf s  snf s  snf  N7f)a{  Restructure layout parsing results from multiple pages.
Args:
    res_list: List of page parsing results
    merge_tables: Whether to merge tables across pages
    relevel_titles: Whether to relevel titles
    concatenate_pages: Whether to concatenate pages to a single document

Returns:
    PaddleOCRVLResult: Combined OCR-VL result after merge_tables or relevel_titles policy
r   c                    U R                  SS 5      (       a  U S   $ U S   S;   d   U S   S:X  a.  UR                  SS5      (       d  [        U S   U S   5      nUS S.$ g )	Nr+   r   )r+   r   r   rF   Fr  r  )rZ   r   )r   rQ  r  s      rq   _get_img_obj<_PaddleOCRVLPipeline.restructure_pages.<locals>._get_img_obj  sp    yy$''W~%]#'88m$/&**+BEJJ)%*>l@ST $T22rx   c                  > / nU  Hu  n[        US   US   UR                  SS 5      [        R                  " SSUS   5      UR                  SS 5      S9nT" X15      =n(       a  XTl        UR                  U5        Mw     U$ )	Nr   r  block_polygon_pointsz^#+\sr  r  r  )r   r	  r  r
  r  )r    rZ   resubr+   r   )r   rQ  r  r   objr   r  s         rq   _conver_blocks_to_objE_PaddleOCRVLPipeline.restructure_pages.<locals>._conver_blocks_to_obj  s    C&.|,#(99-CT#JFF8R1GH"YYz48 'u==3= #I

3   Jrx   r  r/  r  rQ  r   Nr@  rY  r=   r   r9   r\  )r  
isinstancer   rZ   global_block_idglobal_group_idr   r   r   extendr   r@  r!   )rg   r  merge_tablesrelevel_titlesr  r  r  obj_res_listone_page_resr   rQ  r  r   r  blocks_by_pageconcatenate_resall_imgs_in_docall_page_res
all_blocksr   blksblkr  s                         @rq   r  &_PaddleOCRVLPipeline.restructure_pages  s    $ x=AI			 $LlJ77+E2.0]+%))*<bA!-!1!12BB!G.vF%&89!-!1!12BB!G!(7%(7%1$ ''.	   0@+,-% %&  =EFXc01XF6~FN9.IN O&&sww}b'AB  )8HQK&#A;LJ"+N";$C%-CN%%c*   #< 0:L+,)-L&),XL&455=25=c()X2-. 45;C8;CC./8834 ""#4\#BC*3H*=&,3A(3K/0&&'8'FG +> #""I G.28 	#s8   CIH=$CI*I8IIAI7I8I)r`   rb   r\   rI   ra   r^   rV   rP   rO   rF   r9   r=   rJ   rN   rG   r_   )rh   r   r1   Optional[str]r2   r  r3   zOptional[Dict[str, Any]]r4   zOptional[PaddlePredictorOption]r5   boolr6   z*Optional[Union[Dict[str, Any], HPIConfig]]r7   r  returnNoner   )r{   Union[bool, None]r|   r  r=   r  rF   r  rG   r  rJ   r  rI   r  rO   r  rP   Optional[List[str]]r  rz   )r   rz   r  r  )FFFNTauto)FFNNNNNNNNr  NNNNNNNNNNNN)2re  z3Union[str, List[str], np.ndarray, List[np.ndarray]]r{   r  r|   r  r=   r  rF   r  rG   r  rJ   r  rP  zOptional[Union[float, dict]]rC   Optional[bool]rD   z1Optional[Union[float, Tuple[float, float], dict]]rE   r  r   r  rN   r  rR  zOptional[Union[str, None]]rI   r  rV  Optional[float]rW  r  rX  r  r   Optional[int]r   r  r   r  rO   r  rP   r  r_  zOptional[dict]r  r!   )r  r   r  tuple)TTF)r  r   r  r  r  r  r  r  )r  r   r  r  r  r  r  r  )__name__
__module____qualname____firstlineno____doc__rY   ru   r}   r   r   timeit_with_optionsr   r   r   r   r   r   r   r   r   r   r&  r4  r   r  r  r  __static_attributes____classcell__)rp   s   @rq   r/   r/   7   sA   ' !% $2659AE"&}} 	}
 } 0} 3} } ?}  } 
} }~& 7;=
&7=
 -=
 0	=

  1=
 0=
 "3=
 0=
 /=
 !4=
 
=
~& ""(KLG MG ""(JKC LC ""(LM
 N
EFN ""(OP
 Q

<"
H ""(KL M ""(LM/
 N/
b ""(QR J S JD ""(OPpE QpEn $" %  t
r ;@/4263726599=%)QU26+1%)3726.2'+!%$($((,.26:)-3uLBuL '8uL -	uL
 0uL  1uL 0uL "3uL 7uL #uL OuL #0uL )uL #uL 1uL  0!uL" ,#uL$ %%uL& 'uL( ")uL* "+uL, &-uL. ,/uL0 !41uL2 '3uL6 
7uLn( ! !WW W 	W
 W4 "#"'j#j# j# 	j#
  j# j#rx   r/   c                  *    \ rS rSr\S 5       rS rSrg)_BasePaddleOCRVLPipelineiJ  c                    [         $ r   )r/   rv   s    rq   _pipeline_cls&_BasePaddleOCRVLPipeline._pipeline_clsK  s    ##rx   c                &    UR                  SS5      $ )NrK   r   )rZ   )rg   rh   s     rq   _get_batch_size(_BasePaddleOCRVLPipeline._get_batch_sizeO  s    zz,**rx   rW   N)r  r  r  r  propertyr  r  r  rW   rx   rq   r  r  J  s    $ $+rx   r  r9  c                      \ rS rSrSrSrg)PaddleOCRVLPipelineiS  zPaddleOCR-VLrW   Nr  r  r  r  entitiesr  rW   rx   rq   r  r  S  s    Hrx   r  c                      \ rS rSrSrSrg)PaddleOCRVL15PipelineiX  zPaddleOCR-VL-1.5rW   Nr  rW   rx   rq   r  r  X      !Hrx   r  c                      \ rS rSrSrSrg)PaddleOCRVL16Pipelinei]  zPaddleOCR-VL-1.6rW   Nr  rW   rx   rq   r  r  ]  r  rx   r  )D
__future__r   rx  r  r  r  concurrent.futuresr   	itertoolsr   typingr   r   r   r	   r
   r   numpynpPILr   utilsr   
utils.depsr   common.batch_samplerr   common.readerr   modelsr   r   utils.benchmarkr   	_parallelr   baser   
componentsr   layout_parsing.merge_tabler   layout_parsing.title_levelr   layout_parsing.utilsr   r   r  r   r    r!   uiltsr"   r#   r$   r%   r&   r'   r(   r)   r*   r*  time_methodsr/   r  r  r  r  rW   rx   rq   <module>r     s    #  	   1  : :    2 5 & 6 ( @  $ B E B C C
 
 
 9 O#< O# O#d(+G + 2    "4 "  " "4 "  "rx   