
'''simple docstring'''



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(SCREAMING_SNAKE_CASE__			)
class   UpperCAmelCase_					(		SCREAMING_SNAKE_CASE__			):






						'''simple docstring'''






						def __init__( self   ,       **_lowercase				):

												"""simple docstring"""



												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   ,       _lowercase   ,       **_lowercase				):

												"""simple docstring"""



												return super().__call__(A__   ,       **A__				)




						def  _lowercase     ( self   ,       **_lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			{}
												if "candidate_labels" in kwargs:
																		_lowerCAmelCase  						=			kwargs["""candidate_labels"""]
												if "hypothesis_template" in kwargs:
																		_lowerCAmelCase  						=			kwargs["""hypothesis_template"""]

												return preprocess_params, {}, {}




						def  _lowercase     ( self   ,       _lowercase   ,       _lowercase=None   ,       _lowercase="This is a photo of {}."				):

												"""simple docstring"""



												_lowerCAmelCase  						=			load_image(A__				)
												_lowerCAmelCase  						=			self.image_processor(images=[image]   ,       return_tensors=self.framework				)
												_lowerCAmelCase  						=			candidate_labels
												_lowerCAmelCase  						=			[hypothesis_template.format(A__				) for x in candidate_labels]
												_lowerCAmelCase  						=			self.tokenizer(A__   ,       return_tensors=self.framework   ,       padding=A__				)
												_lowerCAmelCase  						=			[text_inputs]
												return inputs




						def  _lowercase     ( self   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			model_inputs.pop("""candidate_labels"""				)
												_lowerCAmelCase  						=			model_inputs.pop("""text_inputs"""				)
												if isinstance(text_inputs[0]   ,       A__				):
																		_lowerCAmelCase  						=			text_inputs[0]
												else:
																		# Batching case.
																		_lowerCAmelCase  						=			text_inputs[0][0]

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

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




						def  _lowercase     ( self   ,       _lowercase				):

												"""simple docstring"""



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

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


'''simple docstring'''



from __future__ import annotations




class   UpperCAmelCase_					:






						'''simple docstring'''






						def __init__( self   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			order

												# a_{0} ... a_{k}
												_lowerCAmelCase  						=			[1.0] + [0.0] * order
												# b_{0} ... b_{k}
												_lowerCAmelCase  						=			[1.0] + [0.0] * order

												# x[n-1] ... x[n-k]
												_lowerCAmelCase  						=			[0.0] * self.order
												# y[n-1] ... y[n-k]
												_lowerCAmelCase  						=			[0.0] * self.order




						def  _lowercase     ( self   ,       _lowercase   ,       _lowercase				):

												"""simple docstring"""



												if len(_lowercase				) < self.order:
																		_lowerCAmelCase  						=			[1.0, *a_coeffs]

												if len(_lowercase				) != self.order + 1:
																		_lowerCAmelCase  						=			(
																		    F'Expected a_coeffs to have {self.order + 1} elements '
																		    F'for {self.order}-order filter, got {len(_lowercase				)}'
																		)
																		raise ValueError(_lowercase				)

												if len(_lowercase				) != self.order + 1:
																		_lowerCAmelCase  						=			(
																		    F'Expected b_coeffs to have {self.order + 1} elements '
																		    F'for {self.order}-order filter, got {len(_lowercase				)}'
																		)
																		raise ValueError(_lowercase				)

												_lowerCAmelCase  						=			a_coeffs
												_lowerCAmelCase  						=			b_coeffs




						def  _lowercase     ( self   ,       _lowercase				):

												"""simple docstring"""



												_lowerCAmelCase  						=			0.0

												# Start at index 1 and do index 0 at the end.
												for i in range(1   ,       self.order + 1				):
																		result += (
																		    self.b_coeffs[i] * self.input_history[i - 1]
																		    - self.a_coeffs[i] * self.output_history[i - 1]
																		)

												_lowerCAmelCase  						=			(result + self.b_coeffs[0] * sample) / self.a_coeffs[0]

												_lowerCAmelCase  						=			self.input_history[:-1]
												_lowerCAmelCase  						=			self.output_history[:-1]

												_lowerCAmelCase  						=			sample
												_lowerCAmelCase  						=			result

												return result
