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  SCREAMING_SNAKE_CASE_ :
      '''simple docstring'''


      @staticmethod
      def 		SCREAMING_SNAKE_CASE_		(				*SCREAMING_SNAKE_CASE__    :					Optional[Any]      ,	**SCREAMING_SNAKE_CASE__    :					int     )    ->				int:
             pass



@is_pipeline_test
@require_vision
@require_torch
class  SCREAMING_SNAKE_CASE_ (					unittest.TestCase			):
  '''simple docstring'''


  lowercase		:     Any            =				MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING







  def 		SCREAMING_SNAKE_CASE_		(				self    :					Tuple      ,	SCREAMING_SNAKE_CASE__    :					Union[str, Any]      ,	SCREAMING_SNAKE_CASE__    :					Optional[Any]      ,	SCREAMING_SNAKE_CASE__    :					Optional[Any]     )    ->				Dict:
         A						:     str         =pipeline(
             'zero-shot-object-detection'      ,	model='hf-internal-testing/tiny-random-owlvit-object-detection'     )

         A						:     Tuple         =[
             {
                 'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                 'candidate_labels': ['cat', 'remote', 'couch'],
             }
         ]
         return object_detector, examples







  def 		SCREAMING_SNAKE_CASE_		(				self    :					str      ,	SCREAMING_SNAKE_CASE__    :					Tuple      ,	SCREAMING_SNAKE_CASE__    :					Optional[int]     )    ->				Dict:
         A						:     Any         =object_detector(examples[0]      ,	threshold=0.0     )

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







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







  @require_torch
  def 		SCREAMING_SNAKE_CASE_		(				self    :					Optional[Any]     )    ->				Any:
         A						:     Any         =pipeline(
             'zero-shot-object-detection'      ,	model='hf-internal-testing/tiny-random-owlvit-object-detection'     )

         A						:     List[str]         =object_detector(
             './tests/fixtures/tests_samples/COCO/000000039769.png'      ,	candidate_labels=['cat', 'remote', 'couch']      ,	threshold=0.6_4      ,	)

         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__      ,	decimals=4     )      ,	[
                 {'score': 0.7_2_3_5, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
                 {'score': 0.7_2_1_8, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
                 {'score': 0.7_1_8_4, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
                 {'score': 0.6_7_4_8, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
                 {'score': 0.6_6_5_6, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
                 {'score': 0.6_6_1_4, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
                 {'score': 0.6_4_5_6, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
                 {'score': 0.6_4_2, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
                 {'score': 0.6_4_1_9, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
             ]      ,	)

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

         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__      ,	decimals=4     )      ,	[
                 [
                     {'score': 0.7_2_3_5, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
                     {'score': 0.7_2_1_8, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
                     {'score': 0.7_1_8_4, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
                     {'score': 0.6_7_4_8, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
                     {'score': 0.6_6_5_6, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
                     {'score': 0.6_6_1_4, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
                     {'score': 0.6_4_5_6, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
                     {'score': 0.6_4_2, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
                     {'score': 0.6_4_1_9, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
                 ]
             ]      ,	)







  @require_torch
  @slow
  def 		SCREAMING_SNAKE_CASE_		(				self    :					Any     )    ->				Dict:
         A						:     str         =pipeline('zero-shot-object-detection'     )

         A						:     int         =object_detector(
             'http://images.cocodataset.org/val2017/000000039769.jpg'      ,	candidate_labels=['cat', 'remote', 'couch']      ,	)
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__      ,	decimals=4     )      ,	[
                 {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
                 {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
                 {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
                 {'score': 0.1_4_7_4, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
                 {'score': 0.1_2_0_8, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
             ]      ,	)

         A						:     Optional[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(SCREAMING_SNAKE_CASE__      ,	decimals=4     )      ,	[
                 [
                     {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
                     {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
                     {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
                     {'score': 0.1_4_7_4, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
                     {'score': 0.1_2_0_8, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
                 ],
                 [
                     {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
                     {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
                     {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
                     {'score': 0.1_4_7_4, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
                     {'score': 0.1_2_0_8, '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 		SCREAMING_SNAKE_CASE_		(				self    :					Optional[int]     )    ->				int:
         pass







  @require_torch
  @slow
  def 		SCREAMING_SNAKE_CASE_		(				self    :					Union[str, Any]     )    ->				Optional[Any]:
         A						:     Dict         =0.2
         A						:     int         =pipeline('zero-shot-object-detection'     )

         A						:     str         =object_detector(
             'http://images.cocodataset.org/val2017/000000039769.jpg'      ,	candidate_labels=['cat', 'remote', 'couch']      ,	threshold=SCREAMING_SNAKE_CASE__      ,	)
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__      ,	decimals=4     )      ,	[
                 {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
                 {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
                 {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
             ]      ,	)







  @require_torch
  @slow
  def 		SCREAMING_SNAKE_CASE_		(				self    :					int     )    ->				str:
         A						:     Any         =2
         A						:     str         =pipeline('zero-shot-object-detection'     )

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







_lowercase							:      Dict      							='''0.21.0'''

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
    from .utils import rich






