





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

_lowerCamelCase :    List[Any]  							= logging.get_logger(__name__)


@add_end_docstrings(__lowerCamelCase      )
class 						lowerCamelCase       (__lowerCamelCase      ):







		"""simple docstring"""




		def __init__( self     :					int,			*_UpperCAmelCase     :					Tuple,			**_UpperCAmelCase     :					Tuple						)				->			Dict:





									"""simple docstring"""
									super().__init__(*_UpperCAmelCase,			**_UpperCAmelCase						)
									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  A_     ( self     :					Optional[int],			_UpperCAmelCase     :					List[Any]=None						)				->			List[Any]:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      List[str]     =						{}
									if top_k is not None:
																SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						top_k
									return {}, {}, postprocess_params






		def __call__( self     :					List[str],			_UpperCAmelCase     :					Union[str, List[str], "Image.Image", List["Image.Image"]],			**_UpperCAmelCase     :					List[str]						)				->			int:





									"""simple docstring"""
									return super().__call__(_UpperCAmelCase,			**_UpperCAmelCase						)






		def  A_     ( self     :					Optional[Any],			_UpperCAmelCase     :					List[str]						)				->			List[Any]:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      str     =						load_image(_UpperCAmelCase						)
									SCREAMING_SNAKE_CASE__       :      List[Any]     =						self.image_processor(images=_UpperCAmelCase,			return_tensors=self.framework						)
									return model_inputs






		def  A_     ( self     :					List[Any],			_UpperCAmelCase     :					List[str]						)				->			Optional[int]:





									"""simple docstring"""
									SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						self.model(**_UpperCAmelCase						)
									return model_outputs






		def  A_     ( self     :					Optional[int],			_UpperCAmelCase     :					List[str],			_UpperCAmelCase     :					Union[str, Any]=5						)				->			Optional[int]:





									"""simple docstring"""
									if top_k > self.model.config.num_labels:
																SCREAMING_SNAKE_CASE__       :      Dict     =						self.model.config.num_labels

									if self.framework == "pt":
																SCREAMING_SNAKE_CASE__       :      List[str]     =						model_outputs.logits.softmax(-1						)[0]
																SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						probs.topk(_UpperCAmelCase						)
									elif self.framework == "tf":
																SCREAMING_SNAKE_CASE__       :      str     =						stable_softmax(model_outputs.logits,			axis=-1						)[0]
																SCREAMING_SNAKE_CASE__       :      Optional[int]     =						tf.math.top_k(_UpperCAmelCase,			k=_UpperCAmelCase						)
																SCREAMING_SNAKE_CASE__       :      Tuple     =						topk.values.numpy(), topk.indices.numpy()
									else:
																raise ValueError(F'''Unsupported framework: {self.framework}'''						)

									SCREAMING_SNAKE_CASE__       :      List[str]     =						scores.tolist()
									SCREAMING_SNAKE_CASE__       :      int     =						ids.tolist()
									return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_UpperCAmelCase,			_UpperCAmelCase						)]













def _a (  SCREAMING_SNAKE_CASE__      :  int       )	->    str:

							'''simple docstring'''






							if isinstance(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       ):
														raise TypeError("'float' object cannot be interpreted as an integer"       )
							if isinstance(SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__       ):
														raise TypeError("'str' object cannot be interpreted as an integer"       )

							if num == 0:
														return "0b0"

							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						False

							if num < 0:
														SCREAMING_SNAKE_CASE__       :      Any     =						True
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						-num

							SCREAMING_SNAKE_CASE__       :      list[int]     =						[]
							while num > 0:
														binary.insert(0			,				num % 2       )
														num >>= 1

							if negative:
														return "-0b" + "".join(str(SCREAMING_SNAKE_CASE__       ) for e in binary       )

							return "0b" + "".join(str(SCREAMING_SNAKE_CASE__       ) for e in binary       )


if __name__ == "__main__":
		import doctest

		doctest.testmod()






