






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      :       str    =						get_tests_dir('''fixtures''')
lowerCamelCase      :       Tuple    =						get_tests_dir('''fixtures/dummy_feature_extractor_config.json''')
lowerCamelCase      :       Tuple    =						get_tests_dir('''fixtures/dummy-config.json''')



class 				_UpperCamelCase      (unittest.TestCase      ):


      def 			__UpperCAmelCase		(						self						)->						Optional[Any]:
           __lowerCAmelCase             =       0


      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h"						)
           self.assertIsInstance(__lowercase       ,    __lowercase						)


      def 			__UpperCAmelCase		(						self						)->						Union[str, Any]:
           __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained(__lowercase						)
           self.assertIsInstance(__lowercase       ,    __lowercase						)


      def 			__UpperCAmelCase		(						self						)->						Any:







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

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

                config_dict.pop("feature_extractor_type"						)
                __lowerCAmelCase             =       WavaVecaFeatureExtractor(**__lowercase						)

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

                __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained(__lowercase						)

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

           self.assertIsInstance(__lowercase       ,    __lowercase						)


      def 			__UpperCAmelCase		(						self						)->						Optional[int]:
           __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained(__lowercase						)
           self.assertIsInstance(__lowercase       ,    __lowercase						)


      def 			__UpperCAmelCase		(						self						)->						Tuple:







           with self.assertRaisesRegex(
               __lowercase       ,    "bert-base is not a local folder and is not a valid model identifier"						):
                __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained("bert-base"						)


      def 			__UpperCAmelCase		(						self						)->						Dict:







           with self.assertRaisesRegex(
               __lowercase       ,    R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"						):
                __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained(__lowercase       ,    revision="aaaaaa"						)


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







           with self.assertRaisesRegex(
               __lowercase       ,    "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."       ,    ):
                __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained("hf-internal-testing/config-no-model"						)


      def 			__UpperCAmelCase		(						self						)->						Dict:







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

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

           # Test feature extractor can be reloaded.
           with tempfile.TemporaryDirectory() as tmp_dir:
                feature_extractor.save_pretrained(__lowercase						)
                __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained(__lowercase       ,    trust_remote_code=__lowercase						)
           self.assertEqual(reloaded_feature_extractor.__class__.__name__       ,    "NewFeatureExtractor"						)


      def 			__UpperCAmelCase		(						self						)->						Tuple:







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

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

           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 			__UpperCAmelCase		(						self						)->						Dict:



           class 				_UpperCamelCase      (snake_case_      ):
                 snake_case_	=				True

           try:
                AutoConfig.register("custom"       ,    __lowercase						)
                AutoFeatureExtractor.register(__lowercase       ,    __lowercase						)
                # If remote code is not set, the default is to use local
                __lowerCAmelCase             =       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.
                __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained(
                    "hf-internal-testing/test_dynamic_feature_extractor"       ,    trust_remote_code=__lowercase						)
                self.assertEqual(feature_extractor.__class__.__name__       ,    "NewFeatureExtractor"						)
                self.assertTrue(feature_extractor.is_local						)

                # If remote is enabled, we load from the Hub
                __lowerCAmelCase             =       AutoFeatureExtractor.from_pretrained(
                    "hf-internal-testing/test_dynamic_feature_extractor"       ,    trust_remote_code=__lowercase						)
                self.assertEqual(feature_extractor.__class__.__name__       ,    "NewFeatureExtractor"						)
                self.assertTrue(not hasattr(__lowercase       ,    "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]










'''simple docstring'''
# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import ArgumentParser

from accelerate.commands.config import get_config_parser
from accelerate.commands.env import env_command_parser
from accelerate.commands.launch import launch_command_parser
from accelerate.commands.test import test_command_parser
from accelerate.commands.tpu import tpu_command_parser


def       _lowercase							(					):
 __A	:		Dict	  =      ArgumentParser('Accelerate CLI tool',					usage='accelerate <command> [<args>]',					allow_abbrev=UpperCamelCase__      )
 __A	:		Any	  =      parser.add_subparsers(help='accelerate command helpers'      )

 # Register commands
 get_config_parser(subparsers=UpperCamelCase__      )
 env_command_parser(subparsers=UpperCamelCase__      )
 launch_command_parser(subparsers=UpperCamelCase__      )
 tpu_command_parser(subparsers=UpperCamelCase__      )
 test_command_parser(subparsers=UpperCamelCase__      )

 # Let's go
 __A	:		Optional[Any]	  =      parser.parse_args()

 if not hasattr(UpperCamelCase__,					'func'      ):
  parser.print_help()
  exit(1      )

 # Run
 args.func(UpperCamelCase__      )


if __name__ == "__main__":
 main()






