§
    ‚ŠtjÞ,  ã                   ót  — 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d
„  ed¦  «        D ¦   «         d„  ed¦  «        D ¦   «         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#dS )z 
Processor class for PaliGemma.
é    Né   )ÚBatchFeature)Ú
ImageInputÚis_valid_image)ÚMultiModalDataÚProcessingKwargsÚProcessorMixinÚ
TextKwargsÚUnpack)Ú
AddedTokenÚPreTokenizedInputÚ	TextInput)Úauto_docstringÚloggingz<image>c                 ó   — g | ]	}d |d›d�‘Œ
S )z<locz0>4ú>© ©Ú.0Úis     úp/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/paligemma/processing_paligemma.pyú
<listcomp>r   $   s"   € Ð5Ð5Ð5 A��q����Ð5Ð5Ð5ó    i   c                 ó   — g | ]	}d |d›d�‘Œ
S )z<segz0>3r   r   r   s     r   r   r   $   s"   € Ð8]Ð8]Ð8]ÈQ¸À¸¸¸¸Ð8]Ð8]Ð8]r   é€   c                   óN   — e Zd ZU dZeez  ee         z  ee         z  dz  ed<   dS )Ú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__r   r   r   r   r   '   sJ   € € € € € € ðð ð Ð)Ñ)¨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dS )Ú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   sG   € € € € € € Ø$Ð$Ð$Ñ$ð Ø(-ð
ð 
ð
 Ð+ð
ðð €I€I€Ir   r&   F)ÚtotalÚreturnc                 óV   — t          | t          ¦  «        o|                      d¦  «        S )NÚhttp)Ú
isinstanceÚstrÚ
startswith)Úvals    r   Úis_urlr6   ?   s#   € Ý�c�3ÑÔÐ: C§N¢N°6Ñ$:Ô$:Ð:r   c                 ó>   — t          | ¦  «        pt          | ¦  «        S ©N)r6   r   ©Úelems    r   Úis_image_or_image_urlr;   D   s   € Ý�$‰<Œ<Ð/�>¨$Ñ/Ô/Ð/r   c                 óJ   — t          | t          ¦  «        pt          | ¦  «        S r8   )r2   r3   r;   r9   s    r   Ú_is_str_or_imager=   H   s    € Ý�d�SÑ"Ô"ÐAÕ&;¸DÑ&AÔ&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_inputrF   L   s'   € ð& ˜MÑ)¨JÑ6ÐM¸	ÐMÀ6ÐMÐMÐ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¦  «        s]t          t          dd¬¦  «        }d|gi}|                     |¦  «         |                     t          ¦  «        | _        t          | _        n|j        | _        |j        | _        | 	                    t          ¦  «         d|_        d|_        t          ¦   «                              |||¬¦  «         d S )	NÚimage_seq_lengthz;Image processor is missing an `image_seq_length` attribute.rD   FT)Ú
normalizedÚspecialÚadditional_special_tokens)Úchat_template)ÚhasattrÚ
ValueErrorrJ   r   ÚIMAGE_TOKENÚadd_special_tokensÚconvert_tokens_to_idsÚimage_token_idrD   Ú
add_tokensÚEXTRA_TOKENSÚadd_bos_tokenÚadd_eos_tokenÚsuperÚ__init__)ÚselfÚimage_processorÚ	tokenizerrN   ÚkwargsrD   Útokens_to_addÚ	__class__s          €r   rZ   zPaliGemmaProcessor.__init__d   sù   ø€ õ �Ð(:Ñ;Ô;ð 	\ÝÐZÑ[Ô[Ð[à /Ô @ˆÔå�y -Ñ0Ô0ð 	5Ý$¥[¸UÈDÐQÑQÔQˆKØ8¸;¸-ÐHˆMØ×(Ò(¨Ñ7Ô7Ð7Ø"+×"AÒ"AÅ+Ñ"NÔ"NˆDÔÝ*ˆDÔÐà"+Ô":ˆDÔØ(Ô4ˆDÔà×Ò�\Ñ*Ô*Ð*Ø"'ˆ	ÔØ"'ˆ	Ôå‰Œ×Ò˜¨)À=ÐÑQÔQÐQÐQÐQr   ÚimagesÚtextr^   r/   c                 ó*  ‡ —  ‰ j         t          fd‰ j        j        i|¤Ž}|d                              dd¦  «        }d}|€t          d¦  «        ‚|€t                               d¦  «         d}t          |¦  «        r|g}n+t          |t          ¦  «        rt          |d	         ¦  «        r	 |��3|��0t          d
„ |D ¦   «         ¦  «        �skt                               d¦  «         t          |t          ¦  «        ret          |t          ¦  «        rPt          |¦  «        t          |¦  «        k    r0t          dt          |¦  «        › dt          |¦  «        › d�¦  «        ‚t          |¦  «        r|gg}n¦t          |t          t          f¦  «        r"t          |d	         ¦  «        rd„ |D ¦   «         }nht          |t          t          f¦  «        r=t          |d	         t          t          f¦  «        rt          |d	         d	         ¦  «        st          d¦  «        ‚ˆ fd„t!          ||¦  «        D ¦   «         }n«g }|D ]š}	|	                     t$          t$          ‰ j        z  ¦  «        }
|
                     t$          ¦  «        }|dk    r|t          t$          ¦  «        z   nd	}|
