#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/hyperclovax_vision_v2/modular_hyperclovax_vision_v2.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_hyperclovax_vision_v2.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 NAVER Corp. and The HuggingFace 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.

import json
import re

from ...processing_utils import MultiModalData, ProcessingKwargs, ProcessorMixin
from ...utils import auto_docstring
from ...utils.deprecation import deprecate_attr


@auto_docstring
class HyperCLOVAXVisionV2Processor(ProcessorMixin):
    videos_kwargs = {"return_metadata": True}
    # Stopgap for `video_duration`: the hub template renders it as text, leaving the literal
    # `<|video_duration|>` placeholder when missing. These three overrides fill it in after the fact,
    # instead of the single `replace_video_token` hook other video models use (qwen3_vl, glm4v). Remove
    # once the hub template is simplified to emit one placeholder token per video.
    # See https://github.com/huggingface/transformers/pull/44314#issuecomment-5569986614
    video_duration_token = "<|video_duration|>"

    def __init__(self, image_processor=None, tokenizer=None, video_processor=None, chat_template=None, **kwargs):
        self.image_token = "<|image_pad|>" if not hasattr(tokenizer, "image_token") else tokenizer.image_token
        self.video_token = "<|video_pad|>" if not hasattr(tokenizer, "video_token") else tokenizer.video_token
        self.image_token_id = (
            tokenizer.image_token_id
            if getattr(tokenizer, "image_token_id", None)
            else tokenizer.convert_tokens_to_ids(self.image_token)
        )
        self.video_token_id = (
            tokenizer.video_token_id
            if getattr(tokenizer, "video_token_id", None)
            else tokenizer.convert_tokens_to_ids(self.video_token)
        )
        super().__init__(image_processor, tokenizer, video_processor, chat_template=chat_template)
        self._pending_video_durations = []
        self._pending_needs_placeholder = None

    def replace_image_token(self, image_inputs: dict, image_idx: int, **kwargs) -> str:
        merge_length = self.image_processor.merge_size**2
        num_image_tokens = image_inputs["image_grid_thw"][image_idx].prod() // merge_length
        return self.image_token * num_image_tokens

    def replace_video_token(self, video_inputs: dict, video_idx: int, **kwargs) -> str:
        merge_length = self.video_processor.merge_size**2
        num_video_tokens = video_inputs["video_grid_thw"][video_idx].prod() // merge_length
        return self.video_token * num_video_tokens

    def _get_num_multimodal_tokens(self, image_sizes=None, video_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.
            video_sizes (`list[list[int]]`, *optional*):
                The input sizes formatted as (num_frames, height, width) per each video.
        Returns:
            `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
            input modalities, along with other useful data.
        """

        merged_kwargs = self._merge_kwargs(**kwargs)
        vision_data = {}
        if image_sizes is not None:
            images_kwargs = merged_kwargs.get("images_kwargs", {})
            merge_size = images_kwargs.get("merge_size", None) or self.image_processor.merge_size

            num_image_patches = [
                self.image_processor.get_number_of_image_patches(*image_size, images_kwargs)
                for image_size in image_sizes
            ]
            num_image_tokens = [(num_patches // merge_size**2) for num_patches in num_image_patches]
            vision_data.update({"num_image_tokens": num_image_tokens, "num_image_patches": num_image_patches})

        if video_sizes is not None:
            videos_kwargs = merged_kwargs.get("videos_kwargs", {})
            merge_size = videos_kwargs.get("merge_size", None) or self.video_processor.merge_size
            num_video_patches = [
                self.video_processor.get_num_of_video_patches(*video_size, videos_kwargs) for video_size in video_sizes
            ]
            num_video_tokens = [(num_patches // merge_size**2) for num_patches in num_video_patches]
            vision_data["num_video_tokens"] = num_video_tokens

        return MultiModalData(**vision_data)

    def post_process_image_text_to_text(
        self, generated_outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False, **kwargs
    ):
        """
        Post-process the output of the model to decode the text.

        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.
            clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`):
                Whether or not to clean up the tokenization spaces. Argument passed to the tokenizer's `batch_decode` method.
            **kwargs:
                Additional arguments to be passed to the tokenizer's `batch_decode method`.

        Returns:
            `list[str]`: The decoded text.
        """
        return self.tokenizer.batch_decode(
            generated_outputs,
            skip_special_tokens=skip_special_tokens,
            clean_up_tokenization_spaces=clean_up_tokenization_spaces,
            **kwargs,
        )

    @property
    def model_input_names(self):
        return super().model_input_names

    def apply_chat_template(self, conversation, **kwargs):
        is_batched = isinstance(conversation[0], (list, tuple)) if conversation else False
        conversations = conversation if is_batched else [conversation]
        self._pending_needs_placeholder = [
            "video_duration" not in block
            for conv in conversations
            for message in conv
            for block in (message.get("content") or [])
            if isinstance(block, dict) and block.get("type") == "video"
        ]
        result = super().apply_chat_template(conversation, **kwargs)
        return result

    def _process_videos(self, videos, **kwargs):
        processed_videos, video_replacements = super()._process_videos(videos, **kwargs)
        all_durations = [
            round(float(m.duration if m.duration is not None else m.total_num_frames / (m.fps or 24)), 2)
            for m in processed_videos.get("video_metadata", [])
        ]
        needs = self._pending_needs_placeholder
        self._pending_video_durations = (
            [d for d, n in zip(all_durations, needs) if n]
            if needs is not None and len(needs) == len(all_durations)
            else all_durations
        )
        self._pending_needs_placeholder = None
        return processed_videos, video_replacements

    def get_text_with_replacements(
        self,
        text: list[str],
        images_replacements: list[str] = [],
        videos_replacements: list[str] = [],
        audio_replacements: list[str] = [],
    ):
        durations = iter(self._pending_video_durations)
        pattern = re.escape(self.video_duration_token)
        text = [re.sub(pattern, lambda _: json.dumps(next(durations)), sample) for sample in text]
        self._pending_video_durations = []
        result = super().get_text_with_replacements(text, images_replacements, videos_replacements, audio_replacements)
        return result


__getattr__ = deprecate_attr(__name__, rename={"HyperCLOVAXVisionV2_ProcessorKwargs": (ProcessingKwargs, "5.29.0")})

__all__ = ["HyperCLOVAXVisionV2Processor"]
