Ë
    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defd„Zd„ Z G d„ de«      ZdgZy)z
Processor class for Pixtral.
é    )ÚListÚUnioné   )ÚBatchFeature)Ú
ImageInputÚis_valid_imageÚ
load_image)ÚProcessingKwargsÚProcessorMixinÚUnpackÚ!_validate_images_text_input_order)ÚPreTokenizedInputÚ	TextInput)Úloggingc                   ó    — e Zd Zddii ddidœZy)ÚPixtralProcessorKwargsÚpaddingFÚreturn_tensorsÚpt)Útext_kwargsÚimages_kwargsÚcommon_kwargsN)Ú__name__Ú
__module__Ú__qualname__Ú	_defaults© ó    úl/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/pixtral/processing_pixtral.pyr   r      s$   „ ð �uð
ð à˜dð
ñ�Ir   r   F)ÚtotalÚreturnc                 óH   — t        | t        «      xr | j                  d«      S )NÚhttp)Ú
isinstanceÚstrÚ
startswith)Úvals    r   Úis_urlr(   ,   s   € Ü�cœ3ÓÒ: C§N¡N°6Ó$:Ð:r   c                 ó2   — t        | «      xs t        | «      S ©N)r(   r   )Úelems    r   Úis_image_or_image_urlr,   1   s   € Ü�$‹<Ò/œ>¨$Ó/Ð/r   c            
       ó´   ‡ — e Zd ZdZddgZg d¢ZdZdZ	 	 	 	 	 	 	 	 ddedefˆ 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 )ÚPixtralProcessorab  
    Constructs a Pixtral processor which wraps a Pixtral image processor and a Pixtral tokenizer into a single processor.

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

    Args:
        image_processor ([`PixtralImageProcessor`], *optional*):
            The image processor is a required input.
        tokenizer ([`LlamaTokenizerFast`], *optional*):
            The tokenizer is a required input.
        patch_size (`int`, *optional*, defaults to 16):
            Patch size from the vision tower.
        spatial_merge_size (`int`, *optional*, defaults to 1):
            The downsampling factor for the spatial merge operation.
        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 `"[IMG]"`):
            Special token used to denote image location.
        image_break_token (`str`, *optional*, defaults to `"[IMG_BREAK]"`):
            Special token used to denote the end of a line of pixels in an image.
        image_end_token (`str`, *optional*, defaults to `"[IMG_END]"`):
            Special token used to denote the end of an image input.
    Úimage_processorÚ	tokenizer)Úchat_templateÚ
patch_sizeÚspatial_merge_sizeÚimage_tokenÚimage_break_tokenÚimage_end_tokenÚAutoImageProcessorÚAutoTokenizerr2   r3   c	                 óp   •— || _         || _        || _        || _        || _        t
        ‰
| �  |||¬«       y )N)r1   )r2   r3   r4   r5   r6   ÚsuperÚ__init__)Úselfr/   r0   r2   r3   r1   r4   r5   r6   ÚkwargsÚ	__class__s             €r   r;   zPixtralProcessor.__init__[   s@   ø€ ð %ˆŒØ"4ˆÔØ&ˆÔØ!2ˆÔØ.ˆÔÜ‰Ñ˜¨)À=ÐÕQr   ÚimagesÚtextr=   r!   c                 óº  — t        ||«      \  }} | j                  t        fd| j                  j                  i|¤Ž}| j
                  | j                  z  }|�Ût        |«      r|g}n‰t        |t        t        f«      rt        |d   «      rndt        |t        t        f«      rCt        |d   t        t        f«      r*t        |d   d   «      r|D ��	cg c]  }|D ]  }	|	‘Œ Œ }}}	nt        d«      ‚|D �
