
'''simple docstring'''







from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
							from transformers import (
							    TFAutoModel,
							    TFAutoModelForCausalLM,
							    TFAutoModelForMaskedLM,
							    TFAutoModelForPreTraining,
							    TFAutoModelForQuestionAnswering,
							    TFAutoModelForSeqaSeqLM,
							    TFAutoModelForSequenceClassification,
							    TFAutoModelForTableQuestionAnswering,
							    TFAutoModelForTokenClassification,
							    TFAutoModelWithLMHead,
							    TFBertForMaskedLM,
							    TFBertForPreTraining,
							    TFBertForQuestionAnswering,
							    TFBertForSequenceClassification,
							    TFBertModel,
							    TFFunnelBaseModel,
							    TFFunnelModel,
							    TFGPTaLMHeadModel,
							    TFRobertaForMaskedLM,
							    TFTaForConditionalGeneration,
							    TFTapasForQuestionAnswering,
							)
							from transformers.models.auto.modeling_tf_auto import (
							    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
							    TF_MODEL_FOR_MASKED_LM_MAPPING,
							    TF_MODEL_FOR_PRETRAINING_MAPPING,
							    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
							    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
							    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
							    TF_MODEL_MAPPING,
							)
							from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
							from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
							from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST
							from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST






class  lowerCAmelCase__   (  __snake_case     ):
					lowerCAmelCase_        =     '''new-model'''


if is_tf_available():






							class  lowerCAmelCase__   (  __snake_case     ):
												lowerCAmelCase_        =     NewModelConfig






