
    g                     F    d Z ddlZddlmZ ddlmZ  G d de          ZdS )z%
Image/Text processor class for CLIP
    N   )ProcessorMixin)BatchEncodingc                        e Zd ZdZddgZdZdZd fd	ZddZd	 Z	d
 Z
ed             Zed             Zed             Z x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 ([`CLIPTokenizerFast`], *optional*):
            The tokenizer is a required input.
    image_processor	tokenizerCLIPImageProcessor)CLIPTokenizerCLIPTokenizerFastNc                    d }d|v r/t          j        dt                     |                    d          }||n|}|t	          d          |t	          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__s        d/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/clip/processing_clip.pyr   zCLIPProcessor.__init__+   s     &((M  
 !'

+> ? ?-<-H//N_"HIIIABBB)44444    c                 \    i i }}|r@ fd|                                 D             } fd|                                 D             }||t          d          |  j        |fd|i|}|  j        |fd|i|}|||j        |d<   |S ||S t          t          di ||          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 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`.
        c                 8    i | ]\  }}|j         j        v||S  r   _valid_processor_keys.0kvr   s      r   
<dictcomp>z*CLIPProcessor.__call__.<locals>.<dictcomp>a   s/    wwwA1DL`LvCvCv1CvCvCvr   c                 8    i | ]\  }}|j         j        v ||S r   r   r    s      r   r$   z*CLIPProcessor.__call__.<locals>.<dictcomp>b   s6     & & &A18L8b3b3b13b3b3br   Nz?You have to specify either text or images. Both cannot be none.return_tensorspixel_values)datatensor_typer   )itemsr   r	   r   r'   r   dict)	r   textimagesr&   r   tokenizer_kwargsimage_processor_kwargsencodingimage_featuress	   `        r   __call__zCLIPProcessor.__call__=   s   D 46r0 	wwwwwww& & & &!'& & &" <FN^___%t~d^^>^M]^^H1T1&rrr[qrrN 2'5'BH^$OO d&<&<^&<&<.YYYYr   c                 &     | j         j        |i |S )z
        This method forwards all its arguments to CLIPTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r	   batch_decoder   argsr   s      r   r4   zCLIPProcessor.batch_decodew   s    
 +t~*D;F;;;r   c                 &     | j         j        |i |S )z
        This method forwards all its arguments to CLIPTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r	   decoder5   s      r   r8   zCLIPProcessor.decode~   s    
 %t~$d5f555r   c                     | j         j        }| j        j        }t          t                              ||z                       S )N)r	   model_input_namesr   listr+   fromkeys)r   tokenizer_input_namesimage_processor_input_namess      r   r:   zCLIPProcessor.model_input_names   s:     $ @&*&:&L#DMM"7:U"UVVWWWr   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_classr   s    r   feature_extractor_classz%CLIPProcessor.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   rA   s    r   r   zCLIPProcessor.feature_extractor   s'    i	
 	
 	
 ##r   )NN)NNN)__name__
__module____qualname____doc__
attributesr@   tokenizer_classr   r2   r4   r8   propertyr:   rB   r   __classcell__)r   s   @r   r   r      s          $[1J0<O5 5 5 5 5 5$8Z 8Z 8Z 8Zt< < <6 6 6 X X XX
 * * X* $ $ X$ $ $ $ $r   r   )rG   r   processing_utilsr   tokenization_utils_baser   r   r   r   r   <module>rN      s|      . . . . . . 4 4 4 4 4 4@$ @$ @$ @$ @$N @$ @$ @$ @$ @$r   