a
    hK$                     @   s   d dl mZmZ d dl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mZ ddlmZ d	d
lmZ G dd deddZG dd deZdgZdS )    )OptionalUnionN   )BatchFeature)
ImageInput)MultiModalDataProcessingKwargsProcessorMixinUnpack)PreTokenizedInput	TextInput)
TensorType   )AutoTokenizerc                   @   s&   e Zd ZddddddejdZdS )AriaProcessorKwargsF)paddingreturn_mm_token_type_ids  )max_image_sizeZsplit_image)text_kwargsimages_kwargsreturn_tensorsN)__name__
__module____qualname__r   ZPYTORCH	_defaults r   r   d/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/models/aria/processing_aria.pyr   !   s   r   F)totalc                       s   e Zd ZdZddgZdZdZdeee	f e
e	 e
eeeef ef  d fdd	Zdeeeee ee f e
e ee ed
ddZdddZedd Z  ZS )AriaProcessora  
    AriaProcessor is a processor for the Aria model which wraps the Aria image preprocessor and the LLama slow tokenizer.

    Args:
        image_processor (`AriaImageProcessor`, *optional*):
            The AriaImageProcessor to use for image preprocessing.
        tokenizer (`PreTrainedTokenizerBase`, *optional*):
            An instance of [`PreTrainedTokenizerBase`]. This should correspond with the model's text model. The tokenizer is a required input.
        chat_template (`str`, *optional*):
            A Jinja template which will be used to convert lists of messages in a chat into a tokenizable string.
        size_conversion (`Dict`, *optional*):
            A dictionary indicating size conversions for images.
    image_processor	tokenizerZAriaImageProcessorr   N)r!   chat_templatesize_conversionc                    sf   |d u rddd}dd |  D | _|j| _|j| _|d urP|jd u rP|j|_t j|||d d S )N      )i  r   c                 S   s   i | ]\}}t ||qS r   )int).0kvr   r   r   
<dictcomp>K       z*AriaProcessor.__init__.<locals>.<dictcomp>)r"   )itemsr#   image_tokenimage_token_idZ	pad_tokenZ	unk_tokensuper__init__)selfr    r!   r"   r#   	__class__r   r   r0   B   s    
zAriaProcessor.__init__)textimageskwargsreturnc                 K   sd  | j tfd| jji|}t|tr,|g}n t|tsLt|d tsLtd|dur| j|fi |d }| j	|j
jd  }g }	|d| }
|D ]&}|| jj| jj|
 }|	| qni }|}	|d d	d}|d d
d}| j|	fi |d d	di}| j|	|dgd |rPt|d }t|d }d||| jk< | |d< ti |||dS )a  
        Main method to prepare for the model one or several sequences(s) and image(s).

        Args:
            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).
            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.


        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`.
            - **pixel_mask** -- Pixel mask to be fed to a model. Returned when `images` is not `None`.
        Ztokenizer_init_kwargsr   zAInvalid input text. Please provide a string, or a list of stringsNr   r   	num_cropsr   r   r   Fimage)Z
modalitiesZ	input_ids   mm_token_type_ids)dataZtensor_type)Z_merge_kwargsr   r!   Zinit_kwargs
isinstancestrlist	TypeErrorr    r#   Zpixel_valuesshapepopreplacer-   appendZ_check_special_mm_tokensnparrayZ
zeros_liker.   tolistr   )r1   r4   r5   ZaudioZvideosr6   Zoutput_kwargsZimage_inputsZtokens_per_imageZprompt_stringsr8   sampler   r   Ztext_inputsZ	array_idsr;   r   r   r   __call__T   s>    
zAriaProcessor.__call__c                    s~   i }|durpt jdi   |  ddp6jj fdd|D }fdd|D }|||d tf 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.
        Nr   r   c                    s"   g | ]}j jg | R  qS r   )r    Zget_number_of_image_patches)r'   Z
image_size)r   r1   r   r   
<listcomp>   s   z<AriaProcessor._get_num_multimodal_tokens.<locals>.<listcomp>c                    s   g | ]}j   | qS r   )r#   )r'   Znum_patches)max_sizer1   r   r   rJ      r+   )num_image_tokensnum_image_patches)r   r   getupdater    r   r   )r1   Zimage_sizesr6   Zvision_datarM   rL   r   )r   rK   r1   r   _get_num_multimodal_tokens   s    
z(AriaProcessor._get_num_multimodal_tokensc                 C   s0   | j j}| jj}dd |D }tt|| S )Nc                 S   s   g | ]}|d kr|qS )r8   r   )r'   namer   r   r   rJ      r+   z3AriaProcessor.model_input_names.<locals>.<listcomp>)r!   model_input_namesr    r?   dictfromkeys)r1   Ztokenizer_input_namesZimage_processor_input_namesr   r   r   rR      s    zAriaProcessor.model_input_names)NNNN)NNN)N)r   r   r   __doc__
attributesZimage_processor_classZtokenizer_classr   r   r>   r   rS   floatr&   r0   r   r   r?   r   r
   r   r   rI   rP   propertyrR   __classcell__r   r   r2   r   r   /   s2       
   D
r   )typingr   r   numpyrE   Zimage_processing_utilsr   Zimage_utilsr   Zprocessing_utilsr   r   r	   r
   Ztokenization_utilsr   r   utilsr   autor   r   r   __all__r   r   r   r   <module>   s    