"""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        __snake_case	(					__lowerCAmelCase    ):
      a__				=		"""new-model"""


if is_tf_available():




 class        __snake_case	(					__lowerCAmelCase    ):
       a__				=		NewModelConfig




@require_tf
class        __snake_case	(					unittest.TestCase    ):



      @slow
      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





        a__:					str      							=     'bert-base-cased'
        a__:					str      							=     AutoConfig.from_pretrained(lowercase)
        self.assertIsNotNone(lowercase)
        self.assertIsInstance(lowercase   ,      lowercase)

        a__:					int      							=     TFAutoModel.from_pretrained(lowercase)
        self.assertIsNotNone(lowercase)
        self.assertIsInstance(lowercase   ,      lowercase)



      @slow
      def   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





        a__:					Tuple      							=     'bert-base-cased'
        a__:					Optional[Any]      							=     AutoConfig.from_pretrained(lowercase)
        self.assertIsNotNone(lowercase)
        self.assertIsInstance(lowercase   ,      lowercase)

        a__:					int      							=     TFAutoModelForPreTraining.from_pretrained(lowercase)
        self.assertIsNotNone(lowercase)
        self.assertIsInstance(lowercase   ,      lowercase)



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





        '''simple docstring'''





        for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__:					str      							=     AutoConfig.from_pretrained(lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)

          a__:					Tuple      							=     TFAutoModelForCausalLM.from_pretrained(lowercase)
          a__							,   a__:					List[str]      							=     TFAutoModelForCausalLM.from_pretrained(lowercase   ,      output_loading_info=lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)



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





        '''simple docstring'''





        for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__:					Dict      							=     AutoConfig.from_pretrained(lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)

          a__:					int      							=     TFAutoModelWithLMHead.from_pretrained(lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)



      @slow
      def   lowerCamelCase_       (						self)     ->      Any:





        '''simple docstring'''





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

          a__:					Union[str, Any]      							=     TFAutoModelForMaskedLM.from_pretrained(lowercase)
          a__							,   a__:					Optional[int]      							=     TFAutoModelForMaskedLM.from_pretrained(lowercase   ,      output_loading_info=lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)



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





        '''simple docstring'''





        for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__:					str      							=     AutoConfig.from_pretrained(lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)

          a__:					Union[str, Any]      							=     TFAutoModelForSeqaSeqLM.from_pretrained(lowercase)
          a__							,   a__:					Optional[int]      							=     TFAutoModelForSeqaSeqLM.from_pretrained(lowercase   ,      output_loading_info=lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)



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





        '''simple docstring'''





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

          a__:					Optional[Any]      							=     TFAutoModelForSequenceClassification.from_pretrained(lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)



      @slow
      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        for model_name in ["bert-base-uncased"]:
          a__:					Optional[int]      							=     AutoConfig.from_pretrained(lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)

          a__:					Optional[int]      							=     TFAutoModelForQuestionAnswering.from_pretrained(lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)



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





        '''simple docstring'''





        for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
          a__:					Dict      							=     AutoConfig.from_pretrained(lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)

          a__:					Optional[int]      							=     TFAutoModelForTableQuestionAnswering.from_pretrained(lowercase)
          a__							,   a__:					Optional[int]      							=     TFAutoModelForTableQuestionAnswering.from_pretrained(
              lowercase   ,      output_loading_info=lowercase)
          self.assertIsNotNone(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)



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





        '''simple docstring'''





        a__:					int      							=     TFAutoModelWithLMHead.from_pretrained(lowercase)
        self.assertIsInstance(lowercase   ,      lowercase)
        self.assertEqual(model.num_parameters()   ,      1_44_10)
        self.assertEqual(model.num_parameters(only_trainable=lowercase)   ,      1_44_10)



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





        '''simple docstring'''





        a__:					Union[str, Any]      							=     TFAutoModelWithLMHead.from_pretrained(lowercase)
        self.assertIsInstance(lowercase   ,      lowercase)
        self.assertEqual(model.num_parameters()   ,      1_44_10)
        self.assertEqual(model.num_parameters(only_trainable=lowercase)   ,      1_44_10)



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





        a__:					Union[str, Any]      							=     TFAutoModel.from_pretrained('sgugger/funnel-random-tiny')
        self.assertIsInstance(lowercase   ,      lowercase)

        a__:					Tuple      							=     copy.deepcopy(model.config)
        a__:					int      							=     ['FunnelBaseModel']
        a__:					List[str]      							=     TFAutoModel.from_config(lowercase)
        self.assertIsInstance(lowercase   ,      lowercase)

        with tempfile.TemporaryDirectory() as tmp_dir:
          model.save_pretrained(lowercase)
          a__:					Optional[int]      							=     TFAutoModel.from_pretrained(lowercase)
          self.assertIsInstance(lowercase   ,      lowercase)



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





        '''simple docstring'''





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

          a__:					List[Any]      							=     [
              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
              a__:					Any      							=     BertModelTester(self).get_config()
              a__:					Tuple      							=     NewModelConfig(**tiny_config.to_dict())
              a__:					Tuple      							=     auto_class.from_config(lowercase)
              self.assertIsInstance(lowercase   ,      lowercase)

              with tempfile.TemporaryDirectory() as tmp_dir:
                model.save_pretrained(lowercase)
                a__:					Dict      							=     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   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        with self.assertRaisesRegex(
            lowercase   ,      'bert-base is not a local folder and is not a valid model identifier'):
          a__:					List[str]      							=     TFAutoModel.from_pretrained('bert-base')



      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        with self.assertRaisesRegex(
            lowercase   ,      r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'):
          a__:					Optional[int]      							=     TFAutoModel.from_pretrained(lowercase   ,      revision='aaaaaa')



      def   lowerCamelCase_       (						self)     ->      Any:





        '''simple docstring'''





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



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





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



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





        '''simple docstring'''





        a__:					str      							=     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__:					Dict      							=     TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded')
        with RequestCounter() as counter:
          a__:					Optional[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 Any, Dict, List, Union

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


if is_vision_available():
 from ..image_utils import load_image


if is_torch_available():
 import torch

 from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING

lowercase__ 	=     logging.get_logger(__name__)


lowercase__ 	=     Dict[str, Any]
lowercase__ 	=     List[Prediction]




@add_end_docstrings(__lowerCAmelCase    )
class        __snake_case	(					__lowerCAmelCase    ):



      def __init__(						self   ,      *lowercase   ,      **lowercase)     ->      Dict:





        '''simple docstring'''





        super().__init__(*lowercase   ,      **lowercase)

        if self.framework == "tf":
          raise ValueError(f'The {self.__class__} is only available in PyTorch.')

        requires_backends(self   ,      'vision')
        self.check_model_type(
            dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items()))



      def   lowerCamelCase_       (						self   ,      **lowercase)     ->      int:





        '''simple docstring'''





        a__:					Optional[Any]      							=     {}
        if "threshold" in kwargs:
          a__:					Dict      							=     kwargs['threshold']
        return {}, {}, postprocess_kwargs



      def __call__(						self   ,      *lowercase   ,      **lowercase)     ->      Union[Predictions, List[Prediction]]:





        '''simple docstring'''





        return super().__call__(*lowercase   ,      **lowercase)



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





        '''simple docstring'''





        a__:					Optional[Any]      							=     load_image(lowercase)
        a__:					List[Any]      							=     torch.IntTensor([[image.height, image.width]])
        a__:					Any      							=     self.image_processor(images=[image]   ,      return_tensors='pt')
        if self.tokenizer is not None:
          a__:					Any      							=     self.tokenizer(text=inputs['words']   ,      boxes=inputs['boxes']   ,      return_tensors='pt')
        a__:					List[str]      							=     target_size
        return inputs



      def   lowerCamelCase_       (						self   ,      lowercase)     ->      int:





        '''simple docstring'''





        a__:					Any      							=     model_inputs.pop('target_size')
        a__:					Union[str, Any]      							=     self.model(**lowercase)
        a__:					List[str]      							=     outputs.__class__({'target_size': target_size, **outputs})
        if self.tokenizer is not None:
          a__:					Union[str, Any]      							=     model_inputs['bbox']
        return model_outputs



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase=0.9)     ->      Optional[Any]:





        '''simple docstring'''





        a__:					int      							=     model_outputs['target_size']
        if self.tokenizer is not None:
          # This is a LayoutLMForTokenClassification variant.
          # The OCR got the boxes and the model classified the words.
          a__							,   a__:					str      							=     target_size[0].tolist()

          def unnormalize(lowercase):
            return self._get_bounding_box(
                torch.Tensor(
                    [
                        (width * bbox[0] / 10_00),
                        (height * bbox[1] / 10_00),
                        (width * bbox[2] / 10_00),
                        (height * bbox[3] / 10_00),
                    ]))

          a__							,   a__:					Optional[Any]      							=     model_outputs['logits'].squeeze(0).softmax(dim=-1).max(dim=-1)
          a__:					str      							=     [self.model.config.idalabel[prediction] for prediction in classes.tolist()]
          a__:					Union[str, Any]      							=     [unnormalize(lowercase) for bbox in model_outputs['bbox'].squeeze(0)]
          a__:					Dict      							=     ['score', 'label', 'box']
          a__:					Any      							=     [dict(zip(lowercase   ,      lowercase)) for vals in zip(scores.tolist()   ,      lowercase   ,      lowercase) if vals[0] > threshold]
        else:
          # This is a regular ForObjectDetectionModel
          a__:					List[str]      							=     self.image_processor.post_process_object_detection(lowercase   ,      lowercase   ,      lowercase)
          a__:					Tuple      							=     raw_annotations[0]
          a__:					List[str]      							=     raw_annotation['scores']
          a__:					int      							=     raw_annotation['labels']
          a__:					int      							=     raw_annotation['boxes']

          a__:					List[Any]      							=     scores.tolist()
          a__:					Any      							=     [self.model.config.idalabel[label.item()] for label in labels]
          a__:					Dict      							=     [self._get_bounding_box(lowercase) for box in boxes]

          # {"scores": [...], ...} --> [{"score":x, ...}, ...]
          a__:					Optional[Any]      							=     ['score', 'label', 'box']
          a__:					List[Any]      							=     [
              dict(zip(lowercase   ,      lowercase))
              for vals in zip(raw_annotation['scores']   ,      raw_annotation['labels']   ,      raw_annotation['boxes'])
          ]

        return annotation



      def   lowerCamelCase_       (						self   ,      lowercase)     ->      Dict[str, int]:





        '''simple docstring'''





        if self.framework != "pt":
          raise ValueError('The ObjectDetectionPipeline is only available in PyTorch.')
        a__							,   a__							,   a__							,   a__:					List[Any]      							=     box.int().tolist()
        a__:					Optional[int]      							=     {
            'xmin': xmin,
            'ymin': ymin,
            'xmax': xmax,
            'ymax': ymax,
        }
        return bbox


