

'''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        a__  :


        @staticmethod
        def 						_lowerCamelCase       (			*_UpperCamelCase      ,    **_UpperCamelCase  ):





           """simple docstring"""





           pass


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


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       pipeline(
          "zero-shot-object-detection"      ,    model="hf-internal-testing/tiny-random-owlvit-object-detection"  )

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


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       object_detector(examples[0]      ,    threshold=0.0  )

      _lowercase      :				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 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      pass


   @require_torch
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Union[str, Any]      					=       pipeline(
          "zero-shot-object-detection"      ,    model="hf-internal-testing/tiny-random-owlvit-object-detection"  )

      _lowercase      :				Optional[int]      					=       object_detector(
          "./tests/fixtures/tests_samples/COCO/000000039769.png"      ,    candidate_labels=["cat", "remote", "couch"]      ,    threshold=0.6_4      ,    )

      self.assertEqual(
          nested_simplify(__UpperCAmelCase      ,    decimals=4  )      ,    [
              {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
              {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
              {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
              {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
              {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
              {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
              {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
              {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
              {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
          ]      ,    )

      _lowercase      :				str      					=       object_detector(
          [
              {
                  "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                  "candidate_labels": ["cat", "remote", "couch"],
              }
          ]      ,    threshold=0.6_4      ,    )

      self.assertEqual(
          nested_simplify(__UpperCAmelCase      ,    decimals=4  )      ,    [
              [
                  {"score": 0.7_2_3_5, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                  {"score": 0.7_2_1_8, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                  {"score": 0.7_1_8_4, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                  {"score": 0.6_7_4_8, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                  {"score": 0.6_6_5_6, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                  {"score": 0.6_6_1_4, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                  {"score": 0.6_4_5_6, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
                  {"score": 0.6_4_2, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
                  {"score": 0.6_4_1_9, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
              ]
          ]      ,    )


   @require_torch
   @slow
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       pipeline("zero-shot-object-detection"  )

      _lowercase      :				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_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
              {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
              {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
              {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
              {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
          ]      ,    )

      _lowercase      :				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_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                  {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                  {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                  {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
                  {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
              ],
              [
                  {"score": 0.2_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                  {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                  {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                  {"score": 0.1_4_7_4, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
                  {"score": 0.1_2_0_8, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
              ],
          ]      ,    )


   @require_tf
   @unittest.skip("Zero Shot Object Detection not implemented in TF"  )
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      pass


   @require_torch
   @slow
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       0.2
      _lowercase      :				List[Any]      					=       pipeline("zero-shot-object-detection"  )

      _lowercase      :				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_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
              {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
              {"score": 0.2_5_3_7, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
          ]      ,    )






   @require_torch
   @slow
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[Any]      					=       2
      _lowercase      :				Optional[int]      					=       pipeline("zero-shot-object-detection"  )

      _lowercase      :				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_8_6_8, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
              {"score": 0.2_7_7, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
          ]      ,    )









'''simple docstring'''
import os
import sys
import unittest


_snake_case         =  os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, 'utils'))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
_snake_case         =  os.path.join(git_repo_path, 'src', 'transformers')

_snake_case         =  '\n{0} = None\n'

_snake_case         =  '\nclass {0}(metaclass=DummyObject):\n    _backends = {1}\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, {1})\n'


_snake_case         =  '\ndef {0}(*args, **kwargs):\n    requires_backends({0}, {1})\n'


class        a__  (							unittest.TestCase  ):


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       find_backend("    _import_structure[\"models.albert\"].append(\"AlbertTokenizerFast\")"  )
      self.assertIsNone(_UpperCamelCase  )

      _lowercase      :				Dict      					=       find_backend("    if not is_tokenizers_available():"  )
      self.assertEqual(_UpperCamelCase      ,    "tokenizers"  )

      _lowercase      :				str      					=       find_backend("    if not is_tensorflow_text_available():"  )
      self.assertEqual(_UpperCamelCase      ,    "tensorflow_text"  )

      _lowercase      :				Optional[Any]      					=       find_backend("    if not (is_sentencepiece_available() and is_tokenizers_available()):"  )
      self.assertEqual(_UpperCamelCase      ,    "sentencepiece_and_tokenizers"  )

      _lowercase      :				List[str]      					=       find_backend(
          "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"  )
      self.assertEqual(_UpperCamelCase      ,    "sentencepiece_and_tensorflow_text"  )

      _lowercase      :				Optional[Any]      					=       find_backend(
          "    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):"  )
      self.assertEqual(_UpperCamelCase      ,    "sentencepiece_and_tokenizers_and_vision"  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				List[Any]      					=       read_init()
      # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
      self.assertIn("torch"      ,    _UpperCamelCase  )
      self.assertIn("tensorflow_text"      ,    _UpperCamelCase  )
      self.assertIn("sentencepiece_and_tokenizers"      ,    _UpperCamelCase  )

      # Likewise, we can't assert on the exact content of a key
      self.assertIn("BertModel"      ,    objects["torch"]  )
      self.assertIn("TFBertModel"      ,    objects["tf"]  )
      self.assertIn("FlaxBertModel"      ,    objects["flax"]  )
      self.assertIn("BertModel"      ,    objects["torch"]  )
      self.assertIn("TFBertTokenizer"      ,    objects["tensorflow_text"]  )
      self.assertIn("convert_slow_tokenizer"      ,    objects["sentencepiece_and_tokenizers"]  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				int      					=       create_dummy_object("CONSTANT"      ,    "'torch'"  )
      self.assertEqual(_UpperCamelCase      ,    "\nCONSTANT = None\n"  )

      _lowercase      :				Optional[Any]      					=       create_dummy_object("function"      ,    "'torch'"  )
      self.assertEqual(
          _UpperCamelCase      ,    "\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"  )

      _lowercase      :				Union[str, Any]      					=       "\nclass FakeClass(metaclass=DummyObject):\n    _backends = 'torch'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, 'torch')\n"
      _lowercase      :				List[str]      					=       create_dummy_object("FakeClass"      ,    "'torch'"  )
      self.assertEqual(_UpperCamelCase      ,    _UpperCamelCase  )






   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       "# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n    requires_backends(function, [\"torch\"])\n\n\nclass FakeClass(metaclass=DummyObject):\n    _backends = [\"torch\"]\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, [\"torch\"])\n"
      _lowercase      :				str      					=       create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]}  )
      self.assertEqual(dummy_files["torch"]      ,    _UpperCamelCase  )






