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






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Dict													=       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_	:     Optional[int]													=       dict(zip(__SCREAMING_SNAKE_CASE				,							range(len(__SCREAMING_SNAKE_CASE		)		)		)		)
											lowercase_	:     int													=       ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', '''''']
											lowercase_	:     Optional[int]													=       {'''unk_token''': '''<unk>'''}

											lowercase_	:     List[Any]													=       os.path.join(self.tmpdirname				,							VOCAB_FILES_NAMES['''vocab_file''']		)
											lowercase_	:     Optional[int]													=       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		)		)

											lowercase_	:     Optional[Any]													=       {
											    '''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],
											}
											lowercase_	:     Any													=       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 	_snake_case    (   self				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def 	_snake_case    (   self				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def 	_snake_case    (   self				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def 	_snake_case    (   self		):


											"""simple docstring"""





											shutil.rmtree(self.tmpdirname		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Union[str, Any]													=       [np.random.randint(2_55				,							size=(3, 30, 4_00)				,							dtype=np.uinta		)]

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

											return image_inputs






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     int													=       self.get_tokenizer()
											lowercase_	:     Tuple													=       self.get_rust_tokenizer()
											lowercase_	:     Union[str, Any]													=       self.get_image_processor()

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

											lowercase_	:     int													=       CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE				,							image_processor=__SCREAMING_SNAKE_CASE		)
											processor_fast.save_pretrained(self.tmpdirname		)
											lowercase_	:     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				,							__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 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Optional[int]													=       CLIPProcessor(tokenizer=self.get_tokenizer()				,							image_processor=self.get_image_processor()		)
											processor.save_pretrained(self.tmpdirname		)

											lowercase_	:     int													=       self.get_tokenizer(bos_token='''(BOS)'''				,							eos_token='''(EOS)'''		)
											lowercase_	:     List[str]													=       self.get_image_processor(do_normalize=__SCREAMING_SNAKE_CASE				,							padding_value=1.0		)

											lowercase_	:     Tuple													=       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 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     int													=       self.get_image_processor()
											lowercase_	:     List[Any]													=       self.get_tokenizer()

											lowercase_	:     Optional[int]													=       CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE				,							image_processor=__SCREAMING_SNAKE_CASE		)

											lowercase_	:     List[Any]													=       self.prepare_image_inputs()

											lowercase_	:     List[Any]													=       image_processor(__SCREAMING_SNAKE_CASE				,							return_tensors='''np'''		)
											lowercase_	:     Optional[Any]													=       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 	_snake_case    (   self		):


											"""simple docstring"""





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

											lowercase_	:     Tuple													=       CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE				,							image_processor=__SCREAMING_SNAKE_CASE		)

											lowercase_	:     List[Any]													=       '''lower newer'''

											lowercase_	:     List[Any]													=       processor(text=__SCREAMING_SNAKE_CASE		)

											lowercase_	:     int													=       tokenizer(__SCREAMING_SNAKE_CASE		)

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






					def 	_snake_case    (   self		):


											"""simple docstring"""





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

											lowercase_	:     Dict													=       CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE				,							image_processor=__SCREAMING_SNAKE_CASE		)

											lowercase_	:     str													=       '''lower newer'''
											lowercase_	:     Optional[int]													=       self.prepare_image_inputs()

											lowercase_	:     Dict													=       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 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Optional[Any]													=       self.get_image_processor()
											lowercase_	:     Tuple													=       self.get_tokenizer()

											lowercase_	:     str													=       CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE				,							image_processor=__SCREAMING_SNAKE_CASE		)

											lowercase_	:     str													=       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											lowercase_	:     Any													=       processor.batch_decode(__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Optional[Any]													=       tokenizer.batch_decode(__SCREAMING_SNAKE_CASE		)

											self.assertListEqual(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Dict													=       self.get_image_processor()
											lowercase_	:     int													=       self.get_tokenizer()

											lowercase_	:     Dict													=       CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE				,							image_processor=__SCREAMING_SNAKE_CASE		)

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

											lowercase_	:     Any													=       processor(text=__SCREAMING_SNAKE_CASE				,							images=__SCREAMING_SNAKE_CASE		)

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





'''simple docstring'''







def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: int							):



						"""simple docstring"""





						lowercase_	:     Optional[int]													=       int(__SCREAMING_SNAKE_CASE							)
						if decimal in (0, 1):  # Exit cases for the recursion
												return str(__SCREAMING_SNAKE_CASE							)
						lowercase_						, lowercase_	:     List[str]													=       divmod(__SCREAMING_SNAKE_CASE      ,      2							)
						return binary_recursive(__SCREAMING_SNAKE_CASE							) + str(__SCREAMING_SNAKE_CASE							)
def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: str							):



						"""simple docstring"""





						lowercase_	:     str													=       str(__SCREAMING_SNAKE_CASE							).strip()
						if not number:
												raise ValueError('''No input value was provided'''							)
						lowercase_	:     Optional[int]													=       '''-''' if number.startswith('''-'''							) else ''''''
						lowercase_	:     Union[str, Any]													=       number.lstrip('''-'''							)
						if not number.isnumeric():
												raise ValueError('''Input value is not an integer'''							)
						return F'''{negative}0b{binary_recursive(int(__SCREAMING_SNAKE_CASE							)							)}'''


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

							testmod()



