U
    ,-e                     @   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ee	e
eee
 ee f ee	eeef e	eeef ee eee ee eeeeeeee	eef  ed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	tokenizerZBlipImageProcessorr   c                    s   t  || || _d S N)super__init__qformer_tokenizer)selfr   r   r   	__class__ y/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/transformers/models/instructblip/processing_instructblip.pyr   2   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kr|dkrt dt }|dk	r| jf ||||||||	|
||||||d|}|| | jf ||||||||	|
||||||d|}|d|d< |d|d< |dk	r| 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+   Z	input_idsZqformer_input_idsZattention_maskZqformer_attention_mask)r+   )
ValueErrorr   r   updater   popr   )r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   kwargsencodingZtext_encodingZqformer_text_encodingZimage_encodingr   r   r   __call__8   sb    

zInstructBlipProcessor.__call__c                 O   s   | j j||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   argsr0   r   r   r   r3      s    z"InstructBlipProcessor.batch_decodec                 O   s   | j j||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   decoder4   r   r   r   r6      s    zInstructBlipProcessor.decodec                 C   s"   | j j}| jj}tt|| S r   )r   model_input_namesr   listdictfromkeys)r   Ztokenizer_input_namesZimage_processor_input_namesr   r   r   r7      s    z'InstructBlipProcessor.model_input_namesc                    sT   t j|rtd| dt j|dd t j|d}| j| t j|f|S )NzProvided path (z#) should be a directory, not a fileT)exist_okr   )	ospathisfiler-   makedirsjoinr   save_pretrainedr   )r   Zsave_directoryr0   Zqformer_tokenizer_pathr   r   r   rA      s    z%InstructBlipProcessor.save_pretrainedc                 K   s.   t j|dd}| j|f|}|| | | S )Nr   )Z	subfolder)r   from_pretrainedZ_get_arguments_from_pretrainedappend)clsZpretrained_model_name_or_pathr0   r   r5   r   r   r   rB      s    
z%InstructBlipProcessor.from_pretrained)NNTFNNr   NNFFFFFTN)__name__
__module____qualname____doc__
attributesZimage_processor_classZtokenizer_classr   r   r   r   r
   r   boolstrr	   r   r   intr   r   r2   r3   r6   propertyr7   rA   classmethodrB   __classcell__r   r   r   r   r      s^                   O
	r   )rH   r<   typingr   r   r   Zimage_processing_utilsr   Zimage_utilsr   Zprocessing_utilsr   Ztokenization_utils_baser	   r
   r   r   utilsr   autor   r   r   r   r   r   <module>   s   