


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





											'''simple docstring'''

											@staticmethod
											def 		lowerCAmelCase       (				*UpperCAmelCase_							:      Dict		,       **UpperCAmelCase_							:      List[str]       )					->Any:



																		"""simple docstring"""



																		pass





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





							'''simple docstring'''

							@require_torch
							def 		lowerCAmelCase       (				self							:      Dict       )					->Optional[Any]:



														"""simple docstring"""



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

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



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



														"""simple docstring"""



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

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

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



							@slow
							@require_torch
							def 		lowerCAmelCase       (				self							:      Any       )					->int:



														"""simple docstring"""



														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
														snake_case_           =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
														snake_case_           =		image_classifier(UpperCAmelCase_		,       candidate_labels=["""cat""", """plane""", """remote"""]       )

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

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



							@slow
							@require_tf
							def 		lowerCAmelCase       (				self							:      int       )					->Any:



														"""simple docstring"""



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

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







"""simple docstring"""
import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Audio, Features, Value
from .base import TaskTemplate





@dataclass(frozen=snake_case__)
class      __A     (snake_case__):





							'''simple docstring'''

							__lowercase:		str							      =    field(default="""automatic-speech-recognition"""						,		metadata={"""include_in_asdict_even_if_is_default""": True})
							__lowercase:		ClassVar[Features]							      =    Features({"""audio""": Audio()})
							__lowercase:		ClassVar[Features]							      =    Features({"""transcription""": Value("""string""")})
							__lowercase:		str							      =    "audio"
							__lowercase:		str							      =    "transcription"



							def 		lowerCAmelCase       (				self							:      List[Any]		,       UpperCAmelCase_							:      Any       )					->int:



														"""simple docstring"""



														if self.audio_column not in features:
																					raise ValueError(F"""Column {self.audio_column} is not present in features."""       )
														if not isinstance(features[self.audio_column]		,       UpperCAmelCase_       ):
																					raise ValueError(F"""Column {self.audio_column} is not an Audio type."""       )
														snake_case_           =		copy.deepcopy(self       )
														snake_case_           =		self.input_schema.copy()
														snake_case_           =		features[self.audio_column]
														snake_case_           =		input_schema
														return task_template



							@property
							def 		lowerCAmelCase       (				self							:      List[str]       )					->Dict[str, str]:



														"""simple docstring"""



														return {self.audio_column: "audio", self.transcription_column: "transcription"}



