


'''simple docstring'''



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

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


A__						:   Dict				=    threading.Lock()
A__						:   Union[str, Any]				=    None

A__						:   Optional[int]				=    {
    """debug""": logging.DEBUG,
    """info""": logging.INFO,
    """warning""": logging.WARNING,
    """error""": logging.ERROR,
    """critical""": logging.CRITICAL,
}

A__						:   Optional[int]				=    logging.WARNING

A__						:   Union[str, Any]				=    True



def      UpperCAmelCase__   (      )	->						Optional[int]:
     __lowerCamelCase  :      Any														=						os.getenv('TRANSFORMERS_VERBOSITY' ,       __lowercase				)
     if env_level_str:
          if env_level_str in log_levels:
               return log_levels[env_level_str]
          else:
               logging.getLogger().warning(
                   F'Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, '
                   F'has to be one of: { ", ".join(log_levels.keys()				) }'				)
     return _default_log_level



def      UpperCAmelCase__   (      )	->						Dict:
     return __name__.split('.'				)[0]



def      UpperCAmelCase__   (      )	->						Tuple:
     return logging.getLogger(_get_library_name()				)



def      UpperCAmelCase__   (      )	->						Optional[int]:
     global _default_handler

     with _lock:
          if _default_handler:
               # This library has already configured the library root logger.
               return
          __lowerCamelCase  :      Optional[Any]														=						logging.StreamHandler()  # Set sys.stderr as stream.
          __lowerCamelCase  :      Tuple														=						sys.stderr.flush

          # Apply our default configuration to the library root logger.
          __lowerCamelCase  :      Optional[Any]														=						_get_library_root_logger()
          library_root_logger.addHandler(_default_handler				)
          library_root_logger.setLevel(_get_default_logging_level()				)
          __lowerCamelCase  :      str														=						False



def      UpperCAmelCase__   (      )	->						Optional[Any]:
     global _default_handler

     with _lock:
          if not _default_handler:
               return

          __lowerCamelCase  :      List[Any]														=						_get_library_root_logger()
          library_root_logger.removeHandler(_default_handler				)
          library_root_logger.setLevel(logging.NOTSET				)
          __lowerCamelCase  :      str														=						None



def      UpperCAmelCase__   (      )	->						Optional[int]:
     return log_levels



def      UpperCAmelCase__   (      UpperCAmelCase_       :							Dict = None				)	->						List[Any]:

     if name is None:
          __lowerCamelCase  :      Tuple														=						_get_library_name()

     _configure_library_root_logger()
     return logging.getLogger(__lowercase				)



def      UpperCAmelCase__   (      )	->						Dict:
     _configure_library_root_logger()
     return _get_library_root_logger().getEffectiveLevel()



def      UpperCAmelCase__   (      UpperCAmelCase_       :							str				)	->						Tuple:
     _configure_library_root_logger()
     _get_library_root_logger().setLevel(__lowercase				)



def      UpperCAmelCase__   (      )	->						List[str]:
     return set_verbosity(__lowercase				)



def      UpperCAmelCase__   (      )	->						List[str]:
     return set_verbosity(__lowercase				)



def      UpperCAmelCase__   (      )	->						Any:
     return set_verbosity(__lowercase				)



def      UpperCAmelCase__   (      )	->						int:
     return set_verbosity(__lowercase				)



def      UpperCAmelCase__   (      )	->						Dict:
     _configure_library_root_logger()

     assert _default_handler is not None
     _get_library_root_logger().removeHandler(_default_handler				)



def      UpperCAmelCase__   (      )	->						Tuple:
     _configure_library_root_logger()

     assert _default_handler is not None
     _get_library_root_logger().addHandler(_default_handler				)



def      UpperCAmelCase__   (      UpperCAmelCase_       :							Tuple				)	->						Tuple:
     _configure_library_root_logger()

     assert handler is not None
     _get_library_root_logger().addHandler(__lowercase				)



def      UpperCAmelCase__   (      UpperCAmelCase_       :							List[Any]				)	->						Optional[Any]:
     _configure_library_root_logger()

     assert handler is not None and handler not in _get_library_root_logger().handlers
     _get_library_root_logger().removeHandler(__lowercase				)



def      UpperCAmelCase__   (      )	->						Optional[int]:
     _configure_library_root_logger()
     __lowerCamelCase  :      Union[str, Any]														=						False



def      UpperCAmelCase__   (      )	->						Dict:
     _configure_library_root_logger()
     __lowerCamelCase  :      Any														=						True



def      UpperCAmelCase__   (      )	->						int:
     __lowerCamelCase  :      Union[str, Any]														=						_get_library_root_logger().handlers

     for handler in handlers:
          __lowerCamelCase  :      str														=						logging.Formatter('[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s'				)
          handler.setFormatter(__lowercase				)



def      UpperCAmelCase__   (      )	->						List[str]:
     __lowerCamelCase  :      Tuple														=						_get_library_root_logger().handlers

     for handler in handlers:
          handler.setFormatter(__lowercase				)



def      UpperCAmelCase__   (      self       :							int ,       *UpperCAmelCase_       :							List[str] ,       **UpperCAmelCase_       :							Optional[Any]				)	->						int:
     __lowerCamelCase  :      Dict														=						os.getenv('TRANSFORMERS_NO_ADVISORY_WARNINGS' ,       __lowercase				)
     if no_advisory_warnings:
          return
     self.warning(*__lowercase ,       **__lowercase				)


A__						:   List[Any]				=    warning_advice



@functools.lru_cache(__lowercase				)
def      UpperCAmelCase__   (      self       :							Optional[int] ,       *UpperCAmelCase_       :							Tuple ,       **UpperCAmelCase_       :							int				)	->						int:
     self.warning(*__lowercase ,       **__lowercase				)


