



"""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__       :




           """simple docstring"""




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







@is_pipeline_test
@require_vision
@require_torch
class 				UpperCamelCase__       (     unittest.TestCase  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



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

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



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				List[str]   ,      SCREAMING_SNAKE_CASE_    :				List[str]	):
        lowerCAmelCase_       :    str								=			object_detector(examples[0]   ,      threshold=0.0	)

        lowerCAmelCase_       :    int								=			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	):
        pass



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

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

        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE_   ,      decimals=4	)   ,      [
                {'score': 0.72_35, 'label': 'cat', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
                {'score': 0.72_18, 'label': 'remote', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
                {'score': 0.71_84, 'label': 'couch', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
                {'score': 0.67_48, 'label': 'remote', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
                {'score': 0.66_56, 'label': 'cat', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
                {'score': 0.66_14, 'label': 'couch', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
                {'score': 0.64_56, 'label': 'remote', 'box': {'xmin': 4_9_4, 'ymin': 1_0_5, 'xmax': 5_2_1, 'ymax': 1_2_7}},
                {'score': 0.6_42, 'label': 'remote', 'box': {'xmin': 6_7, 'ymin': 2_7_4, 'xmax': 9_3, 'ymax': 2_9_7}},
                {'score': 0.64_19, 'label': 'cat', 'box': {'xmin': 4_9_4, 'ymin': 1_0_5, 'xmax': 5_2_1, 'ymax': 1_2_7}},
            ]   ,      )

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

        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE_   ,      decimals=4	)   ,      [
                [
                    {'score': 0.72_35, 'label': 'cat', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
                    {'score': 0.72_18, 'label': 'remote', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
                    {'score': 0.71_84, 'label': 'couch', 'box': {'xmin': 2_0_4, 'ymin': 1_6_7, 'xmax': 2_3_2, 'ymax': 1_9_0}},
                    {'score': 0.67_48, 'label': 'remote', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
                    {'score': 0.66_56, 'label': 'cat', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
                    {'score': 0.66_14, 'label': 'couch', 'box': {'xmin': 5_7_1, 'ymin': 8_3, 'xmax': 5_9_8, 'ymax': 1_0_3}},
                    {'score': 0.64_56, 'label': 'remote', 'box': {'xmin': 4_9_4, 'ymin': 1_0_5, 'xmax': 5_2_1, 'ymax': 1_2_7}},
                    {'score': 0.6_42, 'label': 'remote', 'box': {'xmin': 6_7, 'ymin': 2_7_4, 'xmax': 9_3, 'ymax': 2_9_7}},
                    {'score': 0.64_19, 'label': 'cat', 'box': {'xmin': 4_9_4, 'ymin': 1_0_5, 'xmax': 5_2_1, 'ymax': 1_2_7}},
                ]
            ]   ,      )



      @require_torch
      @slow
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        lowerCAmelCase_       :    str								=			pipeline('zero-shot-object-detection'	)

        lowerCAmelCase_       :    Optional[Any]								=			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.28_68, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
                {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
                {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 5_5, 'xmax': 3_1_5, 'ymax': 4_7_2}},
                {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_3_5, 'ymin': 7_4, 'xmax': 3_7_1, 'ymax': 1_8_7}},
                {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_4_2, 'ymax': 4_7_6}},
            ]   ,      )

        lowerCAmelCase_       :    Dict								=			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.28_68, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
                    {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
                    {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 5_5, 'xmax': 3_1_5, 'ymax': 4_7_2}},
                    {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_3_5, 'ymin': 7_4, 'xmax': 3_7_1, 'ymax': 1_8_7}},
                    {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_4_2, 'ymax': 4_7_6}},
                ],
                [
                    {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
                    {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
                    {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 5_5, 'xmax': 3_1_5, 'ymax': 4_7_2}},
                    {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_3_5, 'ymin': 7_4, 'xmax': 3_7_1, 'ymax': 1_8_7}},
                    {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_4_2, 'ymax': 4_7_6}},
                ],
            ]   ,      )



      @require_tf
      @unittest.skip('Zero Shot Object Detection not implemented in TF'	)
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        pass



      @require_torch
      @slow
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple	):
        lowerCAmelCase_       :    Dict								=			0.2
        lowerCAmelCase_       :    Any								=			pipeline('zero-shot-object-detection'	)

        lowerCAmelCase_       :    Union[str, Any]								=			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.28_68, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
                {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
                {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 5_5, 'xmax': 3_1_5, 'ymax': 4_7_2}},
            ]   ,      )





      @require_torch
      @slow
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any	):
        lowerCAmelCase_       :    Union[str, Any]								=			2
        lowerCAmelCase_       :    Dict								=			pipeline('zero-shot-object-detection'	)

        lowerCAmelCase_       :    List[Any]								=			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.28_68, 'label': 'cat', 'box': {'xmin': 3_2_4, 'ymin': 2_0, 'xmax': 6_4_0, 'ymax': 3_7_3}},
                {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 4_0, 'ymin': 7_2, 'xmax': 1_7_7, 'ymax': 1_1_5}},
            ]   ,      )










"""simple docstring"""







import os

from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home


lowercase__		:    int				   =     HUGGINGFACE_HUB_CACHE


lowercase__		:    Tuple				   =     """config.json"""
lowercase__		:    Union[str, Any]				   =     """diffusion_pytorch_model.bin"""
lowercase__		:    List[Any]				   =     """diffusion_flax_model.msgpack"""
lowercase__		:    List[str]				   =     """model.onnx"""
lowercase__		:    List[Any]				   =     """diffusion_pytorch_model.safetensors"""
lowercase__		:    Dict				   =     """weights.pb"""
lowercase__		:    List[Any]				   =     """https://huggingface.co"""
lowercase__		:    List[Any]				   =     default_cache_path
lowercase__		:    Tuple				   =     """diffusers_modules"""
lowercase__		:    Tuple				   =     os.getenv("""HF_MODULES_CACHE""", os.path.join(hf_cache_home, """modules"""))
lowercase__		:    List[str]				   =     ["""fp16""", """non-ema"""]
lowercase__		:    Optional[Any]				   =     """.self_attn"""





