Ë
    T^(hå(  ã                   óÖ   — d Z ddlZddlmZmZ ddl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mZ dd	lmZ d
dlmZ  ej,                  e«      Z G d„ de
d¬«      Z G d„ de«      ZdgZy)zq
Processor class for InstructBLIP. Largely copy of Blip2Processor with addition of a tokenizer for the Q-Former.
é    N)ÚListÚUnioné   )ÚBatchFeature)Ú
ImageInput)ÚProcessingKwargsÚProcessorMixinÚUnpack)Ú
AddedTokenÚBatchEncodingÚPreTokenizedInputÚ	TextInput)Úloggingé   )ÚAutoTokenizerc            
       ó*   — e Zd Zddddddddddœ	i dœZy)ÚInstructBlipProcessorKwargsTFr   )	Úadd_special_tokensÚpaddingÚstrideÚreturn_overflowing_tokensÚreturn_special_tokens_maskÚreturn_offsets_mappingÚreturn_token_type_idsÚreturn_lengthÚverbose)Útext_kwargsÚimages_kwargsN)Ú__name__Ú
__module__Ú__qualname__Ú	_defaults© ó    úv/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/instructblip/processing_instructblip.pyr   r   &   s0   „ ð #'ØØØ).Ø*/Ø&+Ø%*Ø"Øñ

ð ñ�Ir$   r   F)Útotalc            
       óº   ‡ — 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e   d
efd„Zd„ Zd„ Zed„ «       Zˆ fd„Zeˆ fd„«       Zˆ xZS )ÚInstructBlipProcessora�  
    Constructs an InstructBLIP processor which wraps a BLIP image processor and a LLaMa/T5 tokenizer into a single
    processor.

    [`InstructBlipProcessor`] offers all the functionalities of [`BlipImageProcessor`] and [`AutoTokenizer`]. See the
    docstring of [`~BlipProcessor.__call__`] and [`~BlipProcessor.decode`] for more information.

    Args:
        image_processor (`BlipImageProcessor`):
            An instance of [`BlipImageProcessor`]. 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)ÚBlipImageProcessorÚBlipImageProcessorFastr   c                 óÔ   •— t        |d«      s2t        ddd¬«      | _        |j                  | j                  gd¬«       n|j                  | _        || _        t
        ‰| �  |||«       y )NÚimage_tokenz<image>FT)Ú
normalizedÚspecial)Úspecial_tokens)Úhasattrr   r0   Ú
add_tokensr,   ÚsuperÚ__init__)Úselfr)   r*   r+   r,   ÚkwargsÚ	__class__s         €r%   r7   zInstructBlipProcessor.__init__P   sb   ø€ Ü�y -Ô0Ü)¨)ÀÈtÔTˆDÔØ× Ñ  $×"2Ñ"2Ð!3ÀDÐ ÕIà(×4Ñ4ˆDÔØ 0ˆÔÜ‰Ñ˜¨)Ð5FÕGr$   ÚimagesÚtextr9   Úreturnc                 óà  — |€|€t        d«      ‚ | j                  t        fd| j                  j                  i|¤Ž}t        «       }|��ut        |t        «      r|g}n.t        |t        «      st        |d   t        «      st        d«      ‚|d   j                  dd«      } | j                  |fi |d   ¤ddi¤Ž}	||d   d<   | j                  �{|�yi }
| j                  j                  | j                  z  }| j                  |gt        |«      z  dd¬	«      }|	D ]-  }t        ||   |	|   «      D ��cg c]
  \  }}||z   ‘Œ c}}|
