
    )iL=                     X   d 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
 ddlmZmZmZmZ ddlmZmZ dd	lmZmZmZmZ dd
lmZ  e       r
ddlmZmZmZ erddlmZ  ej>                  e       Z! e       rddl"m"Z" ndZ"dZ#d Z$d Z%d Z& G d ded      Z'e G d de             Z(dgZ)y)z
Processor class for SmolVLM.
    )	timedelta)TYPE_CHECKINGUnion   )BatchFeature)
ImageInputmake_nested_list_of_images)AllKwargsForChatTemplateProcessingKwargsProcessorMixinUnpack)BatchEncoding	TextInput)auto_docstringis_num2words_availableis_vision_availablelogging)
VideoInput   )DEFAULT_MEDIA_OUTTRODEFAULT_VIDEO_INTROFRAME_TIMESTAMP_MESSAGE)PreTokenizedInput)	num2wordsNa  <|im_start|>{% for message in messages %}{{message['role'] | capitalize}}{% if message['content'][0]['type'] == 'image' %}{{':'}}{% else %}{{': '}}{% endif %}{% for line in message['content'] %}{% if line['type'] == 'text' %}{{line['text']}}{% elif line['type'] == 'image' %}{{ '<image>' }}{% elif line['type'] == 'video' %}{{ '<video>' }}{% endif %}{% endfor %}<end_of_utterance>
{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}c           	          d}t        |      D ]4  }t        |      D ]  }|| d|dz    d|dz    dz   | | z  z   z  }! |dz  }6 |d| | z   | | z  z   | z   z  }|S )zKPrompt with expanded image tokens for when the image is split into patches. z<row_r   _col_>
)range)	image_seq_len
image_rows
image_colsfake_token_around_imageimage_tokenglobal_image_tokentext_split_imagesn_hn_ws	            j/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/models/smolvlm/processing_smolvlm.py_prompt_split_imager+   4   s     Z  "$ 	C*+sQwiuS1WIQ/OOU`TaerRrr	 	T!" 
$%& 	"M]
*	+ %%	'     c                 &    | | z   | | z  z   | z   S )z5Prompt with expanded image tokens for a single image. )r!   r$   r%   r&   s       r*   _prompt_single_imager/   I   s6     #
# 	"M]
*	+ %%	'r,   c                 T    | dk(  r|dk(  rt        ||||      S t        || ||||      S )Nr   )r$   r%   r&   )r/   r+   )r"   r#   r!   r$   r%   r&   s         r*   get_image_prompt_stringr1   S   sH     Q:?#$;#1	
 	
 z:/FUg r,   c                   (    e Zd ZddddddiddidZy)SmolVLMProcessorKwargsTF)add_special_tokenspaddingis_split_into_wordsreturn_row_col_inforeturn_metadata)text_kwargsimages_kwargsvideos_kwargsN)__name__
__module____qualname__	_defaultsr.   r,   r*   r3   r3   b   s1     #'#(
 "4
 t
Ir,   r3   F)totalc                   
    e Zd Z	 	 ddededz  f fdZ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dz  dee   def
d       Z	 dde
eeef      e
e
eeef         z  dedz  dee   def fdZ xZS )SmolVLMProcessorNr!   chat_templatec                 R   t        |dd      | _        t        |dd      | _        |j                  | j                        | _        t        |dd      | _        t        |dd      | _        || _        t        |d	d
      | _        t        st        d      t        | 0  |||fd|i| y)a  
        image_seq_len (`int`, *optional*, defaults to 169):
            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
            value the model used. It is computed as: image_seq_len = int(((image_size // patch_size) ** 2) / (scale_factor**2))
        fake_image_tokenz<fake_token_around_image>r%   z<image>end_of_utterance_tokenz<end_of_utterance>r&   z<global-img>video_tokenz<video>zbPackage `num2words` is required to run SmolVLM processor. Install it with `pip install num2words`.rC   N)getattrrE   r%   convert_tokens_to_idsimage_token_idrF   r&   r!   rG   r   ImportErrorsuper__init__)selfimage_processor	tokenizervideo_processorr!   rC   kwargs	__class__s          r*   rM   zSmolVLMProcessor.__init__t   s     !(	3EGb c"9mYG'==d>N>NO&-i9QSg&h#"))5I>"Z*"9mYGt  	)_lTaleklr,   c           
         g }t        |||      D ]  \  }}}g }t        ||      D ]O  \  }	}
