






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__							=     logging.get_logger(__name__)




@add_end_docstrings(lowercase_)
class 			snake_case__   (							lowercase_):

       '''simple docstring'''



       def __init__(							self			,						*a__			,						**a__    ) ->      int:






            '''simple docstring'''





            super().__init__(*a__			,						**a__    )
            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 					__lowercase						(							self			,						a__=None    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Dict          =						{}
            if top_k is not None:
                 __snake_case			:Tuple          =						top_k
            return {}, {}, postprocess_params



       def __call__(							self			,						a__			,						**a__    ) ->      int:






            '''simple docstring'''





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



       def 					__lowercase						(							self			,						a__    ) ->      List[str]:






            '''simple docstring'''





            __snake_case			:int          =						load_image(a__    )
            __snake_case			:Any          =						self.image_processor(images=a__			,						return_tensors=self.framework    )
            return model_inputs



       def 					__lowercase						(							self			,						a__    ) ->      Any:






            '''simple docstring'''





            __snake_case			:Optional[int]          =						self.model(**a__    )
            return model_outputs



       def 					__lowercase						(							self			,						a__			,						a__=5    ) ->      Optional[Any]:






            '''simple docstring'''





            if top_k > self.model.config.num_labels:
                 __snake_case			:List[Any]          =						self.model.config.num_labels

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

            __snake_case			:Dict          =						scores.tolist()
            __snake_case			:List[Any]          =						ids.tolist()
            return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(a__			,						a__    )]










def   UpperCamelCase  (       snake_case__     :   str  ):




     '''simple docstring'''






     __snake_case			:List[Any]          =						hex_num.strip()
     if not hex_num:
          raise ValueError("""No value was passed to the function"""  )

     __snake_case			:str          =						hex_num[0] == """-"""
     if is_negative:
          __snake_case			:List[Any]          =						hex_num[1:]

     try:
          __snake_case			:Tuple          =						int(snake_case__	,16  )
     except ValueError:
          raise ValueError("""Invalid value was passed to the function"""  )

     __snake_case			:int          =						""""""
     while int_num > 0:
          __snake_case			:Optional[int]          =						str(int_num % 2  ) + bin_str
          int_num >>= 1

     return int(("""-""" + bin_str) if is_negative else bin_str  )


if __name__ == "__main__":
     import doctest

     doctest.testmod()


