#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/pp_ocrv5_server_rec/modular_pp_ocrv5_server_rec.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_pp_ocrv5_server_rec.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 The PaddlePaddle Team and The 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.

import math

import torch
import torchvision.transforms.v2.functional as tvF

from ...feature_extraction_utils import BatchFeature
from ...image_processing_backends import TorchvisionBackend
from ...image_transforms import group_images_by_shape, reorder_images
from ...image_utils import PILImageResampling, SizeDict
from ...processing_utils import ImagesKwargs
from ...utils import auto_docstring, requires_backends
from ...utils.constants import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD
from ...utils.generic import TensorType


class PPOCRV5ServerRecImageProcessorKwargs(ImagesKwargs, total=False):
    r"""
    max_image_width (`int`, *optional*, defaults to `3200`):
        Maximum image width used during resizing.
    character_list (`list`, *optional*, defaults to `[]`):
        Vocabulary list used for text recognition.
    """

    max_image_width: int
    character_list: str


@auto_docstring
class PPOCRV5ServerRecImageProcessor(TorchvisionBackend):
    resample = PILImageResampling.BILINEAR
    image_mean = IMAGENET_STANDARD_MEAN
    image_std = IMAGENET_STANDARD_STD
    size = {"height": 48, "width": 320}
    pad_size = {"height": 48, "width": 320}
    do_resize = True
    do_rescale = True
    do_convert_rgb = True
    do_normalize = True
    do_pad = True
    max_image_width = 3200
    character_list = []
    valid_kwargs = PPOCRV5ServerRecImageProcessorKwargs

    def _preprocess(
        self,
        images: list["torch.Tensor"],
        do_resize: bool,
        size: SizeDict,
        resample: "PILImageResampling | tvF.InterpolationMode | int | None",
        do_center_crop: bool,
        crop_size: SizeDict,
        do_rescale: bool,
        rescale_factor: float,
        do_normalize: bool,
        image_mean: float | list[float] | None,
        image_std: float | list[float] | None,
        do_pad: bool | None,
        pad_size: SizeDict | None,
        disable_grouping: bool | None,
        return_tensors: str | TensorType | None,
        **kwargs,
    ) -> BatchFeature:
        # Group images by size for batched resizing
        grouped_images, grouped_images_index = group_images_by_shape(images, disable_grouping=disable_grouping)
        resized_images_grouped = {}

        # [Key Change] Use get_target_size to calculate target_size for resizing.
        shape_list = list(grouped_images.keys())
        target_size = self.get_target_size(shape_list)

        for shape, stacked_images in grouped_images.items():
            if do_resize:
                stacked_images = self.resize(image=stacked_images.float(), size=target_size, resample=resample)
            resized_images_grouped[shape] = stacked_images
        resized_images = reorder_images(resized_images_grouped, grouped_images_index)

        # Group images by size for further processing
        # Needed in case do_resize is False, or resize returns images with different sizes
        grouped_images, grouped_images_index = group_images_by_shape(resized_images, disable_grouping=disable_grouping)
        processed_images_grouped = {}
        for shape, stacked_images in grouped_images.items():
            if do_center_crop:
                stacked_images = self.center_crop(stacked_images, crop_size)
            # Fused rescale and normalize
            stacked_images = self.rescale_and_normalize(
                stacked_images, do_rescale, rescale_factor, do_normalize, image_mean, image_std
            )
            processed_images_grouped[shape] = stacked_images
        processed_images = reorder_images(processed_images_grouped, grouped_images_index)

        if do_pad and target_size.width < pad_size.width:
            processed_images = self.pad(processed_images, pad_size=pad_size, disable_grouping=disable_grouping)

        return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors)

    def get_target_size(self, shape_list: list[torch.Size]):
        """
        Calculate the width and height from the widest image in the batch.
        """
        max_width = -1
        max_height = -1
        for height, width in shape_list:
            # We need the width and height of the widest image in the batch
            if width > max_width:
                max_width = width
                max_height = height

        default_height, default_width = self.size["height"], self.size["width"]
        ratio = max(max_width / max_height, default_width / default_height)

        target_width = int(default_height * ratio)
        target_height = default_height

        if target_width > self.max_image_width:
            target_width = self.max_image_width
        else:
            ratio = max_width / float(max_height)
            if target_width >= math.ceil(default_height * ratio):
                target_width = int(math.ceil(default_height * ratio))

        return SizeDict(height=target_height, width=target_width)

    def post_process_text_recognition(
        self,
        predictions,
    ) -> list[dict[str, str | float]]:
        """
        Post-processes raw model logits to decode the recognized text and its confidence score.

        Args:
            predictions: Model outputs with `logits` attribute (probability maps of shape `(batch_size, height, vocab_size)`).

        Returns:
            A list of dictionaries, where each dictionary corresponds to an image in the batch.
            Each dictionary contains:
                - "text" (str): The decoded text string.
                - "score" (float): The average confidence score of the characters in the decoded text.
        """
        requires_backends(self, ["torch"])

        logits = predictions.last_hidden_state
        batch_size = logits.shape[0]

        preds_prob, preds_idx = logits.max(dim=-1)
        results = []
        for idx in range(batch_size):
            selection = torch.ones(len(preds_idx[idx]), dtype=torch.bool, device=preds_idx.device)

            # remove_duplicate
            selection[1:] = preds_idx[idx][1:] != preds_idx[idx][:-1]
            # ignore blank token
            selection &= preds_idx[idx] != 0

            character_list = []
            for text_id in preds_idx[idx][selection]:
                character_list.append(self.character_list[text_id])

            results.append(
                {
                    "text": "".join(character_list),
                    "score": preds_prob[idx][selection].mean().item(),
                }
            )

        return results


__all__ = ["PPOCRV5ServerRecImageProcessor"]
