


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        _snake_case       (      self		)->List[Any]:




         '''simple docstring'''





         A_      :				List[Any]		     =					tempfile.mkdtemp()

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

         A_      :				int		     =					BlipaProcessor(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)

         processor.save_pretrained(self.tmpdirname		)



       def        _snake_case       (      self				,       **_SCREAMING_SNAKE_CASE		)->Tuple:




         '''simple docstring'''





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



       def        _snake_case       (      self				,       **_SCREAMING_SNAKE_CASE		)->str:




         '''simple docstring'''





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



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




         '''simple docstring'''





         shutil.rmtree(self.tmpdirname		)



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




         '''simple docstring'''





         A_      :				Any		     =					[np.random.randint(255				,       size=(3, 30, 400)				,       dtype=np.uinta		)]

         A_      :				Optional[int]		     =					[Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE				,       0				,       -1		)		) for x in image_inputs]

         return image_inputs



       def        _snake_case       (      self		)->int:




         '''simple docstring'''





         A_      :				Tuple		     =					BlipaProcessor(tokenizer=self.get_tokenizer()				,       image_processor=self.get_image_processor()		)
         processor.save_pretrained(self.tmpdirname		)

         A_      :				Union[str, Any]		     =					self.get_tokenizer(bos_token='''(BOS)'''				,       eos_token='''(EOS)'''		)
         A_      :				Optional[Any]		     =					self.get_image_processor(do_normalize=_SCREAMING_SNAKE_CASE				,       padding_value=1.0		)

         A_      :				List[str]		     =					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        _snake_case       (      self		)->Dict:




         '''simple docstring'''





         A_      :				Tuple		     =					self.get_image_processor()
         A_      :				Any		     =					self.get_tokenizer()

         A_      :				int		     =					BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE				,       image_processor=_SCREAMING_SNAKE_CASE		)

         A_      :				Optional[int]		     =					self.prepare_image_inputs()

         A_      :				Optional[int]		     =					image_processor(_SCREAMING_SNAKE_CASE				,       return_tensors='''np'''		)
         A_      :				int		     =					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        _snake_case       (      self		)->Optional[int]:




         '''simple docstring'''





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

         A_      :				int		     =					BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE				,       image_processor=_SCREAMING_SNAKE_CASE		)

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

         A_      :				Union[str, Any]		     =					processor(text=_SCREAMING_SNAKE_CASE		)

         A_      :				List[Any]		     =					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        _snake_case       (      self		)->List[Any]:




         '''simple docstring'''





         A_      :				Dict		     =					self.get_image_processor()
         A_      :				Dict		     =					self.get_tokenizer()

         A_      :				Dict		     =					BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE				,       image_processor=_SCREAMING_SNAKE_CASE		)

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

         A_      :				Optional[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        _snake_case       (      self		)->Optional[Any]:




         '''simple docstring'''





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

         A_      :				Optional[int]		     =					BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE				,       image_processor=_SCREAMING_SNAKE_CASE		)

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

         A_      :				int		     =					processor.batch_decode(_SCREAMING_SNAKE_CASE		)
         A_      :				List[str]		     =					tokenizer.batch_decode(_SCREAMING_SNAKE_CASE		)

         self.assertListEqual(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)







       def        _snake_case       (      self		)->Any:




         '''simple docstring'''





         A_      :				List[str]		     =					self.get_image_processor()
         A_      :				List[str]		     =					self.get_tokenizer()

         A_      :				Optional[Any]		     =					BlipaProcessor(tokenizer=_SCREAMING_SNAKE_CASE				,       image_processor=_SCREAMING_SNAKE_CASE		)

         A_      :				List[Any]		     =					'''lower newer'''
         A_      :				Dict		     =					self.prepare_image_inputs()

         A_      :				Dict		     =					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''']		)








import copy
import tempfile
import unittest

from huggingface_hub import HfFolder, delete_repo
from parameterized import parameterized
from requests.exceptions import HTTPError

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test






class   _lowerCamelCase (					unittest.TestCase			):




       """simple docstring"""



       @parameterized.expand([(None,), ('''foo.json''',)]		)
       def        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE		)->Tuple:




         '''simple docstring'''





         A_      :				Dict		     =					GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE				,       temperature=0.7				,       length_penalty=1.0				,       bad_words_ids=[[1, 2, 3], [4, 5]]				,       )
         with tempfile.TemporaryDirectory() as tmp_dir:
           config.save_pretrained(_SCREAMING_SNAKE_CASE				,       config_name=_SCREAMING_SNAKE_CASE		)
           A_      :				Tuple		     =					GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE				,       config_name=_SCREAMING_SNAKE_CASE		)

         # Checks parameters that were specified
         self.assertEqual(loaded_config.do_sample				,       _SCREAMING_SNAKE_CASE		)
         self.assertEqual(loaded_config.temperature				,       0.7		)
         self.assertEqual(loaded_config.length_penalty				,       1.0		)
         self.assertEqual(loaded_config.bad_words_ids				,       [[1, 2, 3], [4, 5]]		)

         # Checks parameters that were not specified (defaults)
         self.assertEqual(loaded_config.top_k				,       50		)
         self.assertEqual(loaded_config.max_length				,       20		)
         self.assertEqual(loaded_config.max_time				,       _SCREAMING_SNAKE_CASE		)



       def        _snake_case       (      self		)->Dict:




         '''simple docstring'''





         A_      :				str		     =					AutoConfig.from_pretrained('''gpt2'''		)
         A_      :				int		     =					GenerationConfig.from_model_config(_SCREAMING_SNAKE_CASE		)
         A_      :				Dict		     =					GenerationConfig()

         # The generation config has loaded a few non-default parameters from the model config
         self.assertNotEqual(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)

         # One of those parameters is eos_token_id -- check if it matches
         self.assertNotEqual(generation_config_from_model.eos_token_id				,       default_generation_config.eos_token_id		)
         self.assertEqual(generation_config_from_model.eos_token_id				,       model_config.eos_token_id		)



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




         '''simple docstring'''





         A_      :				List[Any]		     =					GenerationConfig()
         A_      :				Optional[Any]		     =					{
             '''max_new_tokens''': 1024,
             '''foo''': '''bar''',
         }
         A_      :				Dict		     =					copy.deepcopy(_SCREAMING_SNAKE_CASE		)
         A_      :				Optional[int]		     =					generation_config.update(**_SCREAMING_SNAKE_CASE		)

         # update_kwargs was not modified (no side effects)
         self.assertEqual(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)

         # update_kwargs was used to update the config on valid attributes
         self.assertEqual(generation_config.max_new_tokens				,       1024		)

         # `.update()` returns a dictionary of unused kwargs
         self.assertEqual(_SCREAMING_SNAKE_CASE				,       {'''foo''': '''bar'''}		)



       def        _snake_case       (      self		)->str:




         '''simple docstring'''





         A_      :				List[str]		     =					GenerationConfig()
         A_      :				int		     =					'''bar'''

         with tempfile.TemporaryDirectory('''test-generation-config'''		) as tmp_dir:
           generation_config.save_pretrained(_SCREAMING_SNAKE_CASE		)

           A_      :				Optional[Any]		     =					GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
         # update_kwargs was used to update the config on valid attributes
         self.assertEqual(new_config.foo				,       '''bar'''		)

         A_      :				Optional[int]		     =					GenerationConfig.from_model_config(_SCREAMING_SNAKE_CASE		)
         assert not hasattr(_SCREAMING_SNAKE_CASE				,       '''foo'''		)  # no new kwargs should be initialized if from config







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




         '''simple docstring'''





         A_      :				str		     =					GenerationConfig()
         self.assertEqual(default_config.temperature				,       1.0		)
         self.assertEqual(default_config.do_sample				,       _SCREAMING_SNAKE_CASE		)
         self.assertEqual(default_config.num_beams				,       1		)

         A_      :				str		     =					GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE				,       temperature=0.7				,       length_penalty=1.0				,       bad_words_ids=[[1, 2, 3], [4, 5]]				,       )
         self.assertEqual(config.temperature				,       0.7		)
         self.assertEqual(config.do_sample				,       _SCREAMING_SNAKE_CASE		)
         self.assertEqual(config.num_beams				,       1		)

         with tempfile.TemporaryDirectory() as tmp_dir:
           config.save_pretrained(_SCREAMING_SNAKE_CASE		)
           A_      :				Dict		     =					GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE				,       temperature=1.0		)

         self.assertEqual(loaded_config.temperature				,       1.0		)
         self.assertEqual(loaded_config.do_sample				,       _SCREAMING_SNAKE_CASE		)
         self.assertEqual(loaded_config.num_beams				,       1		)  # default value






@is_staging_test
class   _lowerCamelCase (					unittest.TestCase			):




       """simple docstring"""



       @classmethod
       def        _snake_case       (      cls		)->Optional[int]:




         '''simple docstring'''





         A_      :				str		     =					TOKEN
         HfFolder.save_token(_SCREAMING_SNAKE_CASE		)



       @classmethod
       def        _snake_case       (      cls		)->List[Any]:




         '''simple docstring'''





         try:
           delete_repo(token=cls._token				,       repo_id='''test-generation-config'''		)
         except HTTPError:
           pass

         try:
           delete_repo(token=cls._token				,       repo_id='''valid_org/test-generation-config-org'''		)
         except HTTPError:
           pass



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




         '''simple docstring'''





         A_      :				List[Any]		     =					GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE				,       temperature=0.7				,       length_penalty=1.0				,       )
         config.push_to_hub('''test-generation-config'''				,       use_auth_token=self._token		)

         A_      :				Optional[int]		     =					GenerationConfig.from_pretrained(F'''{USER}/test-generation-config'''		)
         for k, v in config.to_dict().items():
           if k != "transformers_version":
             self.assertEqual(_SCREAMING_SNAKE_CASE				,       getattr(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)		)

        # Reset repo
         delete_repo(token=self._token				,       repo_id='''test-generation-config'''		)

         # Push to hub via save_pretrained
         with tempfile.TemporaryDirectory() as tmp_dir:
           config.save_pretrained(
               _SCREAMING_SNAKE_CASE				,       repo_id='''test-generation-config'''				,       push_to_hub=_SCREAMING_SNAKE_CASE				,       use_auth_token=self._token		)

         A_      :				Optional[Any]		     =					GenerationConfig.from_pretrained(F'''{USER}/test-generation-config'''		)
         for k, v in config.to_dict().items():
           if k != "transformers_version":
             self.assertEqual(_SCREAMING_SNAKE_CASE				,       getattr(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)		)







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




         '''simple docstring'''





         A_      :				Tuple		     =					GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE				,       temperature=0.7				,       length_penalty=1.0				,       )
         config.push_to_hub('''valid_org/test-generation-config-org'''				,       use_auth_token=self._token		)

         A_      :				Union[str, Any]		     =					GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''		)
         for k, v in config.to_dict().items():
           if k != "transformers_version":
             self.assertEqual(_SCREAMING_SNAKE_CASE				,       getattr(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)		)

        # Reset repo
         delete_repo(token=self._token				,       repo_id='''valid_org/test-generation-config-org'''		)

         # Push to hub via save_pretrained
         with tempfile.TemporaryDirectory() as tmp_dir:
           config.save_pretrained(
               _SCREAMING_SNAKE_CASE				,       repo_id='''valid_org/test-generation-config-org'''				,       push_to_hub=_SCREAMING_SNAKE_CASE				,       use_auth_token=self._token		)

         A_      :				Dict		     =					GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''		)
         for k, v in config.to_dict().items():
           if k != "transformers_version":
             self.assertEqual(_SCREAMING_SNAKE_CASE				,       getattr(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)		)




