U
    4Af                     @   s8   d Z ddlZddlmZ ddlmZ G dd deZdS )z(
Image/Text processor class for AltCLIP
    N   )ProcessorMixin)BatchEncodingc                       sX   e Zd ZdZddgZdZdZd fdd	Zdd	d
Zdd Z	dd Z
edd Z  ZS )AltCLIPProcessoraD  
    Constructs a AltCLIP processor which wraps a CLIP image processor and a XLM-Roberta tokenizer into a single
    processor.

    [`AltCLIPProcessor`] offers all the functionalities of [`CLIPImageProcessor`] and [`XLMRobertaTokenizerFast`]. See
    the [`~AltCLIPProcessor.__call__`] and [`~AltCLIPProcessor.decode`] for more information.

    Args:
        image_processor ([`CLIPImageProcessor`], *optional*):
            The image processor is a required input.
        tokenizer ([`XLMRobertaTokenizerFast`], *optional*):
            The tokenizer is a required input.
    image_processor	tokenizerZCLIPImageProcessor)ZXLMRobertaTokenizerZXLMRobertaTokenizerFastNc                    sd   d }d|kr"t dt |d}|d k	r.|n|}|d krBtd|d k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__ R/tmp/pip-unpacked-wheel-zw5xktn0/transformers/models/altclip/processing_altclip.pyr   ,   s    
zAltCLIPProcessor.__init__c                 K   s   |dkr|dkrt d|dk	r6| j|fd|i|}|dk	rT| j|fd|i|}|dk	rr|dk	rr|j|d< |S |dk	r~|S ttf ||dS dS )a  
        Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the `text`
        and `kwargs` arguments to XLMRobertaTokenizerFast's [`~XLMRobertaTokenizerFast.__call__`] if `text` is not
        `None` to encode the text. To prepare the image(s), this method forwards the `images` and `kwrags` arguments to
        CLIPImageProcessor's [`~CLIPImageProcessor.__call__`] if `images` is not `None`. Please refer to the doctsring
        of the above two methods for more information.

        Args:
            text (`str`, `List[str]`, `List[List[str]]`):
                The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
                (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
                `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
            images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`):
                The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
                tensor. Both channels-first and channels-last formats are supported.

            return_tensors (`str` or [`~utils.TensorType`], *optional*):
                If set, will return tensors of a particular framework. Acceptable values are:

                - `'tf'`: Return TensorFlow `tf.constant` objects.
                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return NumPy `np.ndarray` objects.
                - `'jax'`: Return JAX `jnp.ndarray` objects.

        Returns:
            [`BatchEncoding`]: A [`BatchEncoding`] with the following fields:

            - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
            - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
              `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
              `None`).
            - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
        Nz?You have to specify either text or images. Both cannot be none.return_tensorspixel_values)dataZtensor_type)r   r   r   r   r   dict)r   textZimagesr   r   encodingZimage_featuresr   r   r   __call__>   s    #
zAltCLIPProcessor.__call__c                 O   s   | j j||S )z
        This method forwards all its arguments to XLMRobertaTokenizerFast's [`~PreTrainedTokenizer.batch_decode`].
        Please refer to the docstring of this method for more information.
        )r   batch_decoder   argsr   r   r   r   r   r   s    zAltCLIPProcessor.batch_decodec                 O   s   | j j||S )z
        This method forwards all its arguments to XLMRobertaTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please
        refer to the docstring of this method for more information.
        )r   decoder   r   r   r   r    y   s    zAltCLIPProcessor.decodec                 C   s"   | j j}| jj}tt|| S )N)r   model_input_namesr   listr   fromkeys)r   Ztokenizer_input_namesZimage_processor_input_namesr   r   r   r!      s    z"AltCLIPProcessor.model_input_names)NN)NNN)__name__
__module____qualname____doc__
attributesZimage_processor_classZtokenizer_classr   r   r   r    propertyr!   __classcell__r   r   r   r   r      s   
4r   )r'   r	   Zprocessing_utilsr   Ztokenization_utils_baser   r   r   r   r   r   <module>   s   