



import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append(""".""")

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:Tuple				)  ->     Dict:
   _A      =       test_file.split(os.path.sep				)
   if components[0:2] != ["tests", "models"]:
      raise ValueError(
          '''`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got '''
          F'''{test_file} instead.'''				)
   _A      =       components[-1]
   if not test_fn.endswith('''py'''				):
      raise ValueError(F'''`test_file` should be a python file. Got {test_fn} instead.'''				)
   if not test_fn.startswith('''test_modeling_'''				):
      raise ValueError(
          F'''`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead.'''				)

   _A      =       components[:-1] + [test_fn.replace('''.py'''	,   ''''''				)]
   _A      =       '''.'''.join(_snake_case				)

   return test_module_path

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:Union[str, Any]				)  ->     Any:
   _A      =       get_module_path(_snake_case				)
   _A      =       importlib.import_module(_snake_case				)

   return test_module

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:List[str]				)  ->     int:
   _A      =       []
   _A      =       get_test_module(_snake_case				)
   for attr in dir(_snake_case				):
      if attr.endswith('''ModelTester'''				):
         tester_classes.append(getattr(_snake_case	,   _snake_case				)				)

    # sort with class names
   return sorted(_snake_case	,   key=lambda _snake_case				: x.__name__				)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:int				)  ->     Union[str, Any]:
   _A      =       []
   _A      =       get_test_module(_snake_case				)
   for attr in dir(_snake_case				):
      _A      =       getattr(_snake_case	,   _snake_case				)
      # (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
      # `all_model_classes` is not empty (which also excludes other special classes).
      _A      =       getattr(_snake_case	,   '''all_model_classes'''	,   []				)
      if len(_snake_case				) > 0:
         test_classes.append(_snake_case				)

    # sort with class names
   return sorted(_snake_case	,   key=lambda _snake_case				: x.__name__				)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:Any				)  ->     Union[str, Any]:
   _A      =       get_test_classes(_snake_case				)
   _A      =       set()
   for test_class in test_classes:
      model_classes.update(test_class.all_model_classes				)

   # sort with class names
   return sorted(_snake_case	,   key=lambda _snake_case				: x.__name__				)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:List[str]				)  ->     Optional[int]:
   _A      =       test_class()
   if hasattr(_snake_case	,   '''setUp'''				):
      test.setUp()

   _A      =       None
   if hasattr(_snake_case	,   '''model_tester'''				):
      # `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
      if test.model_tester is not None:
         _A      =       test.model_tester.__class__

   return model_tester

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:str	,   _snake_case	:Tuple				)  ->     List[str]:
   _A      =       get_test_classes(_snake_case				)

   _A      =       []
   for test_class in test_classes:
      if model_class in test_class.all_model_classes:
         target_test_classes.append(_snake_case				)

    # sort with class names
   return sorted(_snake_case	,   key=lambda _snake_case				: x.__name__				)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:List[str]	,   _snake_case	:List[Any]				)  ->     Dict:
   _A      =       get_test_classes_for_model(_snake_case	,   _snake_case				)

   _A      =       []
   for test_class in test_classes:
      _A      =       get_model_tester_from_test_class(_snake_case				)
      if tester_class is not None:
         tester_classes.append(_snake_case				)

    # sort with class names
   return sorted(_snake_case	,   key=lambda _snake_case				: x.__name__				)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:Optional[int]				)  ->     Tuple:
   _A      =       get_test_classes(_snake_case				)
   _A      =       {test_class: get_model_tester_from_test_class(_snake_case				) for test_class in test_classes}
   return test_tester_mapping

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:str				)  ->     Any:
   _A      =       get_model_classes(_snake_case				)
   _A      =       {
       model_class: get_test_classes_for_model(_snake_case	,   _snake_case				) for model_class in model_classes
   }
   return model_test_mapping

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:List[Any]				)  ->     int:
   _A      =       get_model_classes(_snake_case				)
   _A      =       {
       model_class: get_tester_classes_for_model(_snake_case	,   _snake_case				) for model_class in model_classes
   }
   return model_to_tester_mapping


def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:Optional[int]				)  ->     List[Any]:
   if isinstance(_snake_case	,   _snake_case				):
      return o
   elif isinstance(_snake_case	,   _snake_case				):
      return o.__name__
   elif isinstance(_snake_case	,   (list, tuple)				):
      return [to_json(_snake_case				) for x in o]
   elif isinstance(_snake_case	,   _snake_case				):
      return {to_json(_snake_case				): to_json(_snake_case				) for k, v in o.items()}
   else:
      return o








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     snake_case_      (    *__lowerCAmelCase					:					Optional[Any]			,						**__lowerCAmelCase					:					Any      )   ->	Any:
       pass


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



 """simple docstring"""

 @require_torch
 def     snake_case_      (    self					:					Tuple      )   ->	Tuple:
    _A      =       pipeline(
        model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''			,						)
    _A      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
    _A      =       image_classifier(__lowerCAmelCase			,						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(__lowerCAmelCase      )			,						[
            [{'''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'''}],
        ]			,						)

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

 @require_tf
 def     snake_case_      (    self					:					int      )   ->	Optional[int]:
    _A      =       pipeline(
        model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''			,						framework='''tf'''      )
    _A      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
    _A      =       image_classifier(__lowerCAmelCase			,						candidate_labels=['''a''', '''b''', '''c''']      )

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

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

 @slow
 @require_torch
 def     snake_case_      (    self					:					Optional[int]      )   ->	int:
    _A      =       pipeline(
        task='''zero-shot-image-classification'''			,						model='''openai/clip-vit-base-patch32'''			,						)
    # This is an image of 2 cats with remotes and no planes
    _A      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
    _A      =       image_classifier(__lowerCAmelCase			,						candidate_labels=['''cat''', '''plane''', '''remote''']      )

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

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

 @slow
 @require_tf
 def     snake_case_      (    self					:					Optional[int]      )   ->	Dict:
    _A      =       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
    _A      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
    _A      =       image_classifier(__lowerCAmelCase			,						candidate_labels=['''cat''', '''plane''', '''remote''']      )
    self.assertEqual(
        nested_simplify(__lowerCAmelCase      )			,						[
            {'''score''': 0.511, '''label''': '''remote'''},
            {'''score''': 0.485, '''label''': '''cat'''},
            {'''score''': 0.004, '''label''': '''plane'''},
        ]			,						)

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



