

"""simple docstring"""






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


_lowerCAmelCase						=		get_tests_dir("""fixtures""")







class 	__UpperCamelCase    (    unittest.TestCase						):

		def __lowerCamelCase		(		self      ):
						'''simple docstring'''

						_lowerCAmelCase       :   Optional[int]  =    mock.Mock()
						_lowerCAmelCase       :   Dict  =    500
						_lowerCAmelCase       :   Union[str, Any]  =    {}
						_lowerCAmelCase       :   Optional[int]  =    HTTPError
						_lowerCAmelCase       :   List[Any]  =    {}

						# Download this model to make sure it's in the cache.
						_lowerCAmelCase       :   str  =    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=_A      ) as mock_head:
										_lowerCAmelCase       :   Any  =    WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'      )
										# This check we did call the fake head request
										mock_head.assert_called()







		def __lowerCamelCase		(		self      ):
						'''simple docstring'''

						_lowerCAmelCase       :   Optional[Any]  =    WavaVecaFeatureExtractor.from_pretrained(
						    'https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json'      )







@is_staging_test
class 	__UpperCamelCase    (    unittest.TestCase						):

		@classmethod
		def __lowerCamelCase		(		cls      ):
						'''simple docstring'''

						_lowerCAmelCase       :   Any  =    TOKEN
						HfFolder.save_token(_A      )

		@classmethod
		def __lowerCamelCase		(		cls      ):
						'''simple docstring'''

						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 __lowerCamelCase		(		self      ):
						'''simple docstring'''

						_lowerCAmelCase       :   Tuple  =    WavaVecaFeatureExtractor.from_pretrained(_A      )
						feature_extractor.push_to_hub('test-feature-extractor'    ,use_auth_token=self._token      )

						_lowerCAmelCase       :   List[Any]  =    WavaVecaFeatureExtractor.from_pretrained(F"""{USER}/test-feature-extractor"""      )
						for k, v in feature_extractor.__dict__.items():
										self.assertEqual(_A    ,getattr(_A    ,_A      )      )

						# 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(
										    _A    ,repo_id='test-feature-extractor'    ,push_to_hub=_A    ,use_auth_token=self._token      )

						_lowerCAmelCase       :   Union[str, Any]  =    WavaVecaFeatureExtractor.from_pretrained(F"""{USER}/test-feature-extractor"""      )
						for k, v in feature_extractor.__dict__.items():
										self.assertEqual(_A    ,getattr(_A    ,_A      )      )

		def __lowerCamelCase		(		self      ):
						'''simple docstring'''

						_lowerCAmelCase       :   Optional[int]  =    WavaVecaFeatureExtractor.from_pretrained(_A      )
						feature_extractor.push_to_hub('valid_org/test-feature-extractor'    ,use_auth_token=self._token      )

						_lowerCAmelCase       :   int  =    WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor'      )
						for k, v in feature_extractor.__dict__.items():
										self.assertEqual(_A    ,getattr(_A    ,_A      )      )

						# 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(
										    _A    ,repo_id='valid_org/test-feature-extractor-org'    ,push_to_hub=_A    ,use_auth_token=self._token      )

						_lowerCAmelCase       :   str  =    WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor-org'      )
						for k, v in feature_extractor.__dict__.items():
										self.assertEqual(_A    ,getattr(_A    ,_A      )      )







		def __lowerCamelCase		(		self      ):
						'''simple docstring'''

						CustomFeatureExtractor.register_for_auto_class()
						_lowerCAmelCase       :   List[str]  =    CustomFeatureExtractor.from_pretrained(_A      )

						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'}    ,)

						_lowerCAmelCase       :   Dict  =    AutoFeatureExtractor.from_pretrained(
						    F"""{USER}/test-dynamic-feature-extractor"""    ,trust_remote_code=_A      )
						# 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'      )








"""simple docstring"""






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bert import BertTokenizer


_lowerCAmelCase						=		logging.get_logger(__name__)