cg c]  }
t        |
t        «      rt        |
«      n|
‘Œ! }}
 | j                  |fd|i|d   ¤Ž}ni }t        |t        «      r|g}n.t        |t        «      st        |d   t        «      st        d«      ‚|}|j                  d«      ��t!        |d   «      }g }g }|D ]ý  }| j"                  |v r¯t%        |«      \  }}||z  }||z  }| j"                  g|z  | j&                  gz   g|z  }|D ��cg c]  }|D ]  }|‘Œ Œ }}}| j(                  |d	<   d
j+                  |«      }|j-                  |«       |j/                  | j"                  dd«      }| j"                  |v rŒ¯d|v r)|j1                  d«      }|j/                  d|d«      }d|v rŒ)|j-                  |«       Œÿ  | j                  |fi |d   ¤Ž}t3        i |¥|¥|d   d   ¬«      S c c}	}w c c}
w c c}}w )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`.
        Útokenizer_init_kwargsr   zdInvalid input images. Please provide a single image, a list of images, or a list of lists of images.r2   r   zAInvalid input text. Please provide a string, or a list of stringsÚpixel_valuesÚimage_sizeséÿÿÿÿÚ z<placeholder>é   r   r   r   )ÚdataÚtensor_type)r   Ú_merge_kwargsr   r0   Úinit_kwargsr2   r3   r,   r$   ÚlistÚtupleÚ
ValueErrorr%   r	   r/   ÚgetÚiterr4   Únextr5   r6   ÚjoinÚappendÚreplaceÚpopr   )r<   r?   r@   ÚaudioÚvideosr=   Úoutput_kwargsr2   ÚsublistÚimageÚimÚimage_inputsÚprompt_stringsrD   Úreplace_stringsÚsampleÚheightÚwidthÚnum_height_tokensÚnum_width_tokensÚreplace_tokensÚitemÚreplace_strÚtext_inputss                           r   Ú__call__zPixtralProcessor.__call__n   s'  € ôR 9¸ÀÓF‰ˆ�à*˜×*Ñ*Ü"ñ
à"&§.¡.×"<Ñ"<ð
ð ñ
ˆð —_‘_ t×'>Ñ'>Ñ>ˆ
àÐÜ$ VÔ,Ø ˜‘Ü˜F¤T¬5 MÔ2Ô7LÈVÐTUÉYÔ7WØä˜6¤D¬% =Ô1Ü˜v a™y¬4´¨-Ô8Ü)¨&°©)°A©,Ô7à/5×K GÀ7ÒK¸%š%ÐK˜%ÐK�ÒKä Øzóð ð OUÖUÈ¬
°2´sÔ(;”j ”nÀÑCÐUˆFÐUØ/˜4×/Ñ/°ÑpÀ:ÐpÐQ^Ð_nÑQoÑp‰LàˆLä�dœCÔ Ø�6‰DÜ˜D¤$Ô'´
¸4À¹7ÄCÔ0HÜÐ`ÓaÐað ˆØ×Ñ˜NÓ+Ñ7ä˜|¨MÑ:Ó;ˆKØˆNØ ˆOàò .�Ø×&Ñ&¨&Ñ0Ü$(¨Ó$5‘M�F˜EØ(.°*Ñ(<Ð%Ø',°
Ñ':Ð$à×)Ñ)Ð*Ð-=Ñ=À×AWÑAWÐ@XÑXð&à)ñ&*�Nð ;I×%]¨wÐU\Ò%]ÈT¢dÐ%] dÐ%]�NÑ%]Ø)-×)=Ñ)=�N 2Ñ&Ø"$§'¡'¨.Ó"9�KØ#×*Ñ*¨;Ô7Ø#Ÿ^™^¨D×,<Ñ,<¸oÈqÓQ�Fð ×&Ñ&¨&Ò0ð &¨Ñ/Ø"1×"5Ñ"5°aÓ"8�KØ#Ÿ^™^¨O¸[È!ÓL�Fð &¨Ò/ð ×%Ñ% fÕ-ð%.ð( %�d—n‘n ^ÑT°}À]Ñ7SÑTˆÜØ0�KÐ0 <Ð0¸mÈOÑ>\Ð]mÑ>nô
ð 	
ùóY Lùò
 Vùó6 &^s   ÃKÃ4$KÇ/K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.
        )r0   Úbatch_decode©r<   Úargsr=   s      r   rj   zPixtralProcessor.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.
        )r0   Údecoderk   s      r   rn   zPixtralProcessor.decodeä   s    € ð
 %ˆt�~‰~×$Ñ$ dÐ5¨fÑ5Ð5r   c                 óœ   — | j                   j                  }| j                  j                  }t        t        j                  ||z   «      «      S r*   )r0   Úmodel_input_namesr/   rL   ÚdictÚfromkeys)r<   Útokenizer_input_namesÚimage_processor_input_namess      r   rp   z"PixtralProcessor.model_input_namesë   sA   € ð !%§¡× @Ñ @ÐØ&*×&:Ñ&:×&LÑ&LÐ#Ü”D—M‘MÐ"7Ð:UÑ"UÓVÓWÐWr   )NNé   rG   Nz[IMG]z[IMG_BREAK]z	[IMG_END])NNNN)r   r   r   Ú__doc__Ú
attributesÚvalid_kwargsÚimage_processor_classÚtokenizer_classÚintr;   r   r   r   r   r   r   r   r   rh   rj   rn   Úpropertyrp   Ú__classcell__)r>   s   @r   r.   r.   5   sÜ   ø„ ñð2 $ [Ð1€Jò€Lð 1ÐØ%€Oð ØØØ"#ØØØ'Ø#ñRð ð	Rð
  õRð* "Ø^bØØñk
àðk
ð �IÐ0°$°y±/À4ÐHYÑCZÐZÑ[ðk
ð Ð/Ñ0ðk
ð 
ók
ò\<ò6ð ñXó ôXr   r.   N)rv   Útypingr   r   Úfeature_extraction_utilsr   Úimage_utilsr   r   r	   Úprocessing_utilsr
   r   r   r   Útokenization_utils_baser   r   Úutilsr   Ú
get_loggerr   Úloggerr   Úboolr(   r,   r.   Ú__all__r   r   r   ú<module>rˆ      ss   ðñ÷ å 4ß AÑ Aß kÓ kß CÝ ð 
ˆ×	Ñ	˜HÓ	%€ô	Ð-°Uõ 	ð;�4ó ;ò
0ô{X�~ô {Xð| Ð
�r   