"""simple docstring"""







import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:

 class 		UpperCamelCase      :



     @staticmethod
     def  _lowercase					(*_A    :				Any     ,  **_A    :				List[Any])   ->     Dict:
      pass

@is_pipeline_test
@require_vision
@require_torch
class 		UpperCamelCase      (   unittest.TestCase						):
    UpperCAmelCase    :				List[str]   =		MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



    def  _lowercase					(self    :				Optional[Any]     ,  _A    :				int     ,  _A    :				Union[str, Any]     ,  _A    :				Optional[Any])   ->     Tuple:
     __snake_case      :							Optional[int]						=			pipeline(
         'zero-shot-object-detection'     ,  model='hf-internal-testing/tiny-random-owlvit-object-detection')

     __snake_case      :							Optional[int]						=			[
         {
             """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
             """candidate_labels""": ["""cat""", """remote""", """couch"""],
         }
     ]
     return object_detector, examples



    def  _lowercase					(self    :				Optional[int]     ,  _A    :				Optional[int]     ,  _A    :				Union[str, Any])   ->     Tuple:
     __snake_case      :							Optional[int]						=			object_detector(examples[0]     ,  threshold=0.0)

     __snake_case      :							Tuple						=			len(__UpperCAmelCase)
     self.assertGreater(__UpperCAmelCase     ,  0)
     self.assertEqual(
         __UpperCAmelCase     ,  [
             {
                 'score': ANY(__UpperCAmelCase),
                 'label': ANY(__UpperCAmelCase),
                 'box': {'xmin': ANY(__UpperCAmelCase), 'ymin': ANY(__UpperCAmelCase), 'xmax': ANY(__UpperCAmelCase), 'ymax': ANY(__UpperCAmelCase)},
             }
             for i in range(__UpperCAmelCase)
         ]     ,  )



    @require_tf
    @unittest.skip('Zero Shot Object Detection not implemented in TF')
    def  _lowercase					(self    :				Dict)   ->     Tuple:
     pass



    @require_torch
    def  _lowercase					(self    :				str)   ->     Dict:
     __snake_case      :							Union[str, Any]						=			pipeline(
         'zero-shot-object-detection'     ,  model='hf-internal-testing/tiny-random-owlvit-object-detection')

     __snake_case      :							Optional[int]						=			object_detector(
         './tests/fixtures/tests_samples/COCO/000000039769.png'     ,  candidate_labels=['cat', 'remote', 'couch']     ,  threshold=0.64     ,  )

     self.assertEqual(
         nested_simplify(__UpperCAmelCase     ,  decimals=4)     ,  [
             {'score': 0.7_235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
             {'score': 0.7_218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
             {'score': 0.7_184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
             {'score': 0.6_748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
             {'score': 0.6_656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
             {'score': 0.6_614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
             {'score': 0.6_456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
             {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
             {'score': 0.6_419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
         ]     ,  )

     __snake_case      :							str						=			object_detector(
         [
             {
                 'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                 'candidate_labels': ['cat', 'remote', 'couch'],
             }
         ]     ,  threshold=0.64     ,  )

     self.assertEqual(
         nested_simplify(__UpperCAmelCase     ,  decimals=4)     ,  [
             [
                 {'score': 0.7_235, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
                 {'score': 0.7_218, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
                 {'score': 0.7_184, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
                 {'score': 0.6_748, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
                 {'score': 0.6_656, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
                 {'score': 0.6_614, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
                 {'score': 0.6_456, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
                 {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
                 {'score': 0.6_419, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
             ]
         ]     ,  )



    @require_torch
    @slow
    def  _lowercase					(self    :				Union[str, Any])   ->     Tuple:
     __snake_case      :							Tuple						=			pipeline('zero-shot-object-detection')

     __snake_case      :							List[Any]						=			object_detector(
         'http://images.cocodataset.org/val2017/000000039769.jpg'     ,  candidate_labels=['cat', 'remote', 'couch']     ,  )
     self.assertEqual(
         nested_simplify(__UpperCAmelCase     ,  decimals=4)     ,  [
             {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
             {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
             {'score': 0.2_537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
             {'score': 0.1_474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
             {'score': 0.1_208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
         ]     ,  )

     __snake_case      :							Any						=			object_detector(
         [
             {
                 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 'candidate_labels': ['cat', 'remote', 'couch'],
             },
             {
                 'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
                 'candidate_labels': ['cat', 'remote', 'couch'],
             },
         ]     ,  )
     self.assertEqual(
         nested_simplify(__UpperCAmelCase     ,  decimals=4)     ,  [
             [
                 {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
                 {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
                 {'score': 0.2_537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
                 {'score': 0.1_474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
                 {'score': 0.1_208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
             ],
             [
                 {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
                 {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
                 {'score': 0.2_537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
                 {'score': 0.1_474, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
                 {'score': 0.1_208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
             ],
         ]     ,  )



    @require_tf
    @unittest.skip('Zero Shot Object Detection not implemented in TF')
    def  _lowercase					(self    :				Optional[Any])   ->     List[str]:
     pass



    @require_torch
    @slow
    def  _lowercase					(self    :				str)   ->     List[str]:
     __snake_case      :							Optional[int]						=			0.2
     __snake_case      :							List[Any]						=			pipeline('zero-shot-object-detection')

     __snake_case      :							Optional[int]						=			object_detector(
         'http://images.cocodataset.org/val2017/000000039769.jpg'     ,  candidate_labels=['cat', 'remote', 'couch']     ,  threshold=__UpperCAmelCase     ,  )
     self.assertEqual(
         nested_simplify(__UpperCAmelCase     ,  decimals=4)     ,  [
             {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
             {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
             {'score': 0.2_537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
         ]     ,  )






    @require_torch
    @slow
    def  _lowercase					(self    :				Dict)   ->     List[Any]:
     __snake_case      :							Optional[Any]						=			2
     __snake_case      :							Optional[int]						=			pipeline('zero-shot-object-detection')

     __snake_case      :							List[Any]						=			object_detector(
         'http://images.cocodataset.org/val2017/000000039769.jpg'     ,  candidate_labels=['cat', 'remote', 'couch']     ,  top_k=__UpperCAmelCase     ,  )
     self.assertEqual(
         nested_simplify(__UpperCAmelCase     ,  decimals=4)     ,  [
             {'score': 0.2_868, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
             {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
         ]     ,  )






"""simple docstring"""







from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand
def   __UpperCAmelCase    (   UpperCAmelCase_   :    Namespace  )							->      Union[str, Any]:

	'''simple docstring'''





	return ConvertCommand(
	    args.model_type      ,							args.tf_checkpoint      ,							args.pytorch_dump_output      ,							args.config      ,							args.finetuning_task_name  )


_a					:					str=			"\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n"

class 		UpperCamelCase      (   lowercase						):



				@staticmethod
				def  _lowercase					(_A    :				ArgumentParser)   ->     Tuple:
					__snake_case      :							Optional[Any]						=			parser.add_parser(
					    'convert'     ,  help='CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.'     ,  )
					train_parser.add_argument('--model_type'     ,  type=_A     ,  required=_A     ,  help='Model\'s type.')
					train_parser.add_argument(
					    '--tf_checkpoint'     ,  type=_A     ,  required=_A     ,  help='TensorFlow checkpoint path or folder.')
					train_parser.add_argument(
					    '--pytorch_dump_output'     ,  type=_A     ,  required=_A     ,  help='Path to the PyTorch saved model output.')
					train_parser.add_argument('--config'     ,  type=_A     ,  default=''     ,  help='Configuration file path or folder.')
					train_parser.add_argument(
					    '--finetuning_task_name'     ,  type=_A     ,  default=_A     ,  help='Optional fine-tuning task name if the TF model was a finetuned model.'     ,  )
					train_parser.set_defaults(func=_A)



				def __init__(self    :				List[str]     ,  _A    :				str     ,  _A    :				str     ,  _A    :				str     ,  _A    :				str     ,  _A    :				str     ,  *_A    :				Any     ,  )   ->     Optional[Any]:
					__snake_case      :							List[Any]						=			logging.get_logger('transformers-cli/converting')

					self._logger.info(f"Loading model {model_type}")
					__snake_case      :							List[str]						=			model_type
					__snake_case      :							int						=			tf_checkpoint
					__snake_case      :							Optional[int]						=			pytorch_dump_output
					__snake_case      :							Optional[Any]						=			config
					__snake_case      :							Optional[Any]						=			finetuning_task_name






				def  _lowercase					(self    :				List[str])   ->     str:
					if self._model_type == "albert":
						try:
							from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
							    convert_tf_checkpoint_to_pytorch,
							)
						except ImportError:
							raise ImportError(_A)

						convert_tf_checkpoint_to_pytorch(self._tf_checkpoint     ,  self._config     ,  self._pytorch_dump_output)
					elif self._model_type == "bert":
						try:
							from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
							    convert_tf_checkpoint_to_pytorch,
							)
						except ImportError:
							raise ImportError(_A)

						convert_tf_checkpoint_to_pytorch(self._tf_checkpoint     ,  self._config     ,  self._pytorch_dump_output)
					elif self._model_type == "funnel":
						try:
							from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
							    convert_tf_checkpoint_to_pytorch,
							)
						except ImportError:
							raise ImportError(_A)

						convert_tf_checkpoint_to_pytorch(self._tf_checkpoint     ,  self._config     ,  self._pytorch_dump_output)
					elif self._model_type == "t5":
						try:
							from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
						except ImportError:
							raise ImportError(_A)

						convert_tf_checkpoint_to_pytorch(self._tf_checkpoint     ,  self._config     ,  self._pytorch_dump_output)
					elif self._model_type == "gpt":
						from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
						    convert_openai_checkpoint_to_pytorch,
						)

						convert_openai_checkpoint_to_pytorch(self._tf_checkpoint     ,  self._config     ,  self._pytorch_dump_output)
					elif self._model_type == "transfo_xl":
						try:
							from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
							    convert_transfo_xl_checkpoint_to_pytorch,
							)
						except ImportError:
							raise ImportError(_A)

						if "ckpt" in self._tf_checkpoint.lower():
							__snake_case      :							Union[str, Any]						=			self._tf_checkpoint
							__snake_case      :							List[Any]						=			''
						else:
							__snake_case      :							Optional[Any]						=			self._tf_checkpoint
							__snake_case      :							List[Any]						=			''
						convert_transfo_xl_checkpoint_to_pytorch(
						    _A     ,  self._config     ,  self._pytorch_dump_output     ,  _A)
					elif self._model_type == "gpt2":
						try:
							from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
							    convert_gpta_checkpoint_to_pytorch,
							)
						except ImportError:
							raise ImportError(_A)

						convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint     ,  self._config     ,  self._pytorch_dump_output)
					elif self._model_type == "xlnet":
						try:
							from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
							    convert_xlnet_checkpoint_to_pytorch,
							)
						except ImportError:
							raise ImportError(_A)

						convert_xlnet_checkpoint_to_pytorch(
						    self._tf_checkpoint     ,  self._config     ,  self._pytorch_dump_output     ,  self._finetuning_task_name)
					elif self._model_type == "xlm":
						from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
						    convert_xlm_checkpoint_to_pytorch,
						)

						convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint     ,  self._pytorch_dump_output)
					elif self._model_type == "lxmert":
						from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
						    convert_lxmert_checkpoint_to_pytorch,
						)

						convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint     ,  self._pytorch_dump_output)
					elif self._model_type == "rembert":
						from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
						    convert_rembert_tf_checkpoint_to_pytorch,
						)

						convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint     ,  self._config     ,  self._pytorch_dump_output)
					else:
						raise ValueError(
						    '--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]')





