o
    Zh`                     @   s6   d Z ddlmZ ddlmZ G dd deZdgZdS )z
Processor class for TVP.
   )ProcessorMixin)BatchEncodingc                       s`   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
dd Zedd Z  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	tokenizerZTvpImageProcessor)ZBertTokenizerZBertTokenizerFastNc                    s2   |d u rt d|d u r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__ U/var/www/auris/lib/python3.10/site-packages/transformers/models/tvp/processing_tvp.pyr	   )   s
   zTvpProcessor.__init__c           	   	   K   s   | dd}|du r|du rtdi }|dur.| jj|fdd|d|dd|}|| |durB| j|fd|i|}|| t||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 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 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).
            videos (`List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`, `List[List[PIL.Image.Image]]`, `List[List[np.ndarray]]`,:
                `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)Z
truncationpaddingr   Zpad_to_max_lengthreturn_tensorsZreturn_token_type_idsr   )dataZtensor_type)popr   r   Zbatch_encode_plusupdater   r   )	r
   textZvideosr   r   r   encodingZtextual_inputZimage_featuresr   r   r   __call__1   s,   %


zTvpProcessor.__call__c                 O      | 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   r   r   r   r   o      zTvpProcessor.batch_decodec                 O   r   )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   r   r   r   r   v   r   zTvpProcessor.decodec                 C   s>   t | d d | dt | d d | 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
   ZlogitsZvideo_durationsstartendr   r   r   post_process_video_grounding}   s   z)TvpProcessor.post_process_video_groundingc                 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   r'      s   zTvpProcessor.model_input_names)NN)NNN)__name__
__module____qualname____doc__
attributesZimage_processor_classZtokenizer_classr	   r   r   r   r&   propertyr'   __classcell__r   r   r   r   r      s    
>r   N)r.   Zprocessing_utilsr   Ztokenization_utils_baser   r   __all__r   r   r   r   <module>   s    
