Ë
    T^(h%%  ã                   óÆ   — d Z ddlmZmZ ddlmZ ddlmZmZm	Z	 ddl
mZmZmZmZ ddlmZmZ ddlmZ  ej(                  e«      Z G d	„ d
ed¬«      Z G d„ de«      ZdgZy)z
Processor class for Llava.
é    )ÚListÚUnioné   )ÚBatchFeature)Ú
ImageInputÚget_image_sizeÚto_numpy_array)ÚProcessingKwargsÚProcessorMixinÚUnpackÚ!_validate_images_text_input_order)ÚPreTokenizedInputÚ	TextInput)Úloggingc                   ó   — e Zd Zddii dœZy)ÚLlavaProcessorKwargsÚpaddingF)Útext_kwargsÚimages_kwargsN)Ú__name__Ú
__module__Ú__qualname__Ú	_defaults© ó    úh/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/llava/processing_llava.pyr   r      s   „ ð �uð
ð ñ	�Ir   r   F)Útotalc            
       ó¨   ‡ — e Zd ZdZddgZg d¢ZdZdZ	 	 	 	 	 	 	 dˆ fd„	Z	 	 	 	 dde	d	e
eeee   ee   f   d
ee   defd„Zd„ Zd„ Zed„ «       Zˆ xZS )ÚLlavaProcessoram  
    Constructs a LLaVa processor which wraps a LLaVa image processor and a LLaMa tokenizer into a single processor.

    [`LlavaProcessor`] offers all the functionalities of [`LlavaImageProcessor`] and [`LlamaTokenizerFast`]. See the
    [`~LlavaProcessor.__call__`] and [`~LlavaProcessor.decode`] for more information.

    Args:
        image_processor ([`LlavaImageProcessor`], *optional*):
            The image processor is a required input.
        tokenizer ([`LlamaTokenizerFast`], *optional*):
            The tokenizer is a required input.
        patch_size (`int`, *optional*):
            Patch size from the vision tower.
        vision_feature_select_strategy (`str`, *optional*):
            The feature selection strategy used to select the vision feature from the vision backbone.
            Shoudl be same as in model's config
        chat_template (`str`, *optional*): A Jinja template which will be used to convert lists of messages
            in a chat into a tokenizable string.
        image_token (`str`, *optional*, defaults to `"<image>"`):
            Special token used to denote image location.
        num_additional_image_tokens (`int`, *optional*, defaults to 0):
            Number of additional tokens added to the image embeddings, such as CLS (+1). If the backbone has no CLS or other
            extra tokens appended, no need to set this arg.
    Úimage_processorÚ	tokenizer)Úchat_templateÚ
patch_sizeÚvision_feature_select_strategyÚimage_tokenÚnum_additional_image_tokensÚAutoImageProcessorÚAutoTokenizerc                 ó  •— || _         || _        || _        t        |d«      r|j                  n|| _        t        |dd «      r|j                  n|j                  | j                  «      | _        t        ‰	| �%  |||¬«       y )Nr%   Úimage_token_id)r"   )
r#   r&   r$   Úhasattrr%   Úgetattrr*   Úconvert_tokens_to_idsÚsuperÚ__init__)
Úselfr    r!   r#   r$   r"   r%   r&   ÚkwargsÚ	__class__s
            €r   r/   zLlavaProcessor.__init__M   s‚   ø€ ð %ˆŒØ+FˆÔ(Ø.LˆÔ+Ü4;¸IÀ}Ô4U˜9×0Ò0Ð[fˆÔô �yÐ"2°DÔ9ð ×$Ò$à×0Ñ0°×1AÑ1AÓBð 	Ôô
 	‰Ñ˜¨)À=ÐÕQr   ÚimagesÚtextr1   Úreturnc                 ó  — |€|€t        d«      ‚t        ||«      \  }} | j                  t        fd| j                  j
                  i|¤Ž}|� | j                  |fi |d   ¤Ž}ni }t        |t        «      r|g}n.t        |t        «      st        |d   t        «      st        d«      ‚|}|j                  d«      �¢|d   }	t        t        |	d   «      «      \  }
}|
| j                  z  || j                  z  z  | j                  z   }| j                  dk(  r|dz  }g }|D ]<  }|j!                  | j"                  | j"                  |z  «      }|j%                  |«       Œ>  | j                  |fi |d	   ¤Ž}t'        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 LlamaTokenizerFast's [`~LlamaTokenizerFast.__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:
            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.
            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).
            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:
            [`BatchFeature`]: A [`BatchFeature`] 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`.
        z7You have to specify at least one of `images` or `text`.Útokenizer_init_kwargsr   r   zAInvalid input text. Please provide a string, or a list of stringsÚpixel_valuesÚdefaulté   r   )Údata)Ú
