o
    h                     @   s   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 dd	lmZ d
dlmZ G dd deZdS )zq
Processor class for InstructBLIP. Largely copy of Blip2Processor with addition of a tokenizer for the Q-Former.
    N)ListOptionalUnion   )BatchFeature)
ImageInput)ProcessorMixin)PaddingStrategyPreTokenizedInput	TextInputTruncationStrategy)
TensorType   )AutoTokenizerc                $       s  e Zd ZdZddgZdZdZ fd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dZdd  Zd!d" Zed#d$ Z fd%d&Zed'd( Z  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.
    image_processor	tokenizerBlipImageProcessorr   c                    s   t  || || _d S N)super__init__qformer_tokenizer)selfr   r   r   	__class__ n/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/models/instructblip/processing_instructblip.pyr   3   s   
zInstructBlipProcessor.__init__NTFr   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                 K   s   |du r|du rt dt }|durV| jd	||||||||	|
||||||d|}|| | jd	||||||||	|
||||||d|}|d|d< |d|d< |durf| j||d}|| |S )
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.
        Nz,You have to specify at least images or text.)r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   	input_idsqformer_input_idsattention_maskqformer_attention_mask)r,   r   )
ValueErrorr   r   updater   popr   )r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   kwargsencodingtext_encodingqformer_text_encodingimage_encodingr   r   r   __call__9   sb   

zInstructBlipProcessor.__call__c                 O      | 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   argsr5   r   r   r   r<         z"InstructBlipProcessor.batch_decodec                 O   r;   )z
        This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r   decoder=   r   r   r   r@      r?   zInstructBlipProcessor.decodec                 C   s"   | j j}| jj}tt|| S r   )r   model_input_namesr   listdictfromkeys)r   tokenizer_input_namesimage_processor_input_namesr   r   r   rA      s   z'InstructBlipProcessor.model_input_namesc                    sX   t j|rtd| dt j|dd t j|d}| j| t j|fi |S )NzProvided path (z#) should be a directory, not a fileT)exist_okr   )	ospathisfiler2   makedirsjoinr   save_pretrainedr   )r   save_directoryr5   qformer_tokenizer_pathr   r   r   rM      s   z%InstructBlipProcessor.save_pretrainedc                 K   s2   t j|dd}| j|fi |}|| | | S )Nr   )	subfolder)r   from_pretrained_get_arguments_from_pretrainedappend)clspretrained_model_name_or_pathr5   r   r>   r   r   r   rQ      s   
z%InstructBlipProcessor.from_pretrained)NNTFNNr   NNFFFFFTN)__name__
__module____qualname____doc__
attributesimage_processor_classtokenizer_classr   r   r   r   r
   r   boolstrr	   r   r   intr   r   r:   r<   r@   propertyrA   rM   classmethodrQ   __classcell__r   r   r   r   r      s    	

O
	r   )rY   rH   typingr   r   r   image_processing_utilsr   image_utilsr   processing_utilsr   tokenization_utils_baser	   r
   r   r   utilsr   autor   r   r   r   r   r   <module>   s   