
'''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  A				(							__snake_case						):
     __magic_name__						=		'''new-model'''


if is_tf_available():





      class  A				(							__snake_case						):
           __magic_name__						=		NewModelConfig





@require_tf
class  A				(							unittest.TestCase						):





     @slow
     def 				__lowerCAmelCase							(							self	) ->				Any:






         """simple docstring"""






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

         A       :   Any					=						TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE	)
         self.assertIsNotNone(SCREAMING_SNAKE_CASE	)
         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





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






         """simple docstring"""






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

         A       :   Optional[int]					=						TFAutoModelForPreTraining.from_pretrained(SCREAMING_SNAKE_CASE	)
         self.assertIsNotNone(SCREAMING_SNAKE_CASE	)
         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     @slow
     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






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

             A       :   Tuple					=						TFAutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE	)
             A,  A       :   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 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






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

             A       :   Any					=						TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE	)
             self.assertIsNotNone(SCREAMING_SNAKE_CASE	)
             self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





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






         """simple docstring"""






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

             A       :   int					=						TFAutoModelForMaskedLM.from_pretrained(SCREAMING_SNAKE_CASE	)
             A,  A       :   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 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






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

             A       :   Tuple					=						TFAutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE	)
             A,  A       :   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 				__lowerCAmelCase							(							self	) ->				List[Any]:






         """simple docstring"""






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

             A       :   int					=						TFAutoModelForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE	)
             self.assertIsNotNone(SCREAMING_SNAKE_CASE	)
             self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     @slow
     def 				__lowerCAmelCase							(							self	) ->				int:






         """simple docstring"""






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

             A       :   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 				__lowerCAmelCase							(							self	) ->				List[Any]:






         """simple docstring"""






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

             A       :   List[str]					=						TFAutoModelForTableQuestionAnswering.from_pretrained(SCREAMING_SNAKE_CASE	)
             A,  A       :   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 				__lowerCAmelCase							(							self	) ->				List[Any]:






         """simple docstring"""






         A       :   Dict					=						TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE	)
         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)
         self.assertEqual(model.num_parameters()						,			14410	)
         self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE	)						,			14410	)





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






         """simple docstring"""






         A       :   Union[str, Any]					=						TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE	)
         self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)
         self.assertEqual(model.num_parameters()						,			14410	)
         self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE	)						,			14410	)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






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

         A       :   Optional[int]					=						copy.deepcopy(model.config	)
         A       :   str					=						['''FunnelBaseModel''']
         A       :   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	)
             A       :   Dict					=						TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE	)
             self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





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






         """simple docstring"""






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

             A       :   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
                     A       :   Union[str, Any]					=						BertModelTester(self	).get_config()
                     A       :   List[Any]					=						NewModelConfig(**tiny_config.to_dict()	)
                     A       :   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	)
                         A       :   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 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






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





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






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





     def 				__lowerCAmelCase							(							self	) ->				str:






         """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'''						,			):
             A       :   Any					=						TFAutoModel.from_pretrained('''hf-internal-testing/config-no-model'''	)





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






         """simple docstring"""






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





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






         """simple docstring"""






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




from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
      from PIL import Image

      from ..image_utils import load_image

if is_torch_available():
      from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

lowercase			:				str    		=			logging.get_logger(__name__)





@add_end_docstrings(__snake_case						)
class  A				(							__snake_case						):





     def __init__(							self						,			*SCREAMING_SNAKE_CASE						,			**SCREAMING_SNAKE_CASE	) ->				List[Any]:






         """simple docstring"""






         super().__init__(*SCREAMING_SNAKE_CASE						,			**SCREAMING_SNAKE_CASE	)
         self.check_model_type(SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE=None						,			SCREAMING_SNAKE_CASE=None						,			SCREAMING_SNAKE_CASE=None						,			**SCREAMING_SNAKE_CASE	) ->				Tuple:






         """simple docstring"""






         A,  A       :   Dict					=						{}, {}
         if padding is not None:
             A       :   List[str]					=						padding
         if truncation is not None:
             A       :   Dict					=						truncation
         if top_k is not None:
             A       :   Optional[Any]					=						top_k
         return preprocess_params, {}, postprocess_params





     def __call__(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE = None						,			**SCREAMING_SNAKE_CASE	) ->				Dict:






         """simple docstring"""






         if isinstance(SCREAMING_SNAKE_CASE						,			(Image.Image, str)	) and isinstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	):
             A       :   int					=						{'''image''': image, '''question''': question}
         else:
             A       :   Any					=						image
         A       :   Any					=						super().__call__(SCREAMING_SNAKE_CASE						,			**SCREAMING_SNAKE_CASE	)
         return results





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE=False						,			SCREAMING_SNAKE_CASE=False	) ->				Any:






         """simple docstring"""






         A       :   Union[str, Any]					=						load_image(inputs['''image''']	)
         A       :   Optional[Any]					=						self.tokenizer(
             inputs['''question''']						,			return_tensors=self.framework						,			padding=SCREAMING_SNAKE_CASE						,			truncation=SCREAMING_SNAKE_CASE	)
         A       :   Dict					=						self.image_processor(images=SCREAMING_SNAKE_CASE						,			return_tensors=self.framework	)
         model_inputs.update(SCREAMING_SNAKE_CASE	)
         return model_inputs





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






         """simple docstring"""






         A       :   List[Any]					=						self.model(**SCREAMING_SNAKE_CASE	)
         return model_outputs





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE=5	) ->				int:






         """simple docstring"""






         if top_k > self.model.config.num_labels:
             A       :   Dict					=						self.model.config.num_labels

         if self.framework == "pt":
             A       :   Optional[int]					=						model_outputs.logits.sigmoid()[0]
             A,  A       :   int					=						probs.topk(SCREAMING_SNAKE_CASE	)
         else:
             raise ValueError(F'Unsupported framework: {self.framework}'	)

         A       :   int					=						scores.tolist()
         A       :   List[str]					=						ids.tolist()
         return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)]




