
     `ir                         d Z ddlmZ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mZ ddlmZ  ej        e          Zd	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 Janus.
    )OptionalUnion   )BatchFeature)
ImageInput)ProcessingKwargsProcessorMixin
TextKwargsUnpack)PreTokenizedInput	TextInput)loggingzYou are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.

c                       e Zd ZU eed<   dS )JanusTextKwargsgeneration_modeN)__name__
__module____qualname__str__annotations__     ~/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/janus/processing_janus.pyr   r   %   s         r   r   F)totalc                   .    e Zd ZU eed<   dddddidZdS )	JanusProcessorKwargstext_kwargsFtext)paddingr   return_tensorspt)r   common_kwargsN)r   r   r   r   r   	_defaultsr   r   r   r   r   )   s;             #(VDD*D1 IIIr   r   c            	            e Zd ZdZddgZdZdZd fd	Z	 	 	 	 dd	ee	e
ee	         ee
         f         d
ee         dee         defdZd
efdZ xZS )JanusProcessora7  
    Constructs a Janus processor which wraps a Janus Image Processor and a Llama tokenizer into a single processor.

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

    Args:
        image_processor ([`JanusImageProcessor`]):
            The image processor is a required input.
        tokenizer ([`LlamaTokenizerFast`]):
            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.
        use_default_system_prompt (`str`, *optional*, defaults to `False`):
            Use default system prompt for Text Generation.
    image_processor	tokenizerJanusImageProcessorLlamaTokenizerFastNFc                     d| _         |j        | _        |j        | _        |j        | _        || _        t                                          |||           d S )Ni@  )chat_template)	num_image_tokensimage_token	boi_tokenimage_start_token	eoi_tokenimage_end_tokenuse_default_system_promptsuper__init__)selfr&   r'   r+   r2   kwargs	__class__s         r   r4   zJanusProcessor.__init__G   sY     #$0!*!4(2)B&)=QQQQQr   r   imagesr6   returnc                     | j         t          fd| j        j        i|}||t	          d          |]t          |t                    r|g}nDt          |t          t          f          rt          d |D                       st	          d          |d         
                    d          }g }| j        | j        | j        z  z   | j        z   }	|D ]Y}
|
                    | j        |	          }
| j        r|dk    r
t"          |
z   }
|d	k    r
|
| j        z  }
|                    |
           Z | j        |fi |d         }|$|d	k    r | j        dd
|i|d         d         |d<   t)          |          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
        JanusImageProcessor's [`~JanusImageProcessor.__call__`] if `images` is not `None`. Please refer to the doctsring
        of the above two methods for more information.

        Args:
            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).
            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.
            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`.
        tokenizer_init_kwargsNz'You must specify either text or images.c              3   @   K   | ]}t          |t                    V  d S )N)
isinstancer   ).0ts     r   	<genexpr>z*JanusProcessor.__call__.<locals>.<genexpr>   s-      =_=_UVjC>P>P=_=_=_=_=_=_r   zAInvalid input text. Please provide a string, or a list of stringsr   r   r   imager8   images_kwargspixel_values)datar   )_merge_kwargsr   r'   init_kwargs
ValueErrorr=   r   listtupleallpopr/   r-   r,   r1   replacer2   DEFAULT_SYSTEM_PROMPTappendr&   r   )r5   r   r8   videosaudior6   output_kwargsr   prompt_stringsone_img_tokenspromptrD   s               r   __call__zJanusProcessor.__call__P   s   P +* 
 
8<8R
V\
 
 <FNFGGG$$$ fv e}55 f#=_=_Z^=_=_=_:_:_ f !deee'6::;LMM /43CdF[3[\_c_ss 	* 	*F^^D$4nEEF- 8/V2K2K.7'))$00!!&))))t~nMMm0LMM /W"<"<#74#7#h#hv#hWfIg#h#h$D  &&&&r   c                 (     | j         j        |fi |S )z
        Forwards all arguments to the image processor's `postprocess` method.
        Refer to the original method's docstring for more details.
        )r&   postprocess)r5   r8   r6   s      r   rW   zJanusProcessor.postprocess   s"    
 0t#/AA&AAAr   )NF)NNNN)r   r   r   __doc__
attributesimage_processor_classtokenizer_classr4   r   r   r   rH   r   r   r   r   r   rU   rW   __classcell__)r7   s   @r   r%   r%   1   s        " $[1J1*OR R R R R R _c'+J' J'I0$y/4HYCZZ[J' $J' -.J' 
J' J' J' J'XB* B B B B B B B Br   r%   N)rX   typingr   r   feature_extraction_utilsr   image_utilsr   processing_utilsr   r	   r
   r   tokenization_utils_baser   r   utilsr   
get_loggerr   loggerrM   r   r   r%   __all__r   r   r   <module>rf      sf    # " " " " " " " 4 4 4 4 4 4 % % % % % % T T T T T T T T T T T T C C C C C C C C       
	H	%	%N     j        +5    pB pB pB pB pB^ pB pB pBf 
r   