
    {-jkn                        d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlZddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZm Z m!Z!m"Z" ej#         ed           G d de                                  Z$dS )    N)deepcopy)sleep)AnyDictListOptionalTupleUnion   )logging)pipeline_requires_extra   )MarkDownBatchSampler)	HPIConfigPaddlePredictorOption)	benchmark   )BasePipeline   )DocumentResultLatexResultMarkdownResult)split_original_textssplit_text_recursivetranslate_code_blocktranslate_html_blocktransc            A           e Zd ZdZdgZdddddddddedee         dee         d	eeeef                  d
ee	         de
deeeeef         ef                  de
ddf fdZd ZdeddfdZdeddfdZdLdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dMdeeee         ej        eej                 f         dee
         dee
         dee
         dee
         dee
         dee
         dee
         dee
         deeeef                  dee
         d eeeeeef         ef                  d!ee         d"ee         d#ee         d$ee         d%ee         d&ee         d'ee         d(ee         d)ee         d*ee         d+ee         d,ee         d-ee         d.e
d/e
d0e
d1e
d2e
d3e
def@d4Zd5 Zd6 Z	 	 	 	 	 	 	 	 	 	 dNd:ee         d;ed<ed=ed>ed?ed@edAedBedCedDefdEZdFedefdGZdHedefdIZ dJedefdKZ! xZ"S )OPP_DocTranslation_Pipelinez%
    PP_ DocTranslation_Pipeline
    zPP-DocTranslationNF)deviceengineengine_config	pp_optionuse_hpip
hpi_configinitial_predictorconfigr    r!   r"   r#   r$   r%   r&   returnc          
      @    t                      j        d||||||d|	 |d         | _        || _        |                    dd          | _        d| _        d| _        |r*|                     |           | 	                    |           t                      | _        dS )a  Initializes the PP-DocTranslation 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 the predictor.
                Defaults to `False`.
        )r    r!   r"   r#   r$   r%   pipeline_nameuse_layout_parserTN )super__init__r*   r'   getr+   layout_parsing_pipelinechat_botinintial_visual_predictorinintial_chat_predictorr   markdown_batch_sampler)selfr'   r    r!   r"   r#   r$   r%   r&   kwargs	__class__s             v/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/pipelines/pp_doctranslation/pipeline.pyr.   z#PP_DocTranslation_Pipeline.__init__.   s    8 	 	
'!	
 	
 	
 	
 	
 $O4!',?!F!F'+$ 	1**6222((000&:&<&<###    c                 J    | j         | j                                          d S d S )N)r0   close)r5   s    r8   r;   z PP_DocTranslation_Pipeline.closea   s.    '3(..00000 43r9   c                     |                     dd          | _        | j        rF|                     di                                dddi          }|                     |          | _        dS )a  
        Initializes the visual predictor with the given configuration.

        Args:
            config (dict): The configuration dictionary containing the necessary
                                parameters for initializing the predictor.
        Returns:
            None
        r+   TSubPipelinesLayoutParserpipeline_config_errorz)config error for layout_parsing_pipeline!N)r/   r+   create_pipeliner0   )r5   r'   layout_parsing_configs      r8   r2   z4PP_DocTranslation_Pipeline.inintial_visual_predictore   su     "(,?!F!F! 	W$*JJ~r$B$B$F$F(*UV% %! ,0+?+?@U+V+VD(r9   c                 6   ddl m} |                    di                               dddi          } ||          | _        ddl m} |                    di                               di                               d	d
di          } ||          | _        dS )a  
        Initializes the chat predictor with the given configuration.

        Args:
            config (dict): The configuration dictionary containing the necessary
                                parameters for initializing the predictor.
        Returns:
            None
        r   create_chat_bot
