
    Pg!                         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Gemma3ProcessorKwargszNo 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).zNo 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).zNo 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     /var/www/html/suriana-translation/venv/lib/python3.12/site-packages/transformers/models/shieldgemma2/processing_shieldgemma2.pyr   r   1   sD    x}%%gc3h/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^_%&CD#&8D#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FH-XF!!YZZ ))/2>./47 MN/4M+, ''r:K'%+ZZ	4%@K	"*0**^V*LK'1
%%1
jj*B/1

 

:..H7.3356H  	.C" . %+!' 1)/9KF9S T(
  &&u-.	. ''5'AwLTLV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5f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     $ @ @DTCU U&*&:&:&L&L#DMM"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92 "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 wsCx0 *
"7u 
BXO BXJ #
#r!   