






'''simple docstring'''



import unittest

from transformers import is_vision_available
from transformers.pipelines import 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 				A__      ( *UpperCAmelCase			:		List[str]     ,      **UpperCAmelCase			:		int		)  ->  List[Any]:


        '''simple docstring'''

        pass
@is_pipeline_test
@require_vision
class UpperCAmelCase_					(    unittest.TestCase				):




      """simple docstring"""




      @require_torch
      def 				A__      ( self			:		List[str]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   Any				  =pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''     ,      )
       lowercase					:   int				  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
       lowercase					:   Any				  =image_classifier(__lowercase     ,      candidate_labels=['''a''', '''b''', '''c''']		)

       # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
       # python and torch versions.
       self.assertIn(
           nested_simplify(__lowercase		)     ,      [
               [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}],
               [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}],
           ]     ,      )

       lowercase					:   Dict				  =image_classifier([image] * 5     ,      candidate_labels=['''A''', '''B''', '''C''']     ,      batch_size=2		)
       self.assertEqual(
           nested_simplify(__lowercase		)     ,      [
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
           ]     ,      )






      @require_tf
      def 				A__      ( self			:		Any		)  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''     ,      framework='''tf'''		)
       lowercase					:   Optional[Any]				  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
       lowercase					:   List[Any]				  =image_classifier(__lowercase     ,      candidate_labels=['''a''', '''b''', '''c''']		)

       self.assertEqual(
           nested_simplify(__lowercase		)     ,      [{'''score''': 0.3_3_3, '''label''': '''a'''}, {'''score''': 0.3_3_3, '''label''': '''b'''}, {'''score''': 0.3_3_3, '''label''': '''c'''}]     ,      )

       lowercase					:   str				  =image_classifier([image] * 5     ,      candidate_labels=['''A''', '''B''', '''C''']     ,      batch_size=2		)
       self.assertEqual(
           nested_simplify(__lowercase		)     ,      [
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
               [
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
                   {'''score''': 0.3_3_3, '''label''': ANY(__lowercase		)},
               ],
           ]     ,      )






      @slow
      @require_torch
      def 				A__      ( self			:		int		)  ->  List[Any]:


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =pipeline(
           task='''zero-shot-image-classification'''     ,      model='''openai/clip-vit-base-patch32'''     ,      )
       # This is an image of 2 cats with remotes and no planes
       lowercase					:   List[Any]				  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
       lowercase					:   Union[str, Any]				  =image_classifier(__lowercase     ,      candidate_labels=['''cat''', '''plane''', '''remote''']		)

       self.assertEqual(
           nested_simplify(__lowercase		)     ,      [
               {'''score''': 0.5_1_1, '''label''': '''remote'''},
               {'''score''': 0.4_8_5, '''label''': '''cat'''},
               {'''score''': 0.0_0_4, '''label''': '''plane'''},
           ]     ,      )

       lowercase					:   Optional[int]				  =image_classifier([image] * 5     ,      candidate_labels=['''cat''', '''plane''', '''remote''']     ,      batch_size=2		)
       self.assertEqual(
           nested_simplify(__lowercase		)     ,      [
               [
                   {'''score''': 0.5_1_1, '''label''': '''remote'''},
                   {'''score''': 0.4_8_5, '''label''': '''cat'''},
                   {'''score''': 0.0_0_4, '''label''': '''plane'''},
               ],
           ]
           * 5     ,      )






      @slow
      @require_tf
      def 				A__      ( self			:		List[Any]		)  ->  List[str]:


       '''simple docstring'''

       lowercase					:   str				  =pipeline(
           task='''zero-shot-image-classification'''     ,      model='''openai/clip-vit-base-patch32'''     ,      framework='''tf'''		)
       # This is an image of 2 cats with remotes and no planes
       lowercase					:   Union[str, Any]				  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
       lowercase					:   str				  =image_classifier(__lowercase     ,      candidate_labels=['''cat''', '''plane''', '''remote''']		)
       self.assertEqual(
           nested_simplify(__lowercase		)     ,      [
               {'''score''': 0.5_1_1, '''label''': '''remote'''},
               {'''score''': 0.4_8_5, '''label''': '''cat'''},
               {'''score''': 0.0_0_4, '''label''': '''plane'''},
           ]     ,      )

       lowercase					:   Any				  =image_classifier([image] * 5     ,      candidate_labels=['''cat''', '''plane''', '''remote''']     ,      batch_size=2		)
       self.assertEqual(
           nested_simplify(__lowercase		)     ,      [
               [
                   {'''score''': 0.5_1_1, '''label''': '''remote'''},
                   {'''score''': 0.4_8_5, '''label''': '''cat'''},
                   {'''score''': 0.0_0_4, '''label''': '''plane'''},
               ],
           ]
           * 5     ,      )











'''simple docstring'''



def 							lowercase_				(    __A :       int					,      __A :       int			)       ->      str:



 """simple docstring"""




 if number < 0 or shift_amount < 0:
  raise ValueError('''both inputs must be positive integers'''			)

 lowercase					:   List[Any]				  =str(bin(__A			)			)
 binary_number += "0" * shift_amount
 return binary_number


def 							lowercase_				(    __A :       int					,      __A :       int			)       ->      str:



 """simple docstring"""




 if number < 0 or shift_amount < 0:
  raise ValueError('''both inputs must be positive integers'''			)

 lowercase					:   Union[str, Any]				  =str(bin(__A			)			)[2:]
 if shift_amount >= len(__A			):
  return "0b0"
 lowercase					:   Any				  =binary_number[: len(__A			) - shift_amount]
 return "0b" + shifted_binary_number


def 							lowercase_				(    __A :       int					,      __A :       int			)       ->      str:



 """simple docstring"""




 if number >= 0:  # Get binary representation of positive number
  lowercase					:   str				  ='''0''' + str(bin(__A			)			).strip('''-'''			)[2:]
 else:  # Get binary (2's complement) representation of negative number
  lowercase					:   Dict				  =len(bin(__A			)[3:]			)  # Find 2's complement of number
  lowercase					:   Optional[Any]				  =bin(abs(__A			) - (1 << binary_number_length)			)[3:]
  lowercase					:   int				  =(
      '''1''' + '''0''' * (binary_number_length - len(__A			)) + binary_number
  )

 if shift_amount >= len(__A			):
  return "0b" + binary_number[0] * len(__A			)
 return (
     "0b"
     + binary_number[0] * shift_amount
     + binary_number[: len(__A			) - shift_amount]
 )


if __name__ == "__main__":
      import doctest

      doctest.testmod()



