
     `i                      x    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	e          Zd	gZdS )
z!
Processor class for LayoutLMv3.
    N)OptionalUnion   )ProcessorMixin)BatchEncodingPaddingStrategyPreTokenizedInput	TextInputTruncationStrategy)
TensorTypec            (       R    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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ed              Zed!             Zed"             Z x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	tokenizerLayoutLMv3ImageProcessor)LayoutLMv3TokenizerLayoutLMv3TokenizerFastNc                     d }d|v r/t          j        dt                     |                    d          }||n|}t	                                          ||           d S )Nfeature_extractorzhThe `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor` instead.)warningswarnFutureWarningpopsuper__init__)selfr   r   kwargsr   	__class__s        /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/layoutlmv3/processing_layoutlmv3.pyr   zLayoutLMv3Processor.__init__3   st     &((M  
 !'

+> ? ?-<-H//N_)44444    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                 2   | j         j        r|t          d          | j         j        r|t          d          |                      ||          }|.| 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||}|                    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.)imagesr2   wordsr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   pixel_valuesToverflow_to_sample_mapping )r   	apply_ocr
ValueError
isinstancestrr   r   get_overflowing_images)r   r5   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r   featuresencoded_inputss                          r   __call__zLayoutLMv3Processor.__call__A   s   F ) 	u/@v   ) 	{/Fs  
 ''vn'UU  4 >9CT$$$ v )I' 
 
 
)x/@
#,#8iid
 !,%%(72C
 $	

  21
 G
 "z
 "z
 6
  21
 #8"7
 #8"7
 '@&?
 (B'A
 $:#9
  (-!
" G#
$ *>'
 
. n--$,,00Hd9effF)/~&r    c                     g }|D ]}|                     ||                    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   r5   r8   images_with_overflow
sample_idxs        r   r>   z*LayoutLMv3Processor.get_overflowing_images   s    !4 	< 	<J ''z(:;;;;#$$,F(G(GGGV,--V V478R4S4SV V  
 $#r    c                 
    g dS )N)	input_idsbboxattention_maskr7   r9   r   s    r   model_input_namesz%LayoutLMv3Processor.model_input_names   s    FFFFr    c                 D    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_classrK   s    r   feature_extractor_classz+LayoutLMv3Processor.feature_extractor_class   s'    u	
 	
 	
 ))r    c                 D    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   rK   s    r   r   z%LayoutLMv3Processor.feature_extractor   s'    i	
 	
 	
 ##r    )NN)NNNNTFNNr   NNNFFFFTN)__name__
__module____qualname____doc__
attributesrN   tokenizer_classr   r   r
   r	   listr   intboolr=   r   r   r   r   rA   r>   propertyrL   rO   r   __classcell__)r   s   @r   r   r      s        & $[1J6HO5 5 5 5 5 5" _cQUIMCG#'5:;?$(,00404*/+0',#;?)R R I0$y/4HYCZZ[R E"3T:K5L"LMN	R
 d49otDcO/DDEFR eDItDI$>?@R !R tS/12R $%778R SMR R %SMR  (~R  (~R $(R  %)!R" !%#R$ %R& 'R( !sJ!78)R, 
-R R R Rh$ $ $ G G XG * * X* $ $ X$ $ $ $ $r    r   )rT   r   typingr   r   processing_utilsr   tokenization_utils_baser   r   r	   r
   r   utilsr   r   __all__r9   r    r   <module>ra      s      " " " " " " " " . . . . . . w w w w w w w w w w w w w w      Z$ Z$ Z$ Z$ Z$. Z$ Z$ Z$z !
!r    