"""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 AutoImageProcessor, ViTImageProcessor
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_image_processing import CustomImageProcessor  # noqa E402


lowercase__ 	=     get_tests_dir('fixtures')




class        __snake_case	(					unittest.TestCase    ):



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





        a__:					Optional[Any]      							=     mock.Mock()
        a__:					Any      							=     5_00
        a__:					Union[str, Any]      							=     {}
        a__:					List[str]      							=     HTTPError
        a__:					Optional[Any]      							=     {}

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



      def   lowerCamelCase_       (						self)     ->      List[Any]:





        '''simple docstring'''





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



      def   lowerCamelCase_       (						self)     ->      List[Any]:





        '''simple docstring'''





        with self.assertRaises(lowercase):
          # config is in subfolder, the following should not work without specifying the subfolder
          a__:					int      							=     AutoImageProcessor.from_pretrained('hf-internal-testing/stable-diffusion-all-variants')

        a__:					List[str]      							=     AutoImageProcessor.from_pretrained(
            'hf-internal-testing/stable-diffusion-all-variants'   ,      subfolder='feature_extractor')

        self.assertIsNotNone(lowercase)







@is_staging_test
class        __snake_case	(					unittest.TestCase    ):



      @classmethod
      def   lowerCamelCase_       (						cls)     ->      Optional[Any]:





        '''simple docstring'''





        a__:					Any      							=     TOKEN
        HfFolder.save_token(lowercase)



      @classmethod
      def   lowerCamelCase_       (						cls)     ->      Tuple:





        '''simple docstring'''





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

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

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



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





        '''simple docstring'''





        a__:					Optional[int]      							=     ViTImageProcessor.from_pretrained(lowercase)
        image_processor.push_to_hub('test-image-processor'   ,      use_auth_token=self._token)

        a__:					Optional[int]      							=     ViTImageProcessor.from_pretrained(f'{USER}/test-image-processor')
        for k, v in image_processor.__dict__.items():
          self.assertEqual(lowercase   ,      getattr(lowercase   ,      lowercase))

        # Reset repo
        delete_repo(token=self._token   ,      repo_id='test-image-processor')

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
          image_processor.save_pretrained(
              lowercase   ,      repo_id='test-image-processor'   ,      push_to_hub=lowercase   ,      use_auth_token=self._token)

        a__:					Optional[Any]      							=     ViTImageProcessor.from_pretrained(f'{USER}/test-image-processor')
        for k, v in image_processor.__dict__.items():
          self.assertEqual(lowercase   ,      getattr(lowercase   ,      lowercase))



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





        '''simple docstring'''





        a__:					str      							=     ViTImageProcessor.from_pretrained(lowercase)
        image_processor.push_to_hub('valid_org/test-image-processor'   ,      use_auth_token=self._token)

        a__:					List[Any]      							=     ViTImageProcessor.from_pretrained('valid_org/test-image-processor')
        for k, v in image_processor.__dict__.items():
          self.assertEqual(lowercase   ,      getattr(lowercase   ,      lowercase))

        # Reset repo
        delete_repo(token=self._token   ,      repo_id='valid_org/test-image-processor')

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
          image_processor.save_pretrained(
              lowercase   ,      repo_id='valid_org/test-image-processor-org'   ,      push_to_hub=lowercase   ,      use_auth_token=self._token)

        a__:					Optional[Any]      							=     ViTImageProcessor.from_pretrained('valid_org/test-image-processor-org')
        for k, v in image_processor.__dict__.items():
          self.assertEqual(lowercase   ,      getattr(lowercase   ,      lowercase))



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





        '''simple docstring'''





        CustomImageProcessor.register_for_auto_class()
        a__:					Dict      							=     CustomImageProcessor.from_pretrained(lowercase)

        image_processor.push_to_hub('test-dynamic-image-processor'   ,      use_auth_token=self._token)

        # This has added the proper auto_map field to the config
        self.assertDictEqual(
            image_processor.auto_map   ,      {'AutoImageProcessor': 'custom_image_processing.CustomImageProcessor'}   ,      )

        a__:					Optional[int]      							=     AutoImageProcessor.from_pretrained(
            f'{USER}/test-dynamic-image-processor'   ,      trust_remote_code=lowercase)
        # Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module
        self.assertEqual(new_image_processor.__class__.__name__   ,      'CustomImageProcessor')



"""simple docstring"""
import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_lxmert import LxmertTokenizer


lowercase__ 	=     {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

lowercase__ 	=     {
    'vocab_file': {
        'unc-nlp/lxmert-base-uncased': 'https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt',
    },
    'tokenizer_file': {
        'unc-nlp/lxmert-base-uncased': (
            'https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json'
        ),
    },
}

lowercase__ 	=     {
    'unc-nlp/lxmert-base-uncased': 512,
}

lowercase__ 	=     {
    'unc-nlp/lxmert-base-uncased': {'do_lower_case': True},
}




class        __snake_case	(					__lowerCAmelCase    ):
      a__				=		VOCAB_FILES_NAMES
      a__				=		PRETRAINED_VOCAB_FILES_MAP
      a__				=		PRETRAINED_INIT_CONFIGURATION
      a__				=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      a__				=		LxmertTokenizer



      def __init__(						self   ,      lowercase=None   ,      lowercase=None   ,      lowercase=True   ,      lowercase="[UNK]"   ,      lowercase="[SEP]"   ,      lowercase="[PAD]"   ,      lowercase="[CLS]"   ,      lowercase="[MASK]"   ,      lowercase=True   ,      lowercase=None   ,      **lowercase   ,      )     ->      Dict:





        '''simple docstring'''





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

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

        a__:					Optional[int]      							=     do_lower_case



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase=None)     ->      Tuple:





        '''simple docstring'''





        a__:					Optional[Any]      							=     [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   ,      lowercase   ,      lowercase = None)     ->      List[int]:





        '''simple docstring'''





        a__:					List[Any]      							=     [self.sep_token_id]
        a__:					List[Any]      							=     [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   ,      lowercase   ,      lowercase = None)     ->      Tuple[str]:





        '''simple docstring'''





        a__:					List[Any]      							=     self._tokenizer.model.save(lowercase   ,      name=lowercase)
        return tuple(lowercase)


