
    Z j                        S r SSKrSSKJr  SSKJrJr  SSKJr  SSK	J
r
JrJr  SSKJrJrJr  SS	KJrJr  SS
KJrJr  \(       a  SSKJr  \R0                  " \5      rS\4S jrS r " S S\SS9r\ " S S\5      5       rS/r g)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                 R    [        U [        5      =(       a    U R                  S5      $ )Nhttp)
isinstancestr
startswith)vals    ځ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/idefics2/processing_idefics2.pyis_urlr   (   s    c3:CNN6$::    c                 <    [        U 5      =(       d    [        U 5      $ N)r   r	   )elems    r   is_image_or_image_urlr    ,   s    $</>$//r   c                   $    \ rS rSrSSSSS.0rSrg)Idefics2ProcessorKwargs0   text_kwargsTF)add_special_tokenspaddingis_split_into_words N)__name__
__module____qualname____firstlineno__	_defaults__static_attributes__r(   r   r   r"   r"   0   s    "&#(
Ir   r"   F)totalc                      ^  \ rS rSr SS\S\S-  4U 4S jjjrS r\  SS\	\
\	   -  \
\
\	      -  S\\S	\
\   \
S	   4   S
\\   S\4S jj5       rSrU =r$ )Idefics2Processor:   Nimage_seq_lenchat_templatec                   > [        US5      (       d  [        SSSS9R                  U l        [        SSSS9R                  U l        SU R                  U R                  /0nUR                  U5        UR                  U R                  5      U l        O3UR                  U l        UR                  U l        UR                  U l        [        SSSS9U l	        UR                  SU R                  /05        X0l
        [        TU ]1  XUS	9  g
)aH  
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>)r4   N)hasattrr   contentfake_image_tokenr6   r%   convert_tokens_to_idsimage_token_idimage_boundary_tokenend_of_utterance_tokenr3   super__init__)selfimage_processor	tokenizerr3   r4   kwargstokens_to_add	__class__s          r   rB   Idefics2Processor.__init__<   s     y-00$./JW\fj$k$s$sD!))tT\\D84;P;PRVRbRb:cdM((7"+"A"A$BRBR"SD$-$B$BD!(44D"+":":D&01ERWae&f#$$&ADD_D_C`%ab*=Qr   c                     / nU Hn  n/ nU HR  n[        U5      (       a  UR                  U5        M&  [        U5      (       d  M8  UR                  [        U5      5        MT     UR                  U5        Mp     U$ r   )r	   appendr   r
   )rC   promptsprompt_imagespromptimagesr   s         r   _extract_images_from_prompts.Idefics2Processor._extract_images_from_promptsV   si    FF!$''MM$'D\\MM*T"23	 
   (  r   rO   textr   rF   r   c                 d   Uc  Uc  [        S5      eU R                  " [        4SU R                  R                  0UD6nUS   R                  SS 5      n/ n0 nUGb{  [        U[        5      (       a  U/nO8[        U[        5      (       d#  [        US   [        5      (       d  [        S5      eU R                  nU R                  n	U XR                  -   U 3n
U R                  R                  (       a  U
S-  n
/ n[        R                  " [        R                   " U5       S35      nU Hq  nUR#                  UR%                  U	5      5        UR'                  X5      nUR'                  U U 3U 5      nUR)                  U S	3U5      nUR#                  U5        Ms     U R                  " U40 US   D6nU R+                  XS
/S9  UR-                  U5        UGb  [/        U5      (       a  U//nGO[        U[        [0        45      (       a  [/        US   5      (       a  Ub  [3        U5      [5        U5      :w  a*  [        SW	 S[3        U5       S	U	 S[5        U5       S3	5      eS/[        [7        U5      5      -   n[9        [5        U5      5       Vs/ s H  nXU   UUS-       PM     nnO^U/nOZ[        U[        [0        45      (       d?  [        US   [        [0        45      (       d!  [/        US   S   5      (       d  [        S5      eU Vs/ s H  n[5        U5      PM     nnUb  UU:X  d  [        SU SU S35      eU VVs/ s H  o Vs/ s H  n[;        U5      PM     snPM      nnnU R                  " U40 US   D6nUR-                  U5        [=        XuS9$ s  snf s  snf s  snf s  snnf )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.   zdInvalid input images. Please provide a single image or a list of images or a list of list of images.z!The number of images in the text z and images  z should be the same.images_kwargs)tensor_type)
ValueError_merge_kwargsr"   rE   init_kwargspopr   r   listr<   r6   r3   rD   do_image_splittingrecompileescaperK   countreplacesub_check_special_mm_tokensupdater    tuplesumlenr   ranger
   r   )rC   rO   rR   rF   output_kwargsrU   n_images_in_textinputsr<   r6   	image_strprompt_stringsclosing_fake_patternsampletext_inputscumsum_images_in_textin_images_in_imagesimimage_inputss                       r   __call__Idefics2Processor.__call__b   s    <FNJKK**#
"&.."<"<
 

 '}599:JDQ$$$vd++JtAw4L4L !dee  $44**K+,[;M;M-M,NO_N`aI##66%M	N#%::"))<L2M1Nj.Y#Z  ''[(AB?+;*<=M<N(OTdSeg-115E4Fa2H&Q%%f-  ..X=;WXK)).SZR[)\MM+&$V,,!(FT5M227LVTUY7W7W#+,F;(2;- @&&)*:&;%<Ak],WZ[aWbVcckm 
 ./C$zBR7S2T,T) "'s+;'<!=!=A Q7:OPQTUPU:VW!=  F
 %XF ve}55"6!9tUm<<-fQil;; z  =C!CF&#f+F!C(:>N(N 78H7IWiVjj~ 
 GMMfF7"z"~7fFM//Y-:XYLMM,'F??7  "D 8Ms$   NN"	N,N'#N,'N,)r@   r<   r3   r6   r>   )N@   N)NN)r)   r*   r+   r,   intr   rB   rP   r   r   ra   r   r   r   r"   r   r|   r.   __classcell__)rH   s   @r   r1   r1   :   s     eiR>ARWZ]aWaR R4
  JNbfX@T*--T*5E0FFX@ I2DOTJ]E^^_X@ 01	X@
 
X@ X@r   r1   )!__doc__rc   	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"   r1   __all__r(   r   r   <module>r      s    
   ' 4 A A 
 = , < 
		H	%;4 ;0.e  @@ @@ @@F 
r   