
def 				SCREAMING_SNAKE_CASE_   (      UpperCamelCase__		):
      if not nums:  # Makes sure that the list is not empty
            raise ValueError('''List is empty'''		)

      UpperCamelCase__     : Optional[Any]											=					sum(UpperCamelCase__		) / len(UpperCamelCase__		)  # Calculate the average
      return sum(abs(x - average		) for x in nums		) / len(UpperCamelCase__		)


if __name__ == "__main__":
    import doctest

    doctest.testmod()




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, BlipaProcessor, BlipImageProcessor, GPTaTokenizer, PreTrainedTokenizerFast




@require_vision
class 					_lowerCamelCase			(   unittest.TestCase			):





     """simple docstring"""







     def  __SCREAMING_SNAKE_CASE      (				self	)	-> int:




           """simple docstring"""







           UpperCamelCase__     : int											=					tempfile.mkdtemp()

           UpperCamelCase__     : str											=					BlipImageProcessor()
           UpperCamelCase__     : List[Any]											=					GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model'''	)

           UpperCamelCase__     : List[Any]											=					BlipaProcessor(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)

           processor.save_pretrained(self.tmpdirname	)






     def  __SCREAMING_SNAKE_CASE      (				self    ,    **__SCREAMING_SNAKE_CASE	)	-> int:




           """simple docstring"""







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






     def  __SCREAMING_SNAKE_CASE      (				self    ,    **__SCREAMING_SNAKE_CASE	)	-> int:




           """simple docstring"""







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






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







           shutil.rmtree(self.tmpdirname	)






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




           """simple docstring"""







           UpperCamelCase__     : List[Any]											=					[np.random.randint(2_5_5    ,    size=(3, 3_0, 4_0_0)    ,    dtype=np.uinta	)]

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

           return image_inputs






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




           """simple docstring"""







           UpperCamelCase__     : str											=					BlipaProcessor(tokenizer=self.get_tokenizer()    ,    image_processor=self.get_image_processor()	)
           processor.save_pretrained(self.tmpdirname	)

           UpperCamelCase__     : Any											=					self.get_tokenizer(bos_token='''(BOS)'''    ,    eos_token='''(EOS)'''	)
           UpperCamelCase__     : str											=					self.get_image_processor(do_normalize=__SCREAMING_SNAKE_CASE    ,    padding_value=1.0	)

           UpperCamelCase__     : Optional[Any]											=					BlipaProcessor.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  __SCREAMING_SNAKE_CASE      (				self	)	-> List[Any]:




           """simple docstring"""







           UpperCamelCase__     : Dict											=					self.get_image_processor()
           UpperCamelCase__     : Tuple											=					self.get_tokenizer()

           UpperCamelCase__     : int											=					BlipaProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,    image_processor=__SCREAMING_SNAKE_CASE	)

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

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




           """simple docstring"""







           UpperCamelCase__     : str											=					self.get_image_processor()
           UpperCamelCase__     : Any											=					self.get_tokenizer()

           UpperCamelCase__     : Tuple											=					BlipaProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,    image_processor=__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : List[str]											=					'''lower newer'''

           UpperCamelCase__     : Tuple											=					processor(text=__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : int											=					tokenizer(__SCREAMING_SNAKE_CASE    ,    return_token_type_ids=__SCREAMING_SNAKE_CASE	)

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






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







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

           UpperCamelCase__     : Dict											=					BlipaProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,    image_processor=__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : Tuple											=					'''lower newer'''
           UpperCamelCase__     : Any											=					self.prepare_image_inputs()

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

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

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






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> int:




           """simple docstring"""







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

           UpperCamelCase__     : Tuple											=					BlipaProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,    image_processor=__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : List[Any]											=					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

           UpperCamelCase__     : Tuple											=					processor.batch_decode(__SCREAMING_SNAKE_CASE	)
           UpperCamelCase__     : List[str]											=					tokenizer.batch_decode(__SCREAMING_SNAKE_CASE	)

           self.assertListEqual(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







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

           UpperCamelCase__     : int											=					BlipaProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,    image_processor=__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : List[Any]											=					'''lower newer'''
           UpperCamelCase__     : List[str]											=					self.prepare_image_inputs()

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

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


