§
    ‚Štj�  ã                   ó  — d Z ddlZddlm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 dd	lmZmZ dd
lmZmZ erddlmZ  ej        e¦  «        Zdefd„Zd„ Z G d„ ded¬¦  «        Ze G d„ de¦  «        ¦   «         ZdgZ dS )z
Processor class for IDEFICS2.
é    N)Ú
accumulate)ÚTYPE_CHECKINGÚUnioné   )ÚBatchFeature)Ú
ImageInputÚis_valid_imageÚ
load_image)ÚProcessingKwargsÚProcessorMixinÚUnpack)Ú
AddedTokenÚ	TextInput)Úauto_docstringÚlogging)ÚPreTokenizedInputÚreturnc                 óV   — t          | t          ¦  «        o|                      d¦  «        S )NÚhttp)Ú
isinstanceÚstrÚ
startswith)Úvals    ún/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/idefics2/processing_idefics2.pyÚis_urlr   (   s#   € Ý�c�3ÑÔÐ: C§N¢N°6Ñ$:Ô$:Ð:ó    c                 ó>   — t          | ¦  «        pt          | ¦  «        S ©N)r   r	   )Úelems    r   Úis_image_or_image_urlr    ,   s   € Ý�$‰<Œ<Ð/�>¨$Ñ/Ô/Ð/r   c                   ó   — e Zd ZdddddœiZdS )ÚIdefics2ProcessorKwargsÚtext_kwargsTF)Úadd_special_tokensÚpaddingÚis_split_into_wordsN)Ú__name__Ú
__module__Ú__qualname__Ú	_defaults© r   r   r"   r"   0   s+   € € € € € àØ"&ØØ#(ð
ð 
ð€I€I€Ir   r"   F)Útotalc                   óÎ   ‡ — e Zd Z	 ddededz  fˆ fd„Zd„ Ze	 	 ddee	e         z  e	e	e                  z  de
ed	e	e         e	d	         f         d
ee         defd„¦   «         Zˆ xZS )ÚIdefics2ProcessorNé@   Úimage_seq_lenÚchat_templatec                 ó(  •— t          |d¦  «        s}t          ddd¬¦  «        j        | _        t          ddd¬¦  «        j        | _        d| j        | j        gi}|                     |¦  «         |                     | j        ¦  «        | _        n$|j        | _        |j        | _        |j        | _        t          ddd¬¦  «        | _	        |                     d| j	        gi¦  «         || _
        t          ¦   «                              |||¬	¦  «         d
S )ap  
        image_seq_len (`int`, *optional*, defaults to 64):
            The length of the image sequence i.e. the number of <image> tokens per image in the input.
            This parameter is used to build the string from the input prompt and image tokens and should match the
            config.perceiver_config.resampler_n_latents value for the model used.
        Úimage_tokenz<fake_token_around_image>FT)Ú
normalizedÚspecialz<image>Úadditional_special_tokensz<end_of_utterance>)r1   N)Úhasattrr   ÚcontentÚfake_image_tokenr3   r$   Úconvert_tokens_to_idsÚimage_token_idÚimage_boundary_tokenÚend_of_utterance_tokenr0   ÚsuperÚ__init__)ÚselfÚimage_processorÚ	tokenizerr0   r1   ÚkwargsÚtokens_to_addÚ	__class__s          €r   r?   zIdefics2Processor.__init__<   s  ø€ õ �y -Ñ0Ô0ð 		;Ý$.Ð/JÐW\ÐfjÐ$kÑ$kÔ$kÔ$sˆDÔ!Ý)¨)ÀÈtÐTÑTÔTÔ\ˆDÔØ8¸4Ô;PÐRVÔRbÐ:cÐdˆMØ×(Ò(¨Ñ7Ô7Ð7Ø"+×"AÒ"AÀ$ÔBRÑ"SÔ"SˆDÔÐà$-Ô$BˆDÔ!Ø(Ô4ˆDÔØ"+Ô":ˆDÔå&0Ð1EÐRWÐaeÐ&fÑ&fÔ&fˆÔ#Ø×$Ò$Ð&AÀDÔD_ÐC`Ð%aÑbÔbÐbØ*ˆÔå‰Œ×Ò˜¨)À=ÐÑQÔQÐQÐQÐQr   c                 óø   — g }|D ]t}g }|D ]X}t          |¦  «        r|                     |¦  «         Œ't          |¦  «        r"|                     t          |¦  «        ¦  «         ŒY|                     |¦  «         Œu|S r   )r	   Úappendr   r
   )r@   ÚpromptsÚprompt_imagesÚpromptÚimagesr   s         r   Ú_extract_images_from_promptsz.Idefics2Processor._extract_images_from_promptsV   s•   € ØˆØð 	)ð 	)ˆFØˆFØð 4ð 4�Ý! $Ñ'Ô'ð 4Ø—M’M $Ñ'Ô'Ð'Ð'Ý˜D‘\”\ð 4Ø—M’M¥*¨TÑ"2Ô"2Ñ3Ô3Ð3øØ× Ò  Ñ(Ô(Ð(Ð(ØÐr   rK   Útextr   rC   r   c                 ó>  ‡‡— |€‰€t          d¦  «        ‚ | j        t          fd| j        j        i|¤Ž}|d                              dd ¦  «        }g }i }|��€t          |t          ¦  «        r|g}n?t          |t          ¦  «        s*t          |d         t          ¦  «        st          d¦  «        ‚| j	        }| j
        }	|› |	| j        z  › |› �}
