o
    h$                     @   sh   d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	m
Z
mZmZmZ ddlmZ G dd	 d	eZdS )
z 
Processor class for LayoutXLM.
    N)ListOptionalUnion   )ProcessorMixin)BatchEncodingPaddingStrategyPreTokenizedInput	TextInputTruncationStrategy)
TensorTypec                (       sr  e Zd ZdZddgZdZdZd- fdd	Z							
							
	
	
	
			d.dee	e
ee	 ee
 f deee
ee
 f  deeee  eeee   f deeee eee  f  dedeeeef deeeef dee dedee dee dee dedededededeeeef  def&dd Zd!d" Zd#d$ Zd%d& Zed'd( Zed)d* Zed+d, Z  ZS )/LayoutXLMProcessoran  
    Constructs a LayoutXLM processor which combines a LayoutXLM image processor and a LayoutXLM tokenizer into a single
    processor.

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

    It first uses [`LayoutLMv2ImageProcessor`] to resize document images to a fixed size, and optionally applies OCR to
    get words and normalized bounding boxes. These are then provided to [`LayoutXLMTokenizer`] or
    [`LayoutXLMTokenizerFast`], 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).

    Args:
        image_processor (`LayoutLMv2ImageProcessor`, *optional*):
            An instance of [`LayoutLMv2ImageProcessor`]. The image processor is a required input.
        tokenizer (`LayoutXLMTokenizer` or `LayoutXLMTokenizerFast`, *optional*):
            An instance of [`LayoutXLMTokenizer`] or [`LayoutXLMTokenizerFast`]. The tokenizer is a required input.
    image_processor	tokenizerLayoutLMv2ImageProcessor)LayoutXLMTokenizerLayoutXLMTokenizerFastNc                    s`   d|v rt dt |d}|d ur|n|}|d u rtd|d u r'tdt || d S )Nfeature_extractorzhThe `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor` instead.z)You need to specify an `image_processor`.z"You need to specify a `tokenizer`.)warningswarnFutureWarningpop
ValueErrorsuper__init__)selfr   r   kwargsr   	__class__ h/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/models/layoutxlm/processing_layoutxlm.pyr   2   s   
zLayoutXLMProcessor.__init__TFr   text	text_pairboxesword_labelsadd_special_tokenspadding
truncation
max_lengthstridepad_to_multiple_ofreturn_token_type_idsreturn_attention_maskreturn_overflowing_tokensreturn_special_tokens_maskreturn_offsets_mappingreturn_lengthverbosereturn_tensorsreturnc                 K   s\  | j jr|durtd| j jr|durtd|du r$|du r$td| j ||d}|durC| j jrC|du rCt|tr?|g}|d }| jdi d	|durN|n|d d
|durY|ndd|durb|n|d d|d|d|d|d|	d|
d|d|d|d|d|d|d|d|d||}|d}|du r| ||d }||d< |S )a  
        This method first forwards the `images` argument to [`~LayoutLMv2ImagePrpcessor.__call__`]. In case
        [`LayoutLMv2ImagePrpcessor`] was initialized with `apply_ocr` set to `True`, it passes the obtained words and
        bounding boxes along with the additional arguments to [`~LayoutXLMTokenizer.__call__`] and returns the output,
        together with resized `images`. In case [`LayoutLMv2ImagePrpcessor`] 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 [`~LayoutXLMTokenizer.__call__`] and returns the output, together with resized `images``.

        Please refer to the docstring of the above two methods for more information.
        NzdYou 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.TFzKYou cannot return overflowing tokens without returning the offsets mapping.)imagesr2   wordsr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   pixel_valuesoverflow_to_sample_mappingimager   )r   	apply_ocrr   
isinstancestrr   r   get_overflowing_images)r   r4   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r   featuresencoded_inputsr   r   r    __call__C   sz   "

	

zLayoutXLMProcessor.__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lenr   )r   r4   r7   images_with_overflow
sample_idxr   r   r    r<      s   z)LayoutXLMProcessor.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   argsr   r   r   r    rE         zLayoutXLMProcessor.batch_decodec                 O   rD   )z
        This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer
        to the docstring of this method for more information.
        )r   decoderF   r   r   r    rI      rH   zLayoutXLMProcessor.decodec                 C   s   g dS )N)	input_idsbboxattention_maskr8   r   r   r   r   r    model_input_names   s   z$LayoutXLMProcessor.model_input_namesc                 C      t dt | jS )Nzg`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.)r   r   r   image_processor_classrM   r   r   r    feature_extractor_class   
   z*LayoutXLMProcessor.feature_extractor_classc                 C   rO   )Nz[`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.)r   r   r   r   rM   r   r   r    r      rR   z$LayoutXLMProcessor.feature_extractor)NN)NNNNTFNNr   NNNFFFFTN)__name__
__module____qualname____doc__
attributesrP   tokenizer_classr   r   r
   r	   r   r   intboolr;   r   r   r   r   r?   r<   rE   rI   propertyrN   rQ   r   __classcell__r   r   r   r    r      s    	

W

r   )rV   r   typingr   r   r   processing_utilsr   tokenization_utils_baser   r   r	   r
   r   utilsr   r   r   r   r   r    <module>   s   