o
    Zh-!                     @   s   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eZd	ZG d
d deddZG dd de	ddZG dd de
ZdgZdS )z
Processor class for Janus.
    )List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                   @   s   e Zd ZU eed< dS )JanusTextKwargsgeneration_modeN)__name__
__module____qualname__str__annotations__ r   r   Y/var/www/auris/lib/python3.10/site-packages/transformers/models/janus/processing_janus.pyr   (   s   
 r   F)totalc                   @   s*   e Zd ZU eed< dddddidZdS )	JanusProcessorKwargstext_kwargsFtext)paddingr   Zreturn_tensorspt)r   Zcommon_kwargsN)r   r   r   r   r   	_defaultsr   r   r   r   r   ,   s
   
 
r   c                	       s   e Zd ZdZddgZddgZdZdZd fd
d	Z				dde	e
eee
 ee f dedee defddZdd Zdd ZdefddZedd Z  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chat_templateuse_default_system_promptZJanusImageProcessorZLlamaTokenizerFastNFc                    s:   d| _ |j| _|j| _|j| _|| _t j|||d d S )Ni@  )r!   )	num_image_tokensimage_tokenZ	boi_tokenimage_start_tokenZ	eoi_tokenimage_end_tokenr"   super__init__)selfr   r    r!   r"   kwargs	__class__r   r   r(   K   s   zJanusProcessor.__init__r   imagesr*   returnc                 K   s8  | j tfd| jji|}|du r|du rtd|dur:t|tr&|g}nt|ttfr6t	dd |D s:td|d 
d}g }| j| j| j  | j }	|D ]"}
|
| j|	}
| jre|d	kret|
 }
|d
krn|
| j7 }
||
 qQ| j|fi |d }|dur|d
kr| jdd|i|d d |d< t|d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 `kwrags` 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`.
        Ztokenizer_init_kwargsNz'You must specify either text or images.c                 s   s    | ]}t |tV  qd S N)
isinstancer   ).0tr   r   r   	<genexpr>   s    z*JanusProcessor.__call__.<locals>.<genexpr>zAInvalid input text. Please provide a string, or a list of stringsr   r   r   imager-   Zimages_kwargsZpixel_values)datar   )Z_merge_kwargsr   r    Zinit_kwargs
ValueErrorr0   r   listtupleallpopr%   r$   r#   r&   replacer"   DEFAULT_SYSTEM_PROMPTappendr   r   )r)   r   r-   ZvideosZaudior*   Zoutput_kwargsr   Zprompt_stringsZone_img_tokenspromptr5   r   r   r   __call__T   s<   (
 

zJanusProcessor.__call__c                 O      | j j|i |S )z
        This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r    batch_decoder)   argsr*   r   r   r   rA         zJanusProcessor.batch_decodec                 O   r@   )z
        This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r    decoderB   r   r   r   rE      rD   zJanusProcessor.decodec                 K   s   | 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)r)   r-   r*   r   r   r   rF      s   zJanusProcessor.postprocessc                 C   s"   | j j}| jj}tt|| S r/   )r    model_input_namesr   r7   dictfromkeys)r)   Ztokenizer_input_namesZimage_processor_input_namesr   r   r   rG      s   z JanusProcessor.model_input_names)NF)NNNN)r   r   r   __doc__
attributesZvalid_kwargsZimage_processor_classZtokenizer_classr(   r   r   r   r   r   r
   r   r   r?   rA   rE   rF   propertyrG   __classcell__r   r   r+   r   r   4   s2    
Lr   N)rJ   typingr   r   Zfeature_extraction_utilsr   Zimage_utilsr   Zprocessing_utilsr   r   r	   r
   Ztokenization_utils_baser   r   utilsr   Z
get_loggerr   loggerr<   r   r   r   __all__r   r   r   r   <module>   s   
 
	