Ë
    ÜÓ^jÞ,  ã                   óŠ  — d Z ddlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZmZ ddlmZmZmZ ddlmZmZ  ej*                  e«      Zd	Z ed
«      D � cg c]	  } d| d›d�‘Œ c}  ed«      D � cg c]	  } d| d›d�‘Œ c} z   Z G d„ de«      Z G d„ de
d¬«      Zdefd„Zd„ Zd„ Z d„ Z!e G d„ de«      «       Z"dgZ#yc c} w c c} w )z 
Processor class for PaliGemma.
é    Né   )ÚBatchFeature)Ú
ImageInputÚis_valid_image)ÚMultiModalDataÚProcessingKwargsÚProcessorMixinÚ
TextKwargsÚUnpack)Ú
AddedTokenÚPreTokenizedInputÚ	TextInput)Úauto_docstringÚloggingz<image>i   z<locz0>4ú>é€   z<segz0>3c                   ó@   — e Zd ZU dZeez  ee   z  ee   z  dz  ed<   y)ÚPaliGemmaTextKwargsa—  
    suffix (`str`, `list[str]`, `list[list[str]]`):
        The suffixes or batch of suffixes to be encoded. Only necessary for finetuning. See https://github.com/google-research/big_vision/blob/main/big_vision/configs/proj/paligemma/README.md
        for more information. If your prompt is "<image> What is on the image", the suffix corresponds to the expected prediction "a cow sitting on a bench".
    NÚsuffix)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚlistÚ__annotations__© ó    ú}/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/paligemma/processing_paligemma.pyr   r   '   s/   … ñð Ð)Ñ)¨D°©OÑ;¸dÐCTÑ>UÑUÐX\Ñ\Ô\r   r   c                   ó,   — e Zd ZU eed<   dddœddidœZy)ÚPaliGemmaProcessorKwargsÚtext_kwargsF)ÚpaddingÚreturn_mm_token_type_idsÚdata_formatÚchannels_first)r!   Úimages_kwargsN)r   r   r   r   r   Ú	_defaultsr   r   r   r    r    1   s)   … Ø$Ó$ð Ø(-ñ
ð
 Ð+ð
ñ�Ir   r    F)ÚtotalÚreturnc                 óH   — t        | t        «      xr | j                  d«      S )NÚhttp)Ú
isinstanceÚstrÚ
startswith)Úvals    r   Úis_urlr0   ?   s   € Ü�cœ3ÓÒ: C§N¡N°6Ó$:Ð:r   c                 ó2   — t        | «      xs t        | «      S ©N)r0   r   ©Úelems    r   Úis_image_or_image_urlr5   D   s   € Ü�$‹<Ò/œ>¨$Ó/Ð/r   c                 ó<   — t        | t        «      xs t        | «      S r2   )r,   r-   r5   r3   s    r   Ú_is_str_or_imager7   H   s   € Ü�dœSÓ"ÒAÔ&;¸DÓ&AÐAr   c                 ó    — ||z  |z  › |› | › d�S )aZ  
    Builds a string from the input prompt and image tokens.
    For example, for the call:
    build_string_from_input(
        prompt="Prefix str"
        bos_token="<s>",
        image_seq_len=3,
        image_token="<im>",
    )
    The output will be:
    "<im><im><im><s>Initial str"
    Args:
        prompt (`list[Union[str, ImageInput]]`): The input prompt.
        bos_token (`str`): The beginning of sentence token.
        image_seq_len (`int`): The length of the image sequence.
        image_token (`str`): The image token.
        num_images (`int`): Number of images in the prompt.
    ú