| j        j        r|
dz  }
g }t          j        t          j        |¦  «        › d�¦  «        }|D ]‰}|                     |                     |	¦  «        ¦  «         |                     |	|
¦  «        }|                     |› |› �|› ¦  «        }|                     |› d	�|¦  «        }|                     |¦  «         ŒŠ | j        |fi |d         ¤Ž}|                      ||d
g¬¦  «         |                     |¦  «         ‰��³t/          ‰¦  «        r‰ggŠ�n?t          ‰t          t0          f¦  «        r»t/          ‰d         ¦  «        r¦|� t3          |¦  «        t5          ‰¦  «        k    r6t          d|	› dt3          |¦  «        › d	|	› dt5          ‰¦  «        › d�	¦  «        ‚dgt          t7          |¦  «        ¦  «        z   Šˆˆfd„t9          t5          |¦  «        ¦  «        D ¦   «         Šnl‰gŠnht          ‰t          t0          f¦  «        sLt          ‰d         t          t0          f¦  «        s*t/          ‰d         d         ¦  «        st          d¦  «        ‚d„ ‰D ¦   «         }|�||k    st          d|› d|› d�¦  «        ‚d„ ‰D ¦   «         Š | j        ‰fi |d         ¤Ž}|                     |¦  «         t;          ||¬¦  «        S )Nz+You must provide either `text` or `images`.Útokenizer_init_kwargsr#   Úreturn_tensorsr   zAInvalid input text. Please provide a string, or a list of stringsé   z
(?=[^\s<])ú Úimage)Ú
modalitieszThe total number of zP tokens in the prompts should be the same as the number of images passed. Found z tokens and z images.c                 óB   •— g | ]}‰‰|         ‰|d z            …         ‘ŒS )é   r+   )Ú.0ÚiÚcumsum_images_in_textrK   s     €€r   ú
<listcomp>z.Idefics2Processor.__call__.<locals>.<listcomp>    sF   ø€ ð ð ð àð Ð4°QÔ7Ð:OÐPQÐTUÑPUÔ:VÐVÔWðð ð r   zdInvalid input images. Please provide a single image or a list of images or a list of list of images.c                 ó,   — g | ]}t          |¦  «        ‘ŒS r+   )Úlen©rW   Úsamples     r   rZ   z.Idefics2Processor.__call__.<locals>.<listcomp>°   s   € Ð!CÐ!CÐ!C°&¥# f¡+¤+Ð!CÐ!CÐ!Cr   z!The number of images in the text z and images  z should be the same.c                 ó&   — g | ]}d „ |D ¦   «         ‘ŒS )c                 ó,   — g | ]}t          |¦  «        ‘ŒS r+   )r
   )rW   Úims     r   rZ   z9Idefics2Processor.__call__.<locals>.<listcomp>.<listcomp>·   s   € Ð7Ð7Ð7¨"•z "‘~”~Ð7Ð7Ð7r   r+   r]   s     r   rZ   z.Idefics2Processor.__call__.<locals>.<listcomp>·   s'   € ÐMÐMÐM¸FÐ7Ð7°Ð7Ñ7Ô7ÐMÐMÐMr   Úimages_kwargs)Útensor_type)Ú