A__						:   Optional[int]				=    warning_once





class   UpperCAmelCase_    :





    """simple docstring"""






    def __init__(				self     ,	*SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	Tuple:  # pylint: disable=unused-argument
         __lowerCamelCase  :      Any														=						args[0] if args else None







    def __iter__(				self      )							->	Dict:
         return iter(self._iterator      )







    def __getattr__(				self     ,	SCREAMING_SNAKE_CASE_      )							->	Dict:



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

         return empty_fn







    def __enter__(				self      )							->	Tuple:
         return self







    def __exit__(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )							->	int:
         return







class   UpperCAmelCase_    :





    """simple docstring"""






    def __call__(				self     ,	*SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	List[str]:



         if _tqdm_active:
              return tqdm_lib.tqdm(*_lowerCamelCase     ,	**_lowerCamelCase      )
         else:
              return EmptyTqdm(*_lowerCamelCase     ,	**_lowerCamelCase      )







    def  lowercase_							(				self     ,	*SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	List[str]:
         __lowerCamelCase  :      Dict														=						None
         if _tqdm_active:
              return tqdm_lib.tqdm.set_lock(*_lowerCamelCase     ,	**_lowerCamelCase      )







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



         if _tqdm_active:
              return tqdm_lib.tqdm.get_lock()







A__						:   List[str]				=    _tqdm_cls()



def      UpperCAmelCase__   (      )	->						List[Any]:
     global _tqdm_active
     return bool(_tqdm_active				)



def      UpperCAmelCase__   (      )	->						Optional[int]:
     global _tqdm_active
     __lowerCamelCase  :      str														=						True
     hf_hub_utils.enable_progress_bars()



def      UpperCAmelCase__   (      )	->						Union[str, Any]:
     global _tqdm_active
     __lowerCamelCase  :      int														=						False
     hf_hub_utils.disable_progress_bars()








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



     @parameterized.expand([(None,), ('foo.json',)]   )
     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :Any   ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=GenerationConfig(
          do_sample=_lowerCamelCase      ,  temperature=0.7      ,  length_penalty=1.0      ,  bad_words_ids=[[1, 2, 3], [4, 5]]      ,  )
      with tempfile.TemporaryDirectory() as tmp_dir:
       config.save_pretrained(_lowerCamelCase      ,  config_name=_lowerCamelCase   )
       UpperCamelCase_     :					Union[str, Any]   	=GenerationConfig.from_pretrained(_lowerCamelCase      ,  config_name=_lowerCamelCase   )

      # Checks parameters that were specified
      self.assertEqual(loaded_config.do_sample      ,  _lowerCamelCase   )
      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      ,  _lowerCamelCase   )



     def  lowerCamelCase_						(							self :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Optional[int]   	=AutoConfig.from_pretrained('gpt2'   )
      UpperCamelCase_     :					Tuple   	=GenerationConfig.from_model_config(_lowerCamelCase   )
      UpperCamelCase_     :					Optional[int]   	=GenerationConfig()

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

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







      '''simple docstring'''



      UpperCamelCase_     :					str   	=GenerationConfig()
      UpperCamelCase_     :					Optional[Any]   	={
          'max_new_tokens': 1_024,
          'foo': 'bar',
      }
      UpperCamelCase_     :					Optional[Any]   	=copy.deepcopy(_lowerCamelCase   )
      UpperCamelCase_     :					Optional[int]   	=generation_config.update(**_lowerCamelCase   )

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

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

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



     def  lowerCamelCase_						(							self :Union[str, Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Union[str, Any]   	=GenerationConfig()
      UpperCamelCase_     :					Union[str, Any]   	='bar'

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

       UpperCamelCase_     :					Dict   	=GenerationConfig.from_pretrained(_lowerCamelCase   )
      # update_kwargs was used to update the config on valid attributes
      self.assertEqual(new_config.foo      ,  'bar'   )

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




     def  lowerCamelCase_						(							self :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[str]   	=GenerationConfig()
      self.assertEqual(default_config.temperature      ,  1.0   )
      self.assertEqual(default_config.do_sample      ,  _lowerCamelCase   )
      self.assertEqual(default_config.num_beams      ,  1   )

      UpperCamelCase_     :					Tuple   	=GenerationConfig(
          do_sample=_lowerCamelCase      ,  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      ,  _lowerCamelCase   )
      self.assertEqual(config.num_beams      ,  1   )

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

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





@is_staging_test
class 				a__					(						unittest.TestCase       ):



     @classmethod
     def  lowerCamelCase_						(							cls :Optional[Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Dict   	=TOKEN
      HfFolder.save_token(_lowerCamelCase   )



     @classmethod
     def  lowerCamelCase_						(							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  lowerCamelCase_						(							self :List[Any]   ):







      '''simple docstring'''



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

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

        # 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(
           _lowerCamelCase      ,  repo_id='test-generation-config'      ,  push_to_hub=_lowerCamelCase      ,  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(_lowerCamelCase      ,  getattr(_lowerCamelCase      ,  _lowerCamelCase   )   )




     def  lowerCamelCase_						(							self :Union[str, Any]   ):







      '''simple docstring'''



      UpperCamelCase_     :					Union[str, Any]   	=GenerationConfig(
          do_sample=_lowerCamelCase      ,  temperature=0.7      ,  length_penalty=1.0      ,  )
      config.push_to_hub('valid_org/test-generation-config-org'      ,  use_auth_token=self._token   )

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

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

      UpperCamelCase_     :					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(_lowerCamelCase      ,  getattr(_lowerCamelCase      ,  _lowerCamelCase   )   )