SubModulesLLM_Chatchat_bot_config_errorzconfig error for llm chat bot!)create_prompt_engineeringPromptEngneeringTranslate_CommonTextpe_config_errorz%config error for translate_pe_config!N) rD   r/   r1   rH   translate_pe)r5   r'   rD   chat_bot_configrH   translate_pe_configs         r8   r3   z2PP_DocTranslation_Pipeline.inintial_chat_predictory   s     	'&&&&& **\266::$&FG
 
 (88000000 JJ|R((S#R((S&"$KL  	 656IJJr9   c                 .    t          j        d           d S )NzPP-Translation Pipeline do not support to call `predict()` directly! Please invoke `visual_predict`, `build_vector`, `chat` sequentially to obtain the result.)r   error)r5   argsr6   s      r8   predictz"PP_DocTranslation_Pipeline.predict   s#     m	
 	
 	
 	r9   Tinputuse_doc_orientation_classifyuse_doc_unwarpinguse_textline_orientationuse_seal_recognitionuse_table_recognitionuse_formula_recognitionuse_chart_recognitionuse_region_detectionlayout_threshold
layout_nmslayout_unclip_ratiolayout_merge_bboxes_modetext_det_limit_side_lentext_det_limit_typetext_det_threshtext_det_box_threshtext_det_unclip_ratiotext_rec_score_threshseal_det_limit_side_lenseal_det_limit_typeseal_det_threshseal_det_box_threshseal_det_unclip_ratioseal_rec_score_thresh#use_wired_table_cells_trans_to_html&use_wireless_table_cells_trans_to_htmluse_table_orientation_classify use_ocr_results_with_table_cellsuse_e2e_wired_table_rec_model use_e2e_wireless_table_rec_modelc               +     K   | j         dk    rt          j        d           ddiV  | j        .t          j        d           |                     | j                    | j        j        |fi d|d|d|d	|d
|d|d|d|	d|
d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d|d |d!|d"|d#|D ]
}!d$|!i}"|"V  dS )%a  
        This function takes an input image or a list of images and performs various visual
        prediction tasks such as document orientation classification, document unwarping,
        general OCR, seal recognition, and table recognition based on the provided flags.

        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_textline_orientation (Optional[bool]): Whether to use textline orientation prediction.
            use_seal_recognition (Optional[bool]): Whether to use seal recognition.
            use_table_recognition (Optional[bool]): Whether to use table recognition.
            use_formula_recognition (Optional[bool]): Whether to use formula recognition.
            use_region_detection (Optional[bool]): Whether to use region detection.
            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`.
            text_det_limit_side_len (Optional[int]): Maximum side length for text detection.
            text_det_limit_type (Optional[str]): Type of limit to apply for text detection.
            text_det_thresh (Optional[float]): Threshold for text detection.
            text_det_box_thresh (Optional[float]): Threshold for text detection boxes.
            text_det_unclip_ratio (Optional[float]): Ratio for unclipping text detection boxes.
            text_rec_score_thresh (Optional[float]): Score threshold for text recognition.
            seal_det_limit_side_len (Optional[int]): Maximum side length for seal detection.
            seal_det_limit_type (Optional[str]): Type of limit to apply for seal detection.
            seal_det_thresh (Optional[float]): Threshold for seal detection.
            seal_det_box_thresh (Optional[float]): Threshold for seal detection boxes.
            seal_det_unclip_ratio (Optional[float]): Ratio for unclipping seal detection boxes.
            seal_rec_score_thresh (Optional[float]): Score threshold for seal recognition.
            use_wired_table_cells_trans_to_html (bool): Whether to use wired table cells trans to HTML.
            use_wireless_table_cells_trans_to_html (bool): Whether to use wireless table cells trans to HTML.
            use_table_orientation_classify (bool): Whether to use table orientation classification.
            use_ocr_results_with_table_cells (bool): Whether to use OCR results processed by table cells.
            use_e2e_wired_table_rec_model (bool): Whether to use end-to-end wired table recognition model.
            use_e2e_wireless_table_rec_model (bool): Whether to use end-to-end wireless table recognition model.
            **kwargs (Any): Additional settings to extend functionality.

        Returns:
            dict: A dictionary containing the layout parsing result.
        Fz1The models for layout parser are not initialized.rQ   NzGThe layout parsing pipeline is not initialized, will initialize it now.rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rj   rg   rh   ri   rk   rl   rm   rn   ro   rp   rq   rr   layout_parsing_result)r+   r   rQ   r0   warningr2   r'   rS   )#r5   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   r6   rt   visual_predict_ress#                                      r8   visual_predictz)PP_DocTranslation_Pipeline.visual_predict   sP     b !U**MMNNNOPPPP'/OY   **4;777%IT%A%I &
  &
  &
)E)E &
 0/ &
 &>%=	 &

 "6!5 &
 #8"7 &
 %<$; &
 #8"7 &
 "6!5 &
 .- &
 "z &
 !4 3 &
 &>%= &
 %<$; &
 !4 3 &
  ,O! &
" !4 3# &
$ #8"7% &
& #8"7' &
( !4 3) &
* %<$;+ &
, !4 3- &
. ,O/ &
0 #8"71 &
2 #8"73 &
4 1T0S5 &
6 4Z3Y7 &
8 ,J+I9 &
: .N-M; &
< +H*G= &
> .N-M? &
 %	% %	%!F ()>" %$$$$K%	% %	%r9   c                     g }| j                             |          D ]E}|j        d         }|j        d         }|d |dd}|                    t          |                     F|S )Nr   TT)
input_path
page_indexmarkdown_textspage_continuation_flags)r4   sample	instancesinput_pathsappendr   )r5   rT   markdown_info_listmarkdown_samplemarkdown_contentrz   markdown_infos          r8   load_from_markdownz-PP_DocTranslation_Pipeline.load_from_markdown   s    #:AA%HH 		E 		EO.8;(4Q7J(""2+7	 M %%n]&C&CDDDD!!r9   c           	         g }d}t          j        dt          |           d           t          j        d           t          |          D ]\  }}|\  }}	|dk    r|                                r2|                     ||                                                     d}t          j        d|dz    dt          |           d	           t          |	|||           t          |	          |k     r|d
k    rt          |          t          |	          z   |k     r	|d|	z   z  }|                                r[t          j        d|dz    dt          |           d	           |                     ||                                                     |	}Mt          j        d|dz    dt          |           d	           |                                r2|                     ||                                                     d}|d
k    r&|                    t          |	||                     |dk    s|dk    rt          |	|||           
t          d|           |                                r0|                     ||                                                     d
                    |          S )a  
        Chunks the given markdown blocks into smaller chunks of size `chunk_size` and translates them using the given
        translate function.

        Args:
            md_blocks (list): A list of tuples representing each block of markdown content. Each tuple consists of a string
          indicating the block type ('text', 'code') and the actual content of the block.
            chunk_size (int): The maximum size of each chunk.
            translate_func (callable): A callable that accepts a string argument and returns the translated version of that string.

        Returns:
            str: A string containing all the translated chunks concatenated together with newlines between them.
        rL   zSplit the original text into z blockszStarting translation...codezTranslating block r   /z...text

text_with_htmlhtmlzUnknown block type: )r   infolen	enumeratestripr   r   r   r   
ValueErrorjoin)
r5   	md_blocks
chunk_sizetranslate_functranslation_resultschunkidxblock
block_typeblock_contents
             r8   chunk_translatez*PP_DocTranslation_Pipeline.chunk_translate/  s    !LS^^LLLMMM.///#I.. !	J !	JJC(-%JV##;;== '..~~ekkmm/L/LMMMEM#a%MM#i..MMMNNN$!:~?R    ]##j00Z65I5Iu::M 2 22Z??Vm33EE{{}} R%U#a%%U%U#i..%U%U%UVVV+22>>%++--3P3PQQQ)EEM#a%MM#i..MMMNNN;;== '..~~ekkmm/L/LMMME'''..,]JWW     #333zV7K7K(%z>CV    %%HJ%H%HIII;;== 	F&&~~ekkmm'D'DEEE{{.///r9   zh          ori_md_info_listtarget_languager   task_descriptionoutput_format	rules_strfew_shot_demo_text_contentfew_shot_demo_key_value_listglossaryllm_request_intervalrN   c           	   +      
K    j         .t          j        d                                 j                   |ddlm}  ||          n j         t          |t                    r1|d         	                    d           
                    |          g}t          
t                    st          
          
t          |	t                    s|	
J d            d}|	O|	                                D ]:\  }}t          |t                    rd	                    |          }|| d
| dz  };|dk    r|n
dz  |z  
 f	d} j                            d          }t#          |          }||k    r||z
  }nt%          d| d| d          |D ]Y}|d         }t'          |          }                     |||          }t+          |d         |d         |d         |d          V  ZdS )a  
        Translate the given original text into the specified target language using the configured translation model.

        Args:
            ori_md_info_list (List[Dict]): A list of dictionaries containing information about the original markdown text to be translated.
            target_language (str, optional): The desired target language code. Defaults to "zh".
            chunk_size (int, optional): The maximum number of characters allowed per chunk when splitting long texts. Defaults to 5000.
            task_description (str, optional): A description of the task being performed by the translation model. Defaults to `None`.
            output_format (str, optional): The desired output format of the translation result. Defaults to `None`.
            rules_str (str, optional): Rules or guidelines for the translation model to follow. Defaults to `None`.
            few_shot_demo_text_content (str, optional): Demo text content for the translation model. Defaults to `None`.
            few_shot_demo_key_value_list (str, optional): Demo text key-value list for the translation model. Defaults to `None`.
            glossary (Dict, optional): A dictionary containing terms and their corresponding definitions. Defaults to `None`.
            llm_request_interval (float, optional): The interval in seconds between each request to the LLM. Defaults to 0.0.
            chat_bot_config (Dict, optional): Configuration for the chat bot used in the translation process. Defaults to `None`.
            **kwargs: Additional keyword arguments passed to the translation model.

        Yields:
            MarkdownResult: A dictionary containing the translation result in the target language.
        Nz;The LLM chat bot is not initialized,will initialize it now.r   rC   r   r{   zglossary must be a dictrL   u   或z: 
c           	      T  	 t                     	j                            | 
          }                    |                              dd          }d|vrt          d          |t          d          |                    dd                                          }|S )	z
            Translate the given text using the configured translation model.

            Args:
                text (str): The text to be translated.

            Returns:
                str: The translated text in the target language.
            original_textlanguager   r   r   r   r   )promptcontentrL   z<<END>>zThe translation did not reach the end. This may happen if your chunk_size is too large. Please reduce chunk_size and try again.Nz#The call to the large model failed.)r   rM   generate_promptgenerate_chat_resultsr/   	Exceptionreplacerstrip)r   r   	translater1   r   r   r   r   r   r5   r   r   s      r8   r   z<PP_DocTranslation_Pipeline.translate.<locals>.translate_func  s     &'''&66"(!1+#+E-I 7  F !66f6EEII)UWXXI	))o     EFFF!)))R88??AAIr9   r   z:Chunk size should be greater than the base prompt length (z), but got .r|   rz   r}   )r   rz   r{   r}   r|   )r1   r   ru   r3   r'   rL   rD   
isinstancelistr/   concatenate_markdown_pagesfloatdictitemsr   rM   r   r   r   r   r   r   )r5   r   r   r   r   r   r   r   r   r   r   rN   r6   rD   glossary_strkvr   base_prompt_contentbase_prompt_lengthori_mdoriginal_textsr   target_language_textsr1   s   ` ` ````` `             @r8   r   z$PP_DocTranslation_Pipeline.translateh  s     F = OM   ((555&******&77HH}H '..	S #''55A !% ? ?@P Q QR.66 	?#()=#>#> (D))XX-=-=?X-=-== (( . .1a&& &

1A1-2+3/;,,,4,,<,	 	 	 	 	 	 	 	 	 	 	 	 	@ #/??$-''A)E @ 
 
 !!455***#&88JJyM_yylvyyy   ' 	 	F#$45N,^<<I$($8$8:~% %! ! /"("6"("6/56O/P&;      	 	r9   markdown_listc                    d}d}t          |          dk    rt          d          |D ]}|d         d         }|d         d         }|sw|su|r|d         nd}|d         r|d         d         nd}|rt          j        d	|          nd
}	|rt          j        d	|          nd
}
|	s|
s|d|d         z   z  }n||d         z  }n|d|d         z   z  }|}|d         d         dd|d}t	          |          S )a  
        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.
        rL   Tr   z$The length of markdown_list is zero.r}   r   r|   z[\u4e00-\u9fff]F r   rz   Nry   )rz   r{   r}   r|   )r   r   rematchr   )r5   r   r|   -previous_page_last_element_paragraph_end_flagres'page_first_element_paragraph_start_flag$page_last_element_paragraph_end_flaglast_char_of_markdownfirst_char_of_handlerlast_is_chinese_charfirst_is_chinese_charconcatenate_results               r8   r   z5PP_DocTranslation_Pipeline.concatenate_markdown_pages  s    8<5}""CDDD  &	 &	C<?)==3 :==V9W:0 <AEA ?M(Tr(:(:RT%034D0EMC()!,,2 & -BH/1FGGG % -BH/1FGGG &
 - <0E <"cC0@,A&AANN"c*:&;;NN&3/?+@"@@4 :9
 (*<8'3,	
 
 0111r9   	word_listc                 ^   t          |          dk    rt          d          g }g }t          |          D ]Y\  }}|d         D ]+}t          |          }||d<   |                    |           ,|d         D ]}|                    |           Zt          ||d         d         |d          S )a  
        Concatenate Word content from multiple pages into a single document.

        Args:
            word_list (list): A list containing Word data for each page.

        Returns:
            tuple: A tuple containing the processed Word document.
        r   z The length of word_list is zero.word_blocksr{   imagesrz   )r   rz   r   )r   r   r   r   r   r   )	r5   r   merged_blocksimagepage_idxpage_blocksr   
block_copyimg_objs	            r8   concatenate_word_pagesz1PP_DocTranslation_Pipeline.concatenate_word_pages7  s     y>>Q?@@@%.y%9%9 	& 	&!Hk$]3 1 1%e__
+3
<($$Z0000&x0 & &W%%%%& ,'l<8 
 
 	
r9   latex_info_listc                 ^   t          |          dk    rt          d          g }g }t          |          D ]Y\  }}|d         D ]+}t          |          }||d<   |                    |           ,|d         D ]}|                    |           Zt          |||d         d         d          S )a  
        Concatenate LaTeX content from multiple pages into a single document.

        Args:
            latex_info_list (list): A list containing LaTeX data for each page.

        Returns:
            tuple: A tuple containing the processed LaTeX document.
        r   z&The length of latex_info_list is zero.latex_blocksr{   r   rz   )r   r   rz   )r   r   r   r   r   r   )	r5   r   r   merged_imagesr   r   r   r   r   s	            r8   concatenate_latex_pagesz2PP_DocTranslation_Pipeline.concatenate_latex_pagesW  s     1$$EFFF%.%?%? 	. 	.!Hk$^4 1 1%e__
+3
<($$Z0000&x0 . .$$W----.  -'-a0> 
 
 	
r9   )r(   N)NNNNNNNNNNNNNNNNNNNNNNNNFFTTFT)
r   r   NNNNNNr   N)#__name__
__module____qualname____doc__entitiesr   r   strr   r   boolr
   r   r.   r;   r   r2   r3   rS   r   npndarrayr   r	   intrw   r   r   r   r   tupler   r   r   __classcell__)r7   s   @r8   r   r   %   s         $$H !% $2659AE"'1= 1= 1=1= 	1=
 1=  S#X/1= 121= 1= U4S>9#<=>1=  1= 
1= 1= 1= 1= 1= 1=f1 1 1     (d t    >    8<,037/3042604/39=%)QU2615-1+//3151515-1+//31515497</315.315A@% @%S$s)RZbj1AAB@% '/tn@% $D>	@%
 #+4.@% 'tn@%  (~@% "*$@%  (~@% 'tn@% #5#56@% TN@% &eE53F,L&MN@% #+3-@% "*#@%  &c]!@%" "%#@%$ &e_%@%&  ('@%(  ()@%* "*#+@%, &c]-@%. "%/@%0 &e_1@%2  (3@%4  (5@%6 .27@%8 159@%: )-;@%< +/=@%> (,?@%@ +/A@%D 
E@% @% @% @%D" " "70 70 70x  $ $!*.,0&) $L Lt*L L 	L
 L L L %(L '*L L $L L L L L\?2 ?2 ?2 ?2 ?2 ?2B
 
 
 
 
 
@
t 
 
 
 
 
 
 
 
 
r9   r   )%r   copyr   timer   typingr   r   r   r   r	   r
   numpyr   utilsr   
utils.depsr   common.batch_samplerr   modelsr   r   utils.benchmarkr   baser   resultr   r   r   r   r   r   r   time_methodsr   r,   r9   r8   <module>r      s   
			             : : : : : : : : : : : : : : : :           2 2 2 2 2 2 8 8 8 8 8 8 6 6 6 6 6 6 6 6 ( ( ( ( ( (       ? ? ? ? ? ? ? ? ? ?            !!O	
 O	
 O	
 O	
 O	
 O	
 O	
 "! O	
 O	
 O	
r9   