r   ©ÚpromptÚ	bos_tokenÚimage_seq_lenÚimage_tokenÚ
num_imagess        r   Úbuild_string_from_inputr@   L   s$   € ð& ˜MÑ)¨JÑ6Ð7¸	°{À6À(È"ÐMÐMr   c            
       ó�   ‡ — e Zd Z	 	 	 d
ˆ fd„	Ze	 	 ddedz  deez  ee   z  ee   z  de	e
   defd„«       Zdd„Zed	„ «       Zˆ xZS )ÚPaliGemmaProcessorNc                 ó¸  •— t        |d«      st        d«      ‚|j                  | _        t        |d«      sNt        t        dd¬«      }d|gi}|j                  |«       |j                  t        «      | _        t        | _        n"|j                  | _        |j                  | _        |j                  t        «       d|_        d|_        t        ‰| �9  |||¬«       y )	NÚimage_seq_lengthz;Image processor is missing an `image_seq_length` attribute.r>   FT)Ú
normalizedÚspecialÚadditional_special_tokens)Úchat_template)ÚhasattrÚ
ValueErrorrD   r   ÚIMAGE_TOKENÚadd_special_tokensÚconvert_tokens_to_idsÚimage_token_idr>   Ú
add_tokensÚEXTRA_TOKENSÚadd_bos_tokenÚadd_eos_tokenÚsuperÚ__init__)ÚselfÚimage_processorÚ	tokenizerrH   Úkwargsr>   Útokens_to_addÚ	__class__s          €r   rT   zPaliGemmaProcessor.__init__d   sÅ   ø€ ô �Ð(:Ô;ÜÐZÓ[Ð[à /× @Ñ @ˆÔä�y -Ô0Ü$¤[¸UÈDÔQˆKØ8¸;¸-ÐHˆMØ×(Ñ(¨Ô7Ø"+×"AÑ"AÄ+Ó"NˆDÔÜ*ˆDÕà"+×":Ñ":ˆDÔØ(×4Ñ4ˆDÔà×Ñœ\Ô*Ø"'ˆ	ÔØ"'ˆ	Ôä‰Ñ˜¨)À=ÐÕQr   ÚimagesÚtextrX   r)   c                 óì  —  | j                   t        fd| j                  j                  i|¤Ž}|d   j	                  dd«      }d}|€t        d«      ‚|€t        j                  d«       d}t        |«      r|g}nt        |t        «      rt        |d	   «      r	 |��#|�� t        d
„ |D «       «      �sjt        j                  d«       t        |t        «      rKt        |t        «      r;t        |«      t        |«      k7  r$t        dt        |«      › dt        |«      › d�«      ‚t        |«      r|gg}n€t        |t        t        f«      rt        |d	   «      r|D �cg c]  }|g‘Œ }}nKt        |t        t        f«      r*t        |d	   t        t        f«      rt        |d	   d	   «      st        d«      ‚t!        ||«      D ��	cg c]R  \  }}	t#        || j                  j$                  | j&                  t(        t        |	t        «      rt        |	«      nd¬«      ‘ŒT }
}}	n£g }|D ]Š  }|j+                  t(        t(        | j&                  z  «      }|j-                  t(        «      }|dk7  r|t        t(        «      z   nd	}|d| | j                  j$                  z   ||d z   }|j/                  |«       ŒŒ |D �cg c]  }|› d�‘Œ	 }
}|�t        |«      r|g}|�&|D �cg c]  }|| j                  j0                  z   ‘Œ }} | j2                  |fi |d   ¤Žd   }|d   j	                  dd«      }|d   j	                  dd«      } | j                  
f||dœ|d   ¤Ž}| j5                  |
|dg¬«       i |¥d|i¥}|rIt7        j8                  |d   «      }d|t7        j8                  |d   «      d	k(  <   |j;                  d|i«       |r| j=                  |d   «      |d<   t?        ||¬ «      S c c}w c c}	}w c c}w c c}w )!aô  
        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`. If `suffix`
              is provided, the `input_ids` will also contain the suffix input ids.
            - **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`.
            - **labels** -- Labels compatible with training if `suffix` is not None
        Útokenizer_init_kwargsr!   r   NTzF`images` are expected as arguments to a `PaliGemmaProcessor` instance.z]You are using PaliGemma without a text prefix. It will perform as a picture-captioning model.Ú r   c              3   ó,   K  — | ]  }t         |v –— Œ y ­wr2   )rK   )Ú.0Úsamples     r   ú	<genexpr>z.PaliGemmaProcessor.__call__.<locals>.<genexpr>«   s   è ø€ Ò@°”{ fÔ,Ñ@ùs   ‚aL  You are passing both `text` and `images` to `PaliGemmaProcessor`. The processor expects special image tokens in the text, as many tokens as there are images per each text. It is recommended to add `<image>` tokens in the very beginning of your text. For this call, we will infer how many images each text has and add special tokens.z	Received z images for zK prompts. Each prompt should be associated with an image or list of images.zAimages must be an image, list of images or list of list of imagesé   r:   éÿÿÿÿr9   r&   Úpixel_valuesÚreturn_tensorsr#   )Ú	text_pairÚreturn_token_type_idsÚimage)Ú
modalitiesÚ	input_idsiœÿÿÿÚtoken_type_idsÚlabelsÚmm_token_type_ids)ÚdataÚtensor_type) Ú_merge_kwargsr    rW   Úinit_kwargsÚpoprJ   ÚloggerÚwarning_oncer7   r,   r   ÚanyÚwarningÚlenr   ÚtupleÚzipr@   r<   rD   rK   ÚreplaceÚrfindÚappendÚ	eos_tokenrV   Ú_check_special_mm_tokensÚnpÚarrayÚupdateÚcreate_mm_token_type_idsr   )rU   r[   r\   rX   Úoutput_kwargsr   ri   rj   r;   Ú
image_listÚinput_stringsÚexpanded_samplesrb   Úexpanded_sampleÚbos_rfind_indexÚ	bos_indexÚsfxrf   rg   r#   ÚinputsÚreturn_datarn   s                          r   Ú__call__zPaliGemmaProcessor.__call__€   s0  € ð( +˜×*Ñ*Ü$ñ
à"&§.¡.×"<Ñ"<ð
ð ñ
ˆð
 ˜}Ñ-×1Ñ1°(¸DÓAˆà $Ðàˆ>ÜÐeÓfÐfØˆ<Ü×ÑØoôð ˆDä˜DÔ!Ø�6‰DÜ˜œdÔ#Ô(8¸¸a¹Ô(AØàÑ Ñ 2ÜÑ@¸4Ô@Õ@Ü—‘ð<ôô ˜d¤DÔ)¬j¸ÄÔ.FÜ˜6“{¤c¨$£iÒ/Ü(Ø'¬¨F« }°LÄÀTÃÀð  LWð  Xóð ô
 " &Ô)Ø%˜h˜Z‘FÜ ¬¬u¨Ô6¼>È&ÐQRÉ)Ô;TØ39Ö:¨%˜ušgÐ:�FÑ:ä˜v¬¬e }Ô5Ü" 6¨!¡9¬t´U¨mÔ<Ü& v¨a¡y°¡|Ô4ä$Ð%hÓiÐiô /2°$¸Ó.?÷	!ñ +˜ 
ô ,Ø%Ø"&§.¡.×":Ñ":Ø&*×&;Ñ&;Ü$/Ü6@ÀÌTÔ6R¤3 z¤?ÐXYöð	!�ò 	!ð $&Ð Ø"ò =�FØ&,§n¡n´[Ä+ÐPT×PeÑPeÑBeÓ&f�OØ&5×&;Ñ&;¼KÓ&H�OØFUÐY[ÒF[ ´#´kÓ2BÒ BÐab�Ià'¨
¨Ð3°d·n±n×6NÑ6NÑNÐQ`ÐajÐakÐQlÑlð $ð %×+Ñ+¨OÕ<ð=ð >NÖ N°6 F 8¨2¢Ð N�Ð NàÐÔ"2°6Ô":Ø�XˆFØÐØ@FÖG¸�c˜DŸN™N×4Ñ4Ó4ÐGˆFÐGØ+�t×+Ñ+¨FÑU°mÀOÑ6TÑUÐVdÑeˆà& }Ñ5×9Ñ9Ð:JÈDÓQˆØ#0°Ñ#?×#CÑ#CÐD^Ð`dÓ#eÐ Ø�—‘Øð
àØ"7ñ
ð ˜MÑ*ñ	
ˆð 	×%Ñ% m°VÈÈ	Ð%ÔRà>˜Ð> °Ñ>ˆñ !Ü—X‘X˜f [Ñ1Ó2ˆFØ>BˆF”2—8‘8˜FÐ#3Ñ4Ó5¸Ñ:Ñ;Ø×Ñ ¨&Ð1Ô2á#Ø/3×/LÑ/LÈ[ÐYdÑMeÓ/fˆKÐ+Ñ,Ü ¸.ÔIÐIùòq ;ùó	!ùò( !Oùò
 Hs   Å
O!ÇAO&Ê1O,Ë O1c                 ó˜   — i }|�<| j                   gt        |«      z  }dgt        |«      z  }|j                  ||dœ«       t        di |¤ŽS )a¸  
        Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.

        Args:
            image_sizes (list[list[str]], *optional*):
                The input sizes formatted as (height, width) per each image.
        Returns:
            `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
            input modalities, along with other useful data.
        rd   )Únum_image_tokensÚnum_image_patchesr   )rD   ry   rƒ   r   )rU   Úimage_sizesrX   Úvision_datar‘   r’   s         r   Ú_get_num_multimodal_tokensz-PaliGemmaProcessor._get_num_multimodal_tokens÷   s]   € ð ˆØÐ"Ø $× 5Ñ 5Ð6¼¸[Ó9IÑIÐØ!" ¤c¨+Ó&6Ñ 6ÐØ×ÑÐ4DÐ[lÑmÔnÜÑ, Ñ,Ð,r   c                 ó€   — | j                   j                  ddgz   }| j                  j                  }t        ||z   «      S )Nrm   rn   )rW   Úmodel_input_namesrV   r   )rU   Útokenizer_input_namesÚimage_processor_input_namess      r   r—   z$PaliGemmaProcessor.model_input_names	  sB   € à $§¡× @Ñ @ÐDTÐV^ÐC_Ñ _ÐØ&*×&:Ñ&:×&LÑ&LÐ#ÜÐ)Ð,GÑGÓHÐHr   )NNN)NNr2   )r   r   r   rT   r   r   r   r   r   r   r    r   r�   r•   Úpropertyr—   Ú__classcell__)rZ   s   @r   rB   rB   b   s£   ø„ ð ØØõ	Rð8 ð %)ØZ^ñtJà˜TÑ!ðtJð Ð+Ñ+¨d°9©oÑ=ÀÐEVÑ@WÑWðtJð Ð1Ñ2ð	tJð
 
òtJó ðtJól-ð$ ñIó ôIr   rB   )$r   Únumpyr�   Úfeature_extraction_utilsr   Úimage_utilsr   r   Úprocessing_utilsr   r   r	   r
   r   Útokenization_utils_baser   r   r   Úutilsr   r   Ú
get_loggerr   ru   rK   ÚrangerP   r   r    Úboolr0   r5   r7   r@   rB   Ú__all__)Úis   0r   ú<module>r§      sò   ðñó å 4ß 5÷õ ÷ PÑ Oß ,ð 
ˆ×	Ñ	˜HÓ	%€à€Ù).¨t«Ö5 A�$�q˜�g˜Q’Ò5ÑRWÐX[ÓR\Ö8]ÈQ¸4ÀÀ#¸wÀaºÒ8]Ñ]€ô]˜*ô ]ô
Ð/°uõ 
ð;�4ó ;ò
0òBòNð, ôjI˜ó jIó ðjIðZ  Ð
 �ùòY 6ùÒ8]s   ÁB;Á-C 