@require_tf
class  lowerCAmelCase__   (  unittest.TestCase     ):






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Dict													=       '''bert-base-cased'''
											lowercase_	:     Union[str, Any]													=       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
											self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Any													=       TFAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE		)
											self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Optional[int]													=       '''bert-base-cased'''
											lowercase_	:     Any													=       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
											self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[int]													=       TFAutoModelForPreTraining.from_pretrained(__SCREAMING_SNAKE_CASE		)
											self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_	:     List[Any]													=       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

																	lowercase_	:     Tuple													=       TFAutoModelForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     Dict													=       TFAutoModelForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE				,							output_loading_info=__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_	:     int													=       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

																	lowercase_	:     Any													=       TFAutoModelWithLMHead.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_	:     Tuple													=       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

																	lowercase_	:     int													=       TFAutoModelForMaskedLM.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     str													=       TFAutoModelForMaskedLM.from_pretrained(__SCREAMING_SNAKE_CASE				,							output_loading_info=__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_	:     int													=       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

																	lowercase_	:     Tuple													=       TFAutoModelForSeqaSeqLM.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     Optional[int]													=       TFAutoModelForSeqaSeqLM.from_pretrained(__SCREAMING_SNAKE_CASE				,							output_loading_info=__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											for model_name in ["bert-base-uncased"]:
																	lowercase_	:     List[str]													=       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

																	lowercase_	:     int													=       TFAutoModelForSequenceClassification.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											for model_name in ["bert-base-uncased"]:
																	lowercase_	:     Tuple													=       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

																	lowercase_	:     Optional[int]													=       TFAutoModelForQuestionAnswering.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					@slow
					@require_tensorflow_probability
					def 	_snake_case    (   self		):


											"""simple docstring"""





											for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
																	lowercase_	:     Optional[int]													=       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

																	lowercase_	:     List[str]													=       TFAutoModelForTableQuestionAnswering.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     int													=       TFAutoModelForTableQuestionAnswering.from_pretrained(
																	    __SCREAMING_SNAKE_CASE				,							output_loading_info=__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Dict													=       TFAutoModelWithLMHead.from_pretrained(__SCREAMING_SNAKE_CASE		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)
											self.assertEqual(model.num_parameters()				,							1_44_10		)
											self.assertEqual(model.num_parameters(only_trainable=__SCREAMING_SNAKE_CASE		)				,							1_44_10		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Union[str, Any]													=       TFAutoModelWithLMHead.from_pretrained(__SCREAMING_SNAKE_CASE		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)
											self.assertEqual(model.num_parameters()				,							1_44_10		)
											self.assertEqual(model.num_parameters(only_trainable=__SCREAMING_SNAKE_CASE		)				,							1_44_10		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     int													=       TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny'''		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[int]													=       copy.deepcopy(model.config		)
											lowercase_	:     str													=       ['''FunnelBaseModel''']
											lowercase_	:     Any													=       TFAutoModel.from_config(__SCREAMING_SNAKE_CASE		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

											with tempfile.TemporaryDirectory() as tmp_dir:
																	model.save_pretrained(__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     Dict													=       TFAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											try:
																	AutoConfig.register('''new-model'''				,							__SCREAMING_SNAKE_CASE		)

																	lowercase_	:     List[str]													=       [
																	    TFAutoModel,
																	    TFAutoModelForCausalLM,
																	    TFAutoModelForMaskedLM,
																	    TFAutoModelForPreTraining,
																	    TFAutoModelForQuestionAnswering,
																	    TFAutoModelForSequenceClassification,
																	    TFAutoModelForTokenClassification,
																	]

																	for auto_class in auto_classes:
																							with self.subTest(auto_class.__name__		):
																													# Wrong config class will raise an error
																													with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																																			auto_class.register(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)
																													auto_class.register(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)
																													# Trying to register something existing in the Transformers library will raise an error
																													with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																																			auto_class.register(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

																													# Now that the config is registered, it can be used as any other config with the auto-API
																													lowercase_	:     Union[str, Any]													=       BertModelTester(self		).get_config()
																													lowercase_	:     List[Any]													=       NewModelConfig(**tiny_config.to_dict()		)
																													lowercase_	:     List[str]													=       auto_class.from_config(__SCREAMING_SNAKE_CASE		)
																													self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

																													with tempfile.TemporaryDirectory() as tmp_dir:
																																			model.save_pretrained(__SCREAMING_SNAKE_CASE		)
																																			lowercase_	:     List[Any]													=       auto_class.from_pretrained(__SCREAMING_SNAKE_CASE		)
																																			self.assertIsInstance(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

											finally:
																	if "new-model" in CONFIG_MAPPING._extra_content:
																							del CONFIG_MAPPING._extra_content["new-model"]
																	for mapping in (
																	    TF_MODEL_MAPPING,
																	    TF_MODEL_FOR_PRETRAINING_MAPPING,
																	    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
																	    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
																	    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
																	    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
																	    TF_MODEL_FOR_MASKED_LM_MAPPING,
																	):
																							if NewModelConfig in mapping._extra_content:
																													del mapping._extra_content[NewModelConfig]






					def 	_snake_case    (   self		):


											"""simple docstring"""





											with self.assertRaisesRegex(
											    __SCREAMING_SNAKE_CASE				,							'''bert-base is not a local folder and is not a valid model identifier'''		):
																	lowercase_	:     Optional[int]													=       TFAutoModel.from_pretrained('''bert-base'''		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											with self.assertRaisesRegex(
											    __SCREAMING_SNAKE_CASE				,							R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''		):
																	lowercase_	:     List[str]													=       TFAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE				,							revision='''aaaaaa'''		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											with self.assertRaisesRegex(
											    __SCREAMING_SNAKE_CASE				,							'''hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'''				,							):
																	lowercase_	:     Any													=       TFAutoModel.from_pretrained('''hf-internal-testing/config-no-model'''		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											with self.assertRaisesRegex(__SCREAMING_SNAKE_CASE				,							'''Use `from_pt=True` to load this model'''		):
																	lowercase_	:     Dict													=       TFAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only'''		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     List[Any]													=       TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''		)
											with RequestCounter() as counter:
																	lowercase_	:     Union[str, Any]													=       TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''		)
																	self.assertEqual(counter.get_request_count				,							0		)
																	self.assertEqual(counter.head_request_count				,							1		)
																	self.assertEqual(counter.other_request_count				,							0		)

											# With a sharded checkpoint
											lowercase_	:     Any													=       TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''		)
											with RequestCounter() as counter:
																	lowercase_	:     List[Any]													=       TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''		)
																	self.assertEqual(counter.get_request_count				,							0		)
																	self.assertEqual(counter.head_request_count				,							1		)
																	self.assertEqual(counter.other_request_count				,							0		)





'''simple docstring'''




import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

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

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    PROCESSOR_MAPPING,
    TOKENIZER_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    AutoProcessor,
    AutoTokenizer,
    BertTokenizer,
    ProcessorMixin,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
)
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test
from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available


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
from test_module.custom_processing import CustomProcessor  # noqa E402
from test_module.custom_tokenization import CustomTokenizer  # noqa E402


