a
    ½Àh“  ã                   @   s>   d Z ddlZddlmZ ddlmZ G dd„ deƒZdgZdS )z%
Image/Text processor class for CLIP
é    Né   )ÚProcessorMixin)ÚBatchEncodingc                       sT   e Zd ZdZddgZdZdZd‡ fdd„	Zdd	d
„Ze	dd„ ƒZ
e	dd„ ƒZ‡  ZS )ÚCLIPProcessora  
    Constructs a CLIP processor which wraps a CLIP image processor and a CLIP tokenizer into a single processor.

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

    Args:
        image_processor ([`CLIPImageProcessor`], *optional*):
            The image processor is a required input.
        tokenizer ([`AutoTokenizer`], *optional*):
            The tokenizer is a required input.
    Úimage_processorÚ	tokenizer)ZCLIPImageProcessorZCLIPImageProcessorFastZAutoTokenizerNc                    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__© úd/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/models/clip/processing_clip.pyr   +   s    ý
zCLIPProcessor.__init__c           	         sÒ   i i  }}|r:‡ fdd„|  ¡ D ƒ}‡ fdd„|  ¡ D ƒ}|du rR|du rRtdƒ‚|durpˆ j|fd|i|¤Ž}|durŽˆ j|fd|i|¤Ž}|dur¬|dur¬|j|d< |S |dur¸|S ttf i |¤Ž|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 CLIPTokenizerFast's [`~CLIPTokenizerFast.__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 docstring
        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`.
        c                    s"   i | ]\}}|ˆ j jvr||“qS r   ©r   Z_valid_processor_keys©Ú.0ÚkÚv©r   r   r   Ú
<dictcomp>a   ó    z*CLIPProcessor.__call__.<locals>.<dictcomp>c                    s"   i | ]\}}|ˆ j jv r||“qS r   r   r   r   r   r   r   b   s   Nz?You have to specify either text or images. Both cannot be none.Úreturn_tensorsÚpixel_values)ÚdataZtensor_type)Úitemsr   r   r   r   r   Údict)	r   ÚtextZimagesr   r   Ztokenizer_kwargsZimage_processor_kwargsÚencodingZimage_featuresr   r   r   Ú__call__=   s$    "

ÿ
zCLIPProcessor.__call__c                 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_classw   s
    þz%CLIPProcessor.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CLIPProcessor.feature_extractor)NN)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
attributesr&   Ztokenizer_classr   r%   Úpropertyr'   r   Ú__classcell__r   r   r   r   r      s   
:
r   )r+   r	   Zprocessing_utilsr   Ztokenization_utils_baser   r   Ú__all__r   r   r   r   Ú<module>   s
   o