
    gD                     >    d Z ddlmZ ddlmZ  G d de          ZdS )z
Processor class for TVP.
   )ProcessorMixin)BatchEncodingc                   d     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
d Zed             Z xZS )TvpProcessora  
    Constructs an TVP processor which wraps a TVP image processor and a Bert tokenizer into a single processor.

    [`TvpProcessor`] offers all the functionalities of [`TvpImageProcessor`] and [`BertTokenizerFast`]. See the
    [`~TvpProcessor.__call__`] and [`~TvpProcessor.decode`] for more information.

    Args:
        image_processor ([`TvpImageProcessor`], *optional*):
            The image processor is a required input.
        tokenizer ([`BertTokenizerFast`], *optional*):
            The tokenizer is a required input.
    image_processor	tokenizerTvpImageProcessor)BertTokenizerBertTokenizerFastNc                     |t          d          |t          d          t                                          ||           d S )Nz)You need to specify an `image_processor`.z"You need to specify a `tokenizer`.)
ValueErrorsuper__init__)selfr   r   kwargs	__class__s       b/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/tvp/processing_tvp.pyr   zTvpProcessor.__init__)   sM    "HIIIABBB)44444    c           
      *   |                     dd          }||t          d          i }|/ | j        j        |fdd|d|dd|}|                    |           |% | j        |fd|i|}|                    |           t          ||	          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 BertTokenizerFast's [`~BertTokenizerFast.__call__`] if `text` is not `None` to encode
        the text. To prepare the image(s), this method forwards the `videos` and `kwargs` arguments to
        TvpImageProcessor's [`~TvpImageProcessor.__call__`] if `videos` 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).
            videos (`List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`, `List[List[PIL.Image.Image]]`, `List[List[np.ndarrray]]`,:
                `List[List[torch.Tensor]]`): The video or batch of videos to be prepared. Each video should be a list
                of frames, which can be either PIL images or NumPy arrays. In case of NumPy arrays/PyTorch tensors,
                each frame should be of shape (H, W, C), where H and W are frame height and width, and C is a number of
                channels.

            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 `videos` is not `None`.
        max_text_lengthNz?You have to specify either text or videos. Both cannot be none.T
max_lengthF)
truncationpaddingr   pad_to_max_lengthreturn_tensorsreturn_token_type_idsr   )datatensor_type)popr   r   batch_encode_plusupdater   r   )	r   textvideosr   r   r   encodingtextual_inputimage_featuress	            r   __call__zTvpProcessor.__call__1   s    J !**%6==<FN^___<DN<	$*"&-&+	 	 	 	M OOM***1T1&bbb[abbNOON+++(GGGGr   c                 &     | j         j        |i |S )z
        This method forwards all its arguments to BertTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r   batch_decoder   argsr   s      r   r)   zTvpProcessor.batch_decodeo   s    
 +t~*D;F;;;r   c                 &     | j         j        |i |S )z
        This method forwards all its arguments to BertTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r   decoder*   s      r   r-   zTvpProcessor.decodev   s    
 %t~$d5f555r   c                     t          |                                d         d         |z  d          t          |                                d         d         |z  d          }}||fS )a  
        Compute the time of the video.

        Args:
            logits (`torch.Tensor`):
                The logits output of TvpForVideoGrounding.
            video_durations (`float`):
                The video's duration.

        Returns:
            start (`float`):
                The start time of the video.
            end (`float`):
                The end time of the video.
               )roundtolist)r   logitsvideo_durationsstartends        r   post_process_video_groundingz)TvpProcessor.post_process_video_grounding}   s_    " &--//!$Q'/91==&--//!$Q'/91== 
 czr   c                     | j         j        }| j        j        }t          t                              ||z                       S )N)r   model_input_namesr   listdictfromkeys)r   tokenizer_input_namesimage_processor_input_namess      r   r9   zTvpProcessor.model_input_names   s<     !% @&*&:&L#DMM"7:U"UVVWWWr   )NN)NNN)__name__
__module____qualname____doc__
attributesimage_processor_classtokenizer_classr   r'   r)   r-   r7   propertyr9   __classcell__)r   s   @r   r   r      s          $[1J/<O5 5 5 5 5 5<H <H <H <H|< < <6 6 6  . X X XX X X X Xr   r   N)rB   processing_utilsr   tokenization_utils_baser   r    r   r   <module>rK      s}     / . . . . . 4 4 4 4 4 4BX BX BX BX BX> BX BX BX BX BXr   