
    ^j                         d 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mZ  ej                   e      ZdZ G d d	ed
      Z G d ded
      Ze G d de             ZdgZy)z
Processor class for Janus.
   )BatchFeature)
ImageInput)ProcessingKwargsProcessorMixin
TextKwargsUnpack)PreTokenizedInput	TextInput)auto_docstring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 dZeed<   y)JanusTextKwargsas  
    generation_mode (`str`, *optional*, defaults to `"text"`):
        The generation mode indicating which modality to generate. Can be one of `"text"` or `"image"`. When set
        to `"text"`, the processor prepares inputs for text generation. When set to `"image"`, it prepares inputs
        for image generation by appending image start tokens to the prompt.
    generation_modeN)__name__
__module____qualname____doc__str__annotations__     u/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/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didZy	)
JanusProcessorKwargstext_kwargsFlefttext)paddingpadding_sider   return_tensorspt)r   common_kwargsN)r   r   r   r   r   	_defaultsr   r   r   r   r   -   s"      #(&U[\*D1Ir   r   c            
            e Zd ZeZ	 	 	 ddedz  dedef fdZe		 	 dde
ez  ee
   z  ee   z  dedz  dee   d	ef fd
       Zdeded	efdZdefdZ	 ddZ xZS )JanusProcessorNchat_templateuse_default_system_promptnum_image_tokensc                     || _         |j                  | _        |j                  | _        |j                  | _        || _        t        | !  |||       y)a  
        use_default_system_prompt (`bool`, *optional*, defaults to `False`):
            Use default system prompt for Text Generation.
        num_image_tokens (`int`, *optional*, defaults to `576`):
            The number of placeholder image tokens needed per one image.
        )r'   N)	r)   image_token	boi_tokenimage_start_token	eoi_tokenimage_end_tokenr(   super__init__)selfimage_processor	tokenizerr'   r(   r)   kwargs	__class__s          r   r1   zJanusProcessor.__init__9   sS     !1$00!*!4!4(22)B&)=Qr   r   imagesr5   returnc                 X    | j                   t        fd| j                  j                  i|}|d   j	                  d      }| j
                  r|dk(  r|D cg c]  }t         |  }}n"|dk(  r|D cg c]  }| | j                    }}t        | $  d||d|}|S c c}w c c}w )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`.
        tokenizer_init_kwargsr   r   r   image)r7   r   r   )
_merge_kwargsr   r4   init_kwargspopr(   DEFAULT_SYSTEM_PROMPTr-   r0   __call__)	r2   r   r7   r5   output_kwargsr   samplemodel_inputsr6   s	           r   r@   zJanusProcessor.__call__P   s    $ +** 
8<8R8R
V\
 (6::;LM))o.GEIJ6,-fX6JDJ'FJKFvht5567KDKw'RvDRMR KKs   B"5B'image_inputs	image_idxc                 l    | j                   | j                  | j                  z  z   | j                  z   }|S )N)r-   r+   r)   r/   )r2   rD   rE   one_img_tokenss       r   replace_image_tokenz"JanusProcessor.replace_image_tokeno   s5    //43C3CdF[F[3[\_c_s_ss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.
        )r3   postprocess)r2   r7   r5   s      r   rJ   zJanusProcessor.postprocesss   s"    
 0t##//A&AAr   c                     ||dk(  r | j                   |fd|i|S |dk(  r1t        |j                               }| j                  |d      }|d   S t	        | j
                  j                   d| d      )	a  
        Post-process the output of a multimodal model to return the requested modality output.
        If the model cannot generated the requested modality, an error will be raised.

        Args:
            generated_outputs (`torch.Tensor` or `np.ndarray`):
                The output of the model `generate` function. The output is expected to be a tensor of shape `(batch_size, sequence_length)`
                or `(sequence_length,)`.
            skip_special_tokens (`bool`, *optional*, defaults to `True`):
                Whether or not to remove special tokens in the output. Argument passed to the tokenizer's `batch_decode` method.
            generation_mode (`str`, *optional*):
                Generation mode indicated which modality to output and can be one of `["text", "image", "audio"]`.
            **kwargs:
                Additional arguments to be passed to the tokenizer's `batch_decode method`.

        Returns:
            `list[Union[str, PIL.Image.Image]]`: The decoded text or generated image.
        r   skip_special_tokensr;   zPIL.Image.Image)r!   pixel_valuesz# got an unexpected generation_mode=z.. Supported options are only `text` and `image)post_process_image_text_to_textlistfloatrJ   
ValueErrorr6   r   )r2   generated_outputsrL   r   r5   r7   s         r   post_process_multimodal_outputz-JanusProcessor.post_process_multimodal_outputz   s    * "o&?7477!7JNT  ' $%6%<%<%> ?%%&7HY%ZF.)) >>**++NN_  `N  O r   )NFi@  )NN)TN)r   r   r   r   valid_processor_kwargsr   boolintr1   r   r
   r	   rO   r   r   r   r@   dictrH   rJ   rS   __classcell__)r6   s   @r   r&   r&   5   s    1 %)*/ #R Tz	R
 $(R R.  [_$(++d9o=EV@WW T! -.	
 
 <   B* B LP"r   r&   N)r   feature_extraction_utilsr   image_utilsr   processing_utilsr   r   r   r   tokenization_utils_baser	   r
   utilsr   r   
get_loggerr   loggerr?   r   r   r&   __all__r   r   r   <module>ra      s    5 % T T C , 
		H	%N j +5  f^ f fR 
r   