ValueErrorr   Ú_merge_kwargsr   r!   Úinit_kwargsr    Ú
isinstanceÚstrÚlistÚgetr   r	   r#   r&   r$   Úreplacer%   Úappendr   )r0   r3   r4   ÚaudioÚvideosr1   Úoutput_kwargsÚimage_inputsÚprompt_stringsr8   ÚheightÚwidthÚnum_image_tokensÚsampleÚtext_inputss                  r   Ú__call__zLlavaProcessor.__call__c   s²  € ðN ˆ>˜d˜lÜÐVÓWÐWô 9¸ÀÓF‰ˆ�à*˜×*Ñ*Ü ñ
à"&§.¡.×"<Ñ"<ð
ð ñ
ˆð
 ÐØ/˜4×/Ñ/°ÑY¸-ÈÑ:XÑY‰LàˆLä�dœCÔ Ø�6‰DÜ˜D¤$Ô'´
¸4À¹7ÄCÔ0HÜÐ`ÓaÐað ˆØ×Ñ˜NÓ+Ð7à'¨Ñ7ˆLÜ*¬>¸,Àq¹/Ó+JÓK‰MˆF�EØ &¨$¯/©/Ñ 9Ø˜Ÿ™Ñ(ñ à×0Ñ0ñ 1Ðð ×2Ñ2°iÒ?Ø  AÑ%Ð àˆNØò .�ØŸ™¨×(8Ñ(8¸$×:JÑ:JÐM]Ñ:]Ó^�Ø×%Ñ% fÕ-ð.ð %�d—n‘n ^ÑT°}À]Ñ7SÑTˆÜÐ!@ KÐ!@°<Ð!@ÔAÐAr   c                 ó:   —  | j                   j                  |i |¤ŽS )zÂ
        This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r!   Úbatch_decode©r0   Úargsr1   s      r   rQ   zLlavaProcessor.batch_decode´   s    € ð
 +ˆt�~‰~×*Ñ*¨DÐ;°FÑ;Ð;r   c                 ó:   —  | j                   j                  |i |¤ŽS )z¼
        This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r!   ÚdecoderR   s      r   rU   zLlavaProcessor.decode¼   s    € ð
 %ˆt�~‰~×$Ñ$ dÐ5¨fÑ5Ð5r   c                 óœ   — | j                   j                  }| j                  j                  }t        t        j                  ||z   «      «      S )N)r!   Úmodel_input_namesr    rA   ÚdictÚfromkeys)r0   Útokenizer_input_namesÚimage_processor_input_namess      r   rW   z LlavaProcessor.model_input_namesÃ   sA   € ð !%§¡× @Ñ @ÐØ&*×&:Ñ&:×&LÑ&LÐ#Ü”D—M‘MÐ"7Ð:UÑ"UÓVÓWÐWr   )NNNNNz<image>r   )NNNN)r   r   r   Ú__doc__Ú
attributesÚvalid_kwargsÚimage_processor_classÚtokenizer_classr/   r   r   r   r   r   r   r   r   rO   rQ   rU   ÚpropertyrW   Ú__classcell__)r2   s   @r   r   r   (   sÈ   ø„ ñð2 $ [Ð1€Jò€Lð 1ÐØ%€Oð ØØØ'+ØØØ$%õRð0 "Ø^bØØñNBàðNBð �IÐ0°$°y±/À4ÐHYÑCZÐZÑ[ðNBð Ð-Ñ.ðNBð 
óNBòb<ò6ð ñXó ôXr   r   N)r\   Útypingr   r   Úfeature_extraction_utilsr   Úimage_utilsr   r   r	   Úprocessing_utilsr
   r   r   r   Útokenization_utils_baser   r   Úutilsr   Ú
get_loggerr   Úloggerr   r   Ú__all__r   r   r   ú<module>rl      sb   ðñ÷ å 4ß EÑ Eß kÓ kß CÝ ð 
ˆ×	Ñ	˜HÓ	%€ôÐ+°5õ ô`X�^ô `XðF Ð
�r   