ó
    ‡Ž¡jc%  ã                   óŽ   • S SK rSSKJrJrJr  SSKJrJr  \R                  " \
5      r " S S\SS9r\ " S	 S
\5      5       rS
/rg)é    Né   )ÚMultiModalDataÚProcessingKwargsÚProcessorMixin)Úauto_docstringÚloggingc                   ó*   • \ rS rSrSSSS.SS0S.rSrg)	ÚGlm46VProcessorKwargsé   FT)ÚpaddingÚreturn_token_type_idsÚreturn_mm_token_type_idsÚreturn_metadata)Útext_kwargsÚvideos_kwargs© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú	_defaultsÚ__static_attributes__r   ó    ÚºC:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\transformers/models/glm46v/processing_glm46v.pyr
   r
      s#   † ð Ø%*Ø(,ñ
ð
 ,¨TÐ2ñƒIr   r
   F)Útotalc                   ó¸   ^ • \ rS rSr\rSU 4S jjrS\S\S\	4S jr
S\S\S\	4S	 jrSS
 jr SS jr\U 4S j5       rS\S\\\      4S jrSS\4S jjrSrU =r$ )ÚGlm46VProcessoré)   c                 ó  >• [        US5      (       d  SOUR                  U l        [        US5      (       d  SOUR                  U l        [        USS 5      (       a  UR                  OUR                  U R                  5      U l        [        USS 5      (       a  UR                  OUR                  U R                  5      U l        [        TU ]!  XX4S9  UR                  S5      U l	        UR                  S	5      U l
        g )
NÚimage_tokenz	<|image|>Úvideo_tokenz	<|video|>Úimage_token_idÚvideo_token_id)Úchat_templatez<|begin_of_video|>z<|end_of_video|>)Úhasattrr    r!   Úgetattrr"   Úconvert_tokens_to_idsr#   ÚsuperÚ__init__Úvideo_start_idÚvideo_end_id)ÚselfÚimage_processorÚ	tokenizerÚvideo_processorr$   ÚkwargsÚ	__class__s         €r   r)   ÚGlm46VProcessor.__init__-   së   ø€ Ü.5°iÀ×.OÑ.O™;ÐU^×UjÑUjˆÔÜ.5°iÀ×.OÑ.O™;ÐU^×UjÑUjˆÔô �yÐ"2°D×9Ñ9ð ×$Ò$à×0Ñ0°×1AÑ1AÓBð 	Ôô �yÐ"2°D×9Ñ9ð ×$Ò$à×0Ñ0°×1AÑ1AÓBð 	Ôô
 	‰Ñ˜°_ÐÑbØ'×=Ñ=Ð>RÓSˆÔØ%×;Ñ;Ð<NÓOˆÕr   Úimage_inputsÚ	image_idxÚreturnc                 ó„   • U R                   R                  S-  nUS   U   R                  5       U-  nU R                  U-  $ )Né   Úimage_grid_thw)r-   Ú
merge_sizeÚprodr    )r,   r3   r4   r0   Úmerge_lengthÚnum_image_tokenss         r   Úreplace_image_tokenÚ#Glm46VProcessor.replace_image_token>   sI   € Ø×+Ñ+×6Ñ6¸Ñ9ˆØ'Ð(8Ñ9¸)ÑD×IÑIÓKÈ|Ñ[ÐØ×ÑÐ"2Ñ2Ð2r   Úvideo_inputsÚ	video_idxc                 óz  • U R                   R                  S-  nUS   U   S   nUS   U   R                  5       U-  U-  nUS   U   nSnUR                  c  [        R                  S5        UR                  c  SOUR                  Ul        UR                  S S S2   n	/ n
[        S[        U	5      5       H  nU
R                  X›   5        M     U
S U n[        U5      U:  a.  UR                  U(       a  US   OS5        [        U5      U:  a  M.  [        U5       H  nXÍ   nU R                  XæS	9nX�-  nM     U$ )
Nr7   Úvideo_grid_thwr   Úvideo_metadataÚ a  GLM46V 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.é   éÿÿÿÿ)r<   )r/   r9   r:   ÚfpsÚloggerÚwarning_onceÚ
timestampsÚrangeÚlenÚappendÚreplace_frame_token_id)r,   r?   r@   r0   r;   Ú
num_framesr<   ÚmetadataÚvideo_structurerJ   Úunique_timestampsÚidxÚselected_timestampsÚ	frame_idxÚtimestamp_secÚframe_structures                   r   Úreplace_video_tokenÚ#Glm46VProcessor.replace_video_tokenC   s_  € Ø×+Ñ+×6Ñ6¸Ñ9ˆØ!Ð"2Ñ3°IÑ>¸qÑAˆ
Ø'Ð(8Ñ9¸)ÑD×IÑIÓKÈ|Ñ[Ð_iÑiÐØÐ 0Ñ1°)Ñ<ˆØˆà�<‰<ÑÜ×Ñðeôð
 &Ÿ\™\Ñ1‘r°x·|±|ˆŒØ×(Ñ(©¨1¨Ñ-ˆ
àÐÜ˜œC 
›OÖ,ˆCØ×$Ñ$ Z¡_Ö5ñ -ð 0°°Ð<ÐÜÐ%Ó&¨Ó3Ø×&Ñ&ÖBUÐ':¸2Ò'>Ð[\Ô]ô Ð%Ó&¨Õ3ô ˜zÖ*ˆIØ/Ñ:ˆMØ"×9Ñ9¸-Ð9ÐkˆOØÑ.ŠOñ +ð
 Ðr   c                 ó´  • 0 nUb¸  [         R                  R                  S0 5      nUR                  U5        UR                  SS5      =(       d    U R                  R
                  nU Vs/ s H!  nU R                  R                  " / UQUP76 PM#     nnU V	s/ s H
  o™US-  -  PM     n
n	UR                  X¨S.5        Ubz  [         R                  R                  S0 5      nUR                  U5        U Vs/ s H!  nU R                  R                  " / UQUP76 PM#     nnU V	s/ s H
  o™WS-  -  PM     nn	XäS'   [        S0 UD6$ s  snf s  sn	f s  snf s  sn	f )	aû  
Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.
Args:
    image_sizes (`list[list[int]]`, *optional*):
        The input sizes formatted as (height, width) per each image.
    video_sizes (`list[list[int]]`, *optional*):
        The input sizes formatted as (num_frames, height, width) per each video.
Returns:
    `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
    input modalities, along with other useful data.
NÚimages_kwargsr9   r7   )r<   Únum_image_patchesr   Únum_video_tokensr   )
r
   r   ÚgetÚupdater-   r9   Úget_number_of_image_patchesr/   Úget_number_of_video_patchesr   )r,   Úimage_sizesÚvideo_sizesr0   Úvision_datar[   r9   Ú
image_sizer\   Únum_patchesr<   r   Ú
video_sizeÚnum_video_patchesr]   s                  r   Ú_get_num_multimodal_tokensÚ*Glm46VProcessor._get_num_multimodal_tokensb   s  € ð ˆØÑ"Ü1×;Ñ;×?Ñ?ÀÐQSÓTˆMØ× Ñ  Ô(Ø&×*Ñ*¨<¸Ó>×aÀ$×BVÑBV×BaÑBaˆJñ #.ó!â"-�Jð ×$Ñ$×@Ò@Ð\À*Ð\ÈmÕ\Ù"-ð ð !ñ SdÓdÒRcÀ;°
¸A±Ô!=ÑRcÐÐdØ×ÑÐ4DÑmÔnàÑ"Ü1×;Ñ;×?Ñ?ÀÐQSÓTˆMØ× Ñ  Ô(ñ #.ó!â"-�Jð ×$Ñ$×@Ò@Ð\À*Ð\ÈmÕ\Ù"-ð ð !ñ SdÓdÒRcÀ;°
¸A±Ô!=ÑRcÐÐdØ.>Ð*Ñ+äÑ, Ñ,Ð,ùò#!ùò  eùò!ùò  es   Á*(EÂEÃ6(EÄ$Ec                 óB   • U R                   R                  " U4UUS.UD6$ )a*  
Post-process the output of the model to decode the text.

Args:
    generated_outputs (`torch.Tensor` or `np.ndarray`):
        The output of the model `generate` function. The output is expected to be a tensor of shape `(batch_size, sequence_length)`
        or `(sequence_length,)`.
    skip_special_tokens (`bool`, *optional*, defaults to `True`):
        Whether or not to remove special tokens in the output. Argument passed to the tokenizer's `batch_decode` method.
    clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`):
        Whether or not to clean up the tokenization spaces. Argument passed to the tokenizer's `batch_decode` method.
    **kwargs:
        Additional arguments to be passed to the tokenizer's `batch_decode method`.

Returns:
    `list[str]`: The decoded text.
)Úskip_special_tokensÚclean_up_tokenization_spaces)r.   Úbatch_decode)r,   Úgenerated_outputsrl   rm   r0   s        r   Úpost_process_image_text_to_textÚ/Glm46VProcessor.post_process_image_text_to_textˆ   s3   € ð( �~‰~×*Ò*Øð
à 3Ø)Eñ
ð ñ	
ð 	
r   c                 ó    >• [         TU ]  S/-   $ )NÚmm_token_type_ids)r(   Úmodel_input_names)r,   r1   s    €r   rt   Ú!Glm46VProcessor.model_input_names£   s   ø€ ä‰wÑ(Ð,?Ð+@Ñ@Ð@r   Ú	input_idsc                 ó�  • / nU H½  n[         R                  " U5      n[         R                  " U5      n[         R                  " X@R                  :H  SS9n[         R                  " X@R
                  :H  SS9nXg:„  nSXTU R                  :H  U-  '   SXTU R                  :H  U) -  '   UR                  UR                  5       5        M¿     U$ )Nr   )Úaxisr7   é   )	ÚnpÚarrayÚ
zeros_likeÚcumsumr*   r+   r"   rM   Útolist)	r,   rv   rs   ÚinputÚ	array_idsÚmm_token_typesÚstartsÚendsÚis_video_modalitys	            r   Úcreate_mm_token_type_idsÚ(Glm46VProcessor.create_mm_token_type_ids§   s¿   € ð ÐÛˆEÜŸš ›ˆIÜŸ]š]¨5Ó1ˆNô
 —Y’Y˜y×,?Ñ,?Ñ?ÀaÑHˆFÜ—9’9˜Y×*;Ñ*;Ñ;À!ÑDˆDØ &¡ÐàUVˆN¨×)<Ñ)<Ñ<Ð@QÑQÑRØXYˆN¨×)<Ñ)<Ñ<ÐBSÐASÑTÑUØ×$Ñ$ ^×%:Ñ%:Ó%<Ö=ñ ð !Ð r   r<   c                 ó0   • SU R                   U-   SUS S3$ )Nz<|begin_of_image|>z<|end_of_image|>z.1fz seconds)r    )r,   rV   r<   s      r   rN   Ú&Glm46VProcessor.replace_frame_token_id¼   s+   € Ø# D×$4Ñ$4Ð7GÑ$GÐ#HÐHXÐYfÐgjÐXkÐksÐtÐtr   )r    r"   r+   r*   r!   r#   )NNNN)NN)TF)ry   )r   r   r   r   r
   Úvalid_processor_kwargsr)   ÚdictÚintÚstrr=   rX   ri   rp   Úpropertyrt   Úlistr…   rN   r   Ú__classcell__)r1   s   @r   r   r   )   sœ   ø† à2Ð÷Pð"3°ð 3Àð 3ÐSVô 3ð
°ð Àð ÐSVô ô>$-ðN Y^ô
ð6 ôAó ðAð!°$ð !¸4ÀÀSÁ	¹?ô !ñ*uÀc÷ uó ur   r   )Únumpyrz   Úprocessing_utilsr   r   r   Úutilsr   r   Ú
get_loggerr   rH   r
   r   Ú__all__r   r   r   Ú<module>r•      s`   ðó* ç PÑ Pß ,ð 
×	Ò	˜HÓ	%€ôÐ,°Eò ð ôSu�nó Suó ðSuðl Ð
�r   