'''simple docstring'''






import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
   from PIL import Image

   from transformers import CLIPImageProcessor, CLIPProcessor







@require_vision
class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			Optional[Any]     )  ->			int:
        _A    		=  tempfile.mkdtemp()

        # fmt: off
        _A    		=  ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>']
        # fmt: on
        _A    		=  dict(zip(UpperCamelCase__, range(len(UpperCamelCase__     )     )     )     )
        _A    		=  ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', '']
        _A    		=  {'unk_token': '<unk>'}

        _A    		=  os.path.join(self.tmpdirname, VOCAB_FILES_NAMES['vocab_file']     )
        _A    		=  os.path.join(self.tmpdirname, VOCAB_FILES_NAMES['merges_file']     )
        with open(self.vocab_file, 'w', encoding='utf-8'     ) as fp:
             fp.write(json.dumps(UpperCamelCase__     ) + '\n'     )
        with open(self.merges_file, 'w', encoding='utf-8'     ) as fp:
             fp.write('\n'.join(UpperCamelCase__     )     )

        _A    		=  {
            'do_resize': True,
            'size': 20,
            'do_center_crop': True,
            'crop_size': 18,
            'do_normalize': True,
            'image_mean': [0.48_145_466, 0.4_578_275, 0.40_821_073],
            'image_std': [0.26_862_954, 0.26_130_258, 0.27_577_711],
        }
        _A    		=  os.path.join(self.tmpdirname, UpperCamelCase__     )
        with open(self.image_processor_file, 'w', encoding='utf-8'     ) as fp:
             json.dump(UpperCamelCase__, UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Union[str, Any], **UpperCamelCase__      :			List[Any]     )  ->			int:
        return CLIPTokenizer.from_pretrained(self.tmpdirname, **UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Dict, **UpperCamelCase__      :			List[str]     )  ->			Any:
        return CLIPTokenizerFast.from_pretrained(self.tmpdirname, **UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Optional[int], **UpperCamelCase__      :			List[Any]     )  ->			str:
        return CLIPImageProcessor.from_pretrained(self.tmpdirname, **UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			int:
        shutil.rmtree(self.tmpdirname     )
   def 	__UpperCAmelCase   (      self      :			Optional[Any]     )  ->			Union[str, Any]:
        _A    		=  [np.random.randint(2_55, size=(3, 30, 4_00), dtype=np.uinta     )]

        _A    		=  [Image.fromarray(np.moveaxis(UpperCamelCase__, 0, -1     )     ) for x in image_inputs]

        return image_inputs
   def 	__UpperCAmelCase   (      self      :			Optional[int]     )  ->			Union[str, Any]:
        _A    		=  self.get_tokenizer()
        _A    		=  self.get_rust_tokenizer()
        _A    		=  self.get_image_processor()

        _A    		=  CLIPProcessor(tokenizer=UpperCamelCase__, image_processor=UpperCamelCase__     )
        processor_slow.save_pretrained(self.tmpdirname     )
        _A    		=  CLIPProcessor.from_pretrained(self.tmpdirname, use_fast=UpperCamelCase__     )

        _A    		=  CLIPProcessor(tokenizer=UpperCamelCase__, image_processor=UpperCamelCase__     )
        processor_fast.save_pretrained(self.tmpdirname     )
        _A    		=  CLIPProcessor.from_pretrained(self.tmpdirname     )

        self.assertEqual(processor_slow.tokenizer.get_vocab(), tokenizer_slow.get_vocab()     )
        self.assertEqual(processor_fast.tokenizer.get_vocab(), tokenizer_fast.get_vocab()     )
        self.assertEqual(tokenizer_slow.get_vocab(), tokenizer_fast.get_vocab()     )
        self.assertIsInstance(processor_slow.tokenizer, UpperCamelCase__     )
        self.assertIsInstance(processor_fast.tokenizer, UpperCamelCase__     )

        self.assertEqual(processor_slow.image_processor.to_json_string(), image_processor.to_json_string()     )
        self.assertEqual(processor_fast.image_processor.to_json_string(), image_processor.to_json_string()     )
        self.assertIsInstance(processor_slow.image_processor, UpperCamelCase__     )
        self.assertIsInstance(processor_fast.image_processor, UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			Dict:
        _A    		=  CLIPProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()     )
        processor.save_pretrained(self.tmpdirname     )

        _A    		=  self.get_tokenizer(bos_token='(BOS)', eos_token='(EOS)'     )
        _A    		=  self.get_image_processor(do_normalize=UpperCamelCase__, padding_value=1.0     )

        _A    		=  CLIPProcessor.from_pretrained(
            self.tmpdirname, bos_token='(BOS)', eos_token='(EOS)', do_normalize=UpperCamelCase__, padding_value=1.0     )

        self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()     )
        self.assertIsInstance(processor.tokenizer, UpperCamelCase__     )

        self.assertEqual(processor.image_processor.to_json_string(), image_processor_add_kwargs.to_json_string()     )
        self.assertIsInstance(processor.image_processor, UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			str     )  ->			List[Any]:
        _A    		=  self.get_image_processor()
        _A    		=  self.get_tokenizer()

        _A    		=  CLIPProcessor(tokenizer=UpperCamelCase__, image_processor=UpperCamelCase__     )

        _A    		=  self.prepare_image_inputs()

        _A    		=  image_processor(UpperCamelCase__, return_tensors='np'     )
        _A    		=  processor(images=UpperCamelCase__, return_tensors='np'     )

        for key in input_image_proc.keys():
             self.assertAlmostEqual(input_image_proc[key].sum(), input_processor[key].sum(), delta=1e-2     )
   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			Tuple:
        _A    		=  self.get_image_processor()
        _A    		=  self.get_tokenizer()

        _A    		=  CLIPProcessor(tokenizer=UpperCamelCase__, image_processor=UpperCamelCase__     )

        _A    		=  'lower newer'

        _A    		=  processor(text=UpperCamelCase__     )

        _A    		=  tokenizer(UpperCamelCase__     )

        for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key], encoded_processor[key]     )
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			List[Any]:
        _A    		=  self.get_image_processor()
        _A    		=  self.get_tokenizer()

        _A    		=  CLIPProcessor(tokenizer=UpperCamelCase__, image_processor=UpperCamelCase__     )

        _A    		=  'lower newer'
        _A    		=  self.prepare_image_inputs()

        _A    		=  processor(text=UpperCamelCase__, images=UpperCamelCase__     )

        self.assertListEqual(list(inputs.keys()     ), ['input_ids', 'attention_mask', 'pixel_values']     )

        # test if it raises when no input is passed
        with pytest.raises(UpperCamelCase__     ):
             processor()
   def 	__UpperCAmelCase   (      self      :			Optional[int]     )  ->			Dict:
        _A    		=  self.get_image_processor()
        _A    		=  self.get_tokenizer()

        _A    		=  CLIPProcessor(tokenizer=UpperCamelCase__, image_processor=UpperCamelCase__     )

        _A    		=  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

        _A    		=  processor.batch_decode(UpperCamelCase__     )
        _A    		=  tokenizer.batch_decode(UpperCamelCase__     )

        self.assertListEqual(UpperCamelCase__, UpperCamelCase__     )

   def 	__UpperCAmelCase   (      self      :			Dict     )  ->			Dict:
        _A    		=  self.get_image_processor()
        _A    		=  self.get_tokenizer()

        _A    		=  CLIPProcessor(tokenizer=UpperCamelCase__, image_processor=UpperCamelCase__     )

        _A    		=  'lower newer'
        _A    		=  self.prepare_image_inputs()

        _A    		=  processor(text=UpperCamelCase__, images=UpperCamelCase__     )

        self.assertListEqual(list(inputs.keys()     ), processor.model_input_names     )










