
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__		:List[Any]			  =   logging.get_logger(__name__)







@add_end_docstrings(SCREAMING_SNAKE_CASE__		)
class     lowercase		( SCREAMING_SNAKE_CASE__		):




							def __init__(    self       ,**A__):
												super().__init__(**A__)

												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       ,A__       ,**A__):
												return super().__call__(A__       ,**A__)




							def 			A__				(    self       ,**A__):
												lowercase						     =						{}
												if "candidate_labels" in kwargs:
																	lowercase						     =						kwargs['''candidate_labels''']
												if "hypothesis_template" in kwargs:
																	lowercase						     =						kwargs['''hypothesis_template''']

												return preprocess_params, {}, {}




							def 			A__				(    self       ,A__       ,A__=None       ,A__="This is a photo of {}."):
												lowercase						     =						load_image(A__)
												lowercase						     =						self.image_processor(images=[image]       ,return_tensors=self.framework)
												lowercase						     =						candidate_labels
												lowercase						     =						[hypothesis_template.format(A__) for x in candidate_labels]
												lowercase						     =						self.tokenizer(A__       ,return_tensors=self.framework       ,padding=A__)
												lowercase						     =						[text_inputs]
												return inputs




							def 			A__				(    self       ,A__):
												lowercase						     =						model_inputs.pop('''candidate_labels''')
												lowercase						     =						model_inputs.pop('''text_inputs''')
												if isinstance(text_inputs[0]       ,A__):
																	lowercase						     =						text_inputs[0]
												else:
																	# Batching case.
																	lowercase						     =						text_inputs[0][0]

												lowercase						     =						self.model(**A__       ,**A__)

												lowercase						     =						{
												    '''candidate_labels''': candidate_labels,
												    '''logits''': outputs.logits_per_image,
												}
												return model_outputs





							def 			A__				(    self       ,A__):
												lowercase						     =						model_outputs.pop('''candidate_labels''')
												lowercase						     =						model_outputs['''logits'''][0]
												if self.framework == "pt":
																	lowercase						     =						logits.softmax(dim=-1).squeeze(-1)
																	lowercase						     =						probs.tolist()
																	if not isinstance(A__       ,A__):
																						lowercase						     =						[scores]
												elif self.framework == "tf":
																	lowercase						     =						stable_softmax(A__       ,axis=-1)
																	lowercase						     =						probs.numpy().tolist()
												else:
																	raise ValueError(f'Unsupported framework: {self.framework}')

												lowercase						     =						[
												    {'''score''': score, '''label''': candidate_label}
												    for score, candidate_label in sorted(zip(A__       ,A__)       ,key=lambda A__: -x[0])
												]
												return result






from __future__ import annotations

from decimal import Decimal

from numpy import array



def 	UpperCamelCase   (							lowerCAmelCase__		):
					'''simple docstring'''
					lowercase						     =						Decimal

					# Check if the provided matrix has 2 rows and 2 columns
					# since this implementation only works for 2x2 matrices
					if len(lowerCAmelCase__		) == 2 and len(matrix[0]		) == 2 and len(matrix[1]		) == 2:
										# Calculate the determinant of the matrix
										lowercase						     =						float(
										    d(matrix[0][0]		) * d(matrix[1][1]		) - d(matrix[1][0]		) * d(matrix[0][1]		)		)
										if determinant == 0:
															raise ValueError('''This matrix has no inverse.'''		)

										# Creates a copy of the matrix with swapped positions of the elements
										lowercase						     =						[[0.0, 0.0], [0.0, 0.0]]
										lowercase     ,						lowercase						     =						matrix[1][1], matrix[0][0]
										lowercase     ,						lowercase						     =						-matrix[1][0], -matrix[0][1]

										# Calculate the inverse of the matrix
										return [
										    [(float(d(lowerCAmelCase__		)		) / determinant) or 0.0 for n in row] for row in swapped_matrix
										]
					elif (
					    len(lowerCAmelCase__		) == 3
					    and len(matrix[0]		) == 3
					    and len(matrix[1]		) == 3
					    and len(matrix[2]		) == 3
					):
										# Calculate the determinant of the matrix using Sarrus rule
										lowercase						     =						float(
										    (
										        (d(matrix[0][0]		) * d(matrix[1][1]		) * d(matrix[2][2]		))
										        + (d(matrix[0][1]		) * d(matrix[1][2]		) * d(matrix[2][0]		))
										        + (d(matrix[0][2]		) * d(matrix[1][0]		) * d(matrix[2][1]		))
										    )
										    - (
										        (d(matrix[0][2]		) * d(matrix[1][1]		) * d(matrix[2][0]		))
										        + (d(matrix[0][1]		) * d(matrix[1][0]		) * d(matrix[2][2]		))
										        + (d(matrix[0][0]		) * d(matrix[1][2]		) * d(matrix[2][1]		))
										    )		)
										if determinant == 0:
															raise ValueError('''This matrix has no inverse.'''		)

										# Creating cofactor matrix
										lowercase						     =						[
										    [d(0.0		), d(0.0		), d(0.0		)],
										    [d(0.0		), d(0.0		), d(0.0		)],
										    [d(0.0		), d(0.0		), d(0.0		)],
										]
										lowercase						     =						(d(matrix[1][1]		) * d(matrix[2][2]		)) - (
										    d(matrix[1][2]		) * d(matrix[2][1]		)
										)
										lowercase						     =						-(
										    (d(matrix[1][0]		) * d(matrix[2][2]		)) - (d(matrix[1][2]		) * d(matrix[2][0]		))
										)
										lowercase						     =						(d(matrix[1][0]		) * d(matrix[2][1]		)) - (
										    d(matrix[1][1]		) * d(matrix[2][0]		)
										)
										lowercase						     =						-(
										    (d(matrix[0][1]		) * d(matrix[2][2]		)) - (d(matrix[0][2]		) * d(matrix[2][1]		))
										)
										lowercase						     =						(d(matrix[0][0]		) * d(matrix[2][2]		)) - (
										    d(matrix[0][2]		) * d(matrix[2][0]		)
										)
										lowercase						     =						-(
										    (d(matrix[0][0]		) * d(matrix[2][1]		)) - (d(matrix[0][1]		) * d(matrix[2][0]		))
										)
										lowercase						     =						(d(matrix[0][1]		) * d(matrix[1][2]		)) - (
										    d(matrix[0][2]		) * d(matrix[1][1]		)
										)
										lowercase						     =						-(
										    (d(matrix[0][0]		) * d(matrix[1][2]		)) - (d(matrix[0][2]		) * d(matrix[1][0]		))
										)
										lowercase						     =						(d(matrix[0][0]		) * d(matrix[1][1]		)) - (
										    d(matrix[0][1]		) * d(matrix[1][0]		)
										)

										# Transpose the cofactor matrix (Adjoint matrix)
										lowercase						     =						array(lowerCAmelCase__		)
										for i in range(3		):
															for j in range(3		):
																				lowercase						     =						cofactor_matrix[j][i]

        # Inverse of the matrix using the formula (1/determinant) * adjoint matrix
										lowercase						     =						array(lowerCAmelCase__		)
										for i in range(3		):
															for j in range(3		):
																				inverse_matrix[i][j] /= d(lowerCAmelCase__		)

        # Calculate the inverse of the matrix
										return [[float(d(lowerCAmelCase__		)		) or 0.0 for n in row] for row in inverse_matrix]
					raise ValueError('''Please provide a matrix of size 2x2 or 3x3.'''		)




