




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       __snake_case		( *UpperCAmelCase_       , **UpperCAmelCase_						):
             pass


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





    '''simple docstring'''



    __a							: Union[str, Any]										=MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



    def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_						):
           lowerCAmelCase   =		pipeline(
               '''zero-shot-object-detection'''       , model='''hf-internal-testing/tiny-random-owlvit-object-detection'''						)

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



    def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_						):
           lowerCAmelCase   =		object_detector(examples[0]       , threshold=0.0						)

           lowerCAmelCase   =		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       __snake_case		( self						):
           pass



    @require_torch
    def       __snake_case		( self						):
           lowerCAmelCase   =		pipeline(
               '''zero-shot-object-detection'''       , model='''hf-internal-testing/tiny-random-owlvit-object-detection'''						)

           lowerCAmelCase   =		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.7235, '''label''': '''cat''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
                   {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
                   {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
                   {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
                   {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
                   {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
                   {'''score''': 0.6456, '''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.6419, '''label''': '''cat''', '''box''': {'''xmin''': 4_94, '''ymin''': 1_05, '''xmax''': 5_21, '''ymax''': 1_27}},
               ]       , )

           lowerCAmelCase   =		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.7235, '''label''': '''cat''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
                       {'''score''': 0.7218, '''label''': '''remote''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
                       {'''score''': 0.7184, '''label''': '''couch''', '''box''': {'''xmin''': 2_04, '''ymin''': 1_67, '''xmax''': 2_32, '''ymax''': 1_90}},
                       {'''score''': 0.6748, '''label''': '''remote''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
                       {'''score''': 0.6656, '''label''': '''cat''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
                       {'''score''': 0.6614, '''label''': '''couch''', '''box''': {'''xmin''': 5_71, '''ymin''': 83, '''xmax''': 5_98, '''ymax''': 1_03}},
                       {'''score''': 0.6456, '''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.6419, '''label''': '''cat''', '''box''': {'''xmin''': 4_94, '''ymin''': 1_05, '''xmax''': 5_21, '''ymax''': 1_27}},
                   ]
               ]       , )



    @require_torch
    @slow
    def       __snake_case		( self						):
           lowerCAmelCase   =		pipeline('''zero-shot-object-detection'''						)

           lowerCAmelCase   =		object_detector(
               '''http://images.cocodataset.org/val2017/000000039769.jpg'''       , candidate_labels=['''cat''', '''remote''', '''couch''']       , )
           self.assertEqual(
               nested_simplify(UpperCAmelCase_       , decimals=4						)       , [
                   {'''score''': 0.2868, '''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.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
                   {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 3_35, '''ymin''': 74, '''xmax''': 3_71, '''ymax''': 1_87}},
                   {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_42, '''ymax''': 4_76}},
               ]       , )

           lowerCAmelCase   =		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.2868, '''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.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
                       {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 3_35, '''ymin''': 74, '''xmax''': 3_71, '''ymax''': 1_87}},
                       {'''score''': 0.1208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_42, '''ymax''': 4_76}},
                   ],
                   [
                       {'''score''': 0.2868, '''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.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
                       {'''score''': 0.1474, '''label''': '''remote''', '''box''': {'''xmin''': 3_35, '''ymin''': 74, '''xmax''': 3_71, '''ymax''': 1_87}},
                       {'''score''': 0.1208, '''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       __snake_case		( self						):
           pass



    @require_torch
    @slow
    def       __snake_case		( self						):
           lowerCAmelCase   =		0.2
           lowerCAmelCase   =		pipeline('''zero-shot-object-detection'''						)

           lowerCAmelCase   =		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.2868, '''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.2537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 55, '''xmax''': 3_15, '''ymax''': 4_72}},
               ]       , )






    @require_torch
    @slow
    def       __snake_case		( self						):
           lowerCAmelCase   =		2
           lowerCAmelCase   =		pipeline('''zero-shot-object-detection'''						)

           lowerCAmelCase   =		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.2868, '''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}},
               ]       , )









from collections.abc import Sequence



def       UpperCAmelCase       (     _snake_case						, _snake_case = False							):
							if not arr:
														return 0

							lowerCAmelCase   =		0 if allow_empty_subarrays else float('''-inf'''							)
							lowerCAmelCase   =		0.0
							for num in arr:
														lowerCAmelCase   =		max(0 if allow_empty_subarrays else num						, curr_sum + num							)
														lowerCAmelCase   =		max(_snake_case						, _snake_case							)

							return max_sum


if __name__ == "__main__":
		from doctest import testmod

		testmod()

		UpperCAmelCase_						=[-2, 1, -3, 4, -1, 2, 1, -5, 4]
		print(F'''{max_subarray_sum(nums) = }''')



