Ë
    T^(hd+  ã                   ó¾   — d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZmZmZ dd	lmZmZ d
dlmZ  ej0                  e«      Z G d„ de«      Zy)zq
Processor class for InstructBLIP. Largely copy of Blip2Processor with addition of a tokenizer for the Q-Former.
é    N)ÚListÚOptionalÚUnioné   )ÚBatchFeature)Ú
VideoInput)ÚProcessorMixin)Ú
AddedTokenÚBatchEncodingÚPaddingStrategyÚPreTokenizedInputÚ	TextInputÚTruncationStrategy)Ú
TensorTypeÚloggingé   )ÚAutoTokenizerc            $       ó:  ‡ — e Zd ZdZg d¢ZdgZdZdZdZdˆ fd„	Z		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dde
deeeee   ee   f   d	ed
eeeef   deeeef   dee   dedee   dee   dededededededeeeef      def"d„Zd„ Zd„ Zed„ «       Zˆ fd„Zeˆ fd„«       Zˆ xZS ) ÚInstructBlipVideoProcessoraâ  
    Constructs an InstructBLIPVideo processor which wraps a InstructBLIP image processor and a LLaMa/T5 tokenizer into a single
    processor.

    [`InstructBlipVideoProcessor`] offers all the functionalities of [`InstructBlipVideoImageProcessor`] and [`AutoTokenizer`]. See the
    docstring of [`~InstructBlipVideoProcessor.__call__`] and [`~InstructBlipVideoProcessor.decode`] for more information.

    Args:
        image_processor (`InstructBlipVideoImageProcessor`):
            An instance of [`InstructBlipVideoImageProcessor`]. The image processor is a required input.
        tokenizer (`AutoTokenizer`):
            An instance of ['PreTrainedTokenizer`]. The tokenizer is a required input.
        qformer_tokenizer (`AutoTokenizer`):
            An instance of ['PreTrainedTokenizer`]. The Q-Former tokenizer is a required input.
        num_query_tokens (`int`, *optional*):
            Number of tokens used by the Qformer as queries, should be same as in model's config.
    )Úimage_processorÚ	tokenizerÚqformer_tokenizerÚnum_query_tokensÚInstructBlipVideoImageProcessorr   c                 óÔ   •— t        |d«      s2t        ddd¬«      | _        |j                  | j                  gd¬«       n|j                  | _        || _        t
        ‰| �  |||«       y )NÚvideo_tokenz<video>FT)Ú
normalizedÚspecial)Úspecial_tokens)Úhasattrr
   r   Ú
add_tokensr   ÚsuperÚ__init__)Úselfr   r   r   r   ÚkwargsÚ	__class__s         €ú€/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/instructblipvideo/processing_instructblipvideo.pyr#   z#InstructBlipVideoProcessor.__init__A   sb   ø€ Ü�y -Ô0Ü)¨)ÀÈtÔTˆDÔØ× Ñ  $×"2Ñ"2Ð!3ÀDÐ ÕIà(×4Ñ4ˆDÔØ 0ˆÔÜ‰Ñ˜¨)Ð5FÕGó    ÚimagesÚtextÚadd_special_tokensÚpaddingÚ
truncationÚ
max_lengthÚstrideÚpad_to_multiple_ofÚreturn_attention_maskÚreturn_overflowing_tokensÚreturn_special_tokens_maskÚreturn_offsets_mappingÚreturn_token_type_idsÚreturn_lengthÚverboseÚreturn_tensorsÚreturnc                 ót  — |€|€t        d«      ‚t        «       }|��ot        |t        «      r|g}n.t        |t        «      st        |d   t        «      st        d«      ‚ | j
                  d||||||||	|
|||||ddœ|¤Ž}| j                  �~|�|i }| j                  j                  | j                  z  dz  }| j                  |gt        |«      z  dd¬«      }|D ]-  }t        ||   ||   «      D ��cg c]
  \  }}||z   ‘Œ c}}||<   Œ/ n|}|�t        j                  d	«       t        ||¬
