
import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


__lowerCAmelCase			:     Union[str, Any]           =   get_tests_dir('fixtures')
__lowerCAmelCase			:     Union[str, Any]           =   get_tests_dir('fixtures/dummy_feature_extractor_config.json')
__lowerCAmelCase			:     int           =   get_tests_dir('fixtures/dummy-config.json')




class        UpperCAmelCase_			( unittest.TestCase     ):


  '''simple docstring'''







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







    """simple docstring"""






    __magic_name__				     =       0
  def 							_lowercase							( self			: Optional[Any]    )			->	str:







    """simple docstring"""






    __magic_name__				     =       AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""    )
    self.assertIsInstance(UpperCamelCase__			,							UpperCamelCase__    )
  def 							_lowercase							( self			: List[str]    )			->	Optional[Any]:







    """simple docstring"""






    __magic_name__				     =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__    )
    self.assertIsInstance(UpperCamelCase__			,							UpperCamelCase__    )
  def 							_lowercase							( self			: Tuple    )			->	Optional[Any]:







    """simple docstring"""






    with tempfile.TemporaryDirectory() as tmpdirname:
      __magic_name__				     =       WavaVecaConfig()

      # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__    ).to_dict()

      config_dict.pop("""feature_extractor_type"""    )
      __magic_name__				     =       WavaVecaFeatureExtractor(**UpperCamelCase__    )

      # save in new folder
      model_config.save_pretrained(UpperCamelCase__    )
      config.save_pretrained(UpperCamelCase__    )

      __magic_name__				     =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__    )

      # make sure private variable is not incorrectly saved
      __magic_name__				     =       json.loads(config.to_json_string()    )
      self.assertTrue("""_processor_class""" not in dict_as_saved    )

    self.assertIsInstance(UpperCamelCase__			,							UpperCamelCase__    )
  def 							_lowercase							( self			: List[str]    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__    )
    self.assertIsInstance(UpperCamelCase__			,							UpperCamelCase__    )
  def 							_lowercase							( self			: List[str]    )			->	Any:







    """simple docstring"""






    with self.assertRaisesRegex(
        UpperCamelCase__			,							"""bert-base is not a local folder and is not a valid model identifier"""    ):
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained("""bert-base"""    )
  def 							_lowercase							( self			: Any    )			->	Dict:







    """simple docstring"""






    with self.assertRaisesRegex(
        UpperCamelCase__			,							R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""    ):
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__			,							revision="""aaaaaa"""    )
  def 							_lowercase							( self			: Any    )			->	Dict:







    """simple docstring"""






    with self.assertRaisesRegex(
        UpperCamelCase__			,							"""hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""			,							):
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model"""    )
  def 							_lowercase							( self			: Optional[int]    )			->	Union[str, Any]:







    """simple docstring"""






    with self.assertRaises(UpperCamelCase__    ):
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained(
          """hf-internal-testing/test_dynamic_feature_extractor"""    )
    # If remote code is disabled, we can't load this config.
    with self.assertRaises(UpperCamelCase__    ):
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained(
          """hf-internal-testing/test_dynamic_feature_extractor"""			,							trust_remote_code=UpperCamelCase__    )

    __magic_name__				     =       AutoFeatureExtractor.from_pretrained(
        """hf-internal-testing/test_dynamic_feature_extractor"""			,							trust_remote_code=UpperCamelCase__    )
    self.assertEqual(feature_extractor.__class__.__name__			,							"""NewFeatureExtractor"""    )

    # Test feature extractor can be reloaded.
    with tempfile.TemporaryDirectory() as tmp_dir:
      feature_extractor.save_pretrained(UpperCamelCase__    )
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__			,							trust_remote_code=UpperCamelCase__    )
    self.assertEqual(reloaded_feature_extractor.__class__.__name__			,							"""NewFeatureExtractor"""    )
  def 							_lowercase							( self			: List[Any]    )			->	Optional[int]:







    """simple docstring"""






    try:
      AutoConfig.register("""custom"""			,							UpperCamelCase__    )
      AutoFeatureExtractor.register(UpperCamelCase__			,							UpperCamelCase__    )
      # Trying to register something existing in the Transformers library will raise an error
      with self.assertRaises(UpperCamelCase__    ):
        AutoFeatureExtractor.register(UpperCamelCase__			,							UpperCamelCase__    )

      # Now that the config is registered, it can be used as any other config with the auto-API
      __magic_name__				     =       CustomFeatureExtractor.from_pretrained(UpperCamelCase__    )
      with tempfile.TemporaryDirectory() as tmp_dir:
        feature_extractor.save_pretrained(UpperCamelCase__    )
        __magic_name__				     =       AutoFeatureExtractor.from_pretrained(UpperCamelCase__    )
        self.assertIsInstance(UpperCamelCase__			,							UpperCamelCase__    )

    finally:
      if "custom" in CONFIG_MAPPING._extra_content:
        del CONFIG_MAPPING._extra_content["custom"]
      if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
        del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]





  def 							_lowercase							( self			: Optional[int]    )			->	Any:







    """simple docstring"""






    class        UpperCAmelCase_			( _A     ):


      '''simple docstring'''







      a__           =		True

    try:
      AutoConfig.register("""custom"""			,							UpperCamelCase__    )
      AutoFeatureExtractor.register(UpperCamelCase__			,							UpperCamelCase__    )
      # If remote code is not set, the default is to use local
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained(
          """hf-internal-testing/test_dynamic_feature_extractor"""    )
      self.assertEqual(feature_extractor.__class__.__name__			,							"""NewFeatureExtractor"""    )
      self.assertTrue(feature_extractor.is_local    )

      # If remote code is disabled, we load the local one.
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained(
          """hf-internal-testing/test_dynamic_feature_extractor"""			,							trust_remote_code=UpperCamelCase__    )
      self.assertEqual(feature_extractor.__class__.__name__			,							"""NewFeatureExtractor"""    )
      self.assertTrue(feature_extractor.is_local    )

      # If remote is enabled, we load from the Hub
      __magic_name__				     =       AutoFeatureExtractor.from_pretrained(
          """hf-internal-testing/test_dynamic_feature_extractor"""			,							trust_remote_code=UpperCamelCase__    )
      self.assertEqual(feature_extractor.__class__.__name__			,							"""NewFeatureExtractor"""    )
      self.assertTrue(not hasattr(UpperCamelCase__			,							"""is_local"""    )    )

    finally:
      if "custom" in CONFIG_MAPPING._extra_content:
        del CONFIG_MAPPING._extra_content["custom"]
      if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
        del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]



def a__    (       A_					):






  '''simple docstring'''





  if not isinstance(A_,     A_					):
    raise ValueError("""Input series is not valid, valid series - [2, 4, 6]"""					)
  if len(A_					) == 0:
    raise ValueError("""Input list must be a non empty list"""					)
  if len(A_					) == 1:
    return True
  __magic_name__				     =       series[1] - series[0]
  for index in range(len(A_					) - 1					):
    if series[index + 1] - series[index] != common_diff:
      return False
  return True

def a__    (       A_					):






  '''simple docstring'''





  if not isinstance(A_,     A_					):
    raise ValueError("""Input series is not valid, valid series - [2, 4, 6]"""					)
  if len(A_					) == 0:
    raise ValueError("""Input list must be a non empty list"""					)
  __magic_name__				     =       0
  for val in series:
    answer += val
  return answer / len(A_					)


if __name__ == "__main__":
    import doctest

    doctest.testmod()