d|…         ‰ j        j        z   |
|d…         z   }
|                     |
¦  «         Œ›d„ |D ¦   «         }|�t          |¦  «        r|g}|�ˆ fd„|D ¦   «         } ‰ j        |fi |d         ¤Žd         }|d                              dd¦  «        }|d                              dd¦  «        } ‰ j        |f||dœ|d         ¤Ž}‰                      ||dg¬¦  «         i |¥d|i¥}|rRt3          j        |d         ¦  «        }d|t3          j        |d         ¦  «        d	k    <   |                     d|i¦  «         |r‰                      |d         ¦  «        |d <   t;          ||¬!¦  «        S )"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 V — Œd S r8   )rQ   ©r   Úsamples     r   ú	<genexpr>z.PaliGemmaProcessor.__call__.<locals>.<genexpr>«   s'   è è € Ð@Ð@°•{ fÐ,Ð@Ð@Ð@Ð@Ð@Ð@r   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.c                 ó   — g | ]}|g‘ŒS r   r   )r   Úimages     r   r   z/PaliGemmaProcessor.__call__.<locals>.<listcomp>½   s   € Ð:Ð:Ð:¨%˜u˜gÐ:Ð:Ð:r   zAimages must be an image, list of images or list of list of imagesc                 ó®   •— g | ]Q\  }}t          |‰j        j        ‰j        t          t          |t          ¦  «        rt          |¦  «        nd ¬¦  «        ‘ŒRS )é   r@   )rF   r]   rB   rJ   rQ   r2   r#   Úlen)r   rA   Ú
image_listr[   s      €r   r   z/PaliGemmaProcessor.__call__.<locals>.<listcomp>Å   so   ø€ ð 	!ð 	!ð 	!ñ +˜ 
õ ,Ø%Ø"&¤.Ô":Ø&*Ô&;Ý$/Ý6@ÀÍTÑ6RÔ6RÐ#Y¥3 z¡?¤? ?ÐXYðñ ô ð	!ð 	!ð 	!r   éÿÿÿÿc                 ó   — g | ]}|› d �‘ŒS )r?   r   rg   s     r   r   z/PaliGemmaProcessor.__call__.<locals>.<listcomp>Ù   s   € Ð NÐ NÐ N°6 F   Ð NÐ NÐ Nr   c                 ó.   •— g | ]}|‰j         j        z   ‘ŒS r   )r]   Ú	eos_token)r   Úsfxr[   s     €r   r   z/PaliGemmaProcessor.__call__.<locals>.<listcomp>Þ   s#   ø€ ÐGÐGÐG¸�c˜DœNÔ4Ñ4ÐGÐGÐGr   r,   Úpixel_valuesÚreturn_tensorsr)   )Ú	text_pairÚreturn_token_type_idsrk   )Ú
modalitiesÚ	input_idsiœÿÿÿÚtoken_type_idsÚlabelsÚmm_token_type_ids)ÚdataÚtensor_type)Ú_merge_kwargsr&   r]   Úinit_kwargsÚpoprP   ÚloggerÚwarning_oncer=   r2   r#   ÚanyÚwarningrn   r   ÚtupleÚzipÚreplacerQ   rJ   ÚrfindrB   Úappendr\   Ú_check_special_mm_tokensÚnpÚarrayÚupdateÚcreate_mm_token_type_idsr   )r[   ra   rb   r^   Úoutput_kwargsr   rx   Úinput_stringsÚexpanded_samplesrh   Úexpanded_sampleÚbos_rfind_indexÚ	bos_indexru   rv   r)   ÚinputsÚreturn_datar|   s   `                  r   Ú__call__zPaliGemmaProcessor.__call__€   så  ø€ ð( +˜Ô*Ý$ð
ð 
à"&¤.Ô"<ð
ð ð
ð 
ˆð
 ˜}Ô-×1Ò1°(¸DÑAÔAˆà $Ðàˆ>ÝÐeÑfÔfÐfØˆ<Ý×ÒØoñô ð ð ˆDå˜DÑ!Ô!ð 	Ø�6ˆDˆDÝ˜�dÑ#Ô#ð 	Õ(8¸¸a¼Ñ(AÔ(Að 	ØàÑ Ñ 2ÝÐ@Ð@¸4Ð@Ñ@Ô@Ñ@Ô@ñ .OÝ—’ð<ñô ð õ ˜d¥DÑ)Ô)ð ­j¸ÅÑ.FÔ.Fð Ý˜6‘{”{¥c¨$¡i¤iÒ/Ð/Ý(ð X­¨F©¬ð  Xð  XÅÀTÁÄð  Xð  Xð  Xñô ð õ
 " &Ñ)Ô)ð 	jØ%˜h˜Z�F�FÝ ­­u¨Ñ6Ô6ð j½>È&ÐQRÌ)Ñ;TÔ;Tð jØ:Ð:°6Ð:Ñ:Ô:�F�Få˜v­­e }Ñ5Ô5ðjå" 6¨!¤9­tµU¨mÑ<Ô<ðjõ ' v¨a¤y°¤|Ñ4Ô4ðjõ
 %Ð%hÑiÔiÐið	!ð 	!ð 	!ð 	!õ /2°$¸Ñ.?Ô.?ð	!ñ 	!ô 	!��ð $&Ð Ø"ð =ð =�FØ&,§n¢nµ[Å+ÐPTÔPeÑBeÑ&fÔ&f�OØ&5×&;Ò&;½KÑ&HÔ&H�OØFUÐY[ÒF[ÐF[ µ#µkÑ2BÔ2BÑ BÐ BÐab�Ià'¨
¨¨
Ô3°d´nÔ6NÑNÐQ`ÐajÐakÐakÔQlÑlð $ð %×+Ò+¨OÑ<Ô<Ð<Ð<Ø NÐ NÐ=MÐ NÑ NÔ N�àÐÕ"2°6Ñ":Ô":ÐØ�XˆFØÐØGÐGÐGÐGÀÐGÑGÔGˆFØ+�tÔ+¨FÐUÐU°mÀOÔ6TÐUÐUÐVdÔeˆà& }Ô5×9Ò9Ð:JÈDÑQÔQˆØ#0°Ô#?×#CÒ#CÐD^Ð`dÑ#eÔ#eÐ Ø�”Øð
àØ"7ð
ð 
ð ˜MÔ*ð	
ð 
ˆð 	×%Ò% m°VÈÈ	Ð%ÑRÔRÐRà>˜Ð> °Ð>Ð>ˆð !ð 	3Ý”X˜f [Ô1Ñ2Ô2ˆFØ>BˆF•2”8˜FÐ#3Ô4Ñ5Ô5¸Ò:Ñ;Ø×Ò ¨&Ð1Ñ2Ô2Ð2à#ð 	gØ/3×/LÒ/LÈ[ÐYdÔMeÑ/fÔ/fˆKÐ+Ñ,Ý ¸.ÐIÑIÔIÐIr   c                 ó¨   — i }|�C| j         gt          |¦  «        z  }dgt          |¦  «        z  }|                     ||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.
        Nrm   )Únum_image_tokensÚnum_image_patchesr   )rJ   rn   r�   r   )r[   Úimage_sizesr^   Úvision_datar›   rœ   s         r   Ú_get_num_multimodal_tokensz-PaliGemmaProcessor._get_num_multimodal_tokens÷   so   € ð ˆØÐ"Ø $Ô 5Ð6½¸[Ñ9IÔ9IÑIÐØ!" ¥c¨+Ñ&6Ô&6Ñ 6ÐØ×ÒÐ4DÐ[lÐmÐmÑnÔnÐnÝÐ,Ð, Ð,Ð,Ð,r   c                 ó`   — | j         j        ddgz   }| j        j        }t          ||z   ¦  «        S )Nr{   r|   )r]   Úmodel_input_namesr\   r#   )r[   Útokenizer_input_namesÚimage_processor_input_namess      r   r¡   z$PaliGemmaProcessor.model_input_names	  s:   € à $¤Ô @ÐDTÐV^ÐC_Ñ _ÐØ&*Ô&:Ô&LÐ#ÝÐ)Ð,GÑGÑHÔHÐHr   )NNN)NNr8   )r   r    r!   rZ   r   r   r   r   r#   r   r&   r   r™   rŸ   Úpropertyr¡   Ú__classcell__)r`   s   @r   rH   rH   b   s  ø€ € € € € ð ØØð	Rð Rð Rð Rð Rð Rð8 ð %)ØZ^ðtJð tJà˜TÑ!ðtJð Ð+Ñ+¨d°9¬oÑ=ÀÐEVÔ@WÑWðtJð Ð1Ô2ð	tJð
 
ðtJð tJð tJñ „^ðtJðl-ð -ð -ð -ð$ ðIð Iñ „XðIð Ið Ið Ið Ir   rH   )$r"   Únumpyr�   Úfeature_extraction_utilsr   Úimage_utilsr   r   Úprocessing_utilsr   r   r	   r
   r   Útokenization_utils_baser   r   r   Úutilsr   r   Ú
get_loggerr   rƒ   rQ   ÚrangerV   r   r&   Úboolr6   r;   r=   rF   rH   Ú__all__r   r   r   ú<module>r°      s"  ððð ð Ð Ð Ð à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5ðð ð ð ð ð ð ð ð ð ð ð ð ð ð PÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OØ ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,ð 
ˆÔ	˜HÑ	%Ô	%€à€Ø5Ð5¨¨¨t©¬Ð5Ñ5Ô5Ð8]Ð8]ÐRWÐRWÐX[ÑR\ÔR\Ð8]Ñ8]Ô8]Ñ]€ð]ð ]ð ]ð ]ð ]˜*ñ ]ô ]ð ]ð
ð 
ð 
ð 
ð 
Ð/°uð 
ñ 
ô 
ð 
ð;�4ð ;ð ;ð ;ð ;ð
0ð 0ð 0ðBð Bð BðNð Nð Nð, ðjIð jIð jIð jIð jI˜ñ jIô jIñ „ðjIðZ  Ð
 €€€r   