



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_tf_available():
	import tensorflow as tf

	from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
	from ..tf_utils import stable_softmax

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

__A							       =		logging.get_logger(__name__)






@add_end_docstrings(__lowerCAmelCase)
class 						lowercase		(					__lowerCAmelCase):






	"""simple docstring"""


	def __init__(      self      :		Tuple		,    *__UpperCAmelCase      :		Tuple		,    **__UpperCAmelCase      :		Optional[Any]	)     ->					List[str]:
							super().__init__(*_lowerCamelCase		,    **_lowerCamelCase	)
							requires_backends(self		,    """vision"""	)
							self.check_model_type(
							    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
							    if self.framework == """tf"""
							    else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING	)




	def     _SCREAMING_SNAKE_CASE						(      self      :		Dict		,    __UpperCAmelCase      :		Optional[int]=None	)     ->					Optional[Any]:
							UpperCAmelCase_=  {}
							if top_k is not None:
													UpperCAmelCase_=  top_k
							return {}, {}, postprocess_params




	def __call__(      self      :		int		,    __UpperCAmelCase      :		Optional[int]		,    **__UpperCAmelCase      :		Optional[Any]	)     ->					Dict:
							return super().__call__(_lowerCamelCase		,    **_lowerCamelCase	)




	def     _SCREAMING_SNAKE_CASE						(      self      :		List[str]		,    __UpperCAmelCase      :		str	)     ->					int:
							UpperCAmelCase_=  load_image(_lowerCamelCase	)
							UpperCAmelCase_=  self.image_processor(images=_lowerCamelCase		,    return_tensors=self.framework	)
							return model_inputs




	def     _SCREAMING_SNAKE_CASE						(      self      :		Any		,    __UpperCAmelCase      :		List[str]	)     ->					Union[str, Any]:
							UpperCAmelCase_=  self.model(**_lowerCamelCase	)
							return model_outputs




	def     _SCREAMING_SNAKE_CASE						(      self      :		List[str]		,    __UpperCAmelCase      :		Union[str, Any]		,    __UpperCAmelCase      :		Tuple=5	)     ->					Tuple:
							if top_k > self.model.config.num_labels:
													UpperCAmelCase_=  self.model.config.num_labels

							if self.framework == "pt":
													UpperCAmelCase_=  model_outputs.logits.softmax(-1	)[0]
													UpperCAmelCase_,  UpperCAmelCase_=  probs.topk(_lowerCamelCase	)
							elif self.framework == "tf":
													UpperCAmelCase_=  stable_softmax(model_outputs.logits		,    axis=-1	)[0]
													UpperCAmelCase_=  tf.math.top_k(_lowerCamelCase		,    k=_lowerCamelCase	)
													UpperCAmelCase_,  UpperCAmelCase_=  topk.values.numpy(), topk.indices.numpy()
							else:
													raise ValueError(F"""Unsupported framework: {self.framework}"""	)

							UpperCAmelCase_=  scores.tolist()
							UpperCAmelCase_=  ids.tolist()
							return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_lowerCamelCase		,    _lowerCamelCase	)]





"""simple docstring"""


def      lowerCamelCase_					(   __lowerCAmelCase							)				->   bool:




						'''simple docstring'''



						return number & 1 == 0


if __name__ == "__main__":
	import doctest

	doctest.testmod()


