



from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
				from PIL import Image

				from ..image_utils import load_image

if is_torch_available():
				from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
				from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
				from ..tf_utils import stable_softmax

_lowercase     							=						logging.get_logger(__name__)
@add_end_docstrings(lowerCAmelCase__      )
class 				__A					( lowerCAmelCase__      ):





						def __init__(self							,    **__magic_name__				):
									super().__init__(**_lowerCamelCase				)

									requires_backends(self							,    """vision"""				)
									self.check_model_type(
									    TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
									    if self.framework == """tf"""
									    else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING				)





						def __call__(self							,    __magic_name__							,    **__magic_name__				):
									return super().__call__(_lowerCamelCase							,    **_lowerCamelCase				)





						def        _snake_case    (self							,    **__magic_name__				):
									lowerCamelCase__ :      Optional[Any]								=		{}
									if "candidate_labels" in kwargs:
												lowerCamelCase__ :      Optional[int]								=		kwargs["""candidate_labels"""]
									if "hypothesis_template" in kwargs:
												lowerCamelCase__ :      Any								=		kwargs["""hypothesis_template"""]

									return preprocess_params, {}, {}





						def        _snake_case    (self							,    __magic_name__							,    __magic_name__=None							,    __magic_name__="This is a photo of {}."				):
									lowerCamelCase__ :      str								=		load_image(_lowerCamelCase				)
									lowerCamelCase__ :      Union[str, Any]								=		self.image_processor(images=[image]							,    return_tensors=self.framework				)
									lowerCamelCase__ :      int								=		candidate_labels
									lowerCamelCase__ :      int								=		[hypothesis_template.format(_lowerCamelCase				) for x in candidate_labels]
									lowerCamelCase__ :      Optional[Any]								=		self.tokenizer(_lowerCamelCase							,    return_tensors=self.framework							,    padding=_lowerCamelCase				)
									lowerCamelCase__ :      Optional[int]								=		[text_inputs]
									return inputs





						def        _snake_case    (self							,    __magic_name__				):
									lowerCamelCase__ :      Optional[int]								=		model_inputs.pop("""candidate_labels"""				)
									lowerCamelCase__ :      Optional[Any]								=		model_inputs.pop("""text_inputs"""				)
									if isinstance(text_inputs[0]							,    _lowerCamelCase				):
												lowerCamelCase__ :      int								=		text_inputs[0]
									else:
												# Batching case.
												lowerCamelCase__ :      Tuple								=		text_inputs[0][0]

									lowerCamelCase__ :      Optional[Any]								=		self.model(**_lowerCamelCase							,    **_lowerCamelCase				)

									lowerCamelCase__ :      int								=		{
									    """candidate_labels""": candidate_labels,
									    """logits""": outputs.logits_per_image,
									}
									return model_outputs





						def        _snake_case    (self							,    __magic_name__				):
									lowerCamelCase__ :      List[str]								=		model_outputs.pop("""candidate_labels"""				)
									lowerCamelCase__ :      Union[str, Any]								=		model_outputs["""logits"""][0]
									if self.framework == "pt":
												lowerCamelCase__ :      List[str]								=		logits.softmax(dim=-1				).squeeze(-1				)
												lowerCamelCase__ :      Tuple								=		probs.tolist()
												if not isinstance(_lowerCamelCase							,    _lowerCamelCase				):
															lowerCamelCase__ :      str								=		[scores]
									elif self.framework == "tf":
												lowerCamelCase__ :      Union[str, Any]								=		stable_softmax(_lowerCamelCase							,    axis=-1				)
												lowerCamelCase__ :      Any								=		probs.numpy().tolist()
									else:
												raise ValueError(f"Unsupported framework: {self.framework}"				)

									lowerCamelCase__ :      Dict								=		[
									    {"""score""": score, """label""": candidate_label}
									    for score, candidate_label in sorted(zip(_lowerCamelCase							,    _lowerCamelCase				)							,    key=lambda __magic_name__				: -x[0]				)
									]
									return result








