





"""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,
      BertTokenizerFast,
      BlipImageProcessor,
      GPTaTokenizer,
      InstructBlipProcessor,
      PreTrainedTokenizerFast,
  )






@require_vision
class  _SCREAMING_SNAKE_CASE(       unittest.TestCase  ):




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  tempfile.mkdtemp()

             __SCREAMING_SNAKE_CASE       :str		    =  BlipImageProcessor()
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model'''							)
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  BertTokenizerFast.from_pretrained('''hf-internal-testing/tiny-random-bert'''							)

             __SCREAMING_SNAKE_CASE       :str		    =  InstructBlipProcessor(_lowerCAmelCase		,_lowerCAmelCase		,_lowerCAmelCase							)

             processor.save_pretrained(self.tmpdirname							)




       def    _UpperCamelCase					(						self		,**SCREAMING_SNAKE_CASE__							)      ->				Tuple:





             """simple docstring"""






             return AutoProcessor.from_pretrained(self.tmpdirname		,**_lowerCAmelCase							).tokenizer




       def    _UpperCamelCase					(						self		,**SCREAMING_SNAKE_CASE__							)      ->				List[str]:





             """simple docstring"""






             return AutoProcessor.from_pretrained(self.tmpdirname		,**_lowerCAmelCase							).image_processor




       def    _UpperCamelCase					(						self		,**SCREAMING_SNAKE_CASE__							)      ->				Tuple:





             """simple docstring"""






             return AutoProcessor.from_pretrained(self.tmpdirname		,**_lowerCAmelCase							).qformer_tokenizer




       def    _UpperCamelCase					(						self							)      ->				Tuple:





             """simple docstring"""






             shutil.rmtree(self.tmpdirname							)




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





             """simple docstring"""






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

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

             return image_inputs




       def    _UpperCamelCase					(						self							)      ->				str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  InstructBlipProcessor(
                 tokenizer=self.get_tokenizer()		,image_processor=self.get_image_processor()		,qformer_tokenizer=self.get_qformer_tokenizer()		,)
             processor.save_pretrained(self.tmpdirname							)

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

             __SCREAMING_SNAKE_CASE       :List[str]		    =  InstructBlipProcessor.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							)
             self.assertIsInstance(processor.qformer_tokenizer		,_lowerCAmelCase							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  self.get_image_processor()
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  self.get_tokenizer()
             __SCREAMING_SNAKE_CASE       :Dict		    =  self.get_qformer_tokenizer()

             __SCREAMING_SNAKE_CASE       :str		    =  InstructBlipProcessor(
                 tokenizer=_lowerCAmelCase		,image_processor=_lowerCAmelCase		,qformer_tokenizer=_lowerCAmelCase							)

             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  self.prepare_image_inputs()

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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[Any]		    =  self.get_image_processor()
             __SCREAMING_SNAKE_CASE       :Tuple		    =  self.get_tokenizer()
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  self.get_qformer_tokenizer()

             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  InstructBlipProcessor(
                 tokenizer=_lowerCAmelCase		,image_processor=_lowerCAmelCase		,qformer_tokenizer=_lowerCAmelCase							)

             __SCREAMING_SNAKE_CASE       :Any		    =  """lower newer"""

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

             __SCREAMING_SNAKE_CASE       :List[str]		    =  tokenizer(_lowerCAmelCase		,return_token_type_ids=_lowerCAmelCase							)
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  qformer_tokenizer(_lowerCAmelCase		,return_token_type_ids=_lowerCAmelCase							)

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

             for key in encoded_tokens_qformer.keys():
                   self.assertListEqual(encoded_tokens_qformer[key]		,encoded_processor['''qformer_''' + key]							)




       def    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Dict		    =  self.get_image_processor()
             __SCREAMING_SNAKE_CASE       :Tuple		    =  self.get_tokenizer()
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  self.get_qformer_tokenizer()

             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  InstructBlipProcessor(
                 tokenizer=_lowerCAmelCase		,image_processor=_lowerCAmelCase		,qformer_tokenizer=_lowerCAmelCase							)

             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  """lower newer"""
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  self.prepare_image_inputs()

             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  processor(text=_lowerCAmelCase		,images=_lowerCAmelCase							)

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

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




       def    _UpperCamelCase					(						self							)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  self.get_image_processor()
             __SCREAMING_SNAKE_CASE       :Any		    =  self.get_tokenizer()
             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.get_qformer_tokenizer()

             __SCREAMING_SNAKE_CASE       :int		    =  InstructBlipProcessor(
                 tokenizer=_lowerCAmelCase		,image_processor=_lowerCAmelCase		,qformer_tokenizer=_lowerCAmelCase							)

             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  processor.batch_decode(_lowerCAmelCase							)
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  tokenizer.batch_decode(_lowerCAmelCase							)

             self.assertListEqual(_lowerCAmelCase		,_lowerCAmelCase							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  self.get_image_processor()
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  self.get_tokenizer()
             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.get_qformer_tokenizer()

             __SCREAMING_SNAKE_CASE       :str		    =  InstructBlipProcessor(
                 tokenizer=_lowerCAmelCase		,image_processor=_lowerCAmelCase		,qformer_tokenizer=_lowerCAmelCase							)

             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  """lower newer"""
             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.prepare_image_inputs()

             __SCREAMING_SNAKE_CASE       :List[Any]		    =  processor(text=_lowerCAmelCase		,images=_lowerCAmelCase							)

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






"""simple docstring"""
def 					__lowerCamelCase       (							a_					:		int = 10			,       a_					:		int = 22							)      ->				int:
      __SCREAMING_SNAKE_CASE       :Optional[int]		    =  range(1			,       a_							)
      __SCREAMING_SNAKE_CASE       :List[Any]		    =  range(1			,       a_							)
      return sum(
          1 for power in powers for base in bases if len(str(base**power							)							) == power							)


if __name__ == "__main__":
  print(f'{solution(1_0, 2_2) = }')