

'''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 						lowerCAmelCase__   :



   """simple docstring"""



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


        """simple docstring"""




        pass







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



 """simple docstring"""



 @require_torch
 def       UpperCAmelCase__		(		self					:       int			)  -> Optional[int]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      pipeline(
          model="""hf-internal-testing/tiny-random-clip-zero-shot-image-classification"""					,							)
      __SCREAMING_SNAKE_CASE     =      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
      __SCREAMING_SNAKE_CASE     =      image_classifier(__SCREAMING_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(__SCREAMING_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"""}],
          ]					,							)

      __SCREAMING_SNAKE_CASE     =      image_classifier([image] * 5					,							candidate_labels=["""A""", """B""", """C"""]					,							batch_size=2			)
      self.assertEqual(
          nested_simplify(__SCREAMING_SNAKE_CASE			)					,							[
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
          ]					,							)
 @require_tf
 def       UpperCAmelCase__		(		self					:       List[str]			)  -> Tuple:


      """simple docstring"""




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

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

      __SCREAMING_SNAKE_CASE     =      image_classifier([image] * 5					,							candidate_labels=["""A""", """B""", """C"""]					,							batch_size=2			)
      self.assertEqual(
          nested_simplify(__SCREAMING_SNAKE_CASE			)					,							[
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
              [
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
                  {"""score""": 0.333, """label""": ANY(__SCREAMING_SNAKE_CASE			)},
              ],
          ]					,							)
 @slow
 @require_torch
 def       UpperCAmelCase__		(		self					:       Optional[Any]			)  -> Any:


      """simple docstring"""




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

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

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


 @slow
 @require_tf
 def       UpperCAmelCase__		(		self					:       Optional[Any]			)  -> Optional[Any]:


      """simple docstring"""




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

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









'''simple docstring'''



class 						lowerCAmelCase__   :



 """simple docstring"""



 def __init__(		self					:       List[Any]					,							__SCREAMING_SNAKE_CASE					:       int			)  -> List[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      n
      __SCREAMING_SNAKE_CASE     =      [None] * self.n
      __SCREAMING_SNAKE_CASE     =      0  # index of the first element
      __SCREAMING_SNAKE_CASE     =      0
      __SCREAMING_SNAKE_CASE     =      0
 def __len__(		self					:       Tuple			)  -> int:


      """simple docstring"""




      return self.size
 def       UpperCAmelCase__		(		self					:       Optional[Any]			)  -> bool:


      """simple docstring"""




      return self.size == 0
 def       UpperCAmelCase__		(		self					:       Any			)  -> int:


      """simple docstring"""




      return False if self.is_empty() else self.array[self.front]
 def       UpperCAmelCase__		(		self					:       Dict					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]			)  -> Dict:


      """simple docstring"""




      if self.size >= self.n:
           raise Exception("""QUEUE IS FULL"""			)

      __SCREAMING_SNAKE_CASE     =      data
      __SCREAMING_SNAKE_CASE     =      (self.rear + 1) % self.n
      self.size += 1
      return self


 def       UpperCAmelCase__		(		self					:       List[str]			)  -> Optional[Any]:


      """simple docstring"""




      if self.size == 0:
           raise Exception("""UNDERFLOW"""			)

      __SCREAMING_SNAKE_CASE     =      self.array[self.front]
      __SCREAMING_SNAKE_CASE     =      None
      __SCREAMING_SNAKE_CASE     =      (self.front + 1) % self.n
      self.size -= 1
      return temp






