




import os

import pytest
import yaml

from datasets.features.features import Features, Value
from datasets.info import DatasetInfo, DatasetInfosDict







@pytest.mark.parametrize(
    '''files'''						,						[
        ['''full:README.md''', '''dataset_infos.json'''],
        ['''empty:README.md''', '''dataset_infos.json'''],
        ['''dataset_infos.json'''],
        ['''full:README.md'''],
    ]						,						)
def 	lowercase_		(     SCREAMING_SNAKE_CASE						:		Optional[Any]						,						SCREAMING_SNAKE_CASE						:		Any ):






	"""simple docstring"""
	snake_case__						:     Any		=tmp_path_factory.mktemp('''dset_infos_dir''' )
	if "full:README.md" in files:
		with open(dataset_infos_dir / '''README.md'''						,						'''w''' ) as f:
			f.write('''---\ndataset_info:\n  dataset_size: 42\n---''' )
	if "empty:README.md" in files:
		with open(dataset_infos_dir / '''README.md'''						,						'''w''' ) as f:
			f.write('''''' )
			 # we want to support dataset_infos.json for backward compatibility
	if "dataset_infos.json" in files:
		with open(dataset_infos_dir / '''dataset_infos.json'''						,						'''w''' ) as f:
			f.write('''{\"default\": {\"dataset_size\": 42}}''' )
	snake_case__						:     str		=DatasetInfosDict.from_directory(UpperCamelCase_ )
	assert dataset_infos
	assert dataset_infos["default"].dataset_size == 42







@pytest.mark.parametrize(
    '''dataset_info'''						,						[
        DatasetInfo(),
        DatasetInfo(
            description='''foo'''						,						features=Features({'''a''': Value('''int32''' )} )						,						builder_name='''builder'''						,						config_name='''config'''						,						version='''1.0.0'''						,						splits=[{'''name''': '''train'''}]						,						download_size=42						,						),
    ]						,						)
def 	lowercase_		(     SCREAMING_SNAKE_CASE						:		Tuple						,						SCREAMING_SNAKE_CASE						:		Optional[Any] ):






	"""simple docstring"""
	snake_case__						:     List[Any]		=str(UpperCamelCase_ )
	dataset_info.write_to_directory(UpperCamelCase_ )
	snake_case__						:     Any		=DatasetInfo.from_directory(UpperCamelCase_ )
	assert dataset_info == reloaded
	assert os.path.exists(os.path.join(UpperCamelCase_						,						'''dataset_info.json''' ) )







def 	lowercase_		(     ):






	"""simple docstring"""
	snake_case__						:     List[str]		=DatasetInfo(
	    description='''foo'''						,						citation='''bar'''						,						homepage='''https://foo.bar'''						,						license='''CC0'''						,						features=Features({'''a''': Value('''int32''' )} )						,						post_processed={}						,						supervised_keys=()						,						task_templates=[]						,						builder_name='''builder'''						,						config_name='''config'''						,						version='''1.0.0'''						,						splits=[{'''name''': '''train''', '''num_examples''': 42}]						,						download_checksums={}						,						download_size=13_37						,						post_processing_size=4_42						,						dataset_size=12_34						,						size_in_bytes=13_37 + 4_42 + 12_34						,						)
	snake_case__						:     Optional[Any]		=dataset_info._to_yaml_dict()
	assert sorted(UpperCamelCase_ ) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML )
	for key in DatasetInfo._INCLUDED_INFO_IN_YAML:
		assert key in dataset_info_yaml_dict
		assert isinstance(dataset_info_yaml_dict[key]						,						(list, dict, int, str) )
	snake_case__						:     Dict		=yaml.safe_dump(UpperCamelCase_ )
	snake_case__						:     Any		=yaml.safe_load(UpperCamelCase_ )
	assert dataset_info_yaml_dict == reloaded







def 	lowercase_		(     ):






	"""simple docstring"""
	snake_case__						:     Optional[Any]		=DatasetInfo()
	snake_case__						:     Union[str, Any]		=dataset_info._to_yaml_dict()
	assert dataset_info_yaml_dict == {}







