U
    ,-e/                     @   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 Pix2Struct.
    )ListOptionalUnion   )ProcessorMixin)BatchEncodingPaddingStrategyPreTokenizedInput	TextInputTruncationStrategy)
TensorTypec                       s   e Zd ZdZddgZdZdZ fddZd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ef  edddZdd Zdd Zedd Z  ZS )Pix2StructProcessora  
    Constructs a PIX2STRUCT processor which wraps a BERT tokenizer and PIX2STRUCT image processor into a single
    processor.

    [`Pix2StructProcessor`] offers all the functionalities of [`Pix2StructImageProcessor`] and [`T5TokenizerFast`]. See
    the docstring of [`~Pix2StructProcessor.__call__`] and [`~Pix2StructProcessor.decode`] for more information.

    Args:
        image_processor (`Pix2StructImageProcessor`):
            An instance of [`Pix2StructImageProcessor`]. The image processor is a required input.
        tokenizer (Union[`T5TokenizerFast`, `T5Tokenizer`]):
            An instance of ['T5TokenizerFast`] or ['T5Tokenizer`]. The tokenizer is a required input.
    image_processor	tokenizerZPix2StructImageProcessor)ZT5TokenizerZT5TokenizerFastc                    s   d|_ t || d S )NF)return_token_type_idssuper__init__)selfr   r   	__class__ u/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/transformers/models/pix2struct/processing_pix2struct.pyr   ,   s    zPix2StructProcessor.__init__NTF   r   )textadd_special_tokenspadding
truncation
max_lengthmax_patchesstridepad_to_multiple_ofreturn_attention_maskreturn_overflowing_tokensreturn_special_tokens_maskreturn_offsets_mappingr   return_lengthverbosereturn_tensorsreturnc                 K   s0  |dkr|dkrt d|dkrd| jjsd| j| _| jf |||||||	|
|||||||d|}|S | jjs| j|f||d|}n| j|f|||d|}|dk	r| jjs| jf |||||||	|
|||||||d|}d|kr|d|d< d|kr|d|d	< nd}|dk	r,|| |S )
a  
        This method uses [`Pix2StructImageProcessor.preprocess`] method to prepare image(s) for the model, and
        [`T5TokenizerFast.__call__`] to prepare text for the model.

        Please refer to the docstring of the above two methods for more information.
        Nz*You have to specify either images or text.)r   r   r   r   r   r   r    r!   r"   r#   r$   r   r%   r&   r'   )r'   r   )r'   r   Zheader_textZattention_maskZdecoder_attention_maskZ	input_idsZdecoder_input_ids)
ValueErrorr   Zis_vqar   Zcurrent_processorpopupdate)r   Zimagesr   r   r   r   r   r   r   r    r!   r"   r#   r$   r   r%   r&   r'   kwargsZtext_encodingZencoding_image_processorr   r   r   __call__0   s       


zPix2StructProcessor.__call__c                 O   s   | j j||S )z
        This method forwards all its arguments to Pix2StructTokenizerFast's [`~PreTrainedTokenizer.batch_decode`].
        Please refer to the docstring of this method for more information.
        )r   batch_decoder   argsr,   r   r   r   r.      s    z Pix2StructProcessor.batch_decodec                 O   s   | j j||S )z
        This method forwards all its arguments to Pix2StructTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please
        refer to the docstring of this method for more information.
        )r   decoder/   r   r   r   r1      s    zPix2StructProcessor.decodec                 C   s"   | j j}| jj}tt|| S )N)r   model_input_namesr   listdictfromkeys)r   Ztokenizer_input_namesZimage_processor_input_namesr   r   r   r2      s    z%Pix2StructProcessor.model_input_names)NNTFNNr   r   NNFFFFFTN)__name__
__module____qualname____doc__
attributesZimage_processor_classZtokenizer_classr   r   r
   r	   r   boolstrr   r   r   intr   r   r-   r.   r1   propertyr2   __classcell__r   r   r   r   r      sZ                    `r   N)r9   typingr   r   r   Zprocessing_utilsr   Ztokenization_utils_baser   r   r	   r
   r   utilsr   r   r   r   r   r   <module>   s
   