
"""simple docstring"""





import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
				from PIL import Image

				from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast






@require_vision
class    _UpperCAmelCase(   unittest.TestCase     ):



		def 						UpperCAmelCase							( self)	->	List[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  tempfile.mkdtemp()

							_UpperCamelCase										=  BlipImageProcessor()
							_UpperCamelCase										=  BertTokenizer.from_pretrained('''hf-internal-testing/tiny-random-BertModel''')

							_UpperCamelCase										=  BlipProcessor(__a    ,		__a)

							processor.save_pretrained(self.tmpdirname)



		def 						UpperCAmelCase							( self    ,		**__a)	->	Tuple:






							'''simple docstring'''
							return AutoProcessor.from_pretrained(self.tmpdirname    ,		**__a).tokenizer



		def 						UpperCAmelCase							( self    ,		**__a)	->	Any:






							'''simple docstring'''
							return AutoProcessor.from_pretrained(self.tmpdirname    ,		**__a).image_processor



		def 						UpperCAmelCase							( self)	->	List[str]:






							'''simple docstring'''
							shutil.rmtree(self.tmpdirname)



		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  [np.random.randint(2_55    ,		size=(3, 30, 4_00)    ,		dtype=np.uinta)]

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

							return image_inputs



		def 						UpperCAmelCase							( self)	->	Optional[int]:






							'''simple docstring'''
							_UpperCamelCase										=  BlipProcessor(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=__a    ,		padding_value=1.0)

							_UpperCamelCase										=  BlipProcessor.from_pretrained(
							    self.tmpdirname    ,		bos_token='''(BOS)'''    ,		eos_token='''(EOS)'''    ,		do_normalize=__a    ,		padding_value=1.0)

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

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



		def 						UpperCAmelCase							( self)	->	List[str]:






							'''simple docstring'''
							_UpperCamelCase										=  self.get_image_processor()
							_UpperCamelCase										=  self.get_tokenizer()

							_UpperCamelCase										=  BlipProcessor(tokenizer=__a    ,		image_processor=__a)

							_UpperCamelCase										=  self.prepare_image_inputs()

							_UpperCamelCase										=  image_processor(__a    ,		return_tensors='''np''')
							_UpperCamelCase										=  processor(images=__a    ,		return_tensors='''np''')

							for key in input_feat_extract.keys():
												self.assertAlmostEqual(input_feat_extract[key].sum()    ,		input_processor[key].sum()    ,		delta=1e-2)



		def 						UpperCAmelCase							( self)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  self.get_image_processor()
							_UpperCamelCase										=  self.get_tokenizer()

							_UpperCamelCase										=  BlipProcessor(tokenizer=__a    ,		image_processor=__a)

							_UpperCamelCase										=  '''lower newer'''

							_UpperCamelCase										=  processor(text=__a)

							_UpperCamelCase										=  tokenizer(__a    ,		return_token_type_ids=__a)

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



		def 						UpperCAmelCase							( self)	->	Tuple:






							'''simple docstring'''
							_UpperCamelCase										=  self.get_image_processor()
							_UpperCamelCase										=  self.get_tokenizer()

							_UpperCamelCase										=  BlipProcessor(tokenizer=__a    ,		image_processor=__a)

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

							_UpperCamelCase										=  processor(text=__a    ,		images=__a)

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

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



		def 						UpperCAmelCase							( self)	->	str:






							'''simple docstring'''
							_UpperCamelCase										=  self.get_image_processor()
							_UpperCamelCase										=  self.get_tokenizer()

							_UpperCamelCase										=  BlipProcessor(tokenizer=__a    ,		image_processor=__a)

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

							_UpperCamelCase										=  processor.batch_decode(__a)
							_UpperCamelCase										=  tokenizer.batch_decode(__a)

							self.assertListEqual(__a    ,		__a)




		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  self.get_image_processor()
							_UpperCamelCase										=  self.get_tokenizer()

							_UpperCamelCase										=  BlipProcessor(tokenizer=__a    ,		image_processor=__a)

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

							_UpperCamelCase										=  processor(text=__a    ,		images=__a)

							# For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
							self.assertListEqual(list(inputs.keys())    ,		['''pixel_values''', '''input_ids''', '''attention_mask'''])








"""simple docstring"""





def 				lowerCamelCase__		(      __snake_case,					__snake_case     )							->		str:


					"""simple docstring"""





					if number < 0 or shift_amount < 0:
										raise ValueError('''both inputs must be positive integers'''     )

					_UpperCamelCase										=  str(bin(__snake_case     )     )
					binary_number += "0" * shift_amount
					return binary_number



def 				lowerCamelCase__		(      __snake_case,					__snake_case     )							->		str:


					"""simple docstring"""





					if number < 0 or shift_amount < 0:
										raise ValueError('''both inputs must be positive integers'''     )

					_UpperCamelCase										=  str(bin(__snake_case     )     )[2:]
					if shift_amount >= len(__snake_case     ):
										return "0b0"
					_UpperCamelCase										=  binary_number[: len(__snake_case     ) - shift_amount]
					return "0b" + shifted_binary_number



def 				lowerCamelCase__		(      __snake_case,					__snake_case     )							->		str:


					"""simple docstring"""





					if number >= 0:  # Get binary representation of positive number
										_UpperCamelCase										=  '''0''' + str(bin(__snake_case     )     ).strip('''-'''     )[2:]
					else:  # Get binary (2's complement) representation of negative number
										_UpperCamelCase										=  len(bin(__snake_case     )[3:]     )  # Find 2's complement of number
										_UpperCamelCase										=  bin(abs(__snake_case     ) - (1 << binary_number_length)     )[3:]
										_UpperCamelCase										=  (
										    '''1''' + '''0''' * (binary_number_length - len(__snake_case     )) + binary_number
										)

					if shift_amount >= len(__snake_case     ):
										return "0b" + binary_number[0] * len(__snake_case     )
					return (
					    "0b"
					    + binary_number[0] * shift_amount
					    + binary_number[: len(__snake_case     ) - shift_amount]
					)


if __name__ == "__main__":
				import doctest

				doctest.testmod()