_lowerCAmelCase						=		{"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

_lowerCAmelCase						=		{
    """vocab_file""": {
        """bert-base-uncased""": """https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt""",
        """bert-large-uncased""": """https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt""",
        """bert-base-cased""": """https://huggingface.co/bert-base-cased/resolve/main/vocab.txt""",
        """bert-large-cased""": """https://huggingface.co/bert-large-cased/resolve/main/vocab.txt""",
        """bert-base-multilingual-uncased""": (
            """https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt"""
        ),
        """bert-base-multilingual-cased""": """https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt""",
        """bert-base-chinese""": """https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt""",
        """bert-base-german-cased""": """https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt""",
        """bert-large-uncased-whole-word-masking""": (
            """https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt"""
        ),
        """bert-large-cased-whole-word-masking""": (
            """https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt"""
        ),
        """bert-large-uncased-whole-word-masking-finetuned-squad""": (
            """https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt"""
        ),
        """bert-large-cased-whole-word-masking-finetuned-squad""": (
            """https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt"""
        ),
        """bert-base-cased-finetuned-mrpc""": (
            """https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt"""
        ),
        """bert-base-german-dbmdz-cased""": """https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt""",
        """bert-base-german-dbmdz-uncased""": (
            """https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt"""
        ),
        """TurkuNLP/bert-base-finnish-cased-v1""": (
            """https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt"""
        ),
        """TurkuNLP/bert-base-finnish-uncased-v1""": (
            """https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt"""
        ),
        """wietsedv/bert-base-dutch-cased""": (
            """https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt"""
        ),
    },
    """tokenizer_file""": {
        """bert-base-uncased""": """https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json""",
        """bert-large-uncased""": """https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json""",
        """bert-base-cased""": """https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json""",
        """bert-large-cased""": """https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json""",
        """bert-base-multilingual-uncased""": (
            """https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json"""
        ),
        """bert-base-multilingual-cased""": (
            """https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json"""
        ),
        """bert-base-chinese""": """https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json""",
        """bert-base-german-cased""": """https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json""",
        """bert-large-uncased-whole-word-masking""": (
            """https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json"""
        ),
        """bert-large-cased-whole-word-masking""": (
            """https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json"""
        ),
        """bert-large-uncased-whole-word-masking-finetuned-squad""": (
            """https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json"""
        ),
        """bert-large-cased-whole-word-masking-finetuned-squad""": (
            """https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json"""
        ),
        """bert-base-cased-finetuned-mrpc""": (
            """https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json"""
        ),
        """bert-base-german-dbmdz-cased""": (
            """https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json"""
        ),
        """bert-base-german-dbmdz-uncased""": (
            """https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json"""
        ),
        """TurkuNLP/bert-base-finnish-cased-v1""": (
            """https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json"""
        ),
        """TurkuNLP/bert-base-finnish-uncased-v1""": (
            """https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json"""
        ),
        """wietsedv/bert-base-dutch-cased""": (
            """https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json"""
        ),
    },
}

_lowerCAmelCase						=		{
    """bert-base-uncased""": 5_1_2,
    """bert-large-uncased""": 5_1_2,
    """bert-base-cased""": 5_1_2,
    """bert-large-cased""": 5_1_2,
    """bert-base-multilingual-uncased""": 5_1_2,
    """bert-base-multilingual-cased""": 5_1_2,
    """bert-base-chinese""": 5_1_2,
    """bert-base-german-cased""": 5_1_2,
    """bert-large-uncased-whole-word-masking""": 5_1_2,
    """bert-large-cased-whole-word-masking""": 5_1_2,
    """bert-large-uncased-whole-word-masking-finetuned-squad""": 5_1_2,
    """bert-large-cased-whole-word-masking-finetuned-squad""": 5_1_2,
    """bert-base-cased-finetuned-mrpc""": 5_1_2,
    """bert-base-german-dbmdz-cased""": 5_1_2,
    """bert-base-german-dbmdz-uncased""": 5_1_2,
    """TurkuNLP/bert-base-finnish-cased-v1""": 5_1_2,
    """TurkuNLP/bert-base-finnish-uncased-v1""": 5_1_2,
    """wietsedv/bert-base-dutch-cased""": 5_1_2,
}

_lowerCAmelCase						=		{
    """bert-base-uncased""": {"""do_lower_case""": True},
    """bert-large-uncased""": {"""do_lower_case""": True},
    """bert-base-cased""": {"""do_lower_case""": False},
    """bert-large-cased""": {"""do_lower_case""": False},
    """bert-base-multilingual-uncased""": {"""do_lower_case""": True},
    """bert-base-multilingual-cased""": {"""do_lower_case""": False},
    """bert-base-chinese""": {"""do_lower_case""": False},
    """bert-base-german-cased""": {"""do_lower_case""": False},
    """bert-large-uncased-whole-word-masking""": {"""do_lower_case""": True},
    """bert-large-cased-whole-word-masking""": {"""do_lower_case""": False},
    """bert-large-uncased-whole-word-masking-finetuned-squad""": {"""do_lower_case""": True},
    """bert-large-cased-whole-word-masking-finetuned-squad""": {"""do_lower_case""": False},
    """bert-base-cased-finetuned-mrpc""": {"""do_lower_case""": False},
    """bert-base-german-dbmdz-cased""": {"""do_lower_case""": False},
    """bert-base-german-dbmdz-uncased""": {"""do_lower_case""": True},
    """TurkuNLP/bert-base-finnish-cased-v1""": {"""do_lower_case""": False},
    """TurkuNLP/bert-base-finnish-uncased-v1""": {"""do_lower_case""": True},
    """wietsedv/bert-base-dutch-cased""": {"""do_lower_case""": False},
}







class 	__UpperCamelCase    (    a__						):
  _UpperCAmelCase          =		VOCAB_FILES_NAMES
  _UpperCAmelCase          =		PRETRAINED_VOCAB_FILES_MAP
  _UpperCAmelCase          =		PRETRAINED_INIT_CONFIGURATION
  _UpperCAmelCase          =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  _UpperCAmelCase          =		BertTokenizer

  def __init__(		self    ,_A=None    ,_A=None    ,_A=True    ,_A="[UNK]"    ,_A="[SEP]"    ,_A="[PAD]"    ,_A="[CLS]"    ,_A="[MASK]"    ,_A=True    ,_A=None    ,**_A    ,):
      '''simple docstring'''

      super().__init__(
          _A    ,tokenizer_file=_A    ,do_lower_case=_A    ,unk_token=_A    ,sep_token=_A    ,pad_token=_A    ,cls_token=_A    ,mask_token=_A    ,tokenize_chinese_chars=_A    ,strip_accents=_A    ,**_A    ,)

      _lowerCAmelCase       :   Tuple  =    json.loads(self.backend_tokenizer.normalizer.__getstate__()      )
      if (
          normalizer_state.get('lowercase'    ,_A      ) != do_lower_case
          or normalizer_state.get('strip_accents'    ,_A      ) != strip_accents
          or normalizer_state.get('handle_chinese_chars'    ,_A      ) != tokenize_chinese_chars
      ):
          _lowerCAmelCase       :   Dict  =    getattr(_A    ,normalizer_state.pop('type'      )      )
          _lowerCAmelCase       :   Dict  =    do_lower_case
          _lowerCAmelCase       :   Optional[int]  =    strip_accents
          _lowerCAmelCase       :   Union[str, Any]  =    tokenize_chinese_chars
          _lowerCAmelCase       :   Dict  =    normalizer_class(**_A      )

      _lowerCAmelCase       :   Union[str, Any]  =    do_lower_case

  def __lowerCamelCase		(		self    ,_A    ,_A=None      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Tuple  =    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

      if token_ids_a:
          output += token_ids_a + [self.sep_token_id]

      return output

  def __lowerCamelCase		(		self    ,_A    ,_A = None      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    [self.sep_token_id]
      _lowerCAmelCase       :   str  =    [self.cls_token_id]
      if token_ids_a is None:
          return len(cls + token_ids_a + sep      ) * [0]
      return len(cls + token_ids_a + sep      ) * [0] + len(token_ids_a + sep      ) * [1]







  def __lowerCamelCase		(		self    ,_A    ,_A = None      ):
      '''simple docstring'''

      _lowerCAmelCase       :   str  =    self._tokenizer.model.save(_A    ,name=_A      )
      return tuple(_A      )





