#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/shieldgemma2/modular_shieldgemma2.py.
#               Do NOT edit this file manually as any edits will be overwritten by the generation of
#             the file from the modular. If any change should be done, please apply the change to the
#                          modular_shieldgemma2.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2025 Google Inc. HuggingFace Inc. team. All rights reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections.abc import Mapping, Sequence

from ...feature_extraction_utils import BatchFeature
from ...image_utils import ImageInput, make_nested_list_of_images
from ...processing_utils import MultiModalData, ProcessingKwargs, ProcessorMixin, Unpack
from ...tokenization_utils_base import PreTokenizedInput, TextInput
from ...utils import auto_docstring, logging


logger = logging.get_logger(__name__)


class ShieldGemma2ProcessorKwargs(ProcessingKwargs, total=False):
    r"""
    policies (`list[str]`, *optional*):
        Keys in the joint `self.policy_definitions | custom_policies` dictionary of specific interest
        for the provided images. If `None`, prompts will be generated for every key in the joint dictionary.
    custom_policies (`dict[str, str]`, *optional*):
        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: list[str] | None
    custom_policies: dict[str, str] | None


DEFAULT_SHIELDGEMMA2_POLICIES = {
    "dangerous": (
        "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)."
    ),
    "sexual": (
        "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)."
    ),
    "violence": (
        "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)."
    ),
}


@auto_docstring
class ShieldGemma2Processor(ProcessorMixin):
    images_kwargs = {
        # Value is "null" in preprocessor_config.json
        "do_convert_rgb": True,
        "do_pan_and_scan": False,
    }
    return_mm_token_type_ids = True
    valid_processor_kwargs = ShieldGemma2ProcessorKwargs

    text_kwargs = {
        "padding": True,
        "padding_side": "left",
    }

    def __init__(
        self, image_processor, tokenizer, chat_template=None, image_seq_length=256, policy_definitions=None, **kwargs
    ):
        r"""
        policy_definitions (`Mapping[str, str]`, *optional*):
            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.
        """
        self.image_seq_length = image_seq_length
        self.image_token_id = tokenizer.boi_token_id
        self.boi_token = tokenizer.boi_token
        self.image_token = tokenizer.boi_token
        image_tokens_expanded = "".join([tokenizer.image_token] * image_seq_length)
        self.full_image_sequence = f"\n\n{tokenizer.boi_token}{image_tokens_expanded}{tokenizer.eoi_token}\n\n"

        super().__init__(
            image_processor=image_processor,
            tokenizer=tokenizer,
            chat_template=chat_template,
            **kwargs,
        )
        self.policy_definitions = DEFAULT_SHIELDGEMMA2_POLICIES if policy_definitions is None else policy_definitions

    @auto_docstring(
        custom_intro="""
        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`.
        """
    )
    def __call__(
        self,
        images: ImageInput | None = None,
        text=None,
        **kwargs: Unpack[ShieldGemma2ProcessorKwargs],
    ) -> BatchFeature:
        r"""
        Returns:
            A `BatchFeature` containing `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.
        """
        if not images:
            raise ValueError("ShieldGemma 2 needs images to classify")
        elif not isinstance(images, Sequence):
            images = [images]

        if not self.chat_template:
            raise ValueError("ShieldGemma 2 requires the use of a specific chat template")

        common_kwargs = kwargs.setdefault("common_kwargs", {})
        if "return_tensors" in kwargs:
            common_kwargs["return_tensors"] = kwargs.pop("return_tensors")

        # Disable pan and scan
        images_kwargs = kwargs.setdefault("images_kwargs", {})
        if images_kwargs.get("do_pan_and_scan") is True:
            logger.warning_once("ShieldGemma2 does not support pan and scan.")
            images_kwargs["do_pan_and_scan"] = False

        # Enable padding on the batch during tokenization
        # This happens before super().__call__ for BC.
        text_kwargs = kwargs.setdefault("text_kwargs", {})
        if "padding" not in text_kwargs:
            text_kwargs["padding"] = kwargs.pop("padding", self.text_kwargs["padding"])
            text_kwargs["padding_side"] = kwargs.pop("padding_side", self.text_kwargs["padding_side"])

        # Kwargs validation only accepts lists and dicts, convert for BC
        if kwargs.get("policies") is not None:
            kwargs["policies"] = list(kwargs["policies"])
        if kwargs.get("custom_policies") is not None:
            kwargs["custom_policies"] = dict(kwargs["custom_policies"])

        policy_definitions: Mapping[str, str] = {
            **self.policy_definitions,
            **kwargs.get("custom_policies", {}),
        }

        if (policies := kwargs.get("policies")) is None:
            policies = list(policy_definitions.keys())

        # TODO(ryanmullins): Support images from PIL or URLs.
        messages = []
        expanded_images = []
        for img in images:
            if not isinstance(img, list):
                img = [img]
            elif len(img) > 1:
                raise ValueError(f"ShieldGemma can process at most one image per sample, but got {len(img)} images")

            for policy in policies:
                if img:
                    messages.append(
                        [
                            {
                                "role": "user",
                                "content": [
                                    {"type": "image"},
                                    {"type": "text", "text": policy_definitions[policy]},
                                ],
                            }
                        ]
                    )
                else:
                    messages.append(
                        [
                            {
                                "role": "user",
                                "content": [
                                    {"type": "text", "text": policy_definitions[policy]},
                                ],
                            }
                        ]
                    )
                expanded_images.append(img)
        images = expanded_images

        text = self.apply_chat_template(messages, tokenize=False)
        if text is not None and not isinstance(text, str):
            if not isinstance(text, list) or not isinstance(text[0], str):
                raise TypeError("Invalid input text. Please provide a string, or a list of strings")

        model_inputs = super().__call__(images=images, text=text, **kwargs)
        if "mm_token_type_ids" in model_inputs:
            model_inputs["token_type_ids"] = model_inputs.pop("mm_token_type_ids")
        return model_inputs

    def prepare_inputs_layout(self, images=None, text=None, **kwargs):
        images, text, *_ = super().prepare_inputs_layout(images=images, text=text, **kwargs)
        if images is not None:
            images = make_nested_list_of_images(images)
            # Create empty text to be replaced with placeholders
            if not text:
                text = [" ".join([self.boi_token] * len(image_list)) for image_list in images]
        return images, text, None, None

    def validate_inputs(
        self,
        images: ImageInput | None = None,
        text: TextInput | PreTokenizedInput | list[TextInput] | list[PreTokenizedInput] | None = None,
        **kwargs: Unpack[ProcessingKwargs],
    ):
        super().validate_inputs(images=images, text=text, **kwargs)

        if text is not None and images is not None:
            n_images_in_text = [sample.count(self.image_token) for sample in text]
            n_images_in_images = [len(sublist) for sublist in images]
            if n_images_in_text != n_images_in_images:
                raise ValueError(
                    f"The number of {self.image_token} tokens in each prompt should be the same as the number of images passed."
                    f" Found {n_images_in_text} {self.image_token} tokens and {n_images_in_images} images per sample."
                )

    def _check_special_mm_tokens(self, text: list[str], text_inputs: "BatchFeature", modalities: list[str]):
        """
        Checks that number of special tokens in text and processed text is same. The count can be different
        if tokenized text was truncated, leading to issues in model code.

        ShieldGemma2 uses a different token as placeholder in input text than in the expanded text.
        """
        token_str = self.tokenizer.image_token
        token_id = self.tokenizer.image_token_id
        if token_str is not None and token_id is not None:
            ids_count = [list(ids).count(token_id) for ids in text_inputs["input_ids"]]
            text_count = [sample.count(token_str) for sample in text]

        if ids_count != text_count:
            raise ValueError(
                f"Mismatch in `image` token count between text and `input_ids`. Got ids={ids_count} and text={text_count}. "
                "Likely due to `truncation='max_length'`. Please disable truncation or increase `max_length`."
            )

    def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs):
        """
        Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.

        Args:
            image_sizes (`list[list[int]]`, *optional*):
                The input sizes formatted as (height, width) per each image.

        Returns:
            `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
            input modalities, along with other useful data.
        """

        vision_data = {}
        if image_sizes is not None:
            # NOTE: no image cropping supported yet
            num_image_tokens = [self.image_seq_length] * len(image_sizes)
            num_image_patches = [1] * len(image_sizes)

            vision_data.update({"num_image_tokens": num_image_tokens, "num_image_patches": num_image_patches})

        return MultiModalData(**vision_data)

    @property
    def image_token_ids(self) -> list[int]:
        return [self.tokenizer.image_token_id]

    @property
    def model_input_names(self) -> list[str]:
        return super().model_input_names + ["token_type_ids"]

    @property
    def unused_input_names(self) -> list[str]:
        return ["num_crops"]

    def replace_image_token(self, image_inputs: dict, image_idx: int, **kwargs) -> str:
        num_crops = image_inputs["num_crops"][image_idx]
        if not num_crops:
            return self.full_image_sequence

        # Insert additional image tokens for Pan-and-Scan crops
        return (
            f"Here is the original image {self.full_image_sequence} and here are some crops to help you see better "
            + " ".join([self.full_image_sequence] * num_crops)
        )


__all__ = ["ShieldGemma2Processor"]
