




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



				def       SCREAMING_SNAKE_CASE       (      self      :		List[Any]      )  ->    Tuple:





								'''simple docstring'''




								_lowerCAmelCase    :    int										=      tempfile.mkdtemp()

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

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

								_lowerCAmelCase    :    Dict										=      {
								    '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],
								}
								_lowerCAmelCase    :    Any										=      os.path.join(self.tmpdirname		,	UpperCAmelCase_      )
								with open(self.image_processor_file		,	"""w"""		,	encoding="""utf-8"""      ) as fp:
												json.dump(UpperCAmelCase_		,	UpperCAmelCase_      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Dict		,	**_UpperCAmelCase      :		Union[str, Any]      )  ->    Union[str, Any]:





								'''simple docstring'''




								return CLIPTokenizer.from_pretrained(self.tmpdirname		,	**UpperCAmelCase_      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[int]		,	**_UpperCAmelCase      :		str      )  ->    Any:





								'''simple docstring'''




								return CLIPTokenizerFast.from_pretrained(self.tmpdirname		,	**UpperCAmelCase_      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[Any]		,	**_UpperCAmelCase      :		Union[str, Any]      )  ->    Union[str, Any]:





								'''simple docstring'''




								return CLIPImageProcessor.from_pretrained(self.tmpdirname		,	**UpperCAmelCase_      )



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





								'''simple docstring'''




								shutil.rmtree(self.tmpdirname      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Tuple      )  ->    Any:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      [np.random.randint(255		,	size=(3, 30, 400)		,	dtype=np.uinta      )]

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

								return image_inputs



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





								'''simple docstring'''




								_lowerCAmelCase    :    Union[str, Any]										=      self.get_tokenizer()
								_lowerCAmelCase    :    Optional[Any]										=      self.get_rust_tokenizer()
								_lowerCAmelCase    :    Any										=      self.get_image_processor()

								_lowerCAmelCase    :    str										=      CLIPProcessor(tokenizer=UpperCAmelCase_		,	image_processor=UpperCAmelCase_      )
								processor_slow.save_pretrained(self.tmpdirname      )
								_lowerCAmelCase    :    Any										=      CLIPProcessor.from_pretrained(self.tmpdirname		,	use_fast=UpperCAmelCase_      )

								_lowerCAmelCase    :    str										=      CLIPProcessor(tokenizer=UpperCAmelCase_		,	image_processor=UpperCAmelCase_      )
								processor_fast.save_pretrained(self.tmpdirname      )
								_lowerCAmelCase    :    Optional[int]										=      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       SCREAMING_SNAKE_CASE       (      self      :		List[str]      )  ->    int:





								'''simple docstring'''




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

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

								_lowerCAmelCase    :    Dict										=      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       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    Optional[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[Any]										=      self.get_image_processor()
								_lowerCAmelCase    :    int										=      self.get_tokenizer()

								_lowerCAmelCase    :    List[Any]										=      CLIPProcessor(tokenizer=UpperCAmelCase_		,	image_processor=UpperCAmelCase_      )

								_lowerCAmelCase    :    int										=      self.prepare_image_inputs()

								_lowerCAmelCase    :    int										=      image_processor(UpperCAmelCase_		,	return_tensors="""np"""      )
								_lowerCAmelCase    :    Optional[int]										=      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       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    str:





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[Any]										=      self.get_image_processor()
								_lowerCAmelCase    :    Dict										=      self.get_tokenizer()

								_lowerCAmelCase    :    List[Any]										=      CLIPProcessor(tokenizer=UpperCAmelCase_		,	image_processor=UpperCAmelCase_      )

								_lowerCAmelCase    :    Any										=      'lower newer'

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

								_lowerCAmelCase    :    Optional[Any]										=      tokenizer(UpperCAmelCase_      )

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



				def       SCREAMING_SNAKE_CASE       (      self      :		int      )  ->    Any:





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[int]										=      self.get_image_processor()
								_lowerCAmelCase    :    List[str]										=      self.get_tokenizer()

								_lowerCAmelCase    :    List[Any]										=      CLIPProcessor(tokenizer=UpperCAmelCase_		,	image_processor=UpperCAmelCase_      )

								_lowerCAmelCase    :    Optional[Any]										=      'lower newer'
								_lowerCAmelCase    :    List[Any]										=      self.prepare_image_inputs()

								_lowerCAmelCase    :    str										=      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       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    Union[str, Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Any										=      self.get_image_processor()
								_lowerCAmelCase    :    Dict										=      self.get_tokenizer()

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

								_lowerCAmelCase    :    Optional[Any]										=      [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								_lowerCAmelCase    :    List[Any]										=      processor.batch_decode(UpperCAmelCase_      )
								_lowerCAmelCase    :    Optional[int]										=      tokenizer.batch_decode(UpperCAmelCase_      )

								self.assertListEqual(UpperCAmelCase_		,	UpperCAmelCase_      )



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





								'''simple docstring'''




								_lowerCAmelCase    :    Union[str, Any]										=      self.get_image_processor()
								_lowerCAmelCase    :    List[str]										=      self.get_tokenizer()

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

								_lowerCAmelCase    :    List[Any]										=      'lower newer'
								_lowerCAmelCase    :    Optional[int]										=      self.prepare_image_inputs()

								_lowerCAmelCase    :    List[str]										=      processor(text=UpperCAmelCase_		,	images=UpperCAmelCase_      )

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











def 							_UpperCAmelCase  (UpperCamelCase_ : str      ,    UpperCamelCase_ : bool = False       ):
				'''simple docstring'''

				if not isinstance(UpperCamelCase_      ,    UpperCamelCase_       ):
								_lowerCAmelCase    :    Tuple										=      F"Expected string as input, found {type(UpperCamelCase_       )}"
								raise ValueError(UpperCamelCase_       )
				if not isinstance(UpperCamelCase_      ,    UpperCamelCase_       ):
								_lowerCAmelCase    :    Optional[Any]										=      F"Expected boolean as use_pascal parameter, found {type(UpperCamelCase_       )}"
								raise ValueError(UpperCamelCase_       )

				_lowerCAmelCase    :    Any										=      input_str.split("""_"""       )

				_lowerCAmelCase    :    str										=      0 if use_pascal else 1

				_lowerCAmelCase    :    Any										=      words[start_index:]

				_lowerCAmelCase    :    List[str]										=      [word[0].upper() + word[1:] for word in words_to_capitalize]

				_lowerCAmelCase    :    Any										=      """""" if use_pascal else words[0]

				return "".join([initial_word, *capitalized_words]       )


if __name__ == "__main__":
	from doctest import testmod

	testmod()