t        |	|
| j                  | j                  | j                  | j
                        }|j                  |       Q |j                  | j                        }t        |      dk(  rt        d      |d   }t        |      D ]  \  }}||||dz      z   z  } |j                  |        |S )Nr%   r$   r&   r   z.The image token should be present in the text.r   )zipr1   r!   r%   rE   r&   appendsplitlen
ValueError	enumerate)rN   textr"   r#   prompt_stringssamplesample_rowssample_colsimage_prompt_stringsn_rowsn_colsimage_prompt_stringsplit_sampleis                 r*   expand_text_with_image_tokensz.SmolVLMProcessor.expand_text_with_image_tokens   s   03D*j0Q 	*,FK#% "%k;"? 	A&=&& $ 0 0,0,A,A'+'>'>'# %++,?@	A "<<(8(89L< A% !QRR "!_F*34H*I D&&-QU0CCCD!!&)-	*0 r,   c           	         |d   j                   d   }t        |d         }g }|D ]  }| j                  |v r~t        |      }|j                  t
        j                  d       d|_        |j                  D cg c]  }t        |dz        t        |dz        f  }	}|j                  t        |j                        nt        |j                  d         }
t        t        |
            }t        j                  t        |      t        |      	      }|	D ]f  }t        | j                   | j"                  | j$                  | j&                  
      }|d   dd|d   d}t)        j                  |      |z   }||z  }h |t*        z  }|j-                  | j                  |d      }| j                  |v r~|j/                  |        |S c c}w )Npixel_valuesr   video_metadataa  SmolVLM requires frame timestamps to construct prompts, but the `fps` of the input video could not be inferred. Probably `video_metadata` was missing from inputs and you passed pre-sampled frames. Defaulting to `fps=24`. Please provide `video_metadata` for more accurate results.   <   )seconds)frame_countvideo_durationrU   r   02d:)	timestamp)shapeiterrG   nextfpsloggerwarning_once
