
"""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 __lowercase:




										'''simple docstring'''


										@staticmethod
										def     snake_case_				(    *__a						,	**__a     ):
													pass

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




					'''simple docstring'''


					@require_torch
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	str	       =				pipeline(
								    model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'						,	)
								__lowerCamelCase					:	Optional[int]	       =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
								__lowerCamelCase					:	str	       =				image_classifier(A_						,	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(A_     )						,	[
								        [{'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					:	Optional[Any]	       =				image_classifier([image] * 5						,	candidate_labels=['A', 'B', 'C']						,	batch_size=2     )
								self.assertEqual(
								    nested_simplify(A_     )						,	[
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								    ]						,	)
					@require_tf
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	int	       =				pipeline(
								    model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'						,	framework='tf'     )
								__lowerCamelCase					:	List[Any]	       =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
								__lowerCamelCase					:	Tuple	       =				image_classifier(A_						,	candidate_labels=['a', 'b', 'c']     )

								self.assertEqual(
								    nested_simplify(A_     )						,	[{'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(A_     )						,	[
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								        [
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								            {'score': 0.333, 'label': ANY(A_     )},
								        ],
								    ]						,	)
					@slow
					@require_torch
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Tuple	       =				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					:	Tuple	       =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
								__lowerCamelCase					:	List[Any]	       =				image_classifier(A_						,	candidate_labels=['cat', 'plane', 'remote']     )

								self.assertEqual(
								    nested_simplify(A_     )						,	[
								        {'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(A_     )						,	[
								        [
								            {'score': 0.511, 'label': 'remote'},
								            {'score': 0.485, 'label': 'cat'},
								            {'score': 0.004, 'label': 'plane'},
								        ],
								    ]
								    * 5						,	)





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









"""simple docstring"""
import pytest

import datasets


# Import fixture modules as plugins
a_				:    Tuple										=     ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec''']





def 						UpperCAmelCase				(			A__:    List[Any]							,				A__:    Optional[Any]     )  ->     List[str]:
			# Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit")
			for item in items:
						if any(marker in item.keywords for marker in ['integration', 'unit']     ):
									continue
						item.add_marker(pytest.mark.unit     )





def 						UpperCAmelCase				(			A__:    int     )  ->     Optional[int]:
			config.addinivalue_line('markers'							,				'torchaudio_latest: mark test to run with torchaudio>=0.12'     )





@pytest.fixture(autouse=A__     )
def 						UpperCAmelCase				(			A__:    Optional[Any]							,				A__:    List[Any]     )  ->     Any:
			# test_hf_cache_home = tmp_path_factory.mktemp("cache")  # TODO: why a cache dir per test function does not work?
			__lowerCamelCase					:	str	       =				tmp_path_factory.getbasetemp() / 'cache'
			__lowerCamelCase					:	Dict	       =				test_hf_cache_home / 'datasets'
			__lowerCamelCase					:	int	       =				test_hf_cache_home / 'metrics'
			__lowerCamelCase					:	List[str]	       =				test_hf_cache_home / 'modules'
			monkeypatch.setattr('datasets.config.HF_DATASETS_CACHE'							,				str(A__     )     )
			monkeypatch.setattr('datasets.config.HF_METRICS_CACHE'							,				str(A__     )     )
			monkeypatch.setattr('datasets.config.HF_MODULES_CACHE'							,				str(A__     )     )
			__lowerCamelCase					:	List[Any]	       =				test_hf_datasets_cache / 'downloads'
			monkeypatch.setattr('datasets.config.DOWNLOADED_DATASETS_PATH'							,				str(A__     )     )
			__lowerCamelCase					:	Optional[int]	       =				test_hf_datasets_cache / 'downloads' / 'extracted'
			monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH'							,				str(A__     )     )





@pytest.fixture(autouse=A__							,				scope='session'     )
def 						UpperCAmelCase				(			)  ->     Any:
			datasets.disable_progress_bar()





@pytest.fixture(autouse=A__     )
def 						UpperCAmelCase				(			A__:    Optional[Any]     )  ->     Dict:
			# don't take tests into account when counting downloads
			monkeypatch.setattr('datasets.config.HF_UPDATE_DOWNLOAD_COUNTS'							,				A__     )






@pytest.fixture
def 						UpperCAmelCase				(			A__:    Union[str, Any]     )  ->     Optional[Any]:
			# Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0
			# To be removed once SQLAlchemy 2.0 supported
			monkeypatch.setattr('sqlalchemy.util.deprecations.SILENCE_UBER_WARNING'							,				A__     )



