
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


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


class 			lowerCamelCase_  (     unittest.TestCase				):






    '''simple docstring'''






    def     UpperCamelCase__     (			self) ->			List[str]:
        __UpperCamelCase  :Optional[Any]    						=	0







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







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







    def     UpperCamelCase__     (			self) ->			Dict:
        with tempfile.TemporaryDirectory() as tmpdirname:
            __UpperCamelCase  :Optional[Any]    						=	WavaVecaConfig()

            # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
            __UpperCamelCase  :Optional[Any]    						=	AutoFeatureExtractor.from_pretrained(_lowercase).to_dict()

            config_dict.pop('''feature_extractor_type''')
            __UpperCamelCase  :List[Any]    						=	WavaVecaFeatureExtractor(**_lowercase)

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

            __UpperCamelCase  :Union[str, Any]    						=	AutoFeatureExtractor.from_pretrained(_lowercase)

            # make sure private variable is not incorrectly saved
            __UpperCamelCase  :Optional[int]    						=	json.loads(config.to_json_string())
            self.assertTrue('''_processor_class''' not in dict_as_saved)

        self.assertIsInstance(_lowercase				,				_lowercase)







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







    def     UpperCamelCase__     (			self) ->			Union[str, Any]:
        with self.assertRaisesRegex(
            _lowercase				,				'''bert-base is not a local folder and is not a valid model identifier'''):
            __UpperCamelCase  :Tuple    						=	AutoFeatureExtractor.from_pretrained('''bert-base''')







    def     UpperCamelCase__     (			self) ->			int:
        with self.assertRaisesRegex(
            _lowercase				,				r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''):
            __UpperCamelCase  :int    						=	AutoFeatureExtractor.from_pretrained(_lowercase				,				revision='''aaaaaa''')







    def     UpperCamelCase__     (			self) ->			Any:
        with self.assertRaisesRegex(
            _lowercase				,				'''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''				,				):
            __UpperCamelCase  :Any    						=	AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model''')







    def     UpperCamelCase__     (			self) ->			List[str]:
        # If remote code is not set, we will time out when asking whether to load the model.
        with self.assertRaises(_lowercase):
            __UpperCamelCase  :Any    						=	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  :List[Any]    						=	AutoFeatureExtractor.from_pretrained(
                '''hf-internal-testing/test_dynamic_feature_extractor'''				,				trust_remote_code=_lowercase)

        __UpperCamelCase  :int    						=	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  :Dict    						=	AutoFeatureExtractor.from_pretrained(_lowercase				,				trust_remote_code=_lowercase)
        self.assertEqual(reloaded_feature_extractor.__class__.__name__				,				'''NewFeatureExtractor''')







    def     UpperCamelCase__     (			self) ->			Optional[int]:
        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  :Any    						=	CustomFeatureExtractor.from_pretrained(_lowercase)
            with tempfile.TemporaryDirectory() as tmp_dir:
                feature_extractor.save_pretrained(_lowercase)
                __UpperCamelCase  :str    						=	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) ->			List[Any]:


        class 			lowerCamelCase_  (     UpperCAmelCase_				):






            '''simple docstring'''






            a__			:		Tuple      			=   True

        try:
            AutoConfig.register('''custom'''				,				_lowercase)
            AutoFeatureExtractor.register(_lowercase				,				_lowercase)
            # If remote code is not set, the default is to use local
            __UpperCamelCase  :List[Any]    						=	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  :List[Any]    						=	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  :str    						=	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]






from typing import List

import datasets
from datasets.tasks import AudioClassification

from ..folder_based_builder import folder_based_builder


__lowercase											=      datasets.utils.logging.get_logger(__name__)


class 			lowerCamelCase_  (     folder_based_builder.FolderBasedBuilderConfig				):






    '''simple docstring'''






    a__			:		bool      			=   None
    a__			:		bool      			=   None


class 			lowerCamelCase_  (     folder_based_builder.FolderBasedBuilder				):






    '''simple docstring'''






    a__			:		List[Any]      			=   datasets.Audio()
    a__			:		int      			=   """audio"""
    a__			:		str      			=   AudioFolderConfig
    a__			:		List[str]  # definition at the bottom of the script
    a__			:		int      			=   AudioClassification(audio_column="""audio"""		,			label_column="""label"""				)
__lowercase											=      [
    '''.aiff''',
    '''.au''',
    '''.avr''',
    '''.caf''',
    '''.flac''',
    '''.htk''',
    '''.svx''',
    '''.mat4''',
    '''.mat5''',
    '''.mpc2k''',
    '''.ogg''',
    '''.paf''',
    '''.pvf''',
    '''.raw''',
    '''.rf64''',
    '''.sd2''',
    '''.sds''',
    '''.ircam''',
    '''.voc''',
    '''.w64''',
    '''.wav''',
    '''.nist''',
    '''.wavex''',
    '''.wve''',
    '''.xi''',
    '''.mp3''',
    '''.opus''',
]
__lowercase											=      AUDIO_EXTENSIONS





