




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    __lowerCAmelCase       (							unittest.TestCase):






       def 					SCREAMING_SNAKE_CASE    (   self:					Any				):
         lowercase    :List[str]      	=						tempfile.mkdtemp()

         # fmt: off
         lowercase    :Optional[int]      	=						["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
         lowercase    :List[str]      	=						dict(zip(_lowerCAmelCase       ,					range(len(_lowerCAmelCase				)				)				)				)
         lowercase    :Optional[Any]      	=						["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
         lowercase    :Any      	=						{"unk_token": "<unk>"}

         lowercase    :Union[str, Any]      	=						os.path.join(self.tmpdirname       ,					VOCAB_FILES_NAMES["vocab_file"]				)
         lowercase    :Optional[Any]      	=						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(_lowerCAmelCase				) + "\n"				)
         with open(self.merges_file       ,					"w"       ,					encoding="utf-8"				) as fp:
           fp.write("\n".join(_lowerCAmelCase				)				)

         lowercase    :Optional[int]      	=						{
             "do_resize": True,
             "size": 20,
             "do_center_crop": True,
             "crop_size": 18,
             "do_normalize": True,
             "image_mean": [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
             "image_std": [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
         }
         lowercase    :str      	=						os.path.join(self.tmpdirname       ,					_lowerCAmelCase				)
         with open(self.image_processor_file       ,					"w"       ,					encoding="utf-8"				) as fp:
           json.dump(_lowerCAmelCase       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					str       ,					**_lowerCAmelCase:					str				):
         return CLIPTokenizer.from_pretrained(self.tmpdirname       ,					**_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					int       ,					**_lowerCAmelCase:					Optional[Any]				):
         return CLIPTokenizerFast.from_pretrained(self.tmpdirname       ,					**_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					**_lowerCAmelCase:					Dict				):
         return CLIPImageProcessor.from_pretrained(self.tmpdirname       ,					**_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Any				):
         shutil.rmtree(self.tmpdirname				)






       def 					SCREAMING_SNAKE_CASE    (   self:					str				):
         lowercase    :Tuple      	=						[np.random.randint(2_55       ,					size=(3, 30, 4_00)       ,					dtype=np.uinta				)]

         lowercase    :Dict      	=						[Image.fromarray(np.moveaxis(_lowerCAmelCase       ,					0       ,					-1				)				) for x in image_inputs]

         return image_inputs






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :List[str]      	=						self.get_tokenizer()
         lowercase    :Any      	=						self.get_rust_tokenizer()
         lowercase    :int      	=						self.get_image_processor()

         lowercase    :int      	=						CLIPProcessor(tokenizer=_lowerCAmelCase       ,					image_processor=_lowerCAmelCase				)
         processor_slow.save_pretrained(self.tmpdirname				)
         lowercase    :Tuple      	=						CLIPProcessor.from_pretrained(self.tmpdirname       ,					use_fast=_lowerCAmelCase				)

         lowercase    :List[str]      	=						CLIPProcessor(tokenizer=_lowerCAmelCase       ,					image_processor=_lowerCAmelCase				)
         processor_fast.save_pretrained(self.tmpdirname				)
         lowercase    :List[Any]      	=						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       ,					_lowerCAmelCase				)
         self.assertIsInstance(processor_fast.tokenizer       ,					_lowerCAmelCase				)

         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       ,					_lowerCAmelCase				)
         self.assertIsInstance(processor_fast.image_processor       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :Union[str, Any]      	=						CLIPProcessor(tokenizer=self.get_tokenizer()       ,					image_processor=self.get_image_processor()				)
         processor.save_pretrained(self.tmpdirname				)

         lowercase    :List[Any]      	=						self.get_tokenizer(bos_token="(BOS)"       ,					eos_token="(EOS)"				)
         lowercase    :List[str]      	=						self.get_image_processor(do_normalize=_lowerCAmelCase       ,					padding_value=1.0				)

         lowercase    :Optional[int]      	=						CLIPProcessor.from_pretrained(
             self.tmpdirname       ,					bos_token="(BOS)"       ,					eos_token="(EOS)"       ,					do_normalize=_lowerCAmelCase       ,					padding_value=1.0				)

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

         self.assertEqual(processor.image_processor.to_json_string()       ,					image_processor_add_kwargs.to_json_string()				)
         self.assertIsInstance(processor.image_processor       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         lowercase    :Optional[Any]      	=						self.get_image_processor()
         lowercase    :List[str]      	=						self.get_tokenizer()

         lowercase    :int      	=						CLIPProcessor(tokenizer=_lowerCAmelCase       ,					image_processor=_lowerCAmelCase				)

         lowercase    :str      	=						self.prepare_image_inputs()

         lowercase    :Optional[int]      	=						image_processor(_lowerCAmelCase       ,					return_tensors="np"				)
         lowercase    :Union[str, Any]      	=						processor(images=_lowerCAmelCase       ,					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 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         lowercase    :Tuple      	=						self.get_image_processor()
         lowercase    :Any      	=						self.get_tokenizer()

         lowercase    :List[str]      	=						CLIPProcessor(tokenizer=_lowerCAmelCase       ,					image_processor=_lowerCAmelCase				)

         lowercase    :Tuple      	=						"lower newer"

         lowercase    :Tuple      	=						processor(text=_lowerCAmelCase				)

         lowercase    :Optional[int]      	=						tokenizer(_lowerCAmelCase				)

         for key in encoded_tok.keys():
           self.assertListEqual(encoded_tok[key]       ,					encoded_processor[key]				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :Optional[int]      	=						self.get_image_processor()
         lowercase    :str      	=						self.get_tokenizer()

         lowercase    :str      	=						CLIPProcessor(tokenizer=_lowerCAmelCase       ,					image_processor=_lowerCAmelCase				)

         lowercase    :Tuple      	=						"lower newer"
         lowercase    :Any      	=						self.prepare_image_inputs()

         lowercase    :Union[str, Any]      	=						processor(text=_lowerCAmelCase       ,					images=_lowerCAmelCase				)

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

         # test if it raises when no input is passed
         with pytest.raises(_lowerCAmelCase				):
           processor()






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[int]				):
         lowercase    :str      	=						self.get_image_processor()
         lowercase    :Union[str, Any]      	=						self.get_tokenizer()

         lowercase    :Optional[Any]      	=						CLIPProcessor(tokenizer=_lowerCAmelCase       ,					image_processor=_lowerCAmelCase				)

         lowercase    :Dict      	=						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         lowercase    :List[str]      	=						processor.batch_decode(_lowerCAmelCase				)
         lowercase    :str      	=						tokenizer.batch_decode(_lowerCAmelCase				)

         self.assertListEqual(_lowerCAmelCase       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :Union[str, Any]      	=						self.get_image_processor()
         lowercase    :List[Any]      	=						self.get_tokenizer()

         lowercase    :Optional[Any]      	=						CLIPProcessor(tokenizer=_lowerCAmelCase       ,					image_processor=_lowerCAmelCase				)

         lowercase    :Dict      	=						"lower newer"
         lowercase    :Dict      	=						self.prepare_image_inputs()

         lowercase    :Any      	=						processor(text=_lowerCAmelCase       ,					images=_lowerCAmelCase				)

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









def       UpperCAmelCase__  (     lowerCamelCase = "The quick brown fox jumps over the lazy dog",			):
  lowercase    :Dict      	=						set()

  # Replace all the whitespace in our sentence
  lowercase    :Optional[int]      	=						input_str.replace(" ",			""   )
  for alpha in input_str:
    if "a" <= alpha.lower() <= "z":
      frequency.add(alpha.lower()   )
  return len(lowerCamelCase   ) == 26



def       UpperCAmelCase__  (     lowerCamelCase = "The quick brown fox jumps over the lazy dog",			):
  lowercase    :Optional[Any]      	=						[False] * 26
  for char in input_str:
    if char.islower():
      lowercase    :Union[str, Any]      	=						True
    elif char.isupper():
      lowercase    :int      	=						True
  return all(lowerCamelCase   )



def       UpperCAmelCase__  (     lowerCamelCase = "The quick brown fox jumps over the lazy dog",			):
  return len({char for char in input_str.lower() if char.isalpha()}   ) == 26



def       UpperCAmelCase__  (     ):
  from timeit import timeit

  lowercase    :Optional[Any]      	=						"from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest"
  print(timeit("is_pangram()",			setup=lowerCamelCase   )   )
  print(timeit("is_pangram_faster()",			setup=lowerCamelCase   )   )
  print(timeit("is_pangram_fastest()",			setup=lowerCamelCase   )   )
  # 5.348480500048026, 2.6477354579837993, 1.8470395830227062
  # 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     benchmark()



