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     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= 'new-model'


if is_tf_available():
    class     a_			(						SCREAMING_SNAKE_CASE						):






     '''simple docstring'''






     UpperCAmelCase_     			= NewModelConfig
@require_tf
class     a_			(						unittest.TestCase						):






 '''simple docstring'''






 @slow
 def 		__snake_case						(      self :  Tuple):







        '''simple docstring'''



        lowerCAmelCase__					     = 'bert-base-cased'
        lowerCAmelCase__					     = AutoConfig.from_pretrained(lowercase__)
        self.assertIsNotNone(lowercase__)
        self.assertIsInstance(lowercase__      ,   lowercase__)

        lowerCAmelCase__					     = TFAutoModel.from_pretrained(lowercase__)
        self.assertIsNotNone(lowercase__)
        self.assertIsInstance(lowercase__      ,   lowercase__)




 @slow
 def 		__snake_case						(      self :  Tuple):







        '''simple docstring'''



        lowerCAmelCase__					     = 'bert-base-cased'
        lowerCAmelCase__					     = AutoConfig.from_pretrained(lowercase__)
        self.assertIsNotNone(lowercase__)
        self.assertIsInstance(lowercase__      ,   lowercase__)

        lowerCAmelCase__					     = TFAutoModelForPreTraining.from_pretrained(lowercase__)
        self.assertIsNotNone(lowercase__)
        self.assertIsInstance(lowercase__      ,   lowercase__)




 @slow
 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



        for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowerCAmelCase__					     = AutoConfig.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)

               lowerCAmelCase__					     = TFAutoModelForCausalLM.from_pretrained(lowercase__)
               lowerCAmelCase__     , lowerCAmelCase__					     = TFAutoModelForCausalLM.from_pretrained(lowercase__      ,   output_loading_info=lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)




 @slow
 def 		__snake_case						(      self :  str):







        '''simple docstring'''



        for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowerCAmelCase__					     = AutoConfig.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)

               lowerCAmelCase__					     = TFAutoModelWithLMHead.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)




 @slow
 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowerCAmelCase__					     = AutoConfig.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)

               lowerCAmelCase__					     = TFAutoModelForMaskedLM.from_pretrained(lowercase__)
               lowerCAmelCase__     , lowerCAmelCase__					     = TFAutoModelForMaskedLM.from_pretrained(lowercase__      ,   output_loading_info=lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)




 @slow
 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowerCAmelCase__					     = AutoConfig.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)

               lowerCAmelCase__					     = TFAutoModelForSeqaSeqLM.from_pretrained(lowercase__)
               lowerCAmelCase__     , lowerCAmelCase__					     = TFAutoModelForSeqaSeqLM.from_pretrained(lowercase__      ,   output_loading_info=lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)




 @slow
 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



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

               lowerCAmelCase__					     = TFAutoModelForSequenceClassification.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)




 @slow
 def 		__snake_case						(      self :  str):







        '''simple docstring'''



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

               lowerCAmelCase__					     = TFAutoModelForQuestionAnswering.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)




 @slow
 @require_tensorflow_probability
 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



        for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
               lowerCAmelCase__					     = AutoConfig.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)

               lowerCAmelCase__					     = TFAutoModelForTableQuestionAnswering.from_pretrained(lowercase__)
               lowerCAmelCase__     , lowerCAmelCase__					     = TFAutoModelForTableQuestionAnswering.from_pretrained(
                   lowercase__      ,   output_loading_info=lowercase__)
               self.assertIsNotNone(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)




 def 		__snake_case						(      self :  str):







        '''simple docstring'''



        lowerCAmelCase__					     = TFAutoModelWithLMHead.from_pretrained(lowercase__)
        self.assertIsInstance(lowercase__      ,   lowercase__)
        self.assertEqual(model.num_parameters()      ,   14_410)
        self.assertEqual(model.num_parameters(only_trainable=lowercase__)      ,   14_410)




 def 		__snake_case						(      self :  Tuple):







        '''simple docstring'''



        lowerCAmelCase__					     = TFAutoModelWithLMHead.from_pretrained(lowercase__)
        self.assertIsInstance(lowercase__      ,   lowercase__)
        self.assertEqual(model.num_parameters()      ,   14_410)
        self.assertEqual(model.num_parameters(only_trainable=lowercase__)      ,   14_410)




 def 		__snake_case						(      self :  Tuple):







        '''simple docstring'''



        lowerCAmelCase__					     = TFAutoModel.from_pretrained('sgugger/funnel-random-tiny')
        self.assertIsInstance(lowercase__      ,   lowercase__)

        lowerCAmelCase__					     = copy.deepcopy(model.config)
        lowerCAmelCase__					     = ['FunnelBaseModel']
        lowerCAmelCase__					     = TFAutoModel.from_config(lowercase__)
        self.assertIsInstance(lowercase__      ,   lowercase__)

        with tempfile.TemporaryDirectory() as tmp_dir:
               model.save_pretrained(lowercase__)
               lowerCAmelCase__					     = TFAutoModel.from_pretrained(lowercase__)
               self.assertIsInstance(lowercase__      ,   lowercase__)




 def 		__snake_case						(      self :  int):







        '''simple docstring'''



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

               lowerCAmelCase__					     = [
                   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(lowercase__):
                                    auto_class.register(lowercase__      ,   lowercase__)
                             auto_class.register(lowercase__      ,   lowercase__)
                             # Trying to register something existing in the Transformers library will raise an error
                             with self.assertRaises(lowercase__):
                                    auto_class.register(lowercase__      ,   lowercase__)

                             # Now that the config is registered, it can be used as any other config with the auto-API
                             lowerCAmelCase__					     = BertModelTester(self).get_config()
                             lowerCAmelCase__					     = NewModelConfig(**tiny_config.to_dict())
                             lowerCAmelCase__					     = auto_class.from_config(lowercase__)
                             self.assertIsInstance(lowercase__      ,   lowercase__)

                             with tempfile.TemporaryDirectory() as tmp_dir:
                                    model.save_pretrained(lowercase__)
                                    lowerCAmelCase__					     = auto_class.from_pretrained(lowercase__)
                                    self.assertIsInstance(lowercase__      ,   lowercase__)

        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 :  Any):







        '''simple docstring'''



        with self.assertRaisesRegex(
            lowercase__      ,   'bert-base is not a local folder and is not a valid model identifier'):
               lowerCAmelCase__					     = TFAutoModel.from_pretrained('bert-base')




 def 		__snake_case						(      self :  Tuple):







        '''simple docstring'''



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




 def 		__snake_case						(      self :  str):







        '''simple docstring'''



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




 def 		__snake_case						(      self :  str):







        '''simple docstring'''



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




 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert')
        with RequestCounter() as counter:
               lowerCAmelCase__					     = 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
        lowerCAmelCase__					     = TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded')
        with RequestCounter() as counter:
               lowerCAmelCase__					     = 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)

