o
    0jkn                     @   s   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 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_blockZtransc                A       s  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dZdd ZdeddfddZdeddfddZdXddZ																														dYd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 d0ee d1ee d2ee d3e
d4e
d5e
d6e
d7e
d8e
def@d9d:Zd;d< Zd=d> Z	?	@							A	dZdBee dCedDedEedFedGedHedIedJedKedLefdMdNZdOedefdPdQZdRedefdSdTZ dUedefdVdWZ!  Z"S )[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          
   	      sn   t  jd||||||d|	 |d | _|| _|dd| _d| _d| _|r1| | | 	| 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__r(   w/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/pp_doctranslation/pipeline.pyr*   .   s&   



z#PP_DocTranslation_Pipeline.__init__c                 C   s   | j d ur| j   d S d S )N)r,   close)r1   r(   r(   r5   r6   a   s   
z PP_DocTranslation_Pipeline.closec                 C   s<   | dd| _| jr| 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'   TZSubPipelinesZLayoutParserZpipeline_config_errorz)config error for layout_parsing_pipeline!N)r+   r'   Zcreate_pipeliner,   )r1   r$   Zlayout_parsing_configr(   r(   r5   r.   e   s   
z4PP_DocTranslation_Pipeline.inintial_visual_predictorc                 C   sh   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_botZ
SubModulesZLLM_ChatZchat_bot_config_errorzconfig error for llm chat bot!)create_prompt_engineeringZPromptEngneeringZTranslate_CommonTextZpe_config_errorz%config error for translate_pe_config!N) r8   r+   r-   r9   translate_pe)r1   r$   r8   chat_bot_configr9   Ztranslate_pe_configr(   r(   r5   r/   y   s    



z2PP_DocTranslation_Pipeline.inintial_chat_predictorc                 O   s   t 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)r1   argsr2   r(   r(   r5   predict   s   z"PP_DocTranslation_Pipeline.predictT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   s   | j dkrtd ddiV  | jdu r t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  qd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.r=   NzGThe layout parsing pipeline is not initialized, will initialize it now.rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rV   rS   rT   rU   rW   rX   rY   rZ   r[   r\   r]   r^   layout_parsing_result)r'   r   r=   r,   warningr.   r$   r?   )#r1   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r2   r_   Zvisual_predict_resr(   r(   r5   visual_predict   s   
Q


	
#z)PP_DocTranslation_Pipeline.visual_predictc                 C   sJ   g }| j |D ]}|jd }|jd }|d |dd}|t| q|S )Nr   TT)
input_path
page_indexmarkdown_textspage_continuation_flags)r0   sampleZ	instancesZinput_pathsappendr   )r1   r@   Zmarkdown_info_listZmarkdown_sampleZmarkdown_contentrc   Zmarkdown_infor(   r(   r5   load_from_markdown   s   

z-PP_DocTranslation_Pipeline.load_from_markdownc           
      C   s  g }d}t dt| d t d t|D ]\}}|\}}	|dkrK| r3|||  d}t d|d  dt| d	 t|	||| qt|	|k r|d
krt|t|	 |k rf|d|	 7 }q| rt d|d  dt| d	 |||  |	}qt d|d  dt| d	 | r|||  d}|d
kr|t|	|| q|dks|dkrt|	||| qt	d| | r|||  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.
        r:   zSplit the original text into z blockszStarting translation...codezTranslating block r   /z...text

