
     `i#                         d Z 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mZ ddlmZmZ  G d	 d
ed          Z G d ded          Z G d de          ZdgZdS )z 
Processor class for Chameleon.
    )OptionalUnionN   )BatchFeature)
ImageInput)MultiModalDataProcessingKwargsProcessorMixin
TextKwargsUnpack)PreTokenizedInput	TextInputc                       e Zd ZU eed<   dS )ChameleonTextKwargsreturn_for_text_completionN)__name__
__module____qualname__bool__annotations__     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/chameleon/processing_chameleon.pyr   r   #   s          $$$$$$r   r   F)totalc                   0    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   '   sI         $$$$ */(-
 
 d
	 	IIIr   r   c                        e Zd ZdZddgZdZdZdded	ef fd
Z		 	 	 	 dde
e         de
eeeee         ee         f                  dee         defdZddZ xZS )ChameleonProcessora/  
    Constructs a Chameleon processor which wraps a Chameleon image processor and a Chameleon tokenizer into a single
    processor.

    [`ChameleonProcessor`] offers all the functionalities of [`ChameleonImageProcessor`] and [`LlamaTokenizerFast`].
    See the [`~ChameleonProcessor.__call__`] and [`~ChameleonProcessor.decode`] for more information.

    Args:
        image_processor ([`ChameleonImageProcessor`]):
            The image processor is a required input.
        tokenizer ([`LlamaTokenizerFast`]):
            The tokenizer is a required input.
        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.
    image_processor	tokenizer)LlamaTokenizerLlamaTokenizerFastChameleonImageProcessor   <image>image_seq_lengthimage_tokenc                 8   || _         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 )Nr.   	boi_tokenz<racm3:break>	eoi_tokenz<eoss>)r-   hasattrr.   convert_tokens_to_idsimage_token_idr0   image_start_tokenr1   image_end_tokenimage_start_token_idimage_end_token_id	image_idssuper__init__)selfr&   r'   r-   r.   	__class__s        r   r;   zChameleonProcessor.__init__L   s
    04;I}4U4Uf900[f'==d>NOO#*9k#B#BWI 	 7>i6U6Ucy22[c'==d>NOO$-$C$CDDZ$[$[!"+"A"A$BV"W"W-t/H$Jab)44444r   Nimagestextkwargsreturnc                    t          |t                    r|g}n?t          |t                    s*t          |d         t                    st          d          ||t	          d           | j        t          fd| j        j        i|}|d         	                    dd          }g }| j
        | j        | j        z  z   | j        z   }	|D ]C}
|
                    | j        |	          }
|s|
| j        j        z  }
|                    |
           Di }| | j        |fi |d	         }|d         	                    d
d          }|d         	                    dd          } | j        |fi |d         d
di}|                     ||dg           |rht'          j        |d                   }t'          j        |d                   }d|t'          j        || j                  <   |                                |d<   t3          i |||          S )a  
        Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the `text`
        and `kwargs` arguments to LlamaTokenizerFast's [`~LlamaTokenizerFast.__call__`] if `text` is not `None` to encode
        the text. To prepare the image(s), this method forwards the `images` and `kwargs` arguments to
        CLIPImageProcessor's [`~CLIPImageProcessor.__call__`] if `images` is not `None`. Please refer to the docstring
        of the above two methods for more information.

        Args:
            images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`):
                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.
            text (`str`, `list[str]`, `list[list[str]]`):
                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).
            return_tensors (`str` or [`~utils.TensorType`], *optional*):
                If set, will return tensors of a particular framework. Acceptable values are:

                - `'tf'`: Return TensorFlow `tf.constant` objects.
                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return NumPy `np.ndarray` objects.
                - `'jax'`: Return JAX `jnp.ndarray` objects.

        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   r'   init_kwargspopr5   r.   r-   r6   replace	sep_tokenappendr&   _check_special_mm_tokensnparray
zeros_likeisinr9   tolistr   )r<   r>   r?   audiovideosr@   output_kwargsr   prompt_stringsone_img_tokenssampleimage_inputsr    r   text_inputs	array_idsrI   s                    r   __call__zChameleonProcessor.__call__[   sg   R dC   	a6DDD$'' 	a
47C0H0H 	a_```<FNEFFF**$
 
"&."<
 
 

 &3=%A%E%EFbdi%j%j" /43CdF[3[\_c_ss 	* 	*F^^D$4nEEF- 3$.22!!&))))/4/YY-:XYYL&}599:JDQQ#0#?#C#CD^`e#f#f $dn^ii}]7Siidhiii%%nkwi%XXX# 	J[!9::I "k+.F G GDEbgi@@A/@/G/G/I/IK+,!@K!@<!@n]]]]r   c                     i }|F| j         dz   gt          |          z  }dgt          |          z  }|                    ||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   rH   )num_image_tokensnum_image_patchesr   )r-   lenupdater   )r<   image_sizesr@   vision_datari   rj   s         r   _get_num_multimodal_tokensz-ChameleonProcessor._get_num_multimodal_tokens   st     " $ 5 9:S=M=MM!"c+&6&6 64D[lmmnnn,,,,,r   )r+   r,   )NNNN)N)r   r   r   __doc__
attributestokenizer_classimage_processor_classintrM   r;   r   r   r   r   r   rN   r   r   r   rf   ro   __classcell__)r=   s   @r   r%   r%   5   s        $ $[1J>O55 5S 5^a 5 5 5 5 5 5" (,hlO^ O^$O^ uY(94	?DQbLccdeO^ 12O^ 
O^ O^ O^ O^b- - - - - - - -r   r%   )rp   typingr   r   numpyrX   feature_extraction_utilsr   image_utilsr   processing_utilsr   r	   r
   r   r   tokenization_utils_baser   r   r   r   r%   __all__r   r   r   <module>r}      s^    # " " " " " " "     4 4 4 4 4 4 % % % % % %              D C C C C C C C% % % % %*E % % % %    /u    L- L- L- L- L- L- L- L-^  
 r   