a
    hy!                     @   sj   d Z ddlZddl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	gZdS )
z!
Processor class for LayoutLMv3.
    N)OptionalUnion   )ProcessorMixin)BatchEncodingPaddingStrategyPreTokenizedInput	TextInputTruncationStrategy)
TensorTypec                       s  e Zd ZdZddgZdZdZd fd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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edd Zedd Zedd Z  ZS )LayoutLMv3Processorav  
    Constructs a LayoutLMv3 processor which combines a LayoutLMv3 image processor and a LayoutLMv3 tokenizer into a
    single processor.

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

    It first uses [`LayoutLMv3ImageProcessor`] to resize and normalize document images, and optionally applies OCR to
    get words and normalized bounding boxes. These are then provided to [`LayoutLMv3Tokenizer`] or
    [`LayoutLMv3TokenizerFast`], 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 (`LayoutLMv3ImageProcessor`, *optional*):
            An instance of [`LayoutLMv3ImageProcessor`]. The image processor is a required input.
        tokenizer (`LayoutLMv3Tokenizer` or `LayoutLMv3TokenizerFast`, *optional*):
            An instance of [`LayoutLMv3Tokenizer`] or [`LayoutLMv3TokenizerFast`]. The tokenizer is a required input.
    image_processor	tokenizerZLayoutLMv3ImageProcessor)ZLayoutLMv3TokenizerZLayoutLMv3TokenizerFastNc                    sd   d }d|v r"t dt |d}|d ur.|n|}|d u rBtd|d u rRt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__ p/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/models/layoutlmv3/processing_layoutlmv3.pyr   3   s    
zLayoutLMv3Processor.__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r0|dur0td| j ||d}|durn| j jrn|du rnt|trf|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 )aF  
        This method first forwards the `images` argument to [`~LayoutLMv3ImageProcessor.__call__`]. In case
        [`LayoutLMv3ImageProcessor`] was initialized with `apply_ocr` set to `True`, it passes the obtained words and
        bounding boxes along with the additional arguments to [`~LayoutLMv3Tokenizer.__call__`] and returns the output,
        together with resized and normalized `pixel_values`. In case [`LayoutLMv3ImageProcessor`] 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 [`~LayoutLMv3Tokenizer.__call__`] and returns the output, together with
        resized and normalized `pixel_values`.

        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.)imagesr.   wordsr   )r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   pixel_valuesToverflow_to_sample_mapping)r   Z	apply_ocrr   
isinstancestrr   r   get_overflowing_images)r   r0   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r   featuresZencoded_inputsr   r   r   __call__E   sP    #

zLayoutLMv3Processor.__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   r0   r3   Zimages_with_overflowZ
sample_idxr   r   r   r6      s    z*LayoutLMv3Processor.get_overflowing_imagesc                 C   s   g dS )N)Z	input_idsZbboxZattention_maskr2   r   r   r   r   r   model_input_names   s    z%LayoutLMv3Processor.model_input_namesc                 C   s   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_classr;   r   r   r   feature_extractor_class   s
    z+LayoutLMv3Processor.feature_extractor_classc                 C   s   t dt | jS )Nz[`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.)r   r   r   r   r;   r   r   r   r      s
    z%LayoutLMv3Processor.feature_extractor)NN)NNNNTFNNr   NNNFFFFTN)__name__
__module____qualname____doc__
attributesr=   Ztokenizer_classr   r   r	   r   listr   intboolr5   r   r
   r   r   r8   r6   propertyr<   r>   r   __classcell__r   r   r   r   r      sf                     "T

r   )rB   r   typingr   r   Zprocessing_utilsr   Ztokenization_utils_baser   r   r   r	   r
   utilsr   r   __all__r   r   r   r   <module>   s    "