Ztext_with_htmlhtmlzUnknown block type: )r   infolen	enumeratestriprh   r   r   r   
ValueErrorjoin)
r1   	md_blocks
chunk_sizetranslate_funcZtranslation_resultschunkidxblockZ
block_typeZblock_contentr(   r(   r5   chunk_translate/  sJ   
   

z*PP_DocTranslation_Pipeline.chunk_translatezh          ori_md_info_listtarget_languagerv   task_descriptionoutput_format	rules_strfew_shot_demo_text_contentfew_shot_demo_key_value_listglossaryllm_request_intervalr<   c              	   +   s   j du rtd j |dur ddlm} || nj  t|tr7|d 	ddur7
|g}tts@tt|	tsM|	du sMJ dd}|	durp|	 D ]\}}t|tred	|}|| d
| d7 }qW|dkrdu r{|nd7 |7  f	dd}jjdd}t|}||kr|| }ntd| d| d|D ]"}|d }t|}|||}t|d |d |d |dV  qd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   r7   r   rd   zglossary must be a dictr:   u   或z: 
c              	      sj   t  jj| d} j|ddd}d|vr#td|du r+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.
            Zoriginal_textlanguager   r   r   r   r   )promptcontentr:   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   r;   generate_promptZgenerate_chat_resultsr+   	Exceptionreplacerstrip)rl   r   	translate	r-   r   r   r   r   r   r1   r   r   r(   r5   rw     s&   
	z<PP_DocTranslation_Pipeline.translate.<locals>.translate_funcr   z:Chunk size should be greater than the base prompt length (z), but got .re   rc   rf   )r   rc   rd   rf   re   )r-   r   r`   r/   r$   r:   r8   
isinstancelistr+   concatenate_markdown_pagesfloatdictitemsrt   r;   r   rp   rs   r   r{   r   )r1   r   r   rv   r   r   r   r   r   r   r   r<   r2   r8   Zglossary_strkvrw   Zbase_prompt_contentZbase_prompt_lengthZori_mdZoriginal_textsru   Ztarget_language_textsr(   r   r5   r   h  sv   
#



 	
z$PP_DocTranslation_Pipeline.translatemarkdown_listc                 C   s   d}d}t |dkrtd|D ]X}|d d }|d d }|s^|s^|r(|d nd}|d r4|d d nd}|r>td	|nd
}	|rHtd	|nd
}
|	sW|
sW|d|d  7 }n||d 7 }n|d|d  7 }|}q|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.
        r:   Tr   z$The length of markdown_list is zero.rf   r   re   z[\u4e00-\u9fff]F rm   rc   Nrb   )rc   rd   rf   re   )rp   rs   rematchr   )r1   r   re   Z-previous_page_last_element_paragraph_end_flagresZ'page_first_element_paragraph_start_flagZ$page_last_element_paragraph_end_flagZlast_char_of_markdownZfirst_char_of_handlerZlast_is_chinese_charZfirst_is_chinese_charZconcatenate_resultr(   r(   r5   r     sP   

z5PP_DocTranslation_Pipeline.concatenate_markdown_pages	word_listc           	      C   s   t |dkr
tdg }g }t|D ]$\}}|d D ]}t|}||d< || q|d D ]}|| q.qt||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_blocksrd   imagesrc   )r   rc   r   )rp   rs   rq   r   rh   r   )	r1   r   merged_blocksimagepage_idxpage_blocksrz   
block_copyimg_objr(   r(   r5   concatenate_word_pages7  s$   

z1PP_DocTranslation_Pipeline.concatenate_word_pageslatex_info_listc           	      C   s   t |dkr
tdg }g }t|D ]$\}}|d D ]}t|}||d< || q|d D ]}|| q.qt|||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_blocksrd   r   rc   )r   r   rc   )rp   rs   rq   r   rh   r   )	r1   r   r   Zmerged_imagesr   r   rz   r   r   r(   r(   r5   concatenate_latex_pagesW  s$   

z2PP_DocTranslation_Pipeline.concatenate_latex_pages)r%   N)NNNNNNNNNNNNNNNNNNNNNNNNFFTTFT)
r|   r}   NNNNNNr~   N)#__name__
__module____qualname____doc__entitiesr   r   strr   r   boolr	   r   r*   r6   r   r.   r/   r?   r   npZndarrayr   r   intra   ri   r{   r   r   tupler   r   r   __classcell__r(   r(   r3   r5   r   %   sP   	
3
		
 "
 <	

 A r   )%r   copyr   timer   typingr   r   r   r   r   r	   numpyr   utilsr   Z
utils.depsr   Zcommon.batch_samplerr   modelsr   r   Zutils.benchmarkr   baser   resultr   r   r   r   r   r   r   Ztime_methodsr   r(   r(   r(   r5   <module>   s     