timestampsintdurationr   r   formatr   strr/   r!   r%   rE   r&   r   r   replacerW   )rN   r\   video_inputs
num_framesrj   r]   r^   metadatasecondrz   r|   duration_tdra   rs   rd   s                  r*   expand_text_with_video_tokensz.SmolVLMProcessor.expand_text_with_video_tokens   s   !.177:
l+;<= 	*F""f,/<<'''m
 $&HLRZReRefs6R<0#frk2BCf
f5=5F5F5R3x001X[\d\o\opr\sXt'H>':'A'A )* 5c+FV($ ", 	@I*>**$($4$4040E0E+/+B+B	+' $-Q<"4Ail35G HI*A*H*HS\*]`s*s'(,??(	@ %(<<$(8(8:NPQR7 ""f,8 !!&);	*< ) gs   9#Gimagesr\   r   videosrR   returnc                     |||t        d      ||d u |d uz  rt        d        j                  t        fd j                  j                  i|}|nt        |t              r|g}n.t        |t              st        |d   t              st        d      t         fd|D              }|dkD  r||t        d| d      i }|
 j                  j                  |      }t        |      }  j                  |fi |d	   }|j                  d
d       }	|j                  dd       }
|j                  |       |L|D cg c]  }|j                   j                         }}|D cg c]  }t!        |       }}||k7  rt        d| d| d      |	|D cg c]  }dg|z  
 }	}|
|D cg c]  }dg|z  
 }
} j#                  ||	|
      }n|  j$                  |fi |d   }|i|D cg c]  }|j                   j&                         }}|D cg c]  }t!        |       }}||k7  rt        d| d| d       j)                  ||      }|j+                  d      s|j                  d       |j                  |       |d   j                  dd       }|<  j                  |fi |d   } j-                  ||dg       |j                  |       t/        ||      S c c}w c c}w c c}w c c}w c c}w c c}w )Nz5You must provide one of `text`, `images` or `videos'.z4You must specify exactly one of `images` or `videos`tokenizer_init_kwargsr   zAInvalid input text. Please provide a string, or a list of stringsc              3   T   K   | ]  }|j                  j                         ! y wN)countr%   ).0r^   rN   s     r*   	<genexpr>z,SmolVLMProcessor.__call__.<locals>.<genexpr>   s      "Uf6<<0@0@#A"Us   %(zWe detected z4 tokens in the text but no images/videos were passedr:   rowscolsz!The number of images in the text z and images z should be the same.)r"   r#   r;   z!The number of videos in the text z and videos r8   rj   r9   return_tensorsimage)
modalities)tensor_type)rZ   _merge_kwargsr3   rP   init_kwargs
isinstancer~   listsumrO   fetch_imagesr	   popupdater   r%   rY   rg   rQ   rG   r   get_check_special_mm_tokensr   )rN   r   r\   r   rR   output_kwargsn_images_in_textinputsvision_inputsr"   r#   r^   sublistn_images_in_imagesn_imagesn_videos_in_textn_videos_in_videosr   text_inputss   `                  r*   __call__zSmolVLMProcessor.__call__   s    <FNv~TUU<fnt1CDSTT***"
"&.."<"<
 
 $$vd+JtAw4L !dee""UPT"UU!#FN <0@/AAu!vww))66v>F/7F0D00Z=;YZM&**648J&**648JMM-(QU#VvFLL1A1A$B#V #VBH%Iwc'l%I"%I%)99$;<L;M\ZlYm  nB  C  %AQ!RX1#.!RJ!R%AQ!RX1#.!RJ!R99$:bl9m0D00Z=;YZMQU#VvFLL1A1A$B#V #VBH%Iwc'l%I"%I%)99$;<L;M\ZlYm  nB  C  99$N ::/0!!"23MM-(&}599:JDQ($..N}1MNK))$	)RMM+&F??K $W%I "S!R $W%Is$   "K++K0K51K:3"K?Lconversationc                 ~   t        |t        t        f      r+t        |d   t        t        f      st        |d   d      r|}n|g}t	        d |D              }||rt
        }|j                  d| j                  j                         |j                  d| j                  j                         t        | ,  ||fi |S )a  
        Similar to the `apply_chat_template` method on tokenizers, this method applies a Jinja template to input
        conversations to turn them into a single tokenizable string.

        The input is expected to be in the following format, where each message content is a list consisting of text and
        optionally image or video inputs. One can also provide an image, video, URL or local path which will be used to form
        `pixel_values` when `return_dict=True`. If not provided, one will get only the formatted text, optionally tokenized text.

        conversation = [
            {
                "role": "user",
                "content": [
                    {"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"},
                    {"type": "text", "text": "Please describe this image in detail."},
                ],
            },
        ]

        Args:
            conversation (`Union[list[Dict, [str, str]], list[list[dict[str, str]]]]`):
                The conversation to format.
            chat_template (`Optional[str]`, *optional*):
                The Jinja template to use for formatting the conversation. If not provided, the tokenizer's
                chat template is used.
        r   contentc              3   r   K   | ]/  }|D ](  }|d    D ]  }t        |t              xr |d   dk(     * 1 yw)r   typevideoN)r   dict)r   r   messager   s       r*   r   z7SmolVLMProcessor.apply_chat_template.<locals>.<genexpr>I  s_      
'
 "9-	
  &E76?g+EE
E
E
s   57r   rw   )r   r   tuplehasattranyDEFAULT_CHAT_TEMPLATE
setdefaultrQ   r   rw   rL   apply_chat_template)rN   r   rC   rR   conversations	has_videorS   s         r*   r   z$SmolVLMProcessor.apply_chat_template#  s    > lT5M2|Au6',q/S\:](M)NM 
 -
 
	  Y1M,(<(<(G(GH%!5!5!9!9:w*<Q&QQr,   )   N)NNNr   )r<   r=   r>   r{   r~   rM   rg   r   r   r   r   r   r   r   r   r3   r   r   r   r
   r   __classcell__)rS   s   @r*   rB   rB   r   s<    !$(m
 m Tzm<8#J  JNbf$(	M@T*--T*5E0FFM@ I2DOTJ]E^^_M@ T!	M@
 /0M@ 
M@ M@d %)3R4S>*T$tCH~2F-GG3R Tz3R 12	3R
 
3R 3Rr,   rB   )*__doc__datetimer   typingr   r   feature_extraction_utilsr   image_utilsr   r	   processing_utilsr
   r   r   r   tokenization_utils_baser   r   utilsr   r   r   r   video_utilsr   video_processing_smolvlmr   r   r   r   
get_loggerr<   rx   r   r   r+   r/   r1   r3   rB   __all__r.   r,   r*   <module>r      s     ' 4 A b b ? Y Y %   <			H	% #I ` *-U   cR~ cR cRL 
r,   