






import math
from typing import Dict, Iterable, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    is_torch_available,
    is_torch_tensor,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_torch_available():
     import torch

if is_vision_available():
     import PIL


UpperCAmelCase__							  =						logging.get_logger(__name__)
def 		_a       ( a					:np.ndarray      ,				a					:Union[int, Iterable[int]]      ,				a					:bool      ,				a					:int					)				->     Tuple[int, int]:
 def constraint_to_multiple_of(a					:Any      ,				a					:str      ,				a					:Optional[Any]=0      ,				a					:Optional[Any]=None					):
  a						    = round(val / multiple					) * multiple

  if max_val is not None and x > max_val:
   a						    = math.floor(val / multiple					) * multiple

  if x < min_val:
   a						    = math.ceil(val / multiple					) * multiple

  return x

 a						    = (output_size, output_size) if isinstance(a      ,				a					) else output_size

 a    ,	a						    = get_image_size(a					)
 a    ,	a						    = output_size

 # determine new height and width
 a						    = output_height / input_height
 a						    = output_width / input_width

 if keep_aspect_ratio:
  # scale as little as possible
  if abs(1 - scale_width					) < abs(1 - scale_height					):
   # fit width
   a						    = scale_width
  else:
   # fit height
   a						    = scale_height

 a						    = constraint_to_multiple_of(scale_height * input_height      ,				multiple=a					)
 a						    = constraint_to_multiple_of(scale_width * input_width      ,				multiple=a					)

 return (new_height, new_width)






