
    sgL$                     n    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	e      Zy)
z!
Processor class for LayoutLMv2.
    N)ListOptionalUnion   )ProcessorMixin)BatchEncodingPaddingStrategyPreTokenizedInput	TextInputTruncationStrategy)
TensorTypec            (           e Zd ZdZddgZdZdZd! f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Zd Zd Zd Zed        Zed        Zed         Z xZS )#LayoutLMv2Processorax  
    Constructs a LayoutLMv2 processor which combines a LayoutLMv2 image processor and a LayoutLMv2 tokenizer into a
    single processor.

    [`LayoutLMv2Processor`] 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 [`LayoutLMv2Tokenizer`] or
    [`LayoutLMv2TokenizerFast`], 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 (`LayoutLMv2Tokenizer` or `LayoutLMv2TokenizerFast`, *optional*):
            An instance of [`LayoutLMv2Tokenizer`] or [`LayoutLMv2TokenizerFast`]. The tokenizer is a required input.
    image_processor	tokenizerLayoutLMv2ImageProcessor)LayoutLMv2TokenizerLayoutLMv2TokenizerFastc                     d }d|v r+t        j                  dt               |j                  d      }||n|}|t	        d      |t	        d      t
        |   ||       y )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__s        g/var/www/html/venv/lib/python3.12/site-packages/transformers/models/layoutlmv2/processing_layoutlmv2.pyr   zLayoutLMv2Processor.__init__3   sw     &(MM
 !'

+> ?-<-H/N_"HIIABB)4    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                 T   | j                   j                  r|t        d      | j                   j                  r|t        d      |du r|du rt        d      | j                  ||      }|0| j                   j                  r|t        |t              r|g}|d   } | j
                  di d	||n|d   d
||ndd||n|d   d|d|d|d|d|	d|
d|d|d|d|d|d|d|d|d||}|j                  d      }|du r| j                  ||d         }||d<   |S )a  
        This method first forwards the `images` argument to [`~LayoutLMv2ImageProcessor.__call__`]. In case
        [`LayoutLMv2ImageProcessor`] was initialized with `apply_ocr` set to `True`, it passes the obtained words and
        bounding boxes along with the additional arguments to [`~LayoutLMv2Tokenizer.__call__`] and returns the output,
        together with resized `images`. In case [`LayoutLMv2ImageProcessor`] 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 [`~LayoutLMv2Tokenizer.__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.)imagesr4   wordsr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   pixel_valuesoverflow_to_sample_mappingimage )r   	apply_ocrr   
isinstancestrr   r   get_overflowing_images)r   r7   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r   featuresencoded_inputss                          r!   __call__zLayoutLMv2Processor.__call__E   s   D ))u/@v  )){/Fs  %,1G51Pjkk ''vn'U  4 4 > >9CT$$v )I' 
)x/@
#,#8id
 !,%(72C
 $	

  2
 
 "
 "
 
  2
 #8
 #8
 '@
 (B
 $:
  (!
" #
$ *'
. n-$,00Hd9efF"(wr"   c                     g }|D ]  }|j                  ||           t        |      t        |      k7  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   r7   r:   images_with_overflow
sample_idxs        r!   r@   z*LayoutLMv2Processor.get_overflowing_images   sy    !4 	<J ''z(:;	< #$,F(GG,-.eC8R4S3TV 
 $#r"   c                 :     | 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   s      r!   rJ   z LayoutLMv2Processor.batch_decode   s     
 +t~~**D;F;;r"   c                 :     | 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   decoderK   s      r!   rN   zLayoutLMv2Processor.decode   s     
 %t~~$$d5f55r"   c                 
    g dS )N)	input_idsbboxtoken_type_idsattention_maskr;   r<   r   s    r!   model_input_namesz%LayoutLMv2Processor.model_input_names   s    QQr"   c                 N    t        j                  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_classrT   s    r!   feature_extractor_classz+LayoutLMv2Processor.feature_extractor_class   s"    u	
 )))r"   c                 N    t        j                  dt               | j                  S )Nz[`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.)r   r   r   r   rT   s    r!   r   z%LayoutLMv2Processor.feature_extractor   s"    i	
 ###r"   )NN)NNNNTFFNr   NNNFFFFTN)__name__
__module____qualname____doc__
attributesrW   tokenizer_classr   r   r   r
   r   r   intboolr?   r	   r   r   r   rC   r@   rJ   rN   propertyrU   rX   r   __classcell__)r    s   @r!   r   r      s#   & $[1J6HO5* _cQU?CCG#'5:;@$(,00404*/+0',#;?)T I0$y/4HYCZZ[T E"3T:K5L"LMN	T
 T$s)_d4S	?&;;<T eDItDI$>?@T !T tS/12T $%778T SMT T %SMT  (~T  (~T $(T  %)!T" !%#T$ %T& 'T( !sJ!78)T, 
-Tl$<6 R R * * $ $r"   r   )r]   r   typingr   r   r   processing_utilsr   tokenization_utils_baser   r	   r
   r   r   utilsr   r   r<   r"   r!   <module>rh      s/     ( ( . w w n$. n$r"   