|<   Œ/ n|	}
|�t        j!                  d
«       t#        |
|¬«      }
|j%                  |
«        | j&                  |fi |d   ¤Ž}|j                  d«      |d<   |j                  d«      |d<   |�' | j(                  |fi |d   ¤Ž}|j%                  |«       |S c c}}w )a°  
        This method uses [`BlipImageProcessor.__call__`] method to prepare image(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.
        Args:
            images (`ImageInput`):
                The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
                tensor. Both channels-first and channels-last formats are supported.
            text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`):
                The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
                (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
                `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
        Nz,You have to specify at least images or text.Útokenizer_init_kwargsr   zAInvalid input text. Please provide a string, or a list of stringsr   Úreturn_tensorsF)r   r@   aA  Expanding inputs for image tokens in InstructBLIP should be done in processing. Please follow instruction here (https://gist.github.com/zucchini-nlp/e9f20b054fa322f84ac9311d9ab67042) to update your InstructBLIP model. Using processors without these attributes in the config is deprecated and will throw an error in v4.50.)Útensor_typeÚ	input_idsÚqformer_input_idsÚattention_maskÚqformer_attention_maskr   )Ú
ValueErrorÚ_merge_kwargsr   r*   Úinit_kwargsr   Ú
isinstanceÚstrÚlistÚpopr,   r0   ÚcontentÚlenÚzipÚloggerÚwarning_oncer   Úupdater+   r)   )r8   r;   r<   ÚaudioÚvideosr9   Úoutput_kwargsÚencodingr@   Ú_text_encodingÚtext_encodingÚimage_tokensÚimage_token_encodingÚkÚimg_encodingÚtxt_encodingÚqformer_text_encodingÚimage_encodings                     r%   Ú__call__zInstructBlipProcessor.__call__Y   s?  € ð, ˆ>˜d˜lÜÐKÓLÐLà*˜×*Ñ*Ü'ñ
à"&§.¡.×"<Ñ"<ð
ð ñ
ˆô  “>ˆàÑÜ˜$¤Ô$Ø�v‘Ü ¤dÔ+´J¸tÀA¹wÌÔ4LÜ Ð!dÓeÐeð +¨=Ñ9×=Ñ=Ð>NÐPTÓUˆNØ+˜TŸ^™^¨DÑf°MÀ-Ñ4PÑfÐaeÒfˆNØ=KˆM˜-Ñ(Ð)9Ñ:ð ×$Ñ$Ð0°VÐ5GØ "�Ø#×/Ñ/×7Ñ7¸$×:OÑ:OÑO�Ø'+§~¡~Ø!�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×$:Ñ$:¸4Ñ$`À=ÐQ^ÑC_Ñ$`Ð!Ø,A×,EÑ,EÀkÓ,RˆHÐ(Ñ)Ø1F×1JÑ1JÐK[Ó1\ˆHÐ-Ñ.àÐØ1˜T×1Ñ1°&Ñ[¸MÈ/Ñ<ZÑ[ˆNØ�O‰O˜NÔ+àˆùó3(s   Ä1G*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©r8   Úargsr9   s      r%   rb   z"InstructBlipProcessor.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*   Údecoderc   s      r%   rf   zInstructBlipProcessor.decode±   s    € ð
 %ˆt�~‰~×$Ñ$ dÐ5¨fÑ5Ð5r$   c                 óœ   — | j                   j                  }| j                  j                  }t        t        j                  ||z   «      «      S ©N)r*   Úmodel_input_namesr)   rK   ÚdictÚfromkeys)r8   Útokenizer_input_namesÚimage_processor_input_namess      r%   ri   z'InstructBlipProcessor.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ÚisfilerF   ÚmakedirsÚjoinr+   Úsave_pretrainedÚ
attributesÚremover6   )r8   Úsave_directoryr9   Úqformer_tokenizer_pathÚqformer_presentÚoutputsr:   s         €r%   ru   z%InstructBlipProcessor.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)r6   Úfrom_pretrainedrI   Útupler   r+   )ÚclsÚpretrained_model_name_or_pathr9   Ú	processorr+   r:   s        €r%   r~   z%InstructBlipProcessor.from_pretrainedÓ   sP   ø€ ä‘GÑ+Ð,IÑTÈVÑTˆ	ô �i¤Ô'Ø! !™ˆIÜ)×9Ñ9Ð:WÐcvÔwÐØ&7ˆ	Ô#ØÐr$   rh   )NNNN)r   r    r!   Ú__doc__rv   Úvalid_kwargsÚimage_processor_classÚtokenizer_classÚqformer_tokenizer_classr7   r   r   r   r   r   r
   r   r   r`   rb   rf   Úpropertyri   ru   Úclassmethodr~   Ú__classcell__)r:   s   @r%   r(   r(   7   sÆ   ø„ ñò$ G€JØ&Ð'€LØLÐØ%€OØ-ÐõHð "Ø^bØØñMàðMð �IÐ0°$°y±/À4ÐHYÑCZÐZÑ[ðMð Ð4Ñ5ðMð 
óMò`<ò6ð ñXó ðXôð& óó ôr$   r(   )rƒ   rp   Útypingr   r   Úimage_processing_utilsr   Úimage_utilsr   Úprocessing_utilsr   r	   r
   Útokenization_utils_baser   r   r   r   Úutilsr   Úautor   Ú
get_loggerr   rP   r   r(   Ú__all__r#   r$   r%   ú<module>r”      sl   ðñó 
ß å 2Ý %ß HÑ H÷ó õ Ý  ð 
ˆ×	Ñ	˜HÓ	%€ôÐ"2¸%õ ô"e˜Nô eðP #Ð
#�r$   