

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__						=							get_tests_dir("fixtures")
A__						=							get_tests_dir("fixtures/dummy_feature_extractor_config.json")
A__						=							get_tests_dir("fixtures/dummy-config.json")

class 				__UpperCamelCase				(					unittest.TestCase			):







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[int]      ):

						'''simple docstring'''





						__magic_name__							=    0







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						__magic_name__							=    AutoFeatureExtractor.from_pretrained('facebook/wav2vec2-base-960h'      )
						self.assertIsInstance(__UpperCamelCase  , __UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]      ):

						'''simple docstring'''





						__magic_name__							=    AutoFeatureExtractor.from_pretrained(__UpperCamelCase      )
						self.assertIsInstance(__UpperCamelCase  , __UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						with tempfile.TemporaryDirectory() as tmpdirname:
											__magic_name__							=    WavaVecaConfig()

											# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
											__magic_name__							=    AutoFeatureExtractor.from_pretrained(__UpperCamelCase      ).to_dict()

											config_dict.pop('feature_extractor_type'      )
											__magic_name__							=    WavaVecaFeatureExtractor(**__UpperCamelCase      )

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

											__magic_name__							=    AutoFeatureExtractor.from_pretrained(__UpperCamelCase      )

											# make sure private variable is not incorrectly saved
											__magic_name__							=    json.loads(config.to_json_string()      )
											self.assertTrue('_processor_class' not in dict_as_saved      )

						self.assertIsInstance(__UpperCamelCase  , __UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''simple docstring'''





						__magic_name__							=    AutoFeatureExtractor.from_pretrained(__UpperCamelCase      )
						self.assertIsInstance(__UpperCamelCase  , __UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						with self.assertRaisesRegex(
						    __UpperCamelCase  , 'bert-base is not a local folder and is not a valid model identifier'      ):
											__magic_name__							=    AutoFeatureExtractor.from_pretrained('bert-base'      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Dict      ):

						'''simple docstring'''





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







	def 	_SCREAMING_SNAKE_CASE							(   self: int      ):

						'''simple docstring'''





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







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

						'''simple docstring'''





						with self.assertRaises(__UpperCamelCase      ):
											__magic_name__							=    AutoFeatureExtractor.from_pretrained(
											    'hf-internal-testing/test_dynamic_feature_extractor'      )
						# If remote code is disabled, we can't load this config.
						with self.assertRaises(__UpperCamelCase      ):
											__magic_name__							=    AutoFeatureExtractor.from_pretrained(
											    'hf-internal-testing/test_dynamic_feature_extractor'  , trust_remote_code=__UpperCamelCase      )

						__magic_name__							=    AutoFeatureExtractor.from_pretrained(
						    'hf-internal-testing/test_dynamic_feature_extractor'  , trust_remote_code=__UpperCamelCase      )
						self.assertEqual(feature_extractor.__class__.__name__  , 'NewFeatureExtractor'      )

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







	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''simple docstring'''





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

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

						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 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						class 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE			):
							_lowercase  :   str       				=  True

						try:
											AutoConfig.register('custom'  , __UpperCamelCase      )
											AutoFeatureExtractor.register(__UpperCamelCase  , __UpperCamelCase      )
											# If remote code is not set, the default is to use local
											__magic_name__							=    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.
											__magic_name__							=    AutoFeatureExtractor.from_pretrained(
											    'hf-internal-testing/test_dynamic_feature_extractor'  , trust_remote_code=__UpperCamelCase      )
											self.assertEqual(feature_extractor.__class__.__name__  , 'NewFeatureExtractor'      )
											self.assertTrue(feature_extractor.is_local      )

											# If remote is enabled, we load from the Hub
											__magic_name__							=    AutoFeatureExtractor.from_pretrained(
											    'hf-internal-testing/test_dynamic_feature_extractor'  , trust_remote_code=__UpperCamelCase      )
											self.assertEqual(feature_extractor.__class__.__name__  , 'NewFeatureExtractor'      )
											self.assertTrue(not hasattr(__UpperCamelCase  , '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]







import os
import unittest

from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast
from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin

@require_tokenizers
class 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE  ,						unittest.TestCase			):
	_lowercase  :   Tuple       				=  LayoutLMTokenizer
	_lowercase  :   List[str]       				=  LayoutLMTokenizerFast
	_lowercase  :   List[Any]       				=  True
	_lowercase  :   Optional[int]       				=  True







	def 	_SCREAMING_SNAKE_CASE							(   self: Any      ):

						'''simple docstring'''





						super().setUp()

						__magic_name__							=    [
						    '[UNK]',
						    '[CLS]',
						    '[SEP]',
						    'want',
						    '##want',
						    '##ed',
						    'wa',
						    'un',
						    'runn',
						    '##ing',
						    ',',
						    'low',
						    'lowest',
						]
						__magic_name__							=    os.path.join(self.tmpdirname  , VOCAB_FILES_NAMES['vocab_file']      )
						with open(self.vocab_file  , 'w'  , encoding='utf-8'      ) as vocab_writer:
											vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]      )      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]  , **__UpperCamelCase: Union[str, Any]      ):

						'''simple docstring'''





						return LayoutLMTokenizer.from_pretrained(self.tmpdirname  , **__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple  , __UpperCamelCase: List[str]      ):

						'''simple docstring'''





						__magic_name__							=    'UNwant\u00E9d,running'
						__magic_name__							=    'unwanted, running'
						return input_text, output_text







	def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]      ):

						'''simple docstring'''





						__magic_name__							=    self.tokenizer_class(self.vocab_file      )

						__magic_name__							=    tokenizer.tokenize('UNwant\u00E9d,running'      )
						self.assertListEqual(__UpperCamelCase  , ['un', '##want', '##ed', ',', 'runn', '##ing']      )
						self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCamelCase      )  , [7, 4, 5, 10, 8, 9]      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Dict      ):

						'''simple docstring'''





						pass




