





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

A_												=     logging.get_logger(__name__)

@add_end_docstrings(lowercase_   )
class SCREAMING_SNAKE_CASE_ (					lowercase_   ):





					"""simple docstring"""


					def __init__( self	,  **_lowerCAmelCase	):
										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	,  _lowerCAmelCase	,  **_lowerCAmelCase	):
										return super().__call__(_lowerCAmelCase	,  **_lowerCAmelCase	)





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

										return preprocess_params, {}, {}





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





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

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

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






					def 					__magic_name__ ( self	,  _lowerCAmelCase	):
										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(_lowerCAmelCase	,  _lowerCAmelCase	):
																				lowerCamelCase__       =       [scores]
										elif self.framework == "tf":
															lowerCamelCase__       =       stable_softmax(_lowerCAmelCase	,  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(_lowerCAmelCase	,  _lowerCAmelCase	)	,  key=lambda _lowerCAmelCase	: -x[0]	)
										]
										return result








def    __UpperCamelCase      (    a,     a) ->Optional[int]:
					lowerCamelCase__       =       [1]
					for i in range(2,     a):
										factorials.append(factorials[-1] * i)
					assert 0 <= k < factorials[-1] * n, "k out of bounds"

					lowerCamelCase__       =       []
					lowerCamelCase__       =       list(range(a))

					# Find permutation
					while factorials:
										lowerCamelCase__       =       factorials.pop()
										lowerCamelCase__    ,		lowerCamelCase__       =       divmod(a,     a)
										permutation.append(elements[number])
										elements.remove(elements[number])
					permutation.append(elements[0])

					return permutation


if __name__ == "__main__":
						import doctest

						doctest.testmod()

