



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    (	*__SCREAMING_SNAKE_CASE							:  Union[str, Any]  ,	**__SCREAMING_SNAKE_CASE							:  str):


            '''simple docstring'''

            pass

@is_pipeline_test
@require_vision
@require_torch
class 					_A (			unittest.TestCase					):
       UpperCamelCase__						:					Optional[Any]		      =		MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



       def 	_lowerCamelCase    (	self							:  int  ,	__SCREAMING_SNAKE_CASE							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]):


           '''simple docstring'''

           __a		 =       pipeline(
               '''zero-shot-object-detection'''  ,	model='''hf-internal-testing/tiny-random-owlvit-object-detection''')

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



       def 	_lowerCamelCase    (	self							:  List[Any]  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  Tuple):


           '''simple docstring'''

           __a		 =       object_detector(examples[0]  ,	threshold=0.0)

           __a		 =       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							:  List[str]):


           '''simple docstring'''

           pass



       @require_torch
       def 	_lowerCamelCase    (	self							:  str):


           '''simple docstring'''

           __a		 =       pipeline(
               '''zero-shot-object-detection'''  ,	model='''hf-internal-testing/tiny-random-owlvit-object-detection''')

           __a		 =       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.72_35, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                   {'''score''': 0.72_18, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                   {'''score''': 0.71_84, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                   {'''score''': 0.67_48, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                   {'''score''': 0.66_56, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                   {'''score''': 0.66_14, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                   {'''score''': 0.64_56, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
                   {'''score''': 0.6_42, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}},
                   {'''score''': 0.64_19, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
               ]  ,	)

           __a		 =       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.72_35, '''label''': '''cat''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                       {'''score''': 0.72_18, '''label''': '''remote''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                       {'''score''': 0.71_84, '''label''': '''couch''', '''box''': {'''xmin''': 204, '''ymin''': 167, '''xmax''': 232, '''ymax''': 190}},
                       {'''score''': 0.67_48, '''label''': '''remote''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                       {'''score''': 0.66_56, '''label''': '''cat''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                       {'''score''': 0.66_14, '''label''': '''couch''', '''box''': {'''xmin''': 571, '''ymin''': 83, '''xmax''': 598, '''ymax''': 103}},
                       {'''score''': 0.64_56, '''label''': '''remote''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
                       {'''score''': 0.6_42, '''label''': '''remote''', '''box''': {'''xmin''': 67, '''ymin''': 274, '''xmax''': 93, '''ymax''': 297}},
                       {'''score''': 0.64_19, '''label''': '''cat''', '''box''': {'''xmin''': 494, '''ymin''': 105, '''xmax''': 521, '''ymax''': 127}},
                   ]
               ]  ,	)



       @require_torch
       @slow
       def 	_lowerCamelCase    (	self							:  Optional[int]):


           '''simple docstring'''

           __a		 =       pipeline('''zero-shot-object-detection''')

           __a		 =       object_detector(
               '''http://images.cocodataset.org/val2017/000000039769.jpg'''  ,	candidate_labels=['''cat''', '''remote''', '''couch''']  ,	)
           self.assertEqual(
               nested_simplify(UpperCamelCase_  ,	decimals=4)  ,	[
                   {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                   {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                   {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
                   {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
                   {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
               ]  ,	)

           __a		 =       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.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                       {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                       {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
                       {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
                       {'''score''': 0.12_08, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 642, '''ymax''': 476}},
                   ],
                   [
                       {'''score''': 0.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                       {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                       {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
                       {'''score''': 0.14_74, '''label''': '''remote''', '''box''': {'''xmin''': 335, '''ymin''': 74, '''xmax''': 371, '''ymax''': 187}},
                       {'''score''': 0.12_08, '''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							:  Optional[Any]):


           '''simple docstring'''

           pass



       @require_torch
       @slow
       def 	_lowerCamelCase    (	self							:  Optional[int]):


           '''simple docstring'''

           __a		 =       0.2
           __a		 =       pipeline('''zero-shot-object-detection''')

           __a		 =       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.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                   {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
                   {'''score''': 0.25_37, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 315, '''ymax''': 472}},
               ]  ,	)





       @require_torch
       @slow
       def 	_lowerCamelCase    (	self							:  List[str]):


           '''simple docstring'''

           __a		 =       2
           __a		 =       pipeline('''zero-shot-object-detection''')

           __a		 =       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.28_68, '''label''': '''cat''', '''box''': {'''xmin''': 324, '''ymin''': 20, '''xmax''': 640, '''ymax''': 373}},
                   {'''score''': 0.2_77, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 72, '''xmax''': 177, '''ymax''': 115}},
               ]  ,	)










import os
import sys
import unittest


__snake_case		:Union[str, Any]   	=  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		:List[str]   	=  os.path.join(git_repo_path, '''src''', '''transformers''')

__snake_case		:Any   	=  '''
{0} = None
'''

__snake_case		:Dict   	=  '''
class {0}(metaclass=DummyObject):
    _backends = {1}

    def __init__(self, *args, **kwargs):
        requires_backends(self, {1})
'''


__snake_case		:str   	=  '''
def {0}(*args, **kwargs):
    requires_backends({0}, {1})
'''

class 					_A (			unittest.TestCase					):



       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           __a		 =       find_backend('''    _import_structure["models.albert"].append("AlbertTokenizerFast")''')
           self.assertIsNone(__SCREAMING_SNAKE_CASE)

           __a		 =       find_backend('''    if not is_tokenizers_available():''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''tokenizers''')

           __a		 =       find_backend('''    if not is_tensorflow_text_available():''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''tensorflow_text''')

           __a		 =       find_backend('''    if not (is_sentencepiece_available() and is_tokenizers_available()):''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''sentencepiece_and_tokenizers''')

           __a		 =       find_backend(
               '''    if not (is_sentencepiece_available() and is_tensorflow_text_available()):''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''sentencepiece_and_tensorflow_text''')

           __a		 =       find_backend(
               '''    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''sentencepiece_and_tokenizers_and_vision''')



       def 	_lowerCamelCase    (	self							:  Optional[Any]):


           '''simple docstring'''

           __a		 =       read_init()
           # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
           self.assertIn('''torch'''  ,	__SCREAMING_SNAKE_CASE)
           self.assertIn('''tensorflow_text'''  ,	__SCREAMING_SNAKE_CASE)
           self.assertIn('''sentencepiece_and_tokenizers'''  ,	__SCREAMING_SNAKE_CASE)

           # 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							:  Any):


           '''simple docstring'''

           __a		 =       create_dummy_object('''CONSTANT'''  ,	'''\'torch\'''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	'''\nCONSTANT = None\n''')

           __a		 =       create_dummy_object('''function'''  ,	'''\'torch\'''')
           self.assertEqual(
               __SCREAMING_SNAKE_CASE  ,	'''\ndef function(*args, **kwargs):\n    requires_backends(function, \'torch\')\n''')

           __a		 =       '''
class FakeClass(metaclass=DummyObject):
    _backends = \'torch\'

    def __init__(self, *args, **kwargs):
        requires_backends(self, \'torch\')
'''
           __a		 =       create_dummy_object('''FakeClass'''  ,	'''\'torch\'''')
           self.assertEqual(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)





       def 	_lowerCamelCase    (	self							:  Optional[Any]):


           '''simple docstring'''

           __a		 =       '''# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, ["torch"])


class FakeClass(metaclass=DummyObject):
    _backends = ["torch"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, ["torch"])
'''
           __a		 =       create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']})
           self.assertEqual(dummy_files['''torch''']  ,	__SCREAMING_SNAKE_CASE)





