


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 		__a				(    unittest.TestCase      ):
		def      UpperCAmelCase__    (      self						)			->	List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      tempfile.mkdtemp()

									# fmt: off
									_UpperCAmelCase					=      ['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
									_UpperCAmelCase					=      dict(zip(_SCREAMING_SNAKE_CASE      ,   range(len(_SCREAMING_SNAKE_CASE						)						)						)						)
									_UpperCAmelCase					=      ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', '']
									_UpperCAmelCase					=      {'unk_token': '<unk>'}

									_UpperCAmelCase					=      os.path.join(self.tmpdirname      ,   VOCAB_FILES_NAMES['vocab_file']						)
									_UpperCAmelCase					=      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(_SCREAMING_SNAKE_CASE						) + '\n'						)
									with open(self.merges_file      ,   'w'      ,   encoding='utf-8'						) as fp:
																fp.write('\n'.join(_SCREAMING_SNAKE_CASE						)						)

									_UpperCAmelCase					=      {
									    'do_resize': True,
									    'size': 20,
									    'do_center_crop': True,
									    'crop_size': 18,
									    'do_normalize': True,
									    'image_mean': [0.48145466, 0.4578275, 0.40821073],
									    'image_std': [0.26862954, 0.26130258, 0.27577711],
									}
									_UpperCAmelCase					=      os.path.join(self.tmpdirname      ,   _SCREAMING_SNAKE_CASE						)
									with open(self.image_processor_file      ,   'w'      ,   encoding='utf-8'						) as fp:
																json.dump(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self      ,   **_SCREAMING_SNAKE_CASE						)			->	Optional[Any]:




									"""simple docstring"""





									return CLIPTokenizer.from_pretrained(self.tmpdirname      ,   **_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self      ,   **_SCREAMING_SNAKE_CASE						)			->	Union[str, Any]:




									"""simple docstring"""





									return CLIPTokenizerFast.from_pretrained(self.tmpdirname      ,   **_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self      ,   **_SCREAMING_SNAKE_CASE						)			->	Any:




									"""simple docstring"""





									return CLIPImageProcessor.from_pretrained(self.tmpdirname      ,   **_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	Optional[Any]:




									"""simple docstring"""





									shutil.rmtree(self.tmpdirname						)
		def      UpperCAmelCase__    (      self						)			->	Dict:




									"""simple docstring"""





									_UpperCAmelCase					=      [np.random.randint(255      ,   size=(3, 30, 400)      ,   dtype=np.uinta						)]

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

									return image_inputs
		def      UpperCAmelCase__    (      self						)			->	Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=      self.get_tokenizer()
									_UpperCAmelCase					=      self.get_rust_tokenizer()
									_UpperCAmelCase					=      self.get_image_processor()

									_UpperCAmelCase					=      CLIPProcessor(tokenizer=_SCREAMING_SNAKE_CASE      ,   image_processor=_SCREAMING_SNAKE_CASE						)
									processor_slow.save_pretrained(self.tmpdirname						)
									_UpperCAmelCase					=      CLIPProcessor.from_pretrained(self.tmpdirname      ,   use_fast=_SCREAMING_SNAKE_CASE						)

									_UpperCAmelCase					=      CLIPProcessor(tokenizer=_SCREAMING_SNAKE_CASE      ,   image_processor=_SCREAMING_SNAKE_CASE						)
									processor_fast.save_pretrained(self.tmpdirname						)
									_UpperCAmelCase					=      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      ,   _SCREAMING_SNAKE_CASE						)
									self.assertIsInstance(processor_fast.tokenizer      ,   _SCREAMING_SNAKE_CASE						)

									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      ,   _SCREAMING_SNAKE_CASE						)
									self.assertIsInstance(processor_fast.image_processor      ,   _SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      CLIPProcessor(tokenizer=self.get_tokenizer()      ,   image_processor=self.get_image_processor()						)
									processor.save_pretrained(self.tmpdirname						)

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

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

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

									self.assertEqual(processor.image_processor.to_json_string()      ,   image_processor_add_kwargs.to_json_string()						)
									self.assertIsInstance(processor.image_processor      ,   _SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      self.get_image_processor()
									_UpperCAmelCase					=      self.get_tokenizer()

									_UpperCAmelCase					=      CLIPProcessor(tokenizer=_SCREAMING_SNAKE_CASE      ,   image_processor=_SCREAMING_SNAKE_CASE						)

									_UpperCAmelCase					=      self.prepare_image_inputs()

									_UpperCAmelCase					=      image_processor(_SCREAMING_SNAKE_CASE      ,   return_tensors='np'						)
									_UpperCAmelCase					=      processor(images=_SCREAMING_SNAKE_CASE      ,   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						)			->	Any:




									"""simple docstring"""





									_UpperCAmelCase					=      self.get_image_processor()
									_UpperCAmelCase					=      self.get_tokenizer()

									_UpperCAmelCase					=      CLIPProcessor(tokenizer=_SCREAMING_SNAKE_CASE      ,   image_processor=_SCREAMING_SNAKE_CASE						)

									_UpperCAmelCase					=      'lower newer'

									_UpperCAmelCase					=      processor(text=_SCREAMING_SNAKE_CASE						)

									_UpperCAmelCase					=      tokenizer(_SCREAMING_SNAKE_CASE						)

									for key in encoded_tok.keys():
																self.assertListEqual(encoded_tok[key]      ,   encoded_processor[key]						)
		def      UpperCAmelCase__    (      self						)			->	Dict:




									"""simple docstring"""





									_UpperCAmelCase					=      self.get_image_processor()
									_UpperCAmelCase					=      self.get_tokenizer()

									_UpperCAmelCase					=      CLIPProcessor(tokenizer=_SCREAMING_SNAKE_CASE      ,   image_processor=_SCREAMING_SNAKE_CASE						)

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

									_UpperCAmelCase					=      processor(text=_SCREAMING_SNAKE_CASE      ,   images=_SCREAMING_SNAKE_CASE						)

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

									# test if it raises when no input is passed
									with pytest.raises(_SCREAMING_SNAKE_CASE						):
																processor()
		def      UpperCAmelCase__    (      self						)			->	Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      self.get_image_processor()
									_UpperCAmelCase					=      self.get_tokenizer()

									_UpperCAmelCase					=      CLIPProcessor(tokenizer=_SCREAMING_SNAKE_CASE      ,   image_processor=_SCREAMING_SNAKE_CASE						)

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

									_UpperCAmelCase					=      processor.batch_decode(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      tokenizer.batch_decode(_SCREAMING_SNAKE_CASE						)

									self.assertListEqual(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)






		def      UpperCAmelCase__    (      self						)			->	Any:




									"""simple docstring"""





									_UpperCAmelCase					=      self.get_image_processor()
									_UpperCAmelCase					=      self.get_tokenizer()

									_UpperCAmelCase					=      CLIPProcessor(tokenizer=_SCREAMING_SNAKE_CASE      ,   image_processor=_SCREAMING_SNAKE_CASE						)

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

									_UpperCAmelCase					=      processor(text=_SCREAMING_SNAKE_CASE      ,   images=_SCREAMING_SNAKE_CASE						)

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




from importlib import import_module

from .logging import get_logger


lowerCAmelCase__  :Optional[Any]             =							get_logger(__name__)

class 		__a				:
		def __init__(      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE=None						)			->	List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      attrs or []
									if module is not None:
																for key in module.__dict__:
																							if key in attrs or not key.startswith('__'						):
																														setattr(self      ,   _SCREAMING_SNAKE_CASE      ,   getattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)						)
									_UpperCAmelCase					=      module._original_module if isinstance(_SCREAMING_SNAKE_CASE      ,   _PatchedModuleObj						) else module



class 		__a				:
		_a     :  Any           =     []
		def __init__(      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE=None						)			->	Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      obj
									_UpperCAmelCase					=      target
									_UpperCAmelCase					=      new
									_UpperCAmelCase					=      target.split('.'						)[0]
									_UpperCAmelCase					=      {}
									_UpperCAmelCase					=      attrs or []
		def __enter__(      self						)			->	int:




									"""simple docstring"""





									*_UpperCAmelCase		,	_UpperCAmelCase					=      self.target.split('.'						)

									# Patch modules:
									# it's used to patch attributes of submodules like "os.path.join";
									# in this case we need to patch "os" and "os.path"

									for i in range(len(_SCREAMING_SNAKE_CASE						)						):
																try:
																							_UpperCAmelCase					=      import_module('.'.join(submodules[: i + 1]						)						)
																except ModuleNotFoundError:
																							continue
																# We iterate over all the globals in self.obj in case we find "os" or "os.path"
																for attr in self.obj.__dir__():
																							_UpperCAmelCase					=      getattr(self.obj      ,   _SCREAMING_SNAKE_CASE						)
																							# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
																							# This allows to patch renamed modules like "from os import path as ospath".
																							if obj_attr is submodule or (
																							    (isinstance(_SCREAMING_SNAKE_CASE      ,   _PatchedModuleObj						) and obj_attr._original_module is submodule)
																							):
																														_UpperCAmelCase					=      obj_attr
																														# patch at top level
																														setattr(self.obj      ,   _SCREAMING_SNAKE_CASE      ,   _PatchedModuleObj(_SCREAMING_SNAKE_CASE      ,   attrs=self.attrs						)						)
																														_UpperCAmelCase					=      getattr(self.obj      ,   _SCREAMING_SNAKE_CASE						)
																														# construct lower levels patches
																														for key in submodules[i + 1 :]:
																																					setattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _PatchedModuleObj(getattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)      ,   attrs=self.attrs						)						)
																																					_UpperCAmelCase					=      getattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)
																														# finally set the target attribute
																														setattr(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   self.new						)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

									if submodules:  # if it's an attribute of a submodule like "os.path.join"
																try:
																							_UpperCAmelCase					=      getattr(import_module('.'.join(_SCREAMING_SNAKE_CASE						)						)      ,   _SCREAMING_SNAKE_CASE						)
																except (AttributeError, ModuleNotFoundError):
																							return
																# We iterate over all the globals in self.obj in case we find "os.path.join"
																for attr in self.obj.__dir__():
																							# We don't check for the name of the global, but rather if its value *is* "os.path.join".
																							# This allows to patch renamed attributes like "from os.path import join as pjoin".
																							if getattr(self.obj      ,   _SCREAMING_SNAKE_CASE						) is attr_value:
																														_UpperCAmelCase					=      getattr(self.obj      ,   _SCREAMING_SNAKE_CASE						)
																														setattr(self.obj      ,   _SCREAMING_SNAKE_CASE      ,   self.new						)
									elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
																_UpperCAmelCase					=      globals()['__builtins__'][target_attr]
																setattr(self.obj      ,   _SCREAMING_SNAKE_CASE      ,   self.new						)
									else:
																raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.'''						)
		def __exit__(      self      ,   *_SCREAMING_SNAKE_CASE						)			->	Union[str, Any]:




									"""simple docstring"""





									for attr in list(self.original						):
																setattr(self.obj      ,   _SCREAMING_SNAKE_CASE      ,   self.original.pop(_SCREAMING_SNAKE_CASE						)						)
		def      UpperCAmelCase__    (      self						)			->	Optional[int]:




									"""simple docstring"""





									self.__enter__()
									self._active_patches.append(self						)






		def      UpperCAmelCase__    (      self						)			->	str:




									"""simple docstring"""





									try:
																self._active_patches.remove(self						)
									except ValueError:
																# If the patch hasn't been started this will fail
																return None

									return self.__exit__()
