Ë
    T^(hˆ!  ã                   óÜ   — U d dl mZmZ d dlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZmZ  ej                   e«      Zd
dddœZeeef   ed<    G d„ ded¬«      Z G d„ de«      ZdgZy)é    )ÚMappingÚSequence)ÚOptionalé   )ÚBatchFeature)Ú
ImageInput)ÚUnpack)Úloggingé   )ÚGemma3ProcessorÚGemma3ProcessorKwargszçNo Dangerous Content: The image shall not contain content that facilitates or encourages activities that could cause real-world harm (e.g., building firearms and explosive devices, promotion of terrorism, instructions for suicide).z¸No Sexually Explicit content: The image shall not contain content that depicts explicit or graphic sexual acts (e.g., pornography, erotic nudity, depictions of rape or sexual assault).zâNo Violence/Gore content: The image shall not contain content that depicts shocking, sensational, or gratuitous violence (e.g., excessive blood and gore, gratuitous violence against animals, extreme injury or moment of death).)Ú	dangerousÚsexualÚviolenceÚDEFAULT_SHIELDGEMMA2_POLICIESc                   óP   — e Zd ZU eee      ed<   eeeef      ed<   ddiddidœZy)	ÚShieldGemma2ProcessorKwargsÚpoliciesÚcustom_policiesÚpaddingTÚdo_pan_and_scanF)Útext_kwargsÚimages_kwargsN)	Ú__name__Ú
__module__Ú__qualname__r   r   ÚstrÚ__annotations__r   Ú	_defaults© ó    úv/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/shieldgemma2/processing_shieldgemma2.pyr   r   1   sD   … Ø�x ‘}Ñ%Ó%Ø˜g c¨3 hÑ/Ñ0Ó0ð �tð
ð ˜uð
ñ	�Ir!   r   F)Útotalc                   óf   ‡ — e Zd Z	 d	ˆ fd„	Z	 	 	 	 d
dedee   defˆ fd„Zd„ Z	d„ Z
ed„ «       Zˆ xZS )ÚShieldGemma2Processorc                 óV   •— t        ‰| �  ||||fi |¤Ž |€t        | _        y|| _        y)a  A processor for the ShieldGemma 2 model.

        Args:
            image_processor: The image processor to use, typically a `Gemma3ImageProcessorFast` instance.
            tokenizer: The tokenizer to use, typically a `GemmaTokenizerFast` instance.
            chat_template: The chat template to use with this processor. Typically, this is unset as the processor
                configuration on Hugging Face Hub includes this value already.
            image_seq_length: The number of soft tokens per image. Typically, this is unset as the processor
                configuration on Hugging Face Hub includes this value already.
            policy_definitions: A mapping from policy name to its description in text used as the default policies to
                classify images against. The policy descriptions are included in the text of the prompts generated by
                this processor. Typically, this is unset as the processor configuration on Hugging Face Hub includes
                the base policies ShieldGemma was trained on.
        N)ÚsuperÚ__init__r   Úpolicy_definitions)ÚselfÚimage_processorÚ	tokenizerÚchat_templateÚimage_seq_lengthr)   ÚkwargsÚ	__class__s          €r"   r(   zShieldGemma2Processor.__init__?   s5   ø€ ô" 	‰Ñ˜¨)°]ÐDTÑ_ÐX^Ò_ØÐ%Ü&CˆDÕ#à&8ˆDÕ#r!   Úimagesr/   Úreturnc           	      óÞ  •— ~~~|st        d«      ‚t        |t        «      s|g}| j                  st        d«      ‚|j	                  di «      }|j                  d«      du rt        j                  d«       d|d<   |j	                  di «      }d	|vr*|j                  d	d«      |d	<   |j                  d
d«      |d
<   i | j                  ¥|j                  di «      ¥}|j                  d«      x}	€t        |j                  «       «      }	g }
g }|D ]:  }|	D ]3  }|
j                  dddid||   dœgdœg«       |j                  |g«       Œ5 Œ< | j                  |
d¬«      }t        ‰| �<  d||dœ|¤ŽS )aÁ  Generates a batch of inputs from the provided images.

        ShieldGemma was trained to classify image content for policy compliance using a specific prompt construction.
        This processor generates a batch of such prompts from the provided images by:

        1.  Creating a list of conversations, one for each `<image, policy>` pair;
        2.  Converting these conversations to text using `self.apply_chat_template()`; and
        3.  Encoding the conversations and images using the same techniques as `Gemma3Processor`.

        Args:
            images: A single image or a list of images to include in the batch.
            text: Not supported.
            videos: Not supported.
            audio: Not supported.
            kwargs: An optional dictionary of keyword arguments to configre the
                processor. Possible values include:

                *   `custom_policies`: Additional policy definitions that augment the `self.policy_definitions` passed
                    into the constructor. Note that `custom_policies` that share a key with `self.policy_definitions`
                    will override the policy description
                *   `policies`: (Optional) a list of keys in the joint `self.policy_definitions | custom_policies`
                    dictionary of specific interest for the provided images. If empty or None, prompts will be
                    generated for every key in the joint dictionary.

        Returns:
            A `BatchFeature` continaing `input_ids`, `pixel_values`, etc. where each Tensor is of shape
            `(len(images) * len(policies), )`, and the order within the batch will be
            img1_policy1, ... img1_policyN, ... imgM_policyN.
        z&ShieldGemma 2 needs images to classifyz:ShieldGemma 2 requires the use of a specific chat templater   r   Tz+ShieldGemma2 does not support pan and scan.Fr   r   Úpadding_sideÚleftr   r   ÚuserÚtypeÚimageÚtext)r7   r9   )ÚroleÚcontent)Útokenize)r1   r9   r    )Ú