from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
							import PIL

UpperCAmelCase_							=				logging.get_logger(__name__)





def  __magic_name__  (				lowercase				)				-> List[List[ImageInput]]:

							"""simple docstring"""



							if isinstance(lowercase				,       (list, tuple)				) and isinstance(videos[0]				,       (list, tuple)				) and is_valid_image(videos[0][0]				):
														return videos

							elif isinstance(lowercase				,       (list, tuple)				) and is_valid_image(videos[0]				):
														return [videos]

							elif is_valid_image(lowercase				):
														return [[videos]]

							raise ValueError(f"""Could not make batched video from {videos}"""				)




class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		__a       :       List[Any]       =	["""pixel_values"""]






		def __init__(							self,   snake_case__ = True,   snake_case__ = None,   snake_case__ = PILImageResampling.BILINEAR,   snake_case__ = True,   snake_case__ = None,   snake_case__ = True,   snake_case__ = 1 / 2_55,   snake_case__ = True,   snake_case__ = True,   snake_case__ = None,   snake_case__ = None,   **snake_case__,   )       -> None:






									"""simple docstring"""
									super().__init__(**snake_case__  )
									lowercase_	:   str  				=     size if size is not None else {"""shortest_edge""": 2_56}
									lowercase_	:   int  				=     get_size_dict(snake_case__,   default_to_square=snake_case__  )
									lowercase_	:   int  				=     crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24}
									lowercase_	:   Optional[int]  				=     get_size_dict(snake_case__,   param_name="""crop_size"""  )

									lowercase_	:   List[Any]  				=     do_resize
									lowercase_	:   int  				=     size
									lowercase_	:   int  				=     do_center_crop
									lowercase_	:   Optional[Any]  				=     crop_size
									lowercase_	:   str  				=     resample
									lowercase_	:   Any  				=     do_rescale
									lowercase_	:   Dict  				=     rescale_factor
									lowercase_	:   List[Any]  				=     offset
									lowercase_	:   int  				=     do_normalize
									lowercase_	:   str  				=     image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
									lowercase_	:   Tuple  				=     image_std if image_std is not None else IMAGENET_STANDARD_STD






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__ = PILImageResampling.BILINEAR,   snake_case__ = None,   **snake_case__,   )       -> np.ndarray:






									"""simple docstring"""
									lowercase_	:   int  				=     get_size_dict(snake_case__,   default_to_square=snake_case__  )
									if "shortest_edge" in size:
																lowercase_	:   Union[str, Any]  				=     get_resize_output_image_size(snake_case__,   size["""shortest_edge"""],   default_to_square=snake_case__  )
									elif "height" in size and "width" in size:
																lowercase_	:   Dict  				=     (size["""height"""], size["""width"""])
									else:
																raise ValueError(f"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}"""  )
									return resize(snake_case__,   size=snake_case__,   resample=snake_case__,   data_format=snake_case__,   **snake_case__  )






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__ = None,   **snake_case__,   )       -> np.ndarray:






									"""simple docstring"""
									lowercase_	:   List[Any]  				=     get_size_dict(snake_case__  )
									if "height" not in size or "width" not in size:
																raise ValueError(f"""Size must have 'height' and 'width' as keys. Got {size.keys()}"""  )
									return center_crop(snake_case__,   size=(size["""height"""], size["""width"""]),   data_format=snake_case__,   **snake_case__  )






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__ = True,   snake_case__ = None,   **snake_case__,   )       -> str:






									"""simple docstring"""
									lowercase_	:   Dict  				=     image.astype(np.floataa  )
									if offset:
																lowercase_	:   Optional[Any]  				=     image - (scale / 2)
									return rescale(snake_case__,   scale=snake_case__,   data_format=snake_case__,   **snake_case__  )






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__,   snake_case__ = None,   **snake_case__,   )       -> np.ndarray:






									"""simple docstring"""
									return normalize(snake_case__,   mean=snake_case__,   std=snake_case__,   data_format=snake_case__,   **snake_case__  )






		def 						snake_case__				(							self,   snake_case__,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = ChannelDimension.FIRST,   )       -> np.ndarray:






									"""simple docstring"""
									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_center_crop and crop_size is None:
																raise ValueError("""Crop size must be specified if do_center_crop 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."""  )

									if offset and not do_rescale:
																raise ValueError("""For offset, do_rescale must also be set to True."""  )

									# All transformations expect numpy arrays.
									lowercase_	:   Optional[int]  				=     to_numpy_array(snake_case__  )

									if do_resize:
																lowercase_	:   Dict  				=     self.resize(image=snake_case__,   size=snake_case__,   resample=snake_case__  )

									if do_center_crop:
																lowercase_	:   Optional[int]  				=     self.center_crop(snake_case__,   size=snake_case__  )

									if do_rescale:
																lowercase_	:   Dict  				=     self.rescale(image=snake_case__,   scale=snake_case__,   offset=snake_case__  )

									if do_normalize:
																lowercase_	:   Optional[Any]  				=     self.normalize(image=snake_case__,   mean=snake_case__,   std=snake_case__  )

									lowercase_	:   Tuple  				=     to_channel_dimension_format(snake_case__,   snake_case__  )
									return image






		def 						snake_case__				(							self,   snake_case__,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = None,   snake_case__ = ChannelDimension.FIRST,   **snake_case__,   )       -> PIL.Image.Image:






									"""simple docstring"""
									lowercase_	:   Any  				=     do_resize if do_resize is not None else self.do_resize
									lowercase_	:   str  				=     resample if resample is not None else self.resample
									lowercase_	:   List[str]  				=     do_center_crop if do_center_crop is not None else self.do_center_crop
									lowercase_	:   Optional[Any]  				=     do_rescale if do_rescale is not None else self.do_rescale
									lowercase_	:   str  				=     rescale_factor if rescale_factor is not None else self.rescale_factor
									lowercase_	:   List[Any]  				=     offset if offset is not None else self.offset
									lowercase_	:   Optional[Any]  				=     do_normalize if do_normalize is not None else self.do_normalize
									lowercase_	:   Any  				=     image_mean if image_mean is not None else self.image_mean
									lowercase_	:   Optional[Any]  				=     image_std if image_std is not None else self.image_std

									lowercase_	:   Optional[Any]  				=     size if size is not None else self.size
									lowercase_	:   Dict  				=     get_size_dict(snake_case__,   default_to_square=snake_case__  )
									lowercase_	:   Union[str, Any]  				=     crop_size if crop_size is not None else self.crop_size
									lowercase_	:   Tuple  				=     get_size_dict(snake_case__,   param_name="""crop_size"""  )

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

									lowercase_	:   Optional[Any]  				=     make_batched(snake_case__  )

									lowercase_	:   Optional[Any]  				=     [
									    [
									        self._preprocess_image(
									            image=snake_case__,   do_resize=snake_case__,   size=snake_case__,   resample=snake_case__,   do_center_crop=snake_case__,   crop_size=snake_case__,   do_rescale=snake_case__,   rescale_factor=snake_case__,   offset=snake_case__,   do_normalize=snake_case__,   image_mean=snake_case__,   image_std=snake_case__,   data_format=snake_case__,   )
									        for img in video
									    ]
									    for video in videos
									]

									lowercase_	:   List[str]  				=     {"""pixel_values""": videos}
									return BatchFeature(data=snake_case__,   tensor_type=snake_case__  )