«      }|j                  |«        | j                  d||||||||	|
||||||dœ|¤Ž}|j!                  d«      |d<   |j!                  d«      |d<   |�$| j#                  ||¬«      }|j                  |«       |S c c}}w )a%  
        This method uses [`InstructBlipVideoImageProcessor.__call__`] method to prepare image(s) or video(s) for the model, and
        [`BertTokenizerFast.__call__`] to prepare text for the model.

        Please refer to the docstring of the above two methods for more information.
        Nz3You have to specify at least one of images or text.r   zAInvalid input text. Please provide a string, or a list of strings)r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   é   F)r+   r8   aK  Expanding inputs for video tokens in InstructBLIPVideo should be done in processing. Please follow instruction here (https://gist.github.com/zucchini-nlp/65f22892b054dc0d68228af56fbeaac2) to update your InstructBLIPVideo model. Using processors without these attributes in the config is deprecated and will throw an error in v4.47.)Útensor_typeÚ	input_idsÚqformer_input_idsÚattention_maskÚqformer_attention_mask)r8   © )Ú
ValueErrorr   Ú
isinstanceÚstrÚlistr   r   r   ÚcontentÚlenÚzipÚloggerÚwarning_oncer   Úupdater   Úpopr   )r$   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r%   ÚencodingÚ_text_encodingÚtext_encodingÚvideo_tokensÚvideo_token_encodingÚkÚimg_encodingÚtxt_encodingÚqformer_text_encodingÚimage_encodings                               r'   Ú__call__z#InstructBlipVideoProcessor.__call__J   sE  € ð4 ˆ>˜d˜lÜÐRÓSÐSä“>ˆàÑÜ˜$¤Ô$Ø�v‘Ü ¤dÔ+´J¸tÀA¹wÌÔ4LÜ Ð!dÓeÐeà+˜TŸ^™^ð ØØ#5ØØ%Ø%ØØ#5Ø&;Ø*CØ+EØ'=Ø&;Ø+ØØ#ñð  ñ!ˆNð* ×$Ñ$Ð0°VÐ5GØ "�à×$Ñ$×,Ñ,¨t×/DÑ/DÑDÀqÑHð ð (,§~¡~Ø!�N¤S¨£YÑ.À5ÐY]ð (6ó (Ð$ð (ò �Aô ;>Ð>RÐSTÑ>UÐWeÐfgÑWhÓ:i÷(á6˜L¨,ð % |Ó3ó(�M !Ò$ñð !/�ØÐ%Ü×'Ñ'ðBôô *¨-À^ÔTˆMØ�O‰O˜MÔ*Ø$: D×$:Ñ$:ð %ØØ#5ØØ%Ø%ØØ#5Ø&;Ø*CØ+EØ'=Ø&;Ø+ØØ-ñ%ð  ñ!%Ð!ð$ -B×,EÑ,EÀkÓ,RˆHÐ(Ñ)Ø1F×1JÑ1JÐK[Ó1\ˆHÐ-Ñ.àÐØ!×1Ñ1°&ÈÐ1ÓXˆNØ�O‰O˜NÔ+àˆùóS(s   Ã2F4c                 ó:   —  | j                   j                  |i |¤ŽS )zÃ
        This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r   Úbatch_decode©r$   Úargsr%   s      r'   rY   z'InstructBlipVideoProcessor.batch_decode¹   s    € ð
 +ˆt�~‰~×*Ñ*¨DÐ;°FÑ;Ð;r(   c                 ó:   —  | j                   j                  |i |¤ŽS )z½
        This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r   ÚdecoderZ   s      r'   r]   z!InstructBlipVideoProcessor.decodeÁ   s    € ð
 %ˆt�~‰~×$Ñ$ dÐ5¨fÑ5Ð5r(   c                 óœ   — | j                   j                  }| j                  j                  }t        t        j                  ||z   «      «      S ©N)r   Úmodel_input_namesr   rE   ÚdictÚfromkeys)r$   Útokenizer_input_namesÚimage_processor_input_namess      r'   r`   z,InstructBlipVideoProcessor.model_input_namesÈ   sA   € ð !%§¡× @Ñ @ÐØ&*×&:Ñ&:×&LÑ&LÐ#Ü”D—M‘MÐ"7Ð:UÑ"UÓVÓWÐWr(   c                 ó®  •— t         j                  j                  |«      rt        d|› d�«      ‚t        j                  |d¬«       t         j                  j                  |d«      }| j                  j                  |«       d| j                  v }|r| j                  j                  d«       t        ‰| �  |fi |¤Ž}|r| xj                  dgz  c_        |S )NzProvided path (z#) should be a directory, not a fileT)Úexist_okr   )ÚosÚpathÚisfilerB   ÚmakedirsÚjoinr   Úsave_pretrainedÚ