lowercase			:				str    		=			get_tests_dir('fixtures/dummy_feature_extractor_config.json')
lowercase			:				str    		=			get_tests_dir('fixtures/vocab.json')
lowercase			:				int    		=			get_tests_dir('fixtures')





class  A				(							unittest.TestCase						):
     __magic_name__						=		['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''bla''', '''blou''']





     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         A       :   Tuple					=						0





     def 				__lowerCAmelCase							(							self	) ->				Optional[int]:






         """simple docstring"""






         A       :   List[Any]					=						AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h'''	)
         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   Union[str, Any]					=						WavaVecaConfig()
             A       :   List[str]					=						AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h'''	)

             # save in new folder
             model_config.save_pretrained(SCREAMING_SNAKE_CASE	)
             processor.save_pretrained(SCREAMING_SNAKE_CASE	)

             A       :   List[str]					=						AutoProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)

         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				int:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             # copy relevant files
             copyfile(SCREAMING_SNAKE_CASE						,			os.path.join(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)	)
             copyfile(SCREAMING_SNAKE_CASE						,			os.path.join(SCREAMING_SNAKE_CASE						,			'''vocab.json'''	)	)

             A       :   Optional[Any]					=						AutoProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)

         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   Dict					=						WavaVecaFeatureExtractor()
             A       :   List[str]					=						AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h'''	)

             A       :   str					=						WavaVecaProcessor(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

             # save in new folder
             processor.save_pretrained(SCREAMING_SNAKE_CASE	)

             # drop `processor_class` in tokenizer
             with open(os.path.join(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)						,			'''r'''	) as f:
                 A       :   Dict					=						json.load(SCREAMING_SNAKE_CASE	)
                 config_dict.pop('''processor_class'''	)

             with open(os.path.join(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)						,			'''w'''	) as f:
                 f.write(json.dumps(SCREAMING_SNAKE_CASE	)	)

             A       :   Optional[Any]					=						AutoProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)

         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				int:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   List[Any]					=						WavaVecaFeatureExtractor()
             A       :   List[Any]					=						AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h'''	)

             A       :   str					=						WavaVecaProcessor(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

             # save in new folder
             processor.save_pretrained(SCREAMING_SNAKE_CASE	)

             # drop `processor_class` in feature extractor
             with open(os.path.join(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)						,			'''r'''	) as f:
                 A       :   str					=						json.load(SCREAMING_SNAKE_CASE	)
                 config_dict.pop('''processor_class'''	)

             with open(os.path.join(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)						,			'''w'''	) as f:
                 f.write(json.dumps(SCREAMING_SNAKE_CASE	)	)

             A       :   str					=						AutoProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)

         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[Any]:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   str					=						WavaVecaConfig(processor_class='''Wav2Vec2Processor'''	)
             model_config.save_pretrained(SCREAMING_SNAKE_CASE	)
             # copy relevant files
             copyfile(SCREAMING_SNAKE_CASE						,			os.path.join(SCREAMING_SNAKE_CASE						,			'''vocab.json'''	)	)
             # create emtpy sample processor
             with open(os.path.join(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)						,			'''w'''	) as f:
                 f.write('''{}'''	)

             A       :   List[str]					=						AutoProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)

         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				Any:






         """simple docstring"""






         with self.assertRaises(SCREAMING_SNAKE_CASE	):
             A       :   Optional[Any]					=						AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor'''	)
         # If remote code is disabled, we can't load this config.
         with self.assertRaises(SCREAMING_SNAKE_CASE	):
             A       :   Union[str, Any]					=						AutoProcessor.from_pretrained(
                 '''hf-internal-testing/test_dynamic_processor'''						,			trust_remote_code=SCREAMING_SNAKE_CASE	)

         A       :   Union[str, Any]					=						AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor'''						,			trust_remote_code=SCREAMING_SNAKE_CASE	)
         self.assertTrue(processor.special_attribute_present	)
         self.assertEqual(processor.__class__.__name__						,			'''NewProcessor'''	)

         A       :   List[str]					=						processor.feature_extractor
         self.assertTrue(feature_extractor.special_attribute_present	)
         self.assertEqual(feature_extractor.__class__.__name__						,			'''NewFeatureExtractor'''	)

         A       :   Tuple					=						processor.tokenizer
         self.assertTrue(tokenizer.special_attribute_present	)
         if is_tokenizers_available():
             self.assertEqual(tokenizer.__class__.__name__						,			'''NewTokenizerFast'''	)

             # Test we can also load the slow version
             A       :   List[str]					=						AutoProcessor.from_pretrained(
                 '''hf-internal-testing/test_dynamic_processor'''						,			trust_remote_code=SCREAMING_SNAKE_CASE						,			use_fast=SCREAMING_SNAKE_CASE	)
             A       :   int					=						new_processor.tokenizer
             self.assertTrue(new_tokenizer.special_attribute_present	)
             self.assertEqual(new_tokenizer.__class__.__name__						,			'''NewTokenizer'''	)
         else:
             self.assertEqual(tokenizer.__class__.__name__						,			'''NewTokenizer'''	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[Any]:






         """simple docstring"""






         try:
             AutoConfig.register('''custom'''						,			SCREAMING_SNAKE_CASE	)
             AutoFeatureExtractor.register(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)
             AutoTokenizer.register(SCREAMING_SNAKE_CASE						,			slow_tokenizer_class=SCREAMING_SNAKE_CASE	)
             AutoProcessor.register(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)
             # Trying to register something existing in the Transformers library will raise an error
             with self.assertRaises(SCREAMING_SNAKE_CASE	):
                 AutoProcessor.register(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

             # Now that the config is registered, it can be used as any other config with the auto-API
             A       :   List[Any]					=						CustomFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE	)

             with tempfile.TemporaryDirectory() as tmp_dir:
                 A       :   Tuple					=						os.path.join(SCREAMING_SNAKE_CASE						,			'''vocab.txt'''	)
                 with open(SCREAMING_SNAKE_CASE						,			'''w'''						,			encoding='''utf-8'''	) as vocab_writer:
                     vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]	)	)
                 A       :   Optional[int]					=						CustomTokenizer(SCREAMING_SNAKE_CASE	)

             A       :   Any					=						CustomProcessor(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

             with tempfile.TemporaryDirectory() as tmp_dir:
                 processor.save_pretrained(SCREAMING_SNAKE_CASE	)
                 A       :   List[str]					=						AutoProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)
                 self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

         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]
             if CustomConfig in TOKENIZER_MAPPING._extra_content:
                 del TOKENIZER_MAPPING._extra_content[CustomConfig]
             if CustomConfig in PROCESSOR_MAPPING._extra_content:
                 del PROCESSOR_MAPPING._extra_content[CustomConfig]





     def 				__lowerCAmelCase							(							self	) ->				Optional[int]:






         """simple docstring"""






         class  A				(							__snake_case						):
              __magic_name__						=		False





         class  A				(							__snake_case						):
              __magic_name__						=		False





         class  A				(							__snake_case						):
              __magic_name__						=		'''AutoFeatureExtractor'''
              __magic_name__						=		'''AutoTokenizer'''
              __magic_name__						=		False

         try:
             AutoConfig.register('''custom'''						,			SCREAMING_SNAKE_CASE	)
             AutoFeatureExtractor.register(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)
             AutoTokenizer.register(SCREAMING_SNAKE_CASE						,			slow_tokenizer_class=SCREAMING_SNAKE_CASE	)
             AutoProcessor.register(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)
             # If remote code is not set, the default is to use local classes.
             A       :   Union[str, Any]					=						AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor'''	)
             self.assertEqual(processor.__class__.__name__						,			'''NewProcessor'''	)
             self.assertFalse(processor.special_attribute_present	)
             self.assertFalse(processor.feature_extractor.special_attribute_present	)
             self.assertFalse(processor.tokenizer.special_attribute_present	)

             # If remote code is disabled, we load the local ones.
             A       :   Optional[int]					=						AutoProcessor.from_pretrained(
                 '''hf-internal-testing/test_dynamic_processor'''						,			trust_remote_code=SCREAMING_SNAKE_CASE	)
             self.assertEqual(processor.__class__.__name__						,			'''NewProcessor'''	)
             self.assertFalse(processor.special_attribute_present	)
             self.assertFalse(processor.feature_extractor.special_attribute_present	)
             self.assertFalse(processor.tokenizer.special_attribute_present	)

             # If remote is enabled, we load from the Hub.
             A       :   Tuple					=						AutoProcessor.from_pretrained(
                 '''hf-internal-testing/test_dynamic_processor'''						,			trust_remote_code=SCREAMING_SNAKE_CASE	)
             self.assertEqual(processor.__class__.__name__						,			'''NewProcessor'''	)
             self.assertTrue(processor.special_attribute_present	)
             self.assertTrue(processor.feature_extractor.special_attribute_present	)
             self.assertTrue(processor.tokenizer.special_attribute_present	)

         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]
             if CustomConfig in TOKENIZER_MAPPING._extra_content:
                 del TOKENIZER_MAPPING._extra_content[CustomConfig]
             if CustomConfig in PROCESSOR_MAPPING._extra_content:
                 del PROCESSOR_MAPPING._extra_content[CustomConfig]





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






         A       :   int					=						AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-bert'''	)
         self.assertEqual(processor.__class__.__name__						,			'''BertTokenizerFast'''	)





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






         A       :   Optional[int]					=						AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-convnext'''	)
         self.assertEqual(processor.__class__.__name__						,			'''ConvNextImageProcessor'''	)





@is_staging_test
class  A				(							unittest.TestCase						):
     __magic_name__						=		['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''bla''', '''blou''']





     @classmethod
     def 				__lowerCAmelCase							(							cls	) ->				Dict:






         """simple docstring"""






         A       :   Optional[int]					=						TOKEN
         HfFolder.save_token(SCREAMING_SNAKE_CASE	)





     @classmethod
     def 				__lowerCAmelCase							(							cls	) ->				Any:






         """simple docstring"""






         try:
             delete_repo(token=cls._token						,			repo_id='''test-processor'''	)
         except HTTPError:
             pass

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

         try:
             delete_repo(token=cls._token						,			repo_id='''test-dynamic-processor'''	)
         except HTTPError:
             pass





     def 				__lowerCAmelCase							(							self	) ->				Dict:






         """simple docstring"""






         A       :   Union[str, Any]					=						WavaVecaProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)
         with tempfile.TemporaryDirectory() as tmp_dir:
             processor.save_pretrained(
                 os.path.join(SCREAMING_SNAKE_CASE						,			'''test-processor'''	)						,			push_to_hub=SCREAMING_SNAKE_CASE						,			use_auth_token=self._token	)

             A       :   int					=						WavaVecaProcessor.from_pretrained(F'{USER}/test-processor'	)
             for k, v in processor.feature_extractor.__dict__.items():
                 self.assertEqual(SCREAMING_SNAKE_CASE						,			getattr(new_processor.feature_extractor						,			SCREAMING_SNAKE_CASE	)	)
             self.assertDictEqual(new_processor.tokenizer.get_vocab()						,			processor.tokenizer.get_vocab()	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[int]:






         """simple docstring"""






         A       :   Tuple					=						WavaVecaProcessor.from_pretrained(SCREAMING_SNAKE_CASE	)

         with tempfile.TemporaryDirectory() as tmp_dir:
             processor.save_pretrained(
                 os.path.join(SCREAMING_SNAKE_CASE						,			'''test-processor-org'''	)						,			push_to_hub=SCREAMING_SNAKE_CASE						,			use_auth_token=self._token						,			organization='''valid_org'''						,			)

             A       :   int					=						WavaVecaProcessor.from_pretrained('''valid_org/test-processor-org'''	)
             for k, v in processor.feature_extractor.__dict__.items():
                 self.assertEqual(SCREAMING_SNAKE_CASE						,			getattr(new_processor.feature_extractor						,			SCREAMING_SNAKE_CASE	)	)
             self.assertDictEqual(new_processor.tokenizer.get_vocab()						,			processor.tokenizer.get_vocab()	)





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






         CustomFeatureExtractor.register_for_auto_class()
         CustomTokenizer.register_for_auto_class()
         CustomProcessor.register_for_auto_class()

         A       :   Any					=						CustomFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE	)

         with tempfile.TemporaryDirectory() as tmp_dir:
             A       :   Union[str, Any]					=						os.path.join(SCREAMING_SNAKE_CASE						,			'''vocab.txt'''	)
             with open(SCREAMING_SNAKE_CASE						,			'''w'''						,			encoding='''utf-8'''	) as vocab_writer:
                 vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]	)	)
             A       :   str					=						CustomTokenizer(SCREAMING_SNAKE_CASE	)

         A       :   Union[str, Any]					=						CustomProcessor(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

         with tempfile.TemporaryDirectory() as tmp_dir:
             create_repo(F'{USER}/test-dynamic-processor'						,			token=self._token	)
             A       :   List[str]					=						Repository(SCREAMING_SNAKE_CASE						,			clone_from=F'{USER}/test-dynamic-processor'						,			token=self._token	)
             processor.save_pretrained(SCREAMING_SNAKE_CASE	)

             # This has added the proper auto_map field to the feature extractor config
             self.assertDictEqual(
                 processor.feature_extractor.auto_map						,			{
                     '''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor''',
                     '''AutoProcessor''': '''custom_processing.CustomProcessor''',
                 }						,			)

             # This has added the proper auto_map field to the tokenizer config
             with open(os.path.join(SCREAMING_SNAKE_CASE						,			'''tokenizer_config.json'''	)	) as f:
                 A       :   Dict					=						json.load(SCREAMING_SNAKE_CASE	)
             self.assertDictEqual(
                 tokenizer_config['''auto_map''']						,			{
                     '''AutoTokenizer''': ['''custom_tokenization.CustomTokenizer''', None],
                     '''AutoProcessor''': '''custom_processing.CustomProcessor''',
                 }						,			)

             # The code has been copied from fixtures
             self.assertTrue(os.path.isfile(os.path.join(SCREAMING_SNAKE_CASE						,			'''custom_feature_extraction.py'''	)	)	)
             self.assertTrue(os.path.isfile(os.path.join(SCREAMING_SNAKE_CASE						,			'''custom_tokenization.py'''	)	)	)
             self.assertTrue(os.path.isfile(os.path.join(SCREAMING_SNAKE_CASE						,			'''custom_processing.py'''	)	)	)

             repo.push_to_hub()

         A       :   Optional[int]					=						AutoProcessor.from_pretrained(F'{USER}/test-dynamic-processor'						,			trust_remote_code=SCREAMING_SNAKE_CASE	)
         # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module
         self.assertEqual(new_processor.__class__.__name__						,			'''CustomProcessor'''	)