ValueErrorÚ_merge_kwargsr"   rB   Úinit_kwargsÚpopr   r   Úlistr9   r3   r0   rA   Údo_image_splittingÚreÚcompileÚescaperG   ÚcountÚreplaceÚsubÚ_check_special_mm_tokensÚupdater    ÚtupleÚsumr\   r   Úranger   )r@   rK   rM   rC   Úoutput_kwargsrP   Ún_images_in_textÚinputsr9   r3   Ú	image_strÚprompt_stringsÚclosing_fake_patternr^   Útext_inputsÚn_images_in_imagesÚimage_inputsrY   s    `               @r   Ú__call__zIdefics2Processor.__call__b   s�  øø€ ð ˆ<˜F˜NÝÐJÑKÔKÐKà*˜Ô*Ý#ð
ð 
à"&¤.Ô"<ð
ð ð
ð 
ˆð
 ' }Ô5×9Ò9Ð:JÈDÑQÔQˆàÐØˆàÑÝ˜$¥Ñ$Ô$ð fØ�v��Ý ¥dÑ+Ô+ð fµJ¸tÀA¼wÍÑ4LÔ4Lð fÝ Ð!dÑeÔeÐeð  $Ô4ÐØÔ*ˆKØ+Ða¨[¸4Ô;MÑ-MÐaÐO_ÐaÐaˆIàÔ#Ô6ð *à%¨™M�	àˆNÝ#%¤:µ"´)Ð<LÑ2MÔ2MÐ.YÐ.YÐ.YÑ#ZÔ#ZÐ Øð .ð .�Ø ×'Ò'¨¯ª°[Ñ(AÔ(AÑBÔBÐBØŸš¨°YÑ?Ô?�àŸšÐ+;Ð(OÐ=MÐ(OÐ(OÐTdÐQfÑgÔg�à-×1Ò1Ð5EÐ2HÐ2HÐ2HÈ&ÑQÔQ�Ø×%Ò% fÑ-Ô-Ð-Ð-à(˜$œ.¨ÐXÐX¸=ÈÔ;WÐXÐXˆKØ×)Ò)¨.¸+ÐSZÐR[Ð)Ñ\Ô\Ð\Ø�MŠM˜+Ñ&Ô&Ð&àÑÝ$ VÑ,Ô,ð Ø!˜(˜�‘Ý˜F¥T­5 MÑ2Ô2ð Õ7LÈVÐTUÌYÑ7WÔ7Wð ØÐ#ÝÐ+Ñ,Ô,µ°F±´Ò;Ð;Ý(ðm°;ð mð mÝ&)Ð*:Ñ&;Ô&;ðmð mØ>Iðmð mÝWZÐ[aÑWbÔWbðmð mð mñô ð ð
 ./¨Cµ$µzÐBRÑ7SÔ7SÑ2TÔ2TÑ,TÐ)ðð ð ð ð å!&¥sÐ+;Ñ'<Ô'<Ñ!=Ô!=ðñ ô �F�Fð
 %˜X�F�Fõ ˜v­­e }Ñ5Ô5ðå" 6¨!¤9­tµU¨mÑ<Ô<ðõ .¨f°Q¬i¸¬lÑ;Ô;ðõ
 !Øzñô ð ð "DÐ!C¸FÐ!CÑ!CÔ!CÐØÐÐ(:Ð>NÒ(NÐ(NÝ ØÐ8HÐÐÐWiÐÐÐñô ð ð
 NÐMÀfÐMÑMÔMˆFØ/˜4Ô/°ÐYÐY¸-ÈÔ:XÐYÐYˆLØ�MŠM˜,Ñ'Ô'Ð'å˜F°Ð?Ñ?Ô?Ð?r   )Nr/   N)NN)r'   r(   r)   Úintr   r?   rL   r   r   rh   r   r   r   r"   r   r~   Ú__classcell__)rE   s   @r   r.   r.   :   s  ø€ € € € € ð eiðRð RØ>AðRØWZÐ]aÑWaðRð Rð Rð Rð Rð Rð4
ð 
ð 
ð ð JNØbfðX@ð X@à˜T *Ô-Ñ-°°T¸*Ô5EÔ0FÑFðX@ð �IÐ2°D¸´OÀTÐJ]ÔE^Ð^Ô_ðX@ð Ð0Ô1ð	X@ð
 
ðX@ð X@ð X@ñ „^ðX@ð X@ð X@ð X@ð X@r   r.   )!Ú__doc__rj   Ú	itertoolsr   Útypingr   r   Úfeature_extraction_utilsr   Úimage_utilsr   r	   r
   Úprocessing_utilsr   r   r   Útokenization_utils_baser   r   Úutilsr   r   r   Ú
get_loggerr'   ÚloggerÚboolr   r    r"   r.   Ú__all__r+   r   r   ú<module>r�      s¹  ððð ð 
€	€	€	Ø  Ð  Ð  Ð  Ð  Ð  Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ Aðð ð ð ð ð ð ð ð ð ð
 =Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,ð ð =Ø<Ð<Ð<Ð<Ð<Ð<ð 
ˆÔ	˜HÑ	%Ô	%€ð;�4ð ;ð ;ð ;ð ;ð0ð 0ð 0ðð ð ð ð Ð.°eð ñ ô ð ð ð@@ð @@ð @@ð @@ð @@˜ñ @@ô @@ñ „ð@@ðF Ð
€€€r   