o
    Zh2'                     @   s   d Z ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZ ddlmZmZ eeZG d	d
 d
eddZG dd deddZG dd deZdgZdS )z
Processor class for UDOP.
    )ListOptionalUnion)logging   )BatchFeature)
ImageInput)ProcessingKwargsProcessorMixin
TextKwargsUnpack)PreTokenizedInput	TextInputc                   @   sR   e Zd ZU eeee eee  f  ed< eeee  eeee   f ed< dS )UdopTextKwargsword_labelsboxesN)__name__
__module____qualname__r   r   r   int__annotations__ r   r   W/var/www/auris/lib/python3.10/site-packages/transformers/models/udop/processing_udop.pyr       s   
  (r   F)totalc                
   @   s4   e Zd ZU eed< dddddddddd	i dZdS )UdopProcessorKwargstext_kwargsTFr   )	Zadd_special_tokenspaddingZ
truncationZstridereturn_overflowing_tokensZreturn_special_tokens_maskreturn_offsets_mappingZreturn_lengthverbose)r   images_kwargsN)r   r   r   r   r   	_defaultsr   r   r   r   r   %   s   
 
r   c                       s   e Zd ZdZddgZdZdZdgZ fddZ				dd	d	d
de	e
 deeeee ee f dee defddZdd Zdd Zdd Zedd Z  ZS )UdopProcessora  
    Constructs a UDOP processor which combines a LayoutLMv3 image processor and a UDOP tokenizer into a single processor.

    [`UdopProcessor`] offers all the functionalities you need to prepare data for the model.

    It first uses [`LayoutLMv3ImageProcessor`] to resize, rescale and normalize document images, and optionally applies OCR
    to get words and normalized bounding boxes. These are then provided to [`UdopTokenizer`] or [`UdopTokenizerFast`],
    which turns the words and bounding boxes into token-level `input_ids`, `attention_mask`, `token_type_ids`, `bbox`.
    Optionally, one can provide integer `word_labels`, which are turned into token-level `labels` for token
    classification tasks (such as FUNSD, CORD).

    Additionally, it also supports passing `text_target` and `text_pair_target` to the tokenizer, which can be used to
    prepare labels for language modeling tasks.

    Args:
        image_processor (`LayoutLMv3ImageProcessor`):
            An instance of [`LayoutLMv3ImageProcessor`]. The image processor is a required input.
        tokenizer (`UdopTokenizer` or `UdopTokenizerFast`):
            An instance of [`UdopTokenizer`] or [`UdopTokenizerFast`]. The tokenizer is a required input.
    image_processor	tokenizerZLayoutLMv3ImageProcessor)ZUdopTokenizerZUdopTokenizerFast	text_pairc                    s   t  || d S )N)super__init__)selfr#   r$   	__class__r   r   r'   S   s   zUdopProcessor.__init__N)audiovideosimagestextkwargsreturnc                O   s  | j tfd| jji|| j| }|d dd}|d dd}	|d dd}
|d dd}|d d	d}|d d
d}| jjrN|durNt	d| jjrZ|	durZt	d|rb|sbt	d|durp| jdi |d S | jdd|i|d }|dd}|dd}|d d
d |d dd |
|d d< |dur|n||d d< |	|d d< |dur| jjr|
du rt
|tr|g}||d d< | jdd|dur|n|i|d }|du r| |d |d |d< || |S )a~  
        This method first forwards the `images` argument to [`~UdopImageProcessor.__call__`]. In case
        [`UdopImageProcessor`] was initialized with `apply_ocr` set to `True`, it passes the obtained words and
        bounding boxes along with the additional arguments to [`~UdopTokenizer.__call__`] and returns the output,
        together with the prepared `pixel_values`. In case [`UdopImageProcessor`] was initialized with `apply_ocr` set
        to `False`, it passes the words (`text`/``text_pair`) and `boxes` specified by the user along with the
        additional arguments to [`~UdopTokenizer.__call__`] and returns the output, together with the prepared
        `pixel_values`.

        Alternatively, one can pass `text_target` and `text_pair_target` to prepare the targets of UDOP.

        Please refer to the docstring of the above two methods for more information.
        Ztokenizer_init_kwargsr   r   Nr   r%   r   Fr   text_targetzdYou cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True.zaYou cannot provide word labels if you initialized the image processor with apply_ocr set to True.zKYou cannot return overflowing tokens without returning the offsets mapping.r-   r    wordsZtext_pair_targetr.   Tpixel_valuesoverflow_to_sample_mappingr   )Z_merge_kwargsr   r$   Zinit_kwargsZ'prepare_and_validate_optional_call_argspopgetr#   Z	apply_ocr
ValueError
isinstancestrget_overflowing_imagesupdate)r(   r-   r.   r+   r,   argsr/   Zoutput_kwargsr   r   r%   r   r   r1   featuresZfeatures_wordsZfeatures_boxesZencoded_inputsr   r   r   __call__V   sh   

zUdopProcessor.__call__c                 C   sL   g }|D ]	}| ||  qt|t|kr$tdt| dt| |S )Nz`Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got z and )appendlenr7   )r(   r-   r4   Zimages_with_overflowZ
sample_idxr   r   r   r:      s   z$UdopProcessor.get_overflowing_imagesc                 O      | j j|i |S )z
        This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r$   batch_decoder(   r<   r/   r   r   r   rB         zUdopProcessor.batch_decodec                 O   rA   )z
        This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer
        to the docstring of this method for more information.
        )r$   decoderC   r   r   r   rE      rD   zUdopProcessor.decodec                 C   s   g dS )N)r3   Z	input_idsZbboxZattention_maskr   )r(   r   r   r   model_input_names   s   zUdopProcessor.model_input_names)NN)r   r   r   __doc__
attributesZimage_processor_classZtokenizer_classZoptional_call_argsr'   r   r   r   r   r   r   r   r   r   r>   r:   rB   rE   propertyrF   __classcell__r   r   r)   r   r"   7   s4    

_r"   N)rG   typingr   r   r   Ztransformersr   Zimage_processing_utilsr   Zimage_utilsr   Zprocessing_utilsr	   r
   r   r   Ztokenization_utils_baser   r   Z
get_loggerr   loggerr   r   r"   __all__r   r   r   r   <module>   s   
 
"