o
    ei                     @   s   d Z ddl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mZ ddlmZmZ eeZG d	d
 d
eddZeG dd deZdgZdS )z
Processor class for Llava.
    N   )BatchFeature)
ImageInputget_image_sizeto_numpy_array)MultiModalDataProcessingKwargsProcessorMixinUnpack)PreTokenizedInput	TextInput)auto_docstringloggingc                   @   s   e Zd ZddddiZdS )LlavaProcessorKwargstext_kwargsF)paddingreturn_mm_token_type_idsN)__name__
__module____qualname__	_defaults r   r   h/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/transformers/models/llava/processing_llava.pyr   #   s    
r   F)totalc                
       sv   e Zd Z							d fdd	Ze		ddedB deeB ee B ee B de	e
 d	efd
dZdddZ  ZS )LlavaProcessorN<image>r   c           	         sT   || _ || _|| _t|dr|jn|| _|j| jddd | _t j|||d dS )a  
        patch_size (`int`, *optional*):
            Patch size from the vision tower.
        vision_feature_select_strategy (`str`, *optional*):
            The feature selection strategy used to select the vision feature from the vision backbone.
            Should be same as in model's config
        image_token (`str`, *optional*, defaults to `"<image>"`):
            Special token used to denote image location.
        num_additional_image_tokens (`int`, *optional*, defaults to 0):
            Number of additional tokens added to the image embeddings, such as CLS (+1). If the backbone has no CLS or other
            extra tokens appended, no need to set this arg.
        image_tokenF)add_special_tokensr   )chat_templateN)	
patch_sizenum_additional_image_tokensvision_feature_select_strategyhasattrr   encodeimage_token_idsuper__init__)	selfimage_processor	tokenizerr   r!   r   r   r    kwargs	__class__r   r   r&   +   s   zLlavaProcessor.__init__imagestextr*   returnc                 K   s  |du r|du rt d| jtfd| jji|}|dur)| j|fi |d }ni }t|tr4|g}nt|tsDt|d tsDt	d|}|
ddur|d }tt|d \}}	|| j |	| j  | j }
| jdkrq|
d	8 }
g }|D ]}|| j| j|
 }|| qu|d
 dd}|d
 dd}| j|fi |d
 ddi}| j||dgd |rt|d }t|d }d	||| jk< | |d< ti |||dS )aA  
        Returns:
            [`BatchFeature`]: A [`BatchFeature`] with the following fields:

            - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
            - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
              `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
              `None`).
            - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
        Nz7You have to specify at least one of `images` or `text`.tokenizer_init_kwargsimages_kwargsr   zAInvalid input text. Please provide a string, or a list of stringspixel_valuesdefault   r   return_tensorsr   Fimage)
modalities	input_idsmm_token_type_ids)datatensor_type)
ValueError_merge_kwargsr   r)   init_kwargsr(   
isinstancestrlist	TypeErrorgetr   r   r   r    r!   replacer   appendpop_check_special_mm_tokensnparray
zeros_liker$   tolistr   )r'   r-   r.   r*   output_kwargsimage_inputsprompt_stringsr2   heightwidthnum_image_tokenssampler5   r   text_inputs	array_idsr9   r   r   r   __call__I   sR   

zLlavaProcessor.__call__c           
      K   s   i }|durSt jdi }|| |ddp| jj}|d |d }}|| j || j  }|| j7 }| jdkr=|d8 }|gt	| }dgt	| }	|||	d t
d	i |S )
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.

        Returns:
            `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
            input modalities, along with other useful data.
        Nr1   	crop_sizerO   rP   r3   r4   )rQ   num_image_patchesr   )r   r   rC   updater(   rV   r   r    r!   lenr   )
r'   image_sizesr*   vision_datar1   rV   resized_heightresized_widthrQ   rW   r   r   r   _get_num_multimodal_tokens   s   


z)LlavaProcessor._get_num_multimodal_tokens)NNNNNr   r   )NN)N)r   r   r   r&   r   r   r   r   rA   r
   r   r   rU   r^   __classcell__r   r   r+   r   r   )   s,    @r   )__doc__numpyrH   feature_extraction_utilsr   image_utilsr   r   r   processing_utilsr   r   r	   r
   tokenization_utils_baser   r   utilsr   r   
get_loggerr   loggerr   r   __all__r   r   r   r   <module>   s   
 