from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
    import PIL


lowerCAmelCase__     						=			logging.get_logger(__name__)
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= ['pixel_values']




 def __init__(      self :  Tuple      ,   lowercase__ :  bool = True      ,   lowercase__ :  Dict[str, int] = None      ,   lowercase__ :  PILImageResampling = PILImageResampling.BICUBIC      ,   lowercase__ :  bool = True      ,   lowercase__ :  Union[int, float] = 1 / 255      ,   lowercase__ :  bool = True      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  bool = True      ,   **lowercase__ :  List[Any]      ,   ):







        '''simple docstring'''



        super().__init__(**lowercase__)
        lowerCAmelCase__					     = size if size is not None else {'height': 384, 'width': 384}
        lowerCAmelCase__					     = get_size_dict(lowercase__      ,   default_to_square=lowercase__)

        lowerCAmelCase__					     = do_resize
        lowerCAmelCase__					     = size
        lowerCAmelCase__					     = resample
        lowerCAmelCase__					     = do_rescale
        lowerCAmelCase__					     = rescale_factor
        lowerCAmelCase__					     = do_normalize
        lowerCAmelCase__					     = image_mean if image_mean is not None else OPENAI_CLIP_MEAN
        lowerCAmelCase__					     = image_std if image_std is not None else OPENAI_CLIP_STD
        lowerCAmelCase__					     = do_convert_rgb




 def 		__snake_case						(      self :  List[str]      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  Dict[str, int]      ,   lowercase__ :  PILImageResampling = PILImageResampling.BICUBIC      ,   lowercase__ :  Optional[Union[str, ChannelDimension]] = None      ,   **lowercase__ :  Dict      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = get_size_dict(lowercase__      ,   default_to_square=lowercase__)
        if "height" not in size or "width" not in size:
               raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""")
        lowerCAmelCase__					     = (size['height'], size['width'])
        return resize(lowercase__      ,   size=lowercase__      ,   resample=lowercase__      ,   data_format=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  List[str]      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  Union[int, float]      ,   lowercase__ :  Optional[Union[str, ChannelDimension]] = None      ,   **lowercase__ :  Union[str, Any]      ,   ):







        '''simple docstring'''



        return rescale(lowercase__      ,   scale=lowercase__      ,   data_format=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  Optional[Any]      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  Union[float, List[float]]      ,   lowercase__ :  Union[float, List[float]]      ,   lowercase__ :  Optional[Union[str, ChannelDimension]] = None      ,   **lowercase__ :  Any      ,   ):







        '''simple docstring'''



        return normalize(lowercase__      ,   mean=lowercase__      ,   std=lowercase__      ,   data_format=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  Any      ,   lowercase__ :  ImageInput      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Dict[str, int]] = None      ,   lowercase__ :  PILImageResampling = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[float] = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  Optional[Union[str, TensorType]] = None      ,   lowercase__ :  bool = None      ,   lowercase__ :  ChannelDimension = ChannelDimension.FIRST      ,   **lowercase__ :  Dict      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = do_resize if do_resize is not None else self.do_resize
        lowerCAmelCase__					     = resample if resample is not None else self.resample
        lowerCAmelCase__					     = do_rescale if do_rescale is not None else self.do_rescale
        lowerCAmelCase__					     = rescale_factor if rescale_factor is not None else self.rescale_factor
        lowerCAmelCase__					     = do_normalize if do_normalize is not None else self.do_normalize
        lowerCAmelCase__					     = image_mean if image_mean is not None else self.image_mean
        lowerCAmelCase__					     = image_std if image_std is not None else self.image_std
        lowerCAmelCase__					     = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

        lowerCAmelCase__					     = size if size is not None else self.size
        lowerCAmelCase__					     = get_size_dict(lowercase__      ,   default_to_square=lowercase__)

        lowerCAmelCase__					     = make_list_of_images(lowercase__)

        if not valid_images(lowercase__):
               raise ValueError(
                   'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
                   'torch.Tensor, tf.Tensor or jax.ndarray.')

        if do_resize and size is None or resample is None:
               raise ValueError('Size and resample must be specified if do_resize is True.')

        if do_rescale and rescale_factor is None:
               raise ValueError('Rescale factor must be specified if do_rescale is True.')

        if do_normalize and (image_mean is None or image_std is None):
               raise ValueError('Image mean and std must be specified if do_normalize is True.')

        # PIL RGBA images are converted to RGB
        if do_convert_rgb:
               lowerCAmelCase__					     = [convert_to_rgb(lowercase__) for image in images]

        # All transformations expect numpy arrays.
        lowerCAmelCase__					     = [to_numpy_array(lowercase__) for image in images]

        if do_resize:
               lowerCAmelCase__					     = [self.resize(image=lowercase__      ,   size=lowercase__      ,   resample=lowercase__) for image in images]

        if do_rescale:
               lowerCAmelCase__					     = [self.rescale(image=lowercase__      ,   scale=lowercase__) for image in images]

        if do_normalize:
               lowerCAmelCase__					     = [self.normalize(image=lowercase__      ,   mean=lowercase__      ,   std=lowercase__) for image in images]

        lowerCAmelCase__					     = [to_channel_dimension_format(lowercase__      ,   lowercase__) for image in images]

        lowerCAmelCase__					     = BatchFeature(data={'pixel_values': images}      ,   tensor_type=lowercase__)

        return encoded_outputs
