





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        snake_case_    :






   @staticmethod
   def 				UpperCAmelCase__						(							*_snake_case			:					Optional[int]       ,  **_snake_case			:					Any      )->str:






    '''simple docstring'''

    pass






@is_pipeline_test
@require_vision
class        snake_case_    (		unittest.TestCase						):






  @require_torch
  def 				UpperCAmelCase__						(							self			:					str      )->Dict:






   '''simple docstring'''

   __lowerCAmelCase					:	int      =       pipeline(
       model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification"""       ,  )
   __lowerCAmelCase					:	Dict      =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
   __lowerCAmelCase					:	Optional[Any]      =       image_classifier(_snake_case       ,  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(_snake_case      )       ,  [
           [{"""score""": 0.333, """label""": """a"""}, {"""score""": 0.333, """label""": """b"""}, {"""score""": 0.333, """label""": """c"""}],
           [{"""score""": 0.333, """label""": """a"""}, {"""score""": 0.333, """label""": """c"""}, {"""score""": 0.333, """label""": """b"""}],
       ]       ,  )

   __lowerCAmelCase					:	List[Any]      =       image_classifier([image] * 5       ,  candidate_labels=["""A""", """B""", """C"""]       ,  batch_size=2      )
   self.assertEqual(
       nested_simplify(_snake_case      )       ,  [
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
       ]       ,  )






  @require_tf
  def 				UpperCAmelCase__						(							self			:					str      )->Tuple:






   '''simple docstring'''

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

   self.assertEqual(
       nested_simplify(_snake_case      )       ,  [{"""score""": 0.333, """label""": """a"""}, {"""score""": 0.333, """label""": """b"""}, {"""score""": 0.333, """label""": """c"""}]       ,  )

   __lowerCAmelCase					:	List[str]      =       image_classifier([image] * 5       ,  candidate_labels=["""A""", """B""", """C"""]       ,  batch_size=2      )
   self.assertEqual(
       nested_simplify(_snake_case      )       ,  [
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
           [
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
               {"""score""": 0.333, """label""": ANY(_snake_case      )},
           ],
       ]       ,  )






  @slow
  @require_torch
  def 				UpperCAmelCase__						(							self			:					int      )->Union[str, Any]:






   '''simple docstring'''

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

   self.assertEqual(
       nested_simplify(_snake_case      )       ,  [
           {"""score""": 0.511, """label""": """remote"""},
           {"""score""": 0.485, """label""": """cat"""},
           {"""score""": 0.004, """label""": """plane"""},
       ]       ,  )

   __lowerCAmelCase					:	int      =       image_classifier([image] * 5       ,  candidate_labels=["""cat""", """plane""", """remote"""]       ,  batch_size=2      )
   self.assertEqual(
       nested_simplify(_snake_case      )       ,  [
           [
               {"""score""": 0.511, """label""": """remote"""},
               {"""score""": 0.485, """label""": """cat"""},
               {"""score""": 0.004, """label""": """plane"""},
           ],
       ]
       * 5       ,  )






  @slow
  @require_tf
  def 				UpperCAmelCase__						(							self			:					int      )->Union[str, Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Union[str, Any]      =       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
   __lowerCAmelCase					:	Optional[int]      =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
   __lowerCAmelCase					:	Union[str, Any]      =       image_classifier(_snake_case       ,  candidate_labels=["""cat""", """plane""", """remote"""]      )
   self.assertEqual(
       nested_simplify(_snake_case      )       ,  [
           {"""score""": 0.511, """label""": """remote"""},
           {"""score""": 0.485, """label""": """cat"""},
           {"""score""": 0.004, """label""": """plane"""},
       ]       ,  )

   __lowerCAmelCase					:	Dict      =       image_classifier([image] * 5       ,  candidate_labels=["""cat""", """plane""", """remote"""]       ,  batch_size=2      )
   self.assertEqual(
       nested_simplify(_snake_case      )       ,  [
           [
               {"""score""": 0.511, """label""": """remote"""},
               {"""score""": 0.485, """label""": """cat"""},
               {"""score""": 0.004, """label""": """plane"""},
           ],
       ]
       * 5       ,  )






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:list[list[int | float]]   )      ->   int:
 __lowerCAmelCase					:	str      =       len(SCREAMING_SNAKE_CASE   )
 __lowerCAmelCase					:	Optional[int]      =       len(matrix[0]   )
 __lowerCAmelCase					:	List[Any]      =       min(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   )

 for row in range(SCREAMING_SNAKE_CASE   ):
  # Check if diagonal element is not zero
  if matrix[row][row] != 0:
   # Eliminate all the elements below the diagonal
   for col in range(row + 1   ,   SCREAMING_SNAKE_CASE   ):
    __lowerCAmelCase					:	Union[str, Any]      =       matrix[col][row] / matrix[row][row]
    for i in range(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   ):
     matrix[col][i] -= multiplier * matrix[row][i]
  else:
   # Find a non-zero diagonal element to swap rows
   __lowerCAmelCase					:	Optional[int]      =       True
   for i in range(row + 1   ,   SCREAMING_SNAKE_CASE   ):
    if matrix[i][row] != 0:
     __lowerCAmelCase ,					__lowerCAmelCase					:	Union[str, Any]      =       matrix[i], matrix[row]
     __lowerCAmelCase					:	Union[str, Any]      =       False
     break
   if reduce:
    rank -= 1
    for i in range(SCREAMING_SNAKE_CASE   ):
     __lowerCAmelCase					:	int      =       matrix[i][rank]

            # Reduce the row pointer by one to stay on the same row
   row -= 1

 return rank


if __name__ == "__main__":
      import doctest

      doctest.testmod()