Ë
    ÛÓ^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Úloggingz¼You 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<¿¹×8RÑ8Rð
ØV\ñ
ˆð (¨Ñ6×:Ñ:Ð;LÓMˆØ×)Ò)¨oÀÒ.GØEIÖJ¸6Ô,Ð-¨f¨XÒ6ÐJˆDÑJØ Ò'ØFJÖK¸F�v�h˜t×5Ñ5Ð6Ò7ÐKˆDÐKä‘wÑ'ÐR¨v¸DÑRÀMÑ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   € Ø×/Ñ/°4×3CÑ3CÀd×F[Ñ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"   € ð
 0ˆt×#Ñ#×/Ñ/°Ñ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¸Ò&?Ø7�4×7Ñ7Ø!ñØ7JðØNTñð ð  Ò'Ü $Ð%6×%<Ñ%<Ó%>Ó ?ÐØ×%Ñ%Ð&7ÐHYÐ%ÓZˆFØ˜.Ñ)Ð)ô Ø—>‘>×*Ñ*Ð+Ð+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ð ˜T‘zð	Rð
 $(ðRð õRð. ð [_Ø$(ñàÐ+Ñ+¨d°9©oÑ=ÀÐ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   