




'''simple docstring'''







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


A     =get_tests_dir('fixtures')
A     =get_tests_dir('fixtures/dummy_feature_extractor_config.json')
A     =get_tests_dir('fixtures/dummy-config.json')


class 			_a  (	unittest.TestCase ):
       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		0
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''				)
           self.assertIsInstance(lowercase   ,				lowercase				)
       def     A      (	self						:	int				):



           '''simple docstring'''

           UpperCAmelCase			       =		AutoFeatureExtractor.from_pretrained(lowercase				)
           self.assertIsInstance(lowercase   ,				lowercase				)
       def     A      (	self						:	Any				):



           '''simple docstring'''

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

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

               config_dict.pop('''feature_extractor_type'''				)
               UpperCAmelCase			       =		WavaVecaFeatureExtractor(**lowercase				)

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

               UpperCAmelCase			       =		AutoFeatureExtractor.from_pretrained(lowercase				)

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

           self.assertIsInstance(lowercase   ,				lowercase				)
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase			       =		AutoFeatureExtractor.from_pretrained(lowercase				)
           self.assertIsInstance(lowercase   ,				lowercase				)
       def     A      (	self						:	str				):



           '''simple docstring'''

           with self.assertRaisesRegex(
               lowercase   ,				'''bert-base is not a local folder and is not a valid model identifier'''				):
               UpperCAmelCase			       =		AutoFeatureExtractor.from_pretrained('''bert-base'''				)
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

           with self.assertRaisesRegex(
               lowercase   ,				R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''				):
               UpperCAmelCase			       =		AutoFeatureExtractor.from_pretrained(lowercase   ,				revision='''aaaaaa'''				)
       def     A      (	self						:	Tuple				):



           '''simple docstring'''

           with self.assertRaisesRegex(
               lowercase   ,				'''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''   ,				):
               UpperCAmelCase			       =		AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model'''				)
       def     A      (	self						:	List[str]				):



           '''simple docstring'''

           with self.assertRaises(lowercase				):
               UpperCAmelCase			       =		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				):
               UpperCAmelCase			       =		AutoFeatureExtractor.from_pretrained(
                   '''hf-internal-testing/test_dynamic_feature_extractor'''   ,				trust_remote_code=lowercase				)

           UpperCAmelCase			       =		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				)
               UpperCAmelCase			       =		AutoFeatureExtractor.from_pretrained(lowercase   ,				trust_remote_code=lowercase				)
           self.assertEqual(reloaded_feature_extractor.__class__.__name__   ,				'''NewFeatureExtractor'''				)
       def     A      (	self						:	List[Any]				):



           '''simple docstring'''

           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
               UpperCAmelCase			       =		CustomFeatureExtractor.from_pretrained(lowercase				)
               with tempfile.TemporaryDirectory() as tmp_dir:
                   feature_extractor.save_pretrained(lowercase				)
                   UpperCAmelCase			       =		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     A      (	self						:	str				):



           '''simple docstring'''

           class 			_a  (	__a ):
                  __a				:     Optional[Any]   				=					True

           try:
               AutoConfig.register('''custom'''   ,				lowercase				)
               AutoFeatureExtractor.register(lowercase   ,				lowercase				)
               # If remote code is not set, the default is to use local
               UpperCAmelCase			       =		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.
               UpperCAmelCase			       =		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
               UpperCAmelCase			       =		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'''







from argparse import ArgumentParser

from datasets.commands.convert import ConvertCommand
from datasets.commands.dummy_data import DummyDataCommand
from datasets.commands.env import EnvironmentCommand
from datasets.commands.run_beam import RunBeamCommand
from datasets.commands.test import TestCommand
from datasets.utils.logging import set_verbosity_info





def 						snake_case_							(_a	:							Tuple     ):
    return {key.lstrip('''-'''     ): value for key, value in zip(unknown_args[::2]    ,   unknown_args[1::2]     )}





def 						snake_case_							():
    UpperCAmelCase			       =		ArgumentParser(
        '''HuggingFace Datasets CLI tool'''    ,   usage='''datasets-cli <command> [<args>]'''    ,   allow_abbrev=_a     )
    UpperCAmelCase			       =		parser.add_subparsers(help='''datasets-cli command helpers'''     )
    set_verbosity_info()

    # Register commands
    ConvertCommand.register_subcommand(_a     )
    EnvironmentCommand.register_subcommand(_a     )
    TestCommand.register_subcommand(_a     )
    RunBeamCommand.register_subcommand(_a     )
    DummyDataCommand.register_subcommand(_a     )

    # Parse args
    UpperCAmelCase       ,     UpperCAmelCase			       =		parser.parse_known_args()
    if not hasattr(_a    ,   '''func'''     ):
        parser.print_help()
        exit(1     )
    UpperCAmelCase			       =		parse_unknown_args(_a     )

    # Run
    UpperCAmelCase			       =		args.func(_a    ,   **_a     )
    service.run()


if __name__ == "__main__":
   main()