import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)
class 				__A					( unittest.TestCase      ):





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[str]								=		Vector([1, 2, 3]				)
						self.assertEqual(x.component(0				)							,    1				)
						self.assertEqual(x.component(2				)							,    3				)
						lowerCamelCase__ :      str								=		Vector()





			def        _snake_case    (self				):
						lowerCamelCase__ :      Dict								=		Vector([0, 0, 0, 0, 0, 1]				)
						self.assertEqual(str(__magic_name__				)							,    """(0,0,0,0,0,1)"""				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		Vector([1, 2, 3, 4]				)
						self.assertEqual(len(__magic_name__				)							,    4				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Dict								=		Vector([1, 2]				)
						lowerCamelCase__ :      str								=		Vector([1, 2, 3, 4, 5]				)
						lowerCamelCase__ :      int								=		Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]				)
						lowerCamelCase__ :      int								=		Vector([1, -1, 1, -1, 2, -3, 4, -5]				)
						self.assertAlmostEqual(x.euclidean_length()							,    2.2_36							,    3				)
						self.assertAlmostEqual(y.euclidean_length()							,    7.4_16							,    3				)
						self.assertEqual(z.euclidean_length()							,    0				)
						self.assertAlmostEqual(w.euclidean_length()							,    7.6_16							,    3				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		Vector([1, 2, 3]				)
						lowerCamelCase__ :      Union[str, Any]								=		Vector([1, 1, 1]				)
						self.assertEqual((x + y).component(0				)							,    2				)
						self.assertEqual((x + y).component(1				)							,    3				)
						self.assertEqual((x + y).component(2				)							,    4				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		Vector([1, 2, 3]				)
						lowerCamelCase__ :      Any								=		Vector([1, 1, 1]				)
						self.assertEqual((x - y).component(0				)							,    0				)
						self.assertEqual((x - y).component(1				)							,    1				)
						self.assertEqual((x - y).component(2				)							,    2				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Dict								=		Vector([1, 2, 3]				)
						lowerCamelCase__ :      int								=		Vector([2, -1, 4]				)  # for test of dot product
						lowerCamelCase__ :      List[Any]								=		Vector([1, -2, -1]				)
						self.assertEqual(str(x * 3.0				)							,    """(3.0,6.0,9.0)"""				)
						self.assertEqual((a * b)							,    0				)





			def        _snake_case    (self				):
						self.assertEqual(str(zero_vector(10				)				).count("""0"""				)							,    10				)





			def        _snake_case    (self				):
						self.assertEqual(str(unit_basis_vector(3							,    1				)				)							,    """(0,1,0)"""				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Any								=		Vector([1, 2, 3]				)
						lowerCamelCase__ :      str								=		Vector([1, 0, 1]				)
						self.assertEqual(str(axpy(2							,    __magic_name__							,    __magic_name__				)				)							,    """(3,4,7)"""				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[Any]								=		Vector([1, 0, 0, 0, 0, 0]				)
						lowerCamelCase__ :      int								=		x.copy()
						self.assertEqual(str(__magic_name__				)							,    str(__magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      int								=		Vector([1, 0, 0]				)
						x.change_component(0							,    0				)
						x.change_component(1							,    1				)
						self.assertEqual(str(__magic_name__				)							,    """(0,1,0)"""				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[Any]								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						self.assertEqual("""|1,2,3|\n|2,4,5|\n|6,7,8|\n"""							,    str(__magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      int								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						lowerCamelCase__ :      int								=		[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
						for x in range(a.height()				):
									for y in range(a.width()				):
												self.assertEqual(minors[x][y]							,    a.minor(__magic_name__							,    __magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Any								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						lowerCamelCase__ :      Optional[Any]								=		[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
						for x in range(a.height()				):
									for y in range(a.width()				):
												self.assertEqual(cofactors[x][y]							,    a.cofactor(__magic_name__							,    __magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						self.assertEqual(-5							,    a.determinant()				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[str]								=		Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]							,    3							,    3				)
						lowerCamelCase__ :      Optional[int]								=		Vector([1, 2, 3]				)
						self.assertEqual("""(14,32,50)"""							,    str(a * x				)				)
						self.assertEqual("""|2,4,6|\n|8,10,12|\n|14,16,18|\n"""							,    str(a * 2				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						a.change_component(0							,    2							,    5				)
						self.assertEqual("""|1,2,5|\n|2,4,5|\n|6,7,8|\n"""							,    str(__magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[Any]								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						self.assertEqual(7							,    a.component(2							,    1				)							,    0.01				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						lowerCamelCase__ :      int								=		Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]							,    3							,    3				)
						self.assertEqual("""|2,4,10|\n|4,8,10|\n|12,14,18|\n"""							,    str(a + b				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						lowerCamelCase__ :      Optional[Any]								=		Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]							,    3							,    3				)
						self.assertEqual("""|0,0,-4|\n|0,0,0|\n|0,0,-2|\n"""							,    str(a - b				)				)





			def        _snake_case    (self				):
						self.assertEqual(
						    """|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n"""							,    str(square_zero_matrix(5				)				)							,    )


if __name__ == "__main__":
				unittest.main()



