o
    ei                     @   s   d 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mZ ddlmZmZ ddlmZ G d	d
 d
edd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 Chameleon.
    N   )BatchFeature)
ImageInput)MultiModalDataProcessingKwargsProcessorMixin
TextKwargsUnpack)PreTokenizedInput	TextInput)auto_docstringc                   @   s   e Zd ZU dZeed< dS )ChameleonTextKwargsa  
    return_for_text_completion (`bool`, *optional*, defaults to `False`):
        Whether the processed text is intended for text completion tasks. When `True`, the processor does not
        append the separator token (`sep_token`) to the end of the prompt, which is typically used for chat
        mode. When `False`, the separator token is appended for proper chat formatting.
    return_for_text_completionN)__name__
__module____qualname____doc__bool__annotations__ r   r   p/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/transformers/models/chameleon/processing_chameleon.pyr   !   s   
 r   F)totalc                   @   s,   e Zd ZU eed< ddddddidZdS )ChameleonProcessorKwargstext_kwargsF)paddingr   return_mm_token_type_idsreturn_tensorspt)r   common_kwargsN)r   r   r   r   r   	_defaultsr   r   r   r   r   ,   s   
 
r   c                
       sv   e Zd Zddedef fddZe		ddedB d	ee	B e
e B e
e	 B dB d
ee defddZdddZ  ZS )ChameleonProcessor   <image>image_seq_lengthimage_tokenc                    s   || _ t|dr|jn|| _|| j| _t|dr|jnd| _t|dr(|jnd| _|| j| _|| j| _	|| j| _
| j| j	| j
g| _t || dS )a  
        image_seq_length (`int`, *optional*, defaults to 1024):
            Sequence length of one image embedding.
        image_token (`str`, *optional*, defaults to `"<image>"`):
            The special token used to indicate image in the text.
        r$   	boi_tokenz<racm3:break>	eoi_tokenz<eoss>N)r#   hasattrr$   convert_tokens_to_idsimage_token_idr%   image_start_tokenr&   image_end_tokenimage_start_token_idimage_end_token_id	image_idssuper__init__)selfimage_processor	tokenizerr#   r$   	__class__r   r   r0   <   s   zChameleonProcessor.__init__Nimagestextkwargsreturnc                 K   s  t |tr	|g}nt |tst |d tstd|du r%|du r%td| jtfd| jji|}|d 	dd}g }| j
| j| j  | j }|D ]}|| j|}|s[|| jj7 }|| qJi }	|durr| j|fi |d	 }	|d 	d
d}
|d 	dd}| j|fi |d d
di}| j||dgd |rt|d }t|d }d|t|| j< | |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`.
        r   zAInvalid input text. Please provide a string, or a list of stringsNz&You must provide either text or imagestokenizer_init_kwargsr   r   Fimages_kwargsr   r   image)
modalities	input_ids   mm_token_type_ids)datatensor_type)
isinstancestrlist	TypeError
ValueError_merge_kwargsr   r3   init_kwargspopr*   r$   r#   r+   replace	sep_tokenappendr2   _check_special_mm_tokensnparray
zeros_likeisinr.   tolistr   )r1   r6   r7   r8   output_kwargsr   prompt_stringsone_img_tokenssampleimage_inputsr   r   text_inputs	array_idsr@   r   r   r   __call__Q   sD   
zChameleonProcessor.__call__c                 K   sL   i }|dur| j d 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.
        N   r?   )num_image_tokensnum_image_patchesr   )r#   lenupdater   )r1   image_sizesr8   vision_datar]   r^   r   r   r   _get_num_multimodal_tokens   s   z-ChameleonProcessor._get_num_multimodal_tokens)r!   r"   )NN)N)r   r   r   intrD   r0   r   r   r   r
   rE   r	   r   r   r[   rc   __classcell__r   r   r4   r   r    :   s    9r    )r   numpyrO   feature_extraction_utilsr   image_utilsr   processing_utilsr   r   r   r   r	   tokenization_utils_baser
   r   utilsr   r   r   r    __all__r   r   r   r   <module>   s   
h