class 						lowercase_   (      lowercase  ):






      '''simple docstring'''




      __snake_case	    =    ['''pixel_values''']







      def __init__( self :		int					,   __UpperCAmelCase :		bool = True					,   __UpperCAmelCase :		Dict[str, int] = None					,   __UpperCAmelCase :		PILImageResampling = PILImageResampling.BILINEAR					,   __UpperCAmelCase :		bool = False					,   __UpperCAmelCase :		int = 1					,   __UpperCAmelCase :		bool = True					,   __UpperCAmelCase :		Union[int, float] = 1 / 255					,   __UpperCAmelCase :		bool = True					,   __UpperCAmelCase :		Optional[Union[float, List[float]]] = None					,   __UpperCAmelCase :		Optional[Union[float, List[float]]] = None					,   **__UpperCAmelCase :		Optional[int]					,   )       ->None:




       """simple docstring"""


       super().__init__(**__UpperCAmelCase    )
       a						    = size if size is not None else {'''height''': 384, '''width''': 384}
       a						    = get_size_dict(__UpperCAmelCase    )
       a						    = do_resize
       a						    = size
       a						    = keep_aspect_ratio
       a						    = ensure_multiple_of
       a						    = resample
       a						    = do_rescale
       a						    = rescale_factor
       a						    = do_normalize
       a						    = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
       a						    = image_std if image_std is not None else IMAGENET_STANDARD_STD







      def  __lowerCAmelCase   ( self :		List[str]					,   __UpperCAmelCase :		np.ndarray					,   __UpperCAmelCase :		Dict[str, int]					,   __UpperCAmelCase :		bool = False					,   __UpperCAmelCase :		int = 1					,   __UpperCAmelCase :		PILImageResampling = PILImageResampling.BICUBIC					,   __UpperCAmelCase :		Optional[Union[str, ChannelDimension]] = None					,   **__UpperCAmelCase :		Union[str, Any]					,   )       ->np.ndarray:




       """simple docstring"""


       a						    = get_size_dict(__UpperCAmelCase    )
       if "height" not in size or "width" not in size:
        raise ValueError(F"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}"""    )
       a						    = get_resize_output_image_size(
           __UpperCAmelCase					,   output_size=(size['''height'''], size['''width'''])					,   keep_aspect_ratio=__UpperCAmelCase					,   multiple=__UpperCAmelCase					,   )
       return resize(__UpperCAmelCase					,   size=__UpperCAmelCase					,   resample=__UpperCAmelCase					,   data_format=__UpperCAmelCase					,   **__UpperCAmelCase    )







      def  __lowerCAmelCase   ( self :		Optional[Any]					,   __UpperCAmelCase :		np.ndarray					,   __UpperCAmelCase :		Union[int, float]					,   __UpperCAmelCase :		Optional[Union[str, ChannelDimension]] = None					,   **__UpperCAmelCase :		Dict					,   )       ->Dict:




       """simple docstring"""


       return rescale(__UpperCAmelCase					,   scale=__UpperCAmelCase					,   data_format=__UpperCAmelCase					,   **__UpperCAmelCase    )







      def  __lowerCAmelCase   ( self :		Optional[Any]					,   __UpperCAmelCase :		np.ndarray					,   __UpperCAmelCase :		Union[float, List[float]]					,   __UpperCAmelCase :		Union[float, List[float]]					,   __UpperCAmelCase :		Optional[Union[str, ChannelDimension]] = None					,   **__UpperCAmelCase :		Union[str, Any]					,   )       ->np.ndarray:




       """simple docstring"""


       return normalize(__UpperCAmelCase					,   mean=__UpperCAmelCase					,   std=__UpperCAmelCase					,   data_format=__UpperCAmelCase					,   **__UpperCAmelCase    )







      def  __lowerCAmelCase   ( self :		List[str]					,   __UpperCAmelCase :		ImageInput					,   __UpperCAmelCase :		bool = None					,   __UpperCAmelCase :		int = None					,   __UpperCAmelCase :		bool = None					,   __UpperCAmelCase :		int = None					,   __UpperCAmelCase :		PILImageResampling = None					,   __UpperCAmelCase :		bool = None					,   __UpperCAmelCase :		float = None					,   __UpperCAmelCase :		bool = None					,   __UpperCAmelCase :		Optional[Union[float, List[float]]] = None					,   __UpperCAmelCase :		Optional[Union[float, List[float]]] = None					,   __UpperCAmelCase :		Optional[Union[str, TensorType]] = None					,   __UpperCAmelCase :		ChannelDimension = ChannelDimension.FIRST					,   **__UpperCAmelCase :		Union[str, Any]					,   )       ->PIL.Image.Image:




       """simple docstring"""


       a						    = do_resize if do_resize is not None else self.do_resize
       a						    = size if size is not None else self.size
       a						    = get_size_dict(__UpperCAmelCase    )
       a						    = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio
       a						    = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of
       a						    = resample if resample is not None else self.resample
       a						    = do_rescale if do_rescale is not None else self.do_rescale
       a						    = rescale_factor if rescale_factor is not None else self.rescale_factor
       a						    = do_normalize if do_normalize is not None else self.do_normalize
       a						    = image_mean if image_mean is not None else self.image_mean
       a						    = image_std if image_std is not None else self.image_std

       a						    = make_list_of_images(__UpperCAmelCase    )

       if not valid_images(__UpperCAmelCase    ):
        raise ValueError(
            '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
            '''torch.Tensor, tf.Tensor or jax.ndarray.'''    )

       if do_resize and size is None or resample is None:
        raise ValueError('''Size and resample must be specified if do_resize is True.'''    )

       if do_rescale and rescale_factor is None:
        raise ValueError('''Rescale factor must be specified if do_rescale is True.'''    )

       if do_normalize and (image_mean is None or image_std is None):
        raise ValueError('''Image mean and std must be specified if do_normalize is True.'''    )

       # All transformations expect numpy arrays.
       a						    = [to_numpy_array(__UpperCAmelCase    ) for image in images]

       if do_resize:
        a						    = [self.resize(image=__UpperCAmelCase					,   size=__UpperCAmelCase					,   resample=__UpperCAmelCase    ) for image in images]

       if do_rescale:
        a						    = [self.rescale(image=__UpperCAmelCase					,   scale=__UpperCAmelCase    ) for image in images]

       if do_normalize:
        a						    = [self.normalize(image=__UpperCAmelCase					,   mean=__UpperCAmelCase					,   std=__UpperCAmelCase    ) for image in images]

       a						    = [to_channel_dimension_format(__UpperCAmelCase					,   __UpperCAmelCase    ) for image in images]

       a						    = {'''pixel_values''': images}
       return BatchFeature(data=__UpperCAmelCase					,   tensor_type=__UpperCAmelCase    )







      def  __lowerCAmelCase   ( self :		Dict					,   __UpperCAmelCase :		Any					,   __UpperCAmelCase :		List[Tuple] = None    )       ->str:




       """simple docstring"""


       a						    = outputs.logits

       # Resize logits and compute semantic segmentation maps
       if target_sizes is not None:
        if len(__UpperCAmelCase    ) != len(__UpperCAmelCase    ):
         raise ValueError(
             '''Make sure that you pass in as many target sizes as the batch dimension of the logits'''    )

        if is_torch_tensor(__UpperCAmelCase    ):
         a						    = target_sizes.numpy()

        a						    = []

        for idx in range(len(__UpperCAmelCase    )    ):
         a						    = torch.nn.functional.interpolate(
             logits[idx].unsqueeze(dim=0    )					,   size=target_sizes[idx]					,   mode='''bilinear'''					,   align_corners=__UpperCAmelCase    )
         a						    = resized_logits[0].argmax(dim=0    )
         semantic_segmentation.append(__UpperCAmelCase    )
       else:
        a						    = logits.argmax(dim=1    )
        a						    = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]    )]

       return semantic_segmentation










import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
     from .tokenization_pegasus import PegasusTokenizer
else:
     UpperCAmelCase__							  =						None


UpperCAmelCase__							  =						logging.get_logger(__name__)


UpperCAmelCase__							  =						"▁"

