






'''simple docstring'''






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






       @parameterized.expand([(None,), ('foo.json',)])
       def       lowerCAmelCase		(      self  ,							snake_case)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       List[str]							   =GenerationConfig(
              do_sample=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(snake_case  ,							config_name=snake_case)
             _UpperCAmelCase       :       str							   =GenerationConfig.from_pretrained(snake_case  ,							config_name=snake_case)

          # Checks parameters that were specified
          self.assertEqual(loaded_config.do_sample  ,							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  ,							5_0)
          self.assertEqual(loaded_config.max_length  ,							2_0)
          self.assertEqual(loaded_config.max_time  ,							snake_case)






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




          '''simple docstring'''
          _UpperCAmelCase       :       Tuple							   =AutoConfig.from_pretrained('gpt2')
          _UpperCAmelCase       :       Optional[int]							   =GenerationConfig.from_model_config(snake_case)
          _UpperCAmelCase       :       str							   =GenerationConfig()

          # The generation config has loaded a few non-default parameters from the model config
          self.assertNotEqual(snake_case  ,							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       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =GenerationConfig()
          _UpperCAmelCase       :       Any							   ={
              'max_new_tokens': 1_0_2_4,
              'foo': 'bar',
          }
          _UpperCAmelCase       :       Dict							   =copy.deepcopy(snake_case)
          _UpperCAmelCase       :       Tuple							   =generation_config.update(**snake_case)

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

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

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






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




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =GenerationConfig()
          _UpperCAmelCase       :       Dict							   ='bar'

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

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

          _UpperCAmelCase       :       List[Any]							   =GenerationConfig.from_model_config(snake_case)
          assert not hasattr(snake_case  ,							'foo')  # no new kwargs should be initialized if from config






       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =GenerationConfig()
          self.assertEqual(default_config.temperature  ,							1.0)
          self.assertEqual(default_config.do_sample  ,							snake_case)
          self.assertEqual(default_config.num_beams  ,							1)

          _UpperCAmelCase       :       str							   =GenerationConfig(
              do_sample=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  ,							snake_case)
          self.assertEqual(config.num_beams  ,							1)

          with tempfile.TemporaryDirectory() as tmp_dir:
             config.save_pretrained(snake_case)
             _UpperCAmelCase       :       str							   =GenerationConfig.from_pretrained(snake_case  ,							temperature=1.0)

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






@is_staging_test
class  __magic_name__							(		unittest.TestCase    ):






       @classmethod
       def       lowerCAmelCase		(      cls)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =TOKEN
          HfFolder.save_token(snake_case)






       @classmethod
       def       lowerCAmelCase		(      cls)						->  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       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =GenerationConfig(
              do_sample=snake_case  ,							temperature=0.7  ,							length_penalty=1.0  ,							)
          config.push_to_hub('test-generation-config'  ,							use_auth_token=self._token)

          _UpperCAmelCase       :       Dict							   =GenerationConfig.from_pretrained(f"{USER}/test-generation-config")
          for k, v in config.to_dict().items():
             if k != "transformers_version":
                self.assertEqual(snake_case  ,							getattr(snake_case  ,							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(
                 snake_case  ,							repo_id='test-generation-config'  ,							push_to_hub=snake_case  ,							use_auth_token=self._token)

          _UpperCAmelCase       :       Any							   =GenerationConfig.from_pretrained(f"{USER}/test-generation-config")
          for k, v in config.to_dict().items():
             if k != "transformers_version":
                self.assertEqual(snake_case  ,							getattr(snake_case  ,							snake_case))






       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =GenerationConfig(
              do_sample=snake_case  ,							temperature=0.7  ,							length_penalty=1.0  ,							)
          config.push_to_hub('valid_org/test-generation-config-org'  ,							use_auth_token=self._token)

          _UpperCAmelCase       :       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(snake_case  ,							getattr(snake_case  ,							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(
                 snake_case  ,							repo_id='valid_org/test-generation-config-org'  ,							push_to_hub=snake_case  ,							use_auth_token=self._token)

          _UpperCAmelCase       :       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(snake_case  ,							getattr(snake_case  ,							snake_case))





'''simple docstring'''






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
 from transformers import TvltImageProcessor

if is_speech_available():
 from transformers import TvltFeatureExtractor

from transformers import TvltProcessor






@require_torch
class  __magic_name__							(		unittest.TestCase    ):






       def       lowerCAmelCase		(      self)						->  str:




          '''simple docstring'''
          _UpperCAmelCase       :       Dict							   ='ZinengTang/tvlt-base'
          _UpperCAmelCase       :       Dict							   =tempfile.mkdtemp()






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  Union[str, Any]:




          '''simple docstring'''
          return TvltImageProcessor.from_pretrained(self.checkpoint  ,							**snake_case)






       def       lowerCAmelCase		(      self  ,							**snake_case)						->  Dict:




          '''simple docstring'''
          return TvltFeatureExtractor.from_pretrained(self.checkpoint  ,							**snake_case)






       def       lowerCAmelCase		(      self)						->  Any:




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






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




          '''simple docstring'''
          _UpperCAmelCase       :       Any							   =self.get_image_processor()
          _UpperCAmelCase       :       Optional[Any]							   =self.get_feature_extractor()

          _UpperCAmelCase       :       str							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)
          processor.save_pretrained(self.tmpdirname)
          _UpperCAmelCase       :       str							   =TvltProcessor.from_pretrained(self.tmpdirname)

          self.assertIsInstance(processor.feature_extractor  ,							snake_case)
          self.assertIsInstance(processor.image_processor  ,							snake_case)






       def       lowerCAmelCase		(      self)						->  Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =self.get_image_processor()
          _UpperCAmelCase       :       List[Any]							   =self.get_feature_extractor()

          _UpperCAmelCase       :       str							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          _UpperCAmelCase       :       Optional[int]							   =np.ones([1_2_0_0_0])

          _UpperCAmelCase       :       str							   =feature_extractor(snake_case  ,							return_tensors='np')
          _UpperCAmelCase       :       Dict							   =processor(audio=snake_case  ,							return_tensors='np')

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






       def       lowerCAmelCase		(      self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Dict							   =self.get_image_processor()
          _UpperCAmelCase       :       int							   =self.get_feature_extractor()

          _UpperCAmelCase       :       int							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          _UpperCAmelCase       :       Union[str, Any]							   =np.ones([3, 2_2_4, 2_2_4])

          _UpperCAmelCase       :       Optional[Any]							   =image_processor(snake_case  ,							return_tensors='np')
          _UpperCAmelCase       :       List[Any]							   =processor(images=snake_case  ,							return_tensors='np')

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






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




          '''simple docstring'''
          _UpperCAmelCase       :       List[str]							   =self.get_image_processor()
          _UpperCAmelCase       :       Dict							   =self.get_feature_extractor()

          _UpperCAmelCase       :       Optional[int]							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          _UpperCAmelCase       :       Optional[int]							   =np.ones([1_2_0_0_0])
          _UpperCAmelCase       :       str							   =np.ones([3, 2_2_4, 2_2_4])

          _UpperCAmelCase       :       Optional[int]							   =processor(audio=snake_case  ,							images=snake_case)

          self.assertListEqual(list(inputs.keys())  ,							['audio_values', 'audio_mask', 'pixel_values', 'pixel_mask'])

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






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




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[int]							   =self.get_image_processor()
          _UpperCAmelCase       :       Tuple							   =self.get_feature_extractor()

          _UpperCAmelCase       :       Dict							   =TvltProcessor(image_processor=snake_case  ,							feature_extractor=snake_case)

          self.assertListEqual(
              processor.model_input_names  ,							image_processor.model_input_names + feature_extractor.model_input_names  ,							msg='`processor` and `image_processor`+`feature_extractor` model input names do not match'  ,							)