@pytest.mark.parametrize(
    '''dataset_infos_dict'''						,						[
        DatasetInfosDict(),
        DatasetInfosDict({'''default''': DatasetInfo()} ),
        DatasetInfosDict({'''my_config_name''': DatasetInfo()} ),
        DatasetInfosDict(
            {
                '''default''': DatasetInfo(
                    description='''foo'''						,						features=Features({'''a''': Value('''int32''' )} )						,						builder_name='''builder'''						,						config_name='''config'''						,						version='''1.0.0'''						,						splits=[{'''name''': '''train'''}]						,						download_size=42						,						)
            } ),
        DatasetInfosDict(
            {
                '''v1''': DatasetInfo(dataset_size=42 ),
                '''v2''': DatasetInfo(dataset_size=13_37 ),
            } ),
    ]						,						)
def 	lowercase_		(     SCREAMING_SNAKE_CASE						:		str						,						SCREAMING_SNAKE_CASE						:		Any ):






	"""simple docstring"""
	snake_case__						:     Tuple		=str(UpperCamelCase_ )
	dataset_infos_dict.write_to_directory(UpperCamelCase_ )
	snake_case__						:     Optional[Any]		=DatasetInfosDict.from_directory(UpperCamelCase_ )

	# the config_name of the dataset_infos_dict take over the attribute
	for config_name, dataset_info in dataset_infos_dict.items():
		snake_case__						:     List[Any]		=config_name
		# the yaml representation doesn't include fields like description or citation
		# so we just test that we can recover what we can from the yaml
		snake_case__						:     Optional[Any]		=DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict() )
	assert dataset_infos_dict == reloaded

	if dataset_infos_dict:
		assert os.path.exists(os.path.join(UpperCamelCase_						,						'''README.md''' ) )





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


_SCREAMING_SNAKE_CASE 		=      get_tests_dir("""fixtures""")
_SCREAMING_SNAKE_CASE 		=      get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
_SCREAMING_SNAKE_CASE 		=      get_tests_dir("""fixtures/dummy-config.json""")
class SCREAMING_SNAKE_CASE_		(      unittest.TestCase		):







   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


    UpperCamelCase			  =      0







   def 						lowerCamelCase_	(     self						:						int				):





    """simple docstring"""


    UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""				)
    self.assertIsInstance(lowerCamelCase_   ,   lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						Optional[Any]				):





    """simple docstring"""


    UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_				)
    self.assertIsInstance(lowerCamelCase_   ,   lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						Tuple				):





    """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(lowerCamelCase_				).to_dict()

     config_dict.pop("""feature_extractor_type"""				)
     UpperCamelCase			  =      WavaVecaFeatureExtractor(**lowerCamelCase_				)

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

     UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_				)

     # 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(lowerCamelCase_   ,   lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						List[str]				):





    """simple docstring"""


    UpperCamelCase			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_				)
    self.assertIsInstance(lowerCamelCase_   ,   lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						int				):





    """simple docstring"""


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







   def 						lowerCamelCase_	(     self						:						str				):





    """simple docstring"""


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







   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    with self.assertRaisesRegex(
        lowerCamelCase_   ,   """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 						lowerCamelCase_	(     self						:						List[Any]				):





    """simple docstring"""


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

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

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







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





    """simple docstring"""


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

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

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





    """simple docstring"""


    class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):
       __lowerCAmelCase           =				True

    try:
     AutoConfig.register("""custom"""   ,   lowerCamelCase_				)
     AutoFeatureExtractor.register(lowerCamelCase_   ,   lowerCamelCase_				)
     # 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=lowerCamelCase_				)
     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=lowerCamelCase_				)
     self.assertEqual(feature_extractor.__class__.__name__   ,   """NewFeatureExtractor"""				)
     self.assertTrue(not hasattr(lowerCamelCase_   ,   """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]
