





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

_UpperCAmelCase       : Union[str, Any]			     =       logging.get_logger(__name__)

@add_end_docstrings(UpperCamelCase_    )
class      lowerCAmelCase							(      UpperCamelCase_    ):
 def __init__(   self						:   Optional[Any]    ,  *UpperCAmelCase						:   int    ,  **UpperCAmelCase						:   Dict			)    ->			Optional[Any]:
     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   A_			(   self						:   Tuple    ,  UpperCAmelCase						:   Tuple=None			)    ->			Union[str, Any]:
     lowerCamelCase__					:   Optional[Any]      				=	{}
     if top_k is not None:
         lowerCamelCase__					:   str      				=	top_k
     return {}, {}, postprocess_params
 def __call__(   self						:   Tuple    ,  UpperCAmelCase						:   int    ,  **UpperCAmelCase						:   Optional[int]			)    ->			str:
     return super().__call__(__a    ,  **__a			)
 def   A_			(   self						:   List[str]    ,  UpperCAmelCase						:   str			)    ->			Union[str, Any]:
     lowerCamelCase__					:   Tuple      				=	load_image(__a			)
     lowerCamelCase__					:   Optional[int]      				=	self.image_processor(images=__a    ,  return_tensors=self.framework			)
     return model_inputs
 def   A_			(   self						:   Dict    ,  UpperCAmelCase						:   Tuple			)    ->			Optional[int]:
     lowerCamelCase__					:   Union[str, Any]      				=	self.model(**__a			)
     return model_outputs







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






     if top_k > self.model.config.num_labels:
         lowerCamelCase__					:   Optional[int]      				=	self.model.config.num_labels

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

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










from ..utils import DummyObject, requires_backends

class      lowerCAmelCase							(      metaclass=__UpperCamelCase    ):
 UpperCAmelCase__									=	["""torch""", """transformers""", """onnx"""]
 def __init__(   self						:   Tuple    ,  *UpperCAmelCase						:   str    ,  **UpperCAmelCase						:   Optional[int]			)    ->			List[str]:
     requires_backends(self    ,  ['torch', 'transformers', 'onnx']			)
 @classmethod
 def   A_			(   cls						:   Union[str, Any]    ,  *UpperCAmelCase						:   Dict    ,  **UpperCAmelCase						:   List[str]			)    ->			int:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)







 @classmethod
 def   A_			(   cls						:   Any    ,  *UpperCAmelCase						:   List[Any]    ,  **UpperCAmelCase						:   Tuple			)    ->			Union[str, Any]:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)


class      lowerCAmelCase							(      metaclass=__UpperCamelCase    ):
 UpperCAmelCase__									=	["""torch""", """transformers""", """onnx"""]
 def __init__(   self						:   List[Any]    ,  *UpperCAmelCase						:   Union[str, Any]    ,  **UpperCAmelCase						:   Dict			)    ->			Any:
     requires_backends(self    ,  ['torch', 'transformers', 'onnx']			)
 @classmethod
 def   A_			(   cls						:   str    ,  *UpperCAmelCase						:   Union[str, Any]    ,  **UpperCAmelCase						:   Union[str, Any]			)    ->			Tuple:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)







 @classmethod
 def   A_			(   cls						:   List[Any]    ,  *UpperCAmelCase						:   str    ,  **UpperCAmelCase						:   Dict			)    ->			str:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)


class      lowerCAmelCase							(      metaclass=__UpperCamelCase    ):
 UpperCAmelCase__									=	["""torch""", """transformers""", """onnx"""]
 def __init__(   self						:   Dict    ,  *UpperCAmelCase						:   Dict    ,  **UpperCAmelCase						:   Dict			)    ->			Optional[int]:
     requires_backends(self    ,  ['torch', 'transformers', 'onnx']			)
 @classmethod
 def   A_			(   cls						:   Optional[Any]    ,  *UpperCAmelCase						:   Union[str, Any]    ,  **UpperCAmelCase						:   Any			)    ->			Any:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)







 @classmethod
 def   A_			(   cls						:   List[str]    ,  *UpperCAmelCase						:   Any    ,  **UpperCAmelCase						:   Dict			)    ->			Tuple:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)


class      lowerCAmelCase							(      metaclass=__UpperCamelCase    ):
 UpperCAmelCase__									=	["""torch""", """transformers""", """onnx"""]
 def __init__(   self						:   Union[str, Any]    ,  *UpperCAmelCase						:   Optional[int]    ,  **UpperCAmelCase						:   Optional[int]			)    ->			Any:
     requires_backends(self    ,  ['torch', 'transformers', 'onnx']			)
 @classmethod
 def   A_			(   cls						:   List[Any]    ,  *UpperCAmelCase						:   Dict    ,  **UpperCAmelCase						:   int			)    ->			Dict:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)







 @classmethod
 def   A_			(   cls						:   Dict    ,  *UpperCAmelCase						:   Dict    ,  **UpperCAmelCase						:   Optional[int]			)    ->			Optional[int]:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)


class      lowerCAmelCase							(      metaclass=__UpperCamelCase    ):
 UpperCAmelCase__									=	["""torch""", """transformers""", """onnx"""]
 def __init__(   self						:   List[str]    ,  *UpperCAmelCase						:   List[Any]    ,  **UpperCAmelCase						:   List[str]			)    ->			Optional[int]:
     requires_backends(self    ,  ['torch', 'transformers', 'onnx']			)
 @classmethod
 def   A_			(   cls						:   List[Any]    ,  *UpperCAmelCase						:   Optional[Any]    ,  **UpperCAmelCase						:   Optional[Any]			)    ->			str:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)







 @classmethod
 def   A_			(   cls						:   List[Any]    ,  *UpperCAmelCase						:   Optional[int]    ,  **UpperCAmelCase						:   Any			)    ->			Any:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)


class      lowerCAmelCase							(      metaclass=__UpperCamelCase    ):
 UpperCAmelCase__									=	["""torch""", """transformers""", """onnx"""]
 def __init__(   self						:   List[str]    ,  *UpperCAmelCase						:   Tuple    ,  **UpperCAmelCase						:   Optional[int]			)    ->			Optional[Any]:
     requires_backends(self    ,  ['torch', 'transformers', 'onnx']			)
 @classmethod
 def   A_			(   cls						:   Union[str, Any]    ,  *UpperCAmelCase						:   List[Any]    ,  **UpperCAmelCase						:   Optional[Any]			)    ->			Tuple:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)







 @classmethod
 def   A_			(   cls						:   Tuple    ,  *UpperCAmelCase						:   List[str]    ,  **UpperCAmelCase						:   List[str]			)    ->			Tuple:
     requires_backends(cls    ,  ['torch', 'transformers', 'onnx']			)





