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 normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


lowerCAmelCase__							= logging.get_logger(__name__)
class 				a_ (						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_	 =					['pixel_values']




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







        '''simple docstring'''



        super().__init__(**lowercase__)
        lowerCAmelCase__     	=					size if size is not None else {'height': 224, 'width': 224}
        lowerCAmelCase__     	=					get_size_dict(lowercase__)
        lowerCAmelCase__     	=					do_resize
        lowerCAmelCase__     	=					do_rescale
        lowerCAmelCase__     	=					do_normalize
        lowerCAmelCase__     	=					size
        lowerCAmelCase__     	=					resample
        lowerCAmelCase__     	=					rescale_factor
        lowerCAmelCase__     	=					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
        lowerCAmelCase__     	=					image_std if image_std is not None else IMAGENET_STANDARD_STD




 def 		__snake_case						(			self					:  str			,    lowercase__					:  np.ndarray			,    lowercase__					:  Dict[str, int]			,    lowercase__					:  PILImageResampling = PILImageResampling.BILINEAR			,    lowercase__					:  Optional[Union[str, ChannelDimension]] = None			,    **lowercase__					:  Any			,    ):







        '''simple docstring'''



        lowerCAmelCase__     	=					get_size_dict(lowercase__)
        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()}""")
        return resize(
            lowercase__			,    size=(size['height'], size['width'])			,    resample=lowercase__			,    data_format=lowercase__			,    **lowercase__)




 def 		__snake_case						(			self					:  Optional[int]			,    lowercase__					:  np.ndarray			,    lowercase__					:  float			,    lowercase__					:  Optional[Union[str, ChannelDimension]] = None			,    **lowercase__					:  Optional[int]):







        '''simple docstring'''



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




 def 		__snake_case						(			self					:  Tuple			,    lowercase__					:  np.ndarray			,    lowercase__					:  Union[float, List[float]]			,    lowercase__					:  Union[float, List[float]]			,    lowercase__					:  Optional[Union[str, ChannelDimension]] = None			,    **lowercase__					:  Any			,    ):







        '''simple docstring'''



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




 def 		__snake_case						(			self					:  Optional[int]			,    lowercase__					:  ImageInput			,    lowercase__					:  Optional[bool] = None			,    lowercase__					:  Dict[str, int] = None			,    lowercase__					:  PILImageResampling = None			,    lowercase__					:  Optional[bool] = None			,    lowercase__					:  Optional[float] = None			,    lowercase__					:  Optional[bool] = None			,    lowercase__					:  Optional[Union[float, List[float]]] = None			,    lowercase__					:  Optional[Union[float, List[float]]] = None			,    lowercase__					:  Optional[Union[str, TensorType]] = None			,    lowercase__					:  Union[str, ChannelDimension] = ChannelDimension.FIRST			,    **lowercase__					:  List[str]			,    ):







        '''simple docstring'''



        lowerCAmelCase__     	=					do_resize if do_resize is not None else self.do_resize
        lowerCAmelCase__     	=					do_rescale if do_rescale is not None else self.do_rescale
        lowerCAmelCase__     	=					do_normalize if do_normalize is not None else self.do_normalize
        lowerCAmelCase__     	=					resample if resample is not None else self.resample
        lowerCAmelCase__     	=					rescale_factor if rescale_factor is not None else self.rescale_factor
        lowerCAmelCase__     	=					image_mean if image_mean is not None else self.image_mean
        lowerCAmelCase__     	=					image_std if image_std is not None else self.image_std

        lowerCAmelCase__     	=					size if size is not None else self.size
        lowerCAmelCase__     	=					get_size_dict(lowercase__)

        lowerCAmelCase__     	=					make_list_of_images(lowercase__)

        if not valid_images(lowercase__):
               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.')

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

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

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

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

        lowerCAmelCase__     	=					[to_channel_dimension_format(lowercase__			,    lowercase__) for image in images]

        lowerCAmelCase__     	=					{'pixel_values': images}
        return BatchFeature(data=lowercase__			,    tensor_type=lowercase__)

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


lowerCAmelCase__							= logging.get_logger(__name__)

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

lowerCAmelCase__							= {
    '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'
        ),
    },
}

lowerCAmelCase__							= {
    'openai/clip-vit-base-patch32': 77,
}
class 				a_ (						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_	 =					VOCAB_FILES_NAMES
 UpperCAmelCase_	 =					PRETRAINED_VOCAB_FILES_MAP
 UpperCAmelCase_	 =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 UpperCAmelCase_	 =					['input_ids', 'attention_mask']
 UpperCAmelCase_	 =					CLIPTokenizer




 def __init__(			self					:  Optional[int]			,    lowercase__					:  Optional[int]=None			,    lowercase__					:  Tuple=None			,    lowercase__					:  Tuple=None			,    lowercase__					:  str="<|endoftext|>"			,    lowercase__					:  List[str]="<|startoftext|>"			,    lowercase__					:  Union[str, Any]="<|endoftext|>"			,    lowercase__					:  Any="<|endoftext|>"			,    **lowercase__					:  Any			,    ):







        '''simple docstring'''



        super().__init__(
            lowercase__			,    lowercase__			,    tokenizer_file=lowercase__			,    unk_token=lowercase__			,    bos_token=lowercase__			,    eos_token=lowercase__			,    pad_token=lowercase__			,    **lowercase__			,    )

        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 		__snake_case						(			self					:  Optional[int]):







        '''simple docstring'''



        lowerCAmelCase__     	=					self.backend_tokenizer.decode

        def new_decode_method(*lowercase__					:  str			,    **lowercase__					:  int):
               lowerCAmelCase__     	=					orig_decode_method(*lowercase__			,    **lowercase__)
               lowerCAmelCase__     	=					text.replace(self.backend_tokenizer.model.end_of_word_suffix			,    ' ').strip()
               return text

        lowerCAmelCase__     	=					new_decode_method




 def 		__snake_case						(			self					:  Tuple			,    lowercase__					:  List[int]			,    lowercase__					:  Optional[List[int]] = None):







        '''simple docstring'''



        lowerCAmelCase__     	=					[self.bos_token_id]
        lowerCAmelCase__     	=					[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 		__snake_case						(			self					:  Any			,    lowercase__					:  List[int]			,    lowercase__					:  Optional[List[int]] = None):







        '''simple docstring'''



        lowerCAmelCase__     	=					[self.bos_token_id]
        lowerCAmelCase__     	=					[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 		__snake_case						(			self					:  Dict			,    lowercase__					:  str			,    lowercase__					:  Optional[str] = None):







        '''simple docstring'''



        lowerCAmelCase__     	=					self._tokenizer.model.save(lowercase__			,    name=lowercase__)
        return tuple(lowercase__)
