




"""simple docstring"""




from argparse import ArgumentParser

from . import BaseTransformersCLICommand







def 			__UpperCAmelCase			(      __UpperCamelCase     ):
			return DownloadCommand(args.model     ,							args.cache_dir     ,							args.force     ,							args.trust_remote_code     )


class    UpperCAmelCase_  (       snake_case						):
		@staticmethod
		def      _lowerCamelCase				(		UpperCamelCase_    )		->    Any:
					__lowercase       :   str        =    parser.add_parser('''download'''    )
					download_parser.add_argument(
					    '''--cache-dir'''							,		type=UpperCamelCase_							,		default=UpperCamelCase_							,		help='''Path to location to store the models'''    )
					download_parser.add_argument(
					    '''--force'''							,		action='''store_true'''							,		help='''Force the model to be download even if already in cache-dir'''    )
					download_parser.add_argument(
					    '''--trust-remote-code'''							,		action='''store_true'''							,		help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine'''							,		)
					download_parser.add_argument('''model'''							,		type=UpperCamelCase_							,		help='''Name of the model to download'''    )
					download_parser.set_defaults(func=UpperCamelCase_    )
		def __init__(		self							,		UpperCamelCase_							,		UpperCamelCase_							,		UpperCamelCase_							,		UpperCamelCase_    )		->    Union[str, Any]:
					__lowercase       :   str        =    model
					__lowercase       :   str        =    cache
					__lowercase       :   Optional[Any]        =    force
					__lowercase       :   int        =    trust_remote_code
		def      _lowerCamelCase				(		self    )		->    Dict:
					from ..models.auto import AutoModel, AutoTokenizer

					AutoModel.from_pretrained(
					    self._model							,		cache_dir=self._cache							,		force_download=self._force							,		trust_remote_code=self._trust_remote_code    )
					AutoTokenizer.from_pretrained(
					    self._model							,		cache_dir=self._cache							,		force_download=self._force							,		trust_remote_code=self._trust_remote_code    )





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(__magic_name__	)
class      A__							(     __magic_name__	):






				def __init__(						self					:      int    ,     *a					:      Dict    ,     **a					:      Union[str, Any]   ):






							'''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 							_lowerCamelCase		(						self					:      Dict    ,     a					:      List[str]=None   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Any         =		{}
							if top_k is not None:
										lowerCAmelCase__      :      Tuple         =		top_k
							return {}, {}, postprocess_params






				def __call__(						self					:      Any    ,     a					:      Union[str, List[str], "Image.Image", List["Image.Image"]]    ,     **a					:      List[Any]   ):






							'''simple docstring'''







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






				def 							_lowerCamelCase		(						self					:      Any    ,     a					:      Optional[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[Any]         =		load_image(a   )
							lowerCAmelCase__      :      Optional[int]         =		self.image_processor(images=a    ,     return_tensors=self.framework   )
							return model_inputs






				def 							_lowerCamelCase		(						self					:      Optional[int]    ,     a					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[Any]         =		self.model(**a   )
							return model_outputs






				def 							_lowerCamelCase		(						self					:      Optional[Any]    ,     a					:      List[Any]    ,     a					:      List[Any]=5   ):






							'''simple docstring'''







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

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

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