


import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / "utils"))

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


_snake_case					:		int      =    get_tests_dir("fixtures")

class     a		(unittest.TestCase				):




							"""simple docstring"""


							def    __snake_case (							self	:  Union[str, Any]    )     ->   int:
														# A mock response for an HTTP head request to emulate server down
														__snake_case		:      List[Any]										=       mock.Mock()
														__snake_case		:      str										=       500
														__snake_case		:      Optional[int]										=       {}
														__snake_case		:      Any										=       HTTPError
														__snake_case		:      Any										=       {}

														# Download this model to make sure it's in the cache.
														__snake_case		:      Optional[int]										=       WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2"    )
														# Under the mock environment we get a 500 error when trying to reach the model.
														with mock.patch("requests.Session.request"   ,     return_value=lowerCamelCase    ) as mock_head:
																					__snake_case		:      List[Any]										=       WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2"    )
																					# This check we did call the fake head request
																					mock_head.assert_called()






							def    __snake_case (							self	:  Union[str, Any]    )     ->   List[str]:
														# This test is for deprecated behavior and can be removed in v5
														__snake_case		:      Optional[int]										=       WavaVecaFeatureExtractor.from_pretrained(
														    "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"    )

@is_staging_test
class     a		(unittest.TestCase				):




							"""simple docstring"""


							@classmethod
							def    __snake_case (							cls	:  Optional[Any]    )     ->   List[str]:
														__snake_case		:      List[Any]										=       TOKEN
														HfFolder.save_token(lowerCamelCase    )

							@classmethod
							def    __snake_case (							cls	:  Dict    )     ->   str:
														try:
																					delete_repo(token=cls._token   ,     repo_id="test-feature-extractor"    )
														except HTTPError:
																					pass

														try:
																					delete_repo(token=cls._token   ,     repo_id="valid_org/test-feature-extractor-org"    )
														except HTTPError:
																					pass

														try:
																					delete_repo(token=cls._token   ,     repo_id="test-dynamic-feature-extractor"    )
														except HTTPError:
																					pass

							def    __snake_case (							self	:  str    )     ->   List[Any]:
														__snake_case		:      Tuple										=       WavaVecaFeatureExtractor.from_pretrained(lowerCamelCase    )
														feature_extractor.push_to_hub("test-feature-extractor"   ,     use_auth_token=self._token    )

														__snake_case		:      Optional[int]										=       WavaVecaFeatureExtractor.from_pretrained(F'{USER}/test-feature-extractor'    )
														for k, v in feature_extractor.__dict__.items():
																					self.assertEqual(lowerCamelCase   ,     getattr(lowerCamelCase   ,     lowerCamelCase    )    )

														# Reset repo
														delete_repo(token=self._token   ,     repo_id="test-feature-extractor"    )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					feature_extractor.save_pretrained(
																					    lowerCamelCase   ,     repo_id="test-feature-extractor"   ,     push_to_hub=lowerCamelCase   ,     use_auth_token=self._token    )

														__snake_case		:      Dict										=       WavaVecaFeatureExtractor.from_pretrained(F'{USER}/test-feature-extractor'    )
														for k, v in feature_extractor.__dict__.items():
																					self.assertEqual(lowerCamelCase   ,     getattr(lowerCamelCase   ,     lowerCamelCase    )    )

							def    __snake_case (							self	:  List[Any]    )     ->   int:
														__snake_case		:      Tuple										=       WavaVecaFeatureExtractor.from_pretrained(lowerCamelCase    )
														feature_extractor.push_to_hub("valid_org/test-feature-extractor"   ,     use_auth_token=self._token    )

														__snake_case		:      Tuple										=       WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor"    )
														for k, v in feature_extractor.__dict__.items():
																					self.assertEqual(lowerCamelCase   ,     getattr(lowerCamelCase   ,     lowerCamelCase    )    )

														# Reset repo
														delete_repo(token=self._token   ,     repo_id="valid_org/test-feature-extractor"    )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					feature_extractor.save_pretrained(
																					    lowerCamelCase   ,     repo_id="valid_org/test-feature-extractor-org"   ,     push_to_hub=lowerCamelCase   ,     use_auth_token=self._token    )

														__snake_case		:      List[Any]										=       WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org"    )
														for k, v in feature_extractor.__dict__.items():
																					self.assertEqual(lowerCamelCase   ,     getattr(lowerCamelCase   ,     lowerCamelCase    )    )






							def    __snake_case (							self	:  Any    )     ->   int:
														CustomFeatureExtractor.register_for_auto_class()
														__snake_case		:      Optional[int]										=       CustomFeatureExtractor.from_pretrained(lowerCamelCase    )

														feature_extractor.push_to_hub("test-dynamic-feature-extractor"   ,     use_auth_token=self._token    )

														# This has added the proper auto_map field to the config
														self.assertDictEqual(
														    feature_extractor.auto_map   ,     {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"}   ,     )

														__snake_case		:      int										=       AutoFeatureExtractor.from_pretrained(
														    F'{USER}/test-dynamic-feature-extractor'   ,     trust_remote_code=lowerCamelCase    )
														# Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
														self.assertEqual(new_feature_extractor.__class__.__name__   ,     "CustomFeatureExtractor"    )





import logging

from transformers.configuration_utils import PretrainedConfig


_snake_case					:		Optional[Any]      =    logging.getLogger(__name__)

class     a		(_lowerCAmelCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							Tuple					  =  "masked_bert"

							def __init__(							self	:  Optional[int]   ,     lowerCamelCase	:  Any=30522   ,     lowerCamelCase	:  Tuple=768   ,     lowerCamelCase	:  str=12   ,     lowerCamelCase	:  Dict=12   ,     lowerCamelCase	:  List[str]=3072   ,     lowerCamelCase	:  List[str]="gelu"   ,     lowerCamelCase	:  Any=0.1   ,     lowerCamelCase	:  Any=0.1   ,     lowerCamelCase	:  List[Any]=512   ,     lowerCamelCase	:  int=2   ,     lowerCamelCase	:  str=0.02   ,     lowerCamelCase	:  List[str]=1E-12   ,     lowerCamelCase	:  Any=0   ,     lowerCamelCase	:  Dict="topK"   ,     lowerCamelCase	:  List[Any]="constant"   ,     lowerCamelCase	:  Dict=0.0   ,     **lowerCamelCase	:  List[Any]   ,     )     ->   Dict:
														super().__init__(pad_token_id=lowerCamelCase   ,     **lowerCamelCase    )

														__snake_case		:      Optional[Any]										=       vocab_size
														__snake_case		:      Optional[int]										=       hidden_size
														__snake_case		:      Tuple										=       num_hidden_layers
														__snake_case		:      Union[str, Any]										=       num_attention_heads
														__snake_case		:      Optional[Any]										=       hidden_act
														__snake_case		:      str										=       intermediate_size
														__snake_case		:      Tuple										=       hidden_dropout_prob
														__snake_case		:      Dict										=       attention_probs_dropout_prob
														__snake_case		:      List[str]										=       max_position_embeddings
														__snake_case		:      Union[str, Any]										=       type_vocab_size
														__snake_case		:      Any										=       initializer_range
														__snake_case		:      str										=       layer_norm_eps
														__snake_case		:      str										=       pruning_method
														__snake_case		:      int										=       mask_init
														__snake_case		:      Any										=       mask_scale