ValueErrorÚ
isinstancer   r-   Ú
setdefaultÚgetÚloggerÚwarning_onceÚpopr)   ÚlistÚkeysÚappendÚapply_chat_templater'   Ú__call__)r*   r1   r9   ÚvideosÚaudior/   r   r   r)   r   ÚmessagesÚexpanded_imagesÚimgÚpolicyr0   s                 €r"   rH   zShieldGemma2Processor.__call__V   sÁ  ø€ ðJ �&˜%áÜÐEÓFÐFÜ˜F¤HÔ-Ø�XˆFà×!Ò!ÜÐYÓZÐZð ×)Ñ)¨/¸2Ó>ˆØ×ÑÐ.Ó/°4Ñ7Ü×ÑÐ MÔNØ/4ˆMÐ+Ñ,ð ×'Ñ'¨°rÓ:ˆØ˜KÑ'Ø%+§Z¡Z°	¸4Ó%@ˆK˜	Ñ"Ø*0¯*©*°^ÀVÓ*LˆK˜Ñ'ð1
Ø×%Ñ%ð1
à�j‰jÐ*¨BÓ/ð1
Ðð
 Ÿ
™
 :Ó.Ð.ˆHÐ7ÜÐ.×3Ñ3Ó5Ó6ˆHð ˆØˆØò 	.ˆCØ"ò .�Ø—‘ð %+à!'¨Ð 1Ø)/Ð9KÈFÑ9SÑ Tð(ñðô
ð  ×&Ñ&¨ uÕ-ñ.ð	.ð ×'Ñ'¨¸5Ð'ÓAˆÜ‰wÑÐL ¸TÑLÀVÑLÐLr!   c                 ó:   —  | j                   j                  |i |¤ŽS )zÂ
        This method forwards all its arguments to GemmaTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r,   Úbatch_decode©r*   Úargsr/   s      r"   rP   z"ShieldGemma2Processor.batch_decode®   s    € ð
 +ˆt�~‰~×*Ñ*¨DÐ;°FÑ;Ð;r!   c                 ó:   —  | j                   j                  |i |¤ŽS )z¼
        This method forwards all its arguments to GemmaTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r,   ÚdecoderQ   s      r"   rT   zShieldGemma2Processor.decodeµ   s    € ð
 %ˆt�~‰~×$Ñ$ dÐ5¨fÑ5Ð5r!   c                 ó¤   — | j                   j                  dgz   }| j                  j                  }t        t        j                  ||z   «      «      S )NÚtoken_type_ids)r,   Úmodel_input_namesr+   rD   ÚdictÚfromkeys)r*   Útokenizer_input_namesÚimage_processor_input_namess      r"   rW   z'ShieldGemma2Processor.model_input_names¼   sH   € à $§¡× @Ñ @ÐDTÐCUÑ UÐØ&*×&:Ñ&:×&LÑ&LÐ#Ü”D—M‘MÐ"7Ð:UÑ"UÓVÓWÐWr!   )Né   N)NNNN)r   r   r   r(   r   r	   r   r   rH   rP   rT   ÚpropertyrW   Ú__classcell__)r0   s   @r"   r%   r%   >   si   ø„ àgkõ9ð2 "ØØØñVMàðVMð Ð4Ñ5ðVMð 
õVMòp<ò6ð ñXó ôXr!   r%   N)Úcollections.abcr   r   Útypingr   Úfeature_extraction_utilsr   Úimage_utilsr   Úprocessing_utilsr	   Úutilsr
   Úgemma3.processing_gemma3r   r   Ú
get_loggerr   rA   r   r   r   r   r%   Ú__all__r    r!   r"   ú<module>rh      s„   ð÷  .Ð -Ý å 4Ý %Ý &Ý ß Mð 
ˆ×	Ñ	˜HÓ	%€ð	1ð	:ð
	/ñ4Ð ˜w s¨C xÑ0ó ô*
Ð"7¸uõ 
ôBX˜Oô BXðJ #Ð
#�r!   