




'''simple docstring'''





from typing import Dict, List, Optional, Union

import numpy as np

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


UpperCamelCase_		:    List[str]									=		logging.get_logger(__name__)


if is_vision_available():
   import PIL

class      _a       (		__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:					Dict					       =		["""pixel_values"""]






     def __init__(				self      ,_SCREAMING_SNAKE_CASE = True      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = PILImageResampling.BILINEAR      ,_SCREAMING_SNAKE_CASE = True      ,_SCREAMING_SNAKE_CASE = False      ,_SCREAMING_SNAKE_CASE = True      ,_SCREAMING_SNAKE_CASE = True      ,_SCREAMING_SNAKE_CASE = 1 / 255      ,_SCREAMING_SNAKE_CASE = True      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,**_SCREAMING_SNAKE_CASE      ,)							->    None:
          super().__init__(**_SCREAMING_SNAKE_CASE					)

          _snake_case								=   size if size is not None else {"height": 2_560, "width": 1_920}
          if isinstance(_SCREAMING_SNAKE_CASE      ,(tuple, list)					):
               # The previous feature extractor size parameter was in (width, height) format
               _snake_case								=   size[::-1]
          _snake_case								=   get_size_dict(_SCREAMING_SNAKE_CASE					)

          _snake_case								=   do_resize
          _snake_case								=   size
          _snake_case								=   resample
          _snake_case								=   do_thumbnail
          _snake_case								=   do_align_long_axis
          _snake_case								=   do_pad
          _snake_case								=   do_rescale
          _snake_case								=   rescale_factor
          _snake_case								=   do_normalize
          _snake_case								=   image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
          _snake_case								=   image_std if image_std is not None else IMAGENET_STANDARD_STD






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None					)							->    np.ndarray:
          _snake_case							,      _snake_case								=   get_image_size(_SCREAMING_SNAKE_CASE					)
          _snake_case							,      _snake_case								=   size["height"], size["width"]

          if (output_width < output_height and input_width > input_height) or (
              output_width > output_height and input_width < input_height
          ):
               _snake_case								=   np.rotaa(_SCREAMING_SNAKE_CASE      ,3					)

          if data_format is not None:
               _snake_case								=   to_channel_dimension_format(_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					)

          return image






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = False      ,_SCREAMING_SNAKE_CASE = None      ,)							->    np.ndarray:
          _snake_case							,      _snake_case								=   size["height"], size["width"]
          _snake_case							,      _snake_case								=   get_image_size(_SCREAMING_SNAKE_CASE					)

          _snake_case								=   output_width - input_width
          _snake_case								=   output_height - input_height

          if random_padding:
               _snake_case								=   np.random.randint(low=0      ,high=delta_height + 1					)
               _snake_case								=   np.random.randint(low=0      ,high=delta_width + 1					)
          else:
               _snake_case								=   delta_height // 2
               _snake_case								=   delta_width // 2

          _snake_case								=   delta_height - pad_top
          _snake_case								=   delta_width - pad_left

          _snake_case								=   ((pad_top, pad_bottom), (pad_left, pad_right))
          return pad(_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,data_format=_SCREAMING_SNAKE_CASE					)






     def        _lowercase			(				self      ,*_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					)							->    str:
          logger.info("pad is deprecated and will be removed in version 4.27. Please use pad_image instead."					)
          return self.pad_image(*_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					)






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = PILImageResampling.BICUBIC      ,_SCREAMING_SNAKE_CASE = None      ,**_SCREAMING_SNAKE_CASE      ,)							->    np.ndarray:
          _snake_case							,      _snake_case								=   get_image_size(_SCREAMING_SNAKE_CASE					)
          _snake_case							,      _snake_case								=   size["height"], size["width"]

          # We always resize to the smallest of either the input or output size.
          _snake_case								=   min(_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					)
          _snake_case								=   min(_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					)

          if height == input_height and width == input_width:
               return image

          if input_height > input_width:
               _snake_case								=   int(input_width * height / input_height					)
          elif input_width > input_height:
               _snake_case								=   int(input_height * width / input_width					)

          return resize(
              _SCREAMING_SNAKE_CASE      ,size=(height, width)      ,resample=_SCREAMING_SNAKE_CASE      ,reducing_gap=2.0      ,data_format=_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					)






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = PILImageResampling.BICUBIC      ,_SCREAMING_SNAKE_CASE = None      ,**_SCREAMING_SNAKE_CASE      ,)							->    np.ndarray:
          _snake_case								=   get_size_dict(_SCREAMING_SNAKE_CASE					)
          _snake_case								=   min(size["height"]      ,size["width"]					)
          _snake_case								=   get_resize_output_image_size(_SCREAMING_SNAKE_CASE      ,size=_SCREAMING_SNAKE_CASE      ,default_to_square=_SCREAMING_SNAKE_CASE					)
          _snake_case								=   resize(_SCREAMING_SNAKE_CASE      ,size=_SCREAMING_SNAKE_CASE      ,resample=_SCREAMING_SNAKE_CASE      ,data_format=_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					)
          return resized_image






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None      ,**_SCREAMING_SNAKE_CASE      ,)							->    Tuple:
          return rescale(_SCREAMING_SNAKE_CASE      ,scale=_SCREAMING_SNAKE_CASE      ,data_format=_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					)






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None      ,**_SCREAMING_SNAKE_CASE      ,)							->    np.ndarray:
          return normalize(_SCREAMING_SNAKE_CASE      ,mean=_SCREAMING_SNAKE_CASE      ,std=_SCREAMING_SNAKE_CASE      ,data_format=_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					)






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = False      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = None      ,_SCREAMING_SNAKE_CASE = ChannelDimension.FIRST      ,**_SCREAMING_SNAKE_CASE      ,)							->    PIL.Image.Image:
          _snake_case								=   do_resize if do_resize is not None else self.do_resize
          _snake_case								=   size if size is not None else self.size
          if isinstance(_SCREAMING_SNAKE_CASE      ,(tuple, list)					):
               # Previous feature extractor had size in (width, height) format
               _snake_case								=   size[::-1]
          _snake_case								=   get_size_dict(_SCREAMING_SNAKE_CASE					)
          _snake_case								=   resample if resample is not None else self.resample
          _snake_case								=   do_thumbnail if do_thumbnail is not None else self.do_thumbnail
          _snake_case								=   do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
          _snake_case								=   do_pad if do_pad is not None else self.do_pad
          _snake_case								=   do_rescale if do_rescale is not None else self.do_rescale
          _snake_case								=   rescale_factor if rescale_factor is not None else self.rescale_factor
          _snake_case								=   do_normalize if do_normalize is not None else self.do_normalize
          _snake_case								=   image_mean if image_mean is not None else self.image_mean
          _snake_case								=   image_std if image_std is not None else self.image_std

          _snake_case								=   make_list_of_images(_SCREAMING_SNAKE_CASE					)

          if not valid_images(_SCREAMING_SNAKE_CASE					):
               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:
               raise ValueError("Size 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_pad and size is None:
               raise ValueError("Size must be specified if do_pad 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.
          _snake_case								=   [to_numpy_array(_SCREAMING_SNAKE_CASE					) for image in images]

          if do_align_long_axis:
               _snake_case								=   [self.align_long_axis(_SCREAMING_SNAKE_CASE      ,size=_SCREAMING_SNAKE_CASE					) for image in images]

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

          if do_thumbnail:
               _snake_case								=   [self.thumbnail(image=_SCREAMING_SNAKE_CASE      ,size=_SCREAMING_SNAKE_CASE					) for image in images]

          if do_pad:
               _snake_case								=   [self.pad_image(image=_SCREAMING_SNAKE_CASE      ,size=_SCREAMING_SNAKE_CASE      ,random_padding=_SCREAMING_SNAKE_CASE					) for image in images]

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

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

          _snake_case								=   [to_channel_dimension_format(_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					) for image in images]

          _snake_case								=   {"pixel_values": images}
          return BatchFeature(data=_SCREAMING_SNAKE_CASE      ,tensor_type=_SCREAMING_SNAKE_CASE					)








'''simple docstring'''





from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_clip import CLIPTokenizer


UpperCamelCase_		:    Optional[int]									=		logging.get_logger(__name__)

UpperCamelCase_		:    str									=		{'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

UpperCamelCase_		:    List[str]									=		{
    '''vocab_file''': {
        '''openai/clip-vit-base-patch32''': '''https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/vocab.json''',
    },
    '''merges_file''': {
        '''openai/clip-vit-base-patch32''': '''https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/merges.txt''',
    },
    '''tokenizer_file''': {
        '''openai/clip-vit-base-patch32''': (
            '''https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/tokenizer.json'''
        ),
    },
}

UpperCamelCase_		:    List[Any]									=		{
    '''openai/clip-vit-base-patch32''': 77,
}

class      _a       (		__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:					Any					       =		VOCAB_FILES_NAMES
     SCREAMING_SNAKE_CASE_							:					List[Any]					       =		PRETRAINED_VOCAB_FILES_MAP
     SCREAMING_SNAKE_CASE_							:					Dict					       =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     SCREAMING_SNAKE_CASE_							:					int					       =		["""input_ids""", """attention_mask"""]
     SCREAMING_SNAKE_CASE_							:					Optional[Any]					       =		CLIPTokenizer






     def __init__(				self      ,_SCREAMING_SNAKE_CASE=None      ,_SCREAMING_SNAKE_CASE=None      ,_SCREAMING_SNAKE_CASE=None      ,_SCREAMING_SNAKE_CASE="<|endoftext|>"      ,_SCREAMING_SNAKE_CASE="<|startoftext|>"      ,_SCREAMING_SNAKE_CASE="<|endoftext|>"      ,_SCREAMING_SNAKE_CASE="<|endoftext|>"      ,**_SCREAMING_SNAKE_CASE      ,)							->    List[Any]:
          super().__init__(
              _SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,tokenizer_file=_SCREAMING_SNAKE_CASE      ,unk_token=_SCREAMING_SNAKE_CASE      ,bos_token=_SCREAMING_SNAKE_CASE      ,eos_token=_SCREAMING_SNAKE_CASE      ,pad_token=_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE      ,)

          if not isinstance(self.backend_tokenizer.pre_tokenizer      ,pre_tokenizers.Sequence					):
               raise ValueError(
                   "The `backend_tokenizer` provided does not match the expected format. The CLIP tokenizer has been"
                   " heavily modified from transformers version 4.17.0. You need to convert the tokenizer you are using"
                   " to be compatible with this version.The easiest way to do so is"
                   " `CLIPTokenizerFast.from_pretrained(\"path_to_local_folder_or_hub_repo, from_slow=True)`. If you want"
                   " to use your existing tokenizer, you will have to revert to a version prior to 4.17.0 of"
                   " transformers."					)

          self._wrap_decode_method_backend_tokenizer()






     def        _lowercase			(				self					)							->    Tuple:
          _snake_case							=   self.backend_tokenizer.decode

          def new_decode_method(*_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					):
               _snake_case							=   orig_decode_method(*_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					)
               _snake_case							=   text.replace(self.backend_tokenizer.model.end_of_word_suffix      ," "					).strip()
               return text

          _snake_case							=   new_decode_method






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None					)							->    List[int]:
          _snake_case							=   [self.bos_token_id]
          _snake_case							=   [self.eos_token_id]

          if token_ids_a is None:
               return bos_token + token_ids_a + eos_token
          return bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None					)							->    List[int]:
          _snake_case							=   [self.bos_token_id]
          _snake_case							=   [self.eos_token_id]

          if token_ids_a is None:
               return len(bos_token + token_ids_a + eos_token					) * [0]
          return len(bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token					) * [0]






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None					)							->    Tuple[str]:
          _snake_case							=   self._tokenizer.model.save(_SCREAMING_SNAKE_CASE      ,name=_SCREAMING_SNAKE_CASE					)
          return tuple(_SCREAMING_SNAKE_CASE					)


