a
    d                     @   s`   d 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.
    )ListOptionalUnion   )ProcessorMixin)BatchEncodingPaddingStrategyPreTokenizedInput	TextInputTruncationStrategy)
TensorTypec                   @   s   e Zd ZdZdZdZdeeee	e e	e f e
eee	e f  ee	e	e  e	e	e	e   f e
ee	e e	e	e  f  eeeeef eeeef e
e ee
e e
e e
e eeeeee
eeef  edd	d
Zdd Zdd Zdd Zedd ZdS )LayoutXLMProcessorab  
    Constructs a LayoutXLM processor which combines a LayoutXLM feature extractor and a LayoutXLM tokenizer into a
    single processor.

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

    It first uses [`LayoutLMv2FeatureExtractor`] 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:
        feature_extractor (`LayoutLMv2FeatureExtractor`):
            An instance of [`LayoutLMv2FeatureExtractor`]. The feature extractor is a required input.
        tokenizer (`LayoutXLMTokenizer` or `LayoutXLMTokenizerFast`):
            An instance of [`LayoutXLMTokenizer`] or [`LayoutXLMTokenizerFast`]. The tokenizer is a required input.
    ZLayoutLMv2FeatureExtractor)ZLayoutXLMTokenizerZLayoutXLMTokenizerFastNTFr   )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r0|dur0td|du rH|du rHtd| j ||d}|dur| j jr|du rt|tr~|g}|d }| jf |dur|n|d |dur|nd|dur|n|d	 |||||	|
|||||||||d
|}|d}|du r| ||d }||d< |S )a  
        This method first forwards the `images` argument to [`~LayoutLMv2FeatureExtractor.__call__`]. In case
        [`LayoutLMv2FeatureExtractor`] 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 [`LayoutLMv2FeatureExtractor`] 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.
        NzfYou cannot provide bounding boxes if you initialized the feature extractor with apply_ocr set to True.zcYou cannot provide word labels if you initialized the feature extractor with apply_ocr set to True.TFzKYou cannot return overflowing tokens without returning the offsets mapping.)imagesr   wordsr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zpixel_valuesoverflow_to_sample_mappingimage)Zfeature_extractorZ	apply_ocr
ValueError
isinstancestr	tokenizerpopget_overflowing_images)selfr!   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   kwargsfeaturesZencoded_inputs r.   {/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/models/layoutxlm/processing_layoutxlm.py__call__/   sT    "


zLayoutXLMProcessor.__call__c                 C   sL   g }|D ]}| ||  qt|t|krHt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+   r!   r#   Zimages_with_overflowZ
sample_idxr.   r.   r/   r*      s    z)LayoutXLMProcessor.get_overflowing_imagesc                 O   s   | 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/   r3      s    zLayoutXLMProcessor.batch_decodec                 O   s   | j j|i |S )z
        This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer
        to the docstring of this method for more information.
        )r(   decoder4   r.   r.   r/   r6      s    zLayoutXLMProcessor.decodec                 C   s   g dS )N)Z	input_idsZbboxZattention_maskr$   r.   )r+   r.   r.   r/   model_input_names   s    z$LayoutXLMProcessor.model_input_names)NNNNTFNNr   NNNFFFFTN)__name__
__module____qualname____doc__Zfeature_extractor_classZtokenizer_classr   r
   r	   r   r   intboolr'   r   r   r   r   r0   r*   r3   r6   propertyr7   r.   r.   r.   r/   r      s^                     Wr   N)r;   typingr   r   r   Zprocessing_utilsr   Ztokenization_utils_baser   r   r	   r
   r   utilsr   r   r.   r.   r.   r/   <module>   s
   