attributesÚremover"   )r$   Úsave_directoryr%   Úqformer_tokenizer_pathÚqformer_presentÚoutputsr&   s         €r'   rl   z*InstructBlipVideoProcessor.save_pretrainedÐ   s¶   ø€ Ü�7‰7�>‰>˜.Ô)Ü˜¨~Ð.>Ð>aÐbÓcÐcÜ
�‰�N¨TÕ2Ü!#§¡§¡¨nÐ>QÓ!RÐØ×Ñ×.Ñ.Ð/EÔFð .°·±Ð@ˆÙØ�O‰O×"Ñ"Ð#6Ô7ä‘'Ñ)¨.ÑC¸FÑCˆáØ�OŠOÐ 3Ð4Ñ4�OØˆr(   c                 óŽ   •— t        ‰| �  |fi |¤Ž}t        |t        «      r|d   }t	        j                  |d¬«      }||_        |S )Nr   r   )Ú	subfolder)r"   Úfrom_pretrainedrC   Útupler   r   )ÚclsÚpretrained_model_name_or_pathr%   Ú	processorr   r&   s        €r'   ru   z*InstructBlipVideoProcessor.from_pretrainedã   sP   ø€ ä‘GÑ+Ð,IÑTÈVÑTˆ	ô �i¤Ô'Ø! !™ˆIÜ)×9Ñ9Ð:WÐcvÔwÐØ&7ˆ	Ô#ØÐr(   r_   )NNTFNNr   NNFFFFFTN) Ú__name__Ú
__module__Ú__qualname__Ú__doc__rm   Úvalid_kwargsÚimage_processor_classÚtokenizer_classÚqformer_tokenizer_classr#   r   r   r   r   r   ÚboolrD   r   r   r   Úintr   r   rW   rY   r]   Úpropertyr`   rl   Úclassmethodru   Ú__classcell__)r&   s   @r'   r   r   (   s¦  ø„ ñò$ G€JØ&Ð'€LØ=ÐØ%€OØ-ÐõHð "Ø^bØ#'Ø5:Ø;?Ø$(ØØ,0Ø04Ø*/Ø+0Ø',Ø&+Ø#ØØ;?ñ#làðlð �IÐ0°$°y±/À4ÐHYÑCZÐZÑ[ðlð !ð	lð
 �t˜S /Ð1Ñ2ðlð ˜$ Ð%7Ð7Ñ8ðlð ˜S‘Mðlð ðlð % S™Mðlð  (¨™~ðlð $(ðlð %)ðlð !%ðlð  $ðlð ðlð  ð!lð" !  s¨J Ñ!7Ñ8ð#lð& 
ó'lò^<ò6ð ñXó ðXôð& óó ôr(   r   )r}   rg   Útypingr   r   r   Úimage_processing_utilsr   Úimage_utilsr   Úprocessing_utilsr	   Útokenization_utils_baser
   r   r   r   r   r   Úutilsr   r   Úautor   Ú
get_loggerrz   rI   r   rA   r(   r'   ú<module>r�      sR   ðñó 
ß (Ñ (å 2Ý %Ý .÷÷ ÷ )Ý  ð 
ˆ×	Ñ	˜HÓ	%€ôD õ Dr(   