





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							:      int						=	logging.get_logger(__name__)

_lowerCAmelCase							:      Optional[Any]						=	{'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

_lowerCAmelCase							:      List[Any]						=	{
    '''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							:      str						=	{
    '''openai/clip-vit-base-patch32''': 77,
}


class 					__magic_name__  (							__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       :List[Any]			,      snake_case       :Dict=None			,      snake_case       :int=None			,      snake_case       :Optional[Any]=None			,      snake_case       :str="<|endoftext|>"			,      snake_case       :List[str]="<|startoftext|>"			,      snake_case       :List[Any]="<|endoftext|>"			,      snake_case       :List[str]="<|endoftext|>"			,      **snake_case       :Union[str, Any]			,      ):





        '''simple docstring'''




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

        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 						SCREAMING_SNAKE_CASE		(							self       :Dict  ):





        '''simple docstring'''




        A_					:				Tuple          =			self.backend_tokenizer.decode

        def new_decode_method(*snake_case       :Any			,      **snake_case       :Any  ):
           A_					:				List[str]          =			orig_decode_method(*UpperCamelCase__			,      **UpperCamelCase__  )
           A_					:				List[Any]          =			text.replace(self.backend_tokenizer.model.end_of_word_suffix			,      " "  ).strip()
           return text

        A_					:				Optional[Any]          =			new_decode_method



     def 						SCREAMING_SNAKE_CASE		(							self       :str			,      snake_case       :List[int]			,      snake_case       :Optional[List[int]] = None  ):





        '''simple docstring'''




        A_					:				Tuple          =			[self.bos_token_id]
        A_					:				Any          =			[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 						SCREAMING_SNAKE_CASE		(							self       :Any			,      snake_case       :List[int]			,      snake_case       :Optional[List[int]] = None  ):





        '''simple docstring'''




        A_					:				Optional[int]          =			[self.bos_token_id]
        A_					:				Optional[int]          =			[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 						SCREAMING_SNAKE_CASE		(							self       :str			,      snake_case       :str			,      snake_case       :Optional[str] = None  ):





        '''simple docstring'''




        A_					:				int          =			self._tokenizer.model.save(UpperCamelCase__			,      name=UpperCamelCase__  )
        return tuple(UpperCamelCase__  )











import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import ViltImageProcessor


class 					__magic_name__  (							unittest.TestCase							):





     """simple docstring"""



     def __init__(							self       :Dict			,      snake_case       :str			,      snake_case       :Tuple=7			,      snake_case       :Union[str, Any]=3			,      snake_case       :Any=18			,      snake_case       :Tuple=30			,      snake_case       :List[str]=400			,      snake_case       :Optional[Any]=True			,      snake_case       :Optional[int]=None			,      snake_case       :str=2			,      snake_case       :Tuple=True			,      snake_case       :List[str]=[0.5, 0.5, 0.5]			,      snake_case       :int=[0.5, 0.5, 0.5]			,      ):





        '''simple docstring'''




        A_					:				Optional[Any]          =			size if size is not None else {"shortest_edge": 30}
        A_					:				Tuple          =			parent
        A_					:				Tuple          =			batch_size
        A_					:				Any          =			num_channels
        A_					:				Dict          =			image_size
        A_					:				int          =			min_resolution
        A_					:				Optional[Any]          =			max_resolution
        A_					:				Union[str, Any]          =			do_resize
        A_					:				List[Any]          =			size
        A_					:				List[str]          =			size_divisor
        A_					:				str          =			do_normalize
        A_					:				int          =			image_mean
        A_					:				str          =			image_std



     def 						SCREAMING_SNAKE_CASE		(							self       :List[Any]  ):





        '''simple docstring'''




        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "size_divisor": self.size_divisor,
        }





     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[Any]			,      snake_case       :Tuple			,      snake_case       :List[str]=False  ):





        '''simple docstring'''




        if not batched:
           A_					:				List[Any]          =			self.size["shortest_edge"]
           A_					:				Tuple          =			image_inputs[0]
           if isinstance(snake_case			,      Image.Image  ):
              A_      ,						A_					:				str          =			image.size
           else:
              A_      ,						A_					:				Optional[int]          =			image.shape[1], image.shape[2]
           A_					:				int          =			size / min(snake_case			,      snake_case  )
           if h < w:
              A_      ,						A_					:				Dict          =			size, scale * w
           else:
              A_      ,						A_					:				Tuple          =			scale * h, size

           A_					:				List[Any]          =			int((1_333 / 800) * size  )
           if max(snake_case			,      snake_case  ) > max_size:
              A_					:				Union[str, Any]          =			max_size / max(snake_case			,      snake_case  )
              A_					:				Tuple          =			newh * scale
              A_					:				List[str]          =			neww * scale

           A_      ,						A_					:				Optional[int]          =			int(newh + 0.5  ), int(neww + 0.5  )
           A_      ,						A_					:				Tuple          =			(
               newh // self.size_divisor * self.size_divisor,
               neww // self.size_divisor * self.size_divisor,
           )

        else:
           A_					:				List[Any]          =			[]
           for image in image_inputs:
              A_      ,						A_					:				str          =			self.get_expected_values([image]  )
              expected_values.append((expected_height, expected_width)  )
           A_					:				Optional[Any]          =			max(snake_case			,      key=lambda snake_case  : item[0]  )[0]
           A_					:				Optional[int]          =			max(snake_case			,      key=lambda snake_case  : item[1]  )[1]

        return expected_height, expected_width




@require_torch
@require_vision
class 					__magic_name__  (							lowerCamelCase__    ,				unittest.TestCase							):





     """simple docstring"""



     __UpperCamelCase					    =     ViltImageProcessor if is_vision_available() else None



     def 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]  ):





        '''simple docstring'''




        A_					:				Dict          =			ViltImageProcessingTester(self  )



     @property
     def 						SCREAMING_SNAKE_CASE		(							self       :Union[str, Any]  ):





        '''simple docstring'''




        return self.image_processor_tester.prepare_image_processor_dict()



     def 						SCREAMING_SNAKE_CASE		(							self       :List[Any]  ):





        '''simple docstring'''




        A_					:				Optional[int]          =			self.image_processing_class(**self.image_processor_dict  )
        self.assertTrue(hasattr(snake_case			,      "image_mean"  )  )
        self.assertTrue(hasattr(snake_case			,      "image_std"  )  )
        self.assertTrue(hasattr(snake_case			,      "do_normalize"  )  )
        self.assertTrue(hasattr(snake_case			,      "do_resize"  )  )
        self.assertTrue(hasattr(snake_case			,      "size"  )  )
        self.assertTrue(hasattr(snake_case			,      "size_divisor"  )  )



     def 						SCREAMING_SNAKE_CASE		(							self       :Tuple  ):





        '''simple docstring'''




        A_					:				Tuple          =			self.image_processing_class.from_dict(self.image_processor_dict  )
        self.assertEqual(image_processor.size			,      {"shortest_edge": 30}  )

        A_					:				Any          =			self.image_processing_class.from_dict(self.image_processor_dict			,      size=42  )
        self.assertEqual(image_processor.size			,      {"shortest_edge": 42}  )



     def 						SCREAMING_SNAKE_CASE		(							self       :List[str]  ):





        '''simple docstring'''




        pass



     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[int]  ):





        '''simple docstring'''




        A_					:				Dict          =			self.image_processing_class(**self.image_processor_dict  )
        # create random PIL images
        A_					:				str          =			prepare_image_inputs(self.image_processor_tester			,      equal_resolution=snake_case  )
        for image in image_inputs:
           self.assertIsInstance(snake_case			,      Image.Image  )

        # Test not batched input
        A_					:				Optional[Any]          =			image_processing(image_inputs[0]			,      return_tensors="pt"  ).pixel_values

        A_      ,						A_					:				str          =			self.image_processor_tester.get_expected_values(snake_case  )
        self.assertEqual(
            encoded_images.shape			,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)			,      )

        # Test batched
        A_					:				int          =			image_processing(snake_case			,      return_tensors="pt"  ).pixel_values

        A_      ,						A_					:				Dict          =			self.image_processor_tester.get_expected_values(snake_case			,      batched=snake_case  )
        self.assertEqual(
            encoded_images.shape			,      (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )			,      )



     def 						SCREAMING_SNAKE_CASE		(							self       :Optional[Any]  ):





        '''simple docstring'''




        A_					:				Any          =			self.image_processing_class(**self.image_processor_dict  )
        # create random numpy tensors
        A_					:				List[str]          =			prepare_image_inputs(self.image_processor_tester			,      equal_resolution=snake_case			,      numpify=snake_case  )
        for image in image_inputs:
           self.assertIsInstance(snake_case			,      np.ndarray  )

        # Test not batched input
        A_					:				List[Any]          =			image_processing(image_inputs[0]			,      return_tensors="pt"  ).pixel_values

        A_      ,						A_					:				int          =			self.image_processor_tester.get_expected_values(snake_case  )
        self.assertEqual(
            encoded_images.shape			,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)			,      )

        # Test batched
        A_					:				Dict          =			image_processing(snake_case			,      return_tensors="pt"  ).pixel_values

        A_      ,						A_					:				str          =			self.image_processor_tester.get_expected_values(snake_case			,      batched=snake_case  )
        self.assertEqual(
            encoded_images.shape			,      (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )			,      )





     def 						SCREAMING_SNAKE_CASE		(							self       :str  ):





        '''simple docstring'''




        A_					:				Any          =			self.image_processing_class(**self.image_processor_dict  )
        # create random PyTorch tensors
        A_					:				Optional[Any]          =			prepare_image_inputs(self.image_processor_tester			,      equal_resolution=snake_case			,      torchify=snake_case  )
        for image in image_inputs:
           self.assertIsInstance(snake_case			,      torch.Tensor  )

        # Test not batched input
        A_					:				int          =			image_processing(image_inputs[0]			,      return_tensors="pt"  ).pixel_values

        A_      ,						A_					:				Union[str, Any]          =			self.image_processor_tester.get_expected_values(snake_case  )
        self.assertEqual(
            encoded_images.shape			,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)			,      )

        # Test batched
        A_					:				List[str]          =			image_processing(snake_case			,      return_tensors="pt"  ).pixel_values

        A_      ,						A_					:				Optional[int]          =			self.image_processor_tester.get_expected_values(snake_case			,      batched=snake_case  )
        self.assertEqual(
            encoded_images.shape			,      (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )			,      )
