
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 		UpperCamelCase__  (       unittest.TestCase    ):
   @parameterized.expand([(None,), ('''foo.json''',)]   )
   def       lowerCAmelCase			(self :					Tuple					,				snake_case_ :					int   ):
    __a :  List[Any]						=  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_   )
     __a :  List[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 :					Any   ):
    __a :  str						=  AutoConfig.from_pretrained('''gpt2'''   )
    __a :  Tuple						=  GenerationConfig.from_model_config(snake_case_   )
    __a :  int						=  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 :					str   ):
    __a :  List[Any]						=  GenerationConfig()
    __a :  Tuple						=  {
        '''max_new_tokens''': 1_0_2_4,
        '''foo''': '''bar''',
    }
    __a :  List[str]						=  copy.deepcopy(snake_case_   )
    __a :  Optional[int]						=  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 :					str   ):
    __a :  List[Any]						=  GenerationConfig()
    __a :  Dict						=  '''bar'''

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

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

    __a :  Tuple						=  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   ):
    __a :  Dict						=  GenerationConfig()
    self.assertEqual(default_config.temperature					,				1.0   )
    self.assertEqual(default_config.do_sample					,				snake_case_   )
    self.assertEqual(default_config.num_beams					,				1   )

    __a :  List[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_   )
     __a :  Any						=  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 		UpperCamelCase__  (       unittest.TestCase    ):
   @classmethod
   def       lowerCAmelCase			(cls :					int   ):
    __a :  int						=  TOKEN
    HfFolder.save_token(snake_case_   )
   @classmethod
   def       lowerCAmelCase			(cls :					str   ):
    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 :					Tuple   ):
    __a :  Tuple						=  GenerationConfig(
        do_sample=snake_case_					,				temperature=0.7					,				length_penalty=1.0					,				)
    config.push_to_hub('''test-generation-config'''					,				use_auth_token=self._token   )

    __a :  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_   )   )

        # 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   )

    __a :  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_   )   )







   def       lowerCAmelCase			(self :					Optional[Any]   ):
    __a :  Optional[int]						=  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   )

    __a :  Optional[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   )

    __a :  Optional[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_   )   )


import logging
import os
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

from tqdm import auto as tqdm_lib


lowercase__       ={
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL,
}

lowercase__       =logging.WARNING




def    __UpperCamelCase     (				):
 __a :  Optional[Any]						=  os.getenv('''DATASETS_VERBOSITY'''  ,   lowerCAmelCase__  )
 if env_level_str:
  if env_level_str in log_levels:
   return log_levels[env_level_str]
  else:
   logging.getLogger().warning(
       f"Unknown option DATASETS_VERBOSITY={env_level_str}, "
       f"has to be one of: { ', '.join(log_levels.keys()  ) }"  )
 return _default_log_level




def    __UpperCamelCase     (				):
 return __name__.split('''.'''  )[0]




def    __UpperCamelCase     (				):
 return logging.getLogger(_get_library_name()  )




def    __UpperCamelCase     (				):
 # Apply our default configuration to the library root logger.
 __a :  str						=  _get_library_root_logger()
 library_root_logger.setLevel(_get_default_logging_level()  )




def    __UpperCamelCase     (				):
 __a :  Any						=  _get_library_root_logger()
 library_root_logger.setLevel(logging.NOTSET  )




def    __UpperCamelCase     (				lowerCAmelCase__							:				Optional[str] = None  ):
 if name is None:
  __a :  Union[str, Any]						=  _get_library_name()
 return logging.getLogger(lowerCAmelCase__  )




def    __UpperCamelCase     (				):
 return _get_library_root_logger().getEffectiveLevel()




def    __UpperCamelCase     (				lowerCAmelCase__							:				int  ):
 _get_library_root_logger().setLevel(lowerCAmelCase__  )




def    __UpperCamelCase     (				):
 return set_verbosity(lowerCAmelCase__  )




def    __UpperCamelCase     (				):
 return set_verbosity(lowerCAmelCase__  )




def    __UpperCamelCase     (				):
 return set_verbosity(lowerCAmelCase__  )




def    __UpperCamelCase     (				):
 return set_verbosity(lowerCAmelCase__  )




def    __UpperCamelCase     (				):
 __a :  Union[str, Any]						=  False




def    __UpperCamelCase     (				):
 __a :  Tuple						=  True


# Configure the library root logger at the module level (singleton-like)
_configure_library_root_logger()



class 		UpperCamelCase__  :
   def __init__(self :					str					,				*snake_case_ :					str					,				**snake_case_ :					Union[str, Any]   ):  # pylint: disable=unused-argument
    __a :  Optional[Any]						=  args[0] if args else None
   def __iter__(self :					List[str]   ):
    return iter(self._iterator   )
   def __getattr__(self :					str					,				snake_case_ :					Optional[Any]   ):

    def empty_fn(*snake_case_ :					int					,				**snake_case_ :					int   ):  # pylint: disable=unused-argument
     return

    return empty_fn
   def __enter__(self :					Union[str, Any]   ):
    return self







   def __exit__(self :					str					,				snake_case_ :					List[str]					,				snake_case_ :					int					,				snake_case_ :					Optional[Any]   ):
    return





lowercase__       =True



class 		UpperCamelCase__  :
   def __call__(self :					Tuple					,				*snake_case_ :					str					,				snake_case_ :					str=False					,				**snake_case_ :					Dict   ):
    if _tqdm_active and not disable:
     return tqdm_lib.tqdm(*snake_case_					,				**snake_case_   )
    else:
     return EmptyTqdm(*snake_case_					,				**snake_case_   )
   def       lowerCAmelCase			(self :					Optional[Any]					,				*snake_case_ :					Union[str, Any]					,				**snake_case_ :					Optional[Any]   ):
    __a :  List[Any]						=  None
    if _tqdm_active:
     return tqdm_lib.tqdm.set_lock(*snake_case_					,				**snake_case_   )







   def       lowerCAmelCase			(self :					str   ):
    if _tqdm_active:
     return tqdm_lib.tqdm.get_lock()





lowercase__       =_tqdm_cls()




def    __UpperCamelCase     (				):
 global _tqdm_active
 return bool(_tqdm_active  )




def    __UpperCamelCase     (				):
 global _tqdm_active
 __a :  Dict						=  True




def    __UpperCamelCase     (				):
 global _tqdm_active
 __a :  Union[str, Any]						=  False