UpperCAmelCase__							  =						{"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}

UpperCAmelCase__							  =						{
    "vocab_file": {"google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model"},
    "tokenizer_file": {
        "google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json"
    },
}

UpperCAmelCase__							  =						{
    "google/pegasus-xsum": 512,
}






class 						lowercase_   (      lowercase  ):






      '''simple docstring'''




      __snake_case	    =    VOCAB_FILES_NAMES
      __snake_case	    =    PRETRAINED_VOCAB_FILES_MAP
      __snake_case	    =    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __snake_case	    =    PegasusTokenizer
      __snake_case	    =    ['''input_ids''', '''attention_mask''']







      def __init__( self :		List[Any]					,   __UpperCAmelCase :		str=None					,   __UpperCAmelCase :		List[Any]=None					,   __UpperCAmelCase :		Any="<pad>"					,   __UpperCAmelCase :		str="</s>"					,   __UpperCAmelCase :		List[str]="<unk>"					,   __UpperCAmelCase :		List[str]="<mask_2>"					,   __UpperCAmelCase :		Tuple="<mask_1>"					,   __UpperCAmelCase :		int=None					,   __UpperCAmelCase :		str=103					,   **__UpperCAmelCase :		Optional[int]					,   )       ->Dict:




       """simple docstring"""


       a						    = offset

       if additional_special_tokens is not None:
        if not isinstance(__UpperCAmelCase					,   __UpperCAmelCase    ):
         raise TypeError(
             F"""additional_special_tokens should be of type {type(__UpperCAmelCase    )}, but is"""
             F""" {type(__UpperCAmelCase    )}"""    )

        a						    = (
            ([mask_token_sent] + additional_special_tokens)
            if mask_token_sent not in additional_special_tokens and mask_token_sent is not None
            else additional_special_tokens
        )
        # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken
        additional_special_tokens_extended += [
            F"""<unk_{i}>""" for i in range(len(__UpperCAmelCase    )					,   self.offset - 1    )
        ]

        if len(set(__UpperCAmelCase    )    ) != len(__UpperCAmelCase    ):
         raise ValueError(
             '''Please make sure that the provided additional_special_tokens do not contain an incorrectly'''
             F""" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}."""    )
        a						    = additional_special_tokens_extended
       else:
        a						    = [mask_token_sent] if mask_token_sent is not None else []
        additional_special_tokens += [F"""<unk_{i}>""" for i in range(2					,   self.offset    )]

       super().__init__(
           __UpperCAmelCase					,   tokenizer_file=__UpperCAmelCase					,   pad_token=__UpperCAmelCase					,   eos_token=__UpperCAmelCase					,   unk_token=__UpperCAmelCase					,   mask_token=__UpperCAmelCase					,   mask_token_sent=__UpperCAmelCase					,   offset=__UpperCAmelCase					,   additional_special_tokens=__UpperCAmelCase					,   **__UpperCAmelCase					,   )
       a						    = vocab_file
       a						    = False if not self.vocab_file else True







      def  __lowerCAmelCase   ( self :		Optional[Any]					,   __UpperCAmelCase :		int    )       ->int:




       """simple docstring"""


       a						    = set(self.all_special_ids    )  # call it once instead of inside list comp
       all_special_ids.remove(self.unk_token_id    )  # <unk> is only sometimes special

       if all_special_ids != set(range(len(self.additional_special_tokens    ) + 3    )    ):
        raise ValueError(
            '''There should be 3 special tokens: mask_token, pad_token, and eos_token +'''
            F""" {len(self.additional_special_tokens    )} additional_special_tokens, but got {all_special_ids}"""    )

       return [1 if x in all_special_ids else 0 for x in seq]







      def  __lowerCAmelCase   ( self :		Dict					,   __UpperCAmelCase :		List					,   __UpperCAmelCase :		Optional[List] = None					,   __UpperCAmelCase :		bool = False    )       ->List[int]:




       """simple docstring"""


       if already_has_special_tokens:
        return self._special_token_mask(__UpperCAmelCase    )
       elif token_ids_a is None:
        return self._special_token_mask(__UpperCAmelCase    ) + [1]
       else:
        return self._special_token_mask(token_ids_a + token_ids_a    ) + [1]







      def  __lowerCAmelCase   ( self :		Union[str, Any]					,   __UpperCAmelCase :		int					,   __UpperCAmelCase :		Optional[int]=None    )       ->List[int]:




       """simple docstring"""


       if token_ids_a is None:
        return token_ids_a + [self.eos_token_id]
       # We don't expect to process pairs, but leave the pair logic for API consistency
       return token_ids_a + token_ids_a + [self.eos_token_id]







      def  __lowerCAmelCase   ( self :		Dict					,   __UpperCAmelCase :		str					,   __UpperCAmelCase :		Optional[str] = None    )       ->Tuple[str]:




       """simple docstring"""


       if not self.can_save_slow_tokenizer:
        raise ValueError(
            '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
            '''tokenizer.'''    )

       if not os.path.isdir(__UpperCAmelCase    ):
        logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""    )
        return
       a						    = os.path.join(
           __UpperCAmelCase					,   (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']    )

       if os.path.abspath(self.vocab_file    ) != os.path.abspath(__UpperCAmelCase    ):
        copyfile(self.vocab_file					,   __UpperCAmelCase    )

       return (out_vocab_file,)


