






'''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:      Optional[Any]  				=				get_tests_dir("fixtures")
A:      Union[str, Any]  				=				get_tests_dir("fixtures/dummy_feature_extractor_config.json")
A:      int  				=				get_tests_dir("fixtures/dummy-config.json")






class 							__magic_name__     (	unittest.TestCase				):







	"""simple docstring"""



	def        lowerCamelCase__		(							self       )						->  List[str]:
							lowercase_      :   Optional[Any]        =			0
	def        lowerCamelCase__		(							self       )						->  Union[str, Any]:
							lowercase_      :   List[str]        =			AutoFeatureExtractor.from_pretrained('facebook/wav2vec2-base-960h'       )
							self.assertIsInstance(_lowercase  ,      _lowercase       )
	def        lowerCamelCase__		(							self       )						->  Union[str, Any]:
							lowercase_      :   str        =			AutoFeatureExtractor.from_pretrained(_lowercase       )
							self.assertIsInstance(_lowercase  ,      _lowercase       )
	def        lowerCamelCase__		(							self       )						->  Dict:
							with tempfile.TemporaryDirectory() as tmpdirname:
													lowercase_      :   Optional[Any]        =			WavaVecaConfig()

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

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

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

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

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

							self.assertIsInstance(_lowercase  ,      _lowercase       )
	def        lowerCamelCase__		(							self       )						->  Union[str, Any]:
							lowercase_      :   Union[str, Any]        =			AutoFeatureExtractor.from_pretrained(_lowercase       )
							self.assertIsInstance(_lowercase  ,      _lowercase       )
	def        lowerCamelCase__		(							self       )						->  Union[str, Any]:
							with self.assertRaisesRegex(
							    _lowercase  ,      'bert-base is not a local folder and is not a valid model identifier'       ):
													lowercase_      :   Tuple        =			AutoFeatureExtractor.from_pretrained('bert-base'       )
	def        lowerCamelCase__		(							self       )						->  int:
							with self.assertRaisesRegex(
							    _lowercase  ,      r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'       ):
													lowercase_      :   int        =			AutoFeatureExtractor.from_pretrained(_lowercase  ,      revision='aaaaaa'       )
	def        lowerCamelCase__		(							self       )						->  Any:
							with self.assertRaisesRegex(
							    _lowercase  ,      'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'  ,      ):
													lowercase_      :   Any        =			AutoFeatureExtractor.from_pretrained('hf-internal-testing/config-no-model'       )
	def        lowerCamelCase__		(							self       )						->  List[str]:
							# If remote code is not set, we will time out when asking whether to load the model.
							with self.assertRaises(_lowercase       ):
													lowercase_      :   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       ):
													lowercase_      :   List[Any]        =			AutoFeatureExtractor.from_pretrained(
													    'hf-internal-testing/test_dynamic_feature_extractor'  ,      trust_remote_code=_lowercase       )

							lowercase_      :   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       )
													lowercase_      :   Dict        =			AutoFeatureExtractor.from_pretrained(_lowercase  ,      trust_remote_code=_lowercase       )
							self.assertEqual(reloaded_feature_extractor.__class__.__name__  ,      'NewFeatureExtractor'       )
	def        lowerCamelCase__		(							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
													lowercase_      :   Any        =			CustomFeatureExtractor.from_pretrained(_lowercase       )
													with tempfile.TemporaryDirectory() as tmp_dir:
																			feature_extractor.save_pretrained(_lowercase       )
																			lowercase_      :   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        lowerCamelCase__		(							self       )						->  List[Any]:






							class 							__magic_name__     (	UpperCAmelCase_				):







								"""simple docstring"""



								SCREAMING_SNAKE_CASE_					:      Tuple        =  True

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








'''simple docstring'''

from pathlib import PurePosixPath
from typing import Optional

import fsspec
from fsspec import AbstractFileSystem
from huggingface_hub.hf_api import DatasetInfo

from ..utils.file_utils import get_authentication_headers_for_url
from ..utils.hub import hf_hub_url






class 							__magic_name__     (	UpperCAmelCase_				):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      Union[str, Any]        =  ''
	SCREAMING_SNAKE_CASE_					:      Union[str, Any]        =  'hf-legacy'  # "hf://"" is reserved for hffs
	def __init__(							self  ,      _lowercase = None  ,      _lowercase = None  ,      **_lowercase  ,      )						->  Optional[Any]:
							super().__init__(self  ,      **_lowercase       )
							lowercase_      :   int        =			repo_info
							lowercase_      :   List[Any]        =			token
							lowercase_      :   Union[str, Any]        =			None
	def        lowerCamelCase__		(							self       )						->  Optional[Any]:
							if self.dir_cache is None:
													lowercase_      :   Optional[Any]        =			{}
													for hf_file in self.repo_info.siblings:
																			# TODO(QL): add sizes
																			lowercase_      :   str        =			{
																			    'name': hf_file.rfilename,
																			    'size': None,
																			    'type': 'file',
																			}
																			self.dir_cache.update(
																			    {
																			        str(_lowercase       ): {'name': str(_lowercase       ), 'size': None, 'type': 'directory'}
																			        for d in list(PurePosixPath(hf_file.rfilename       ).parents       )[:-1]
																			    }       )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase = "rb"  ,      **_lowercase  ,      )						->  Dict:
							if not isinstance(self.repo_info  ,      _lowercase       ):
													raise NotImplementedError(f"Open is only implemented for dataset repositories, but got {self.repo_info}"       )
							lowercase_      :   Optional[int]        =			hf_hub_url(self.repo_info.id  ,      _lowercase  ,      revision=self.repo_info.sha       )
							return fsspec.open(
							    _lowercase  ,      mode=_lowercase  ,      headers=get_authentication_headers_for_url(_lowercase  ,      use_auth_token=self.token       )  ,      client_kwargs={'trust_env': True}  ,      ).open()
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      **_lowercase       )						->  Tuple:
							self._get_dirs()
							lowercase_      :   str        =			self._strip_protocol(_lowercase       )
							if path in self.dir_cache:
													return self.dir_cache[path]
							else:
													raise FileNotFoundError(_lowercase       )







	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase=False  ,      **_lowercase       )						->  List[str]:
							self._get_dirs()
							lowercase_      :   List[str]        =			PurePosixPath(path.strip('/'       )       )
							lowercase_      :   List[str]        =			{}
							for p, f in self.dir_cache.items():
													lowercase_      :   Tuple        =			PurePosixPath(p.strip('/'       )       )
													lowercase_      :   Optional[int]        =			p.parent
													if root == path:
																			lowercase_      :   List[str]        =			f
							lowercase_      :   List[str]        =			list(paths.values()       )
							if detail:
													return out
							else:
													return sorted(f['name'] for f in out       )
