

'''simple docstring'''



from __future__ import annotations

from typing import Dict

from ...configuration_utils import PretrainedConfig


_UpperCamelCase						:  Optional[int]							       =				{
    "susnato/ernie-m-base_pytorch": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json",
    "susnato/ernie-m-large_pytorch": "https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json",
}



class 				_snake_case					(   a_			):
  SCREAMING_SNAKE_CASE			:       List[Any]				=     '''ernie_m'''
  SCREAMING_SNAKE_CASE			:       Dict[str, str]				=     {"dropout": "classifier_dropout", "num_classes": "num_labels"}

  def __init__(				self      , _SCREAMING_SNAKE_CASE = 25_00_02      , _SCREAMING_SNAKE_CASE = 7_68      , _SCREAMING_SNAKE_CASE = 12      , _SCREAMING_SNAKE_CASE = 12      , _SCREAMING_SNAKE_CASE = 30_72      , _SCREAMING_SNAKE_CASE = "gelu"      , _SCREAMING_SNAKE_CASE = 0.1      , _SCREAMING_SNAKE_CASE = 0.1      , _SCREAMING_SNAKE_CASE = 5_14      , _SCREAMING_SNAKE_CASE = 0.02      , _SCREAMING_SNAKE_CASE = 1      , _SCREAMING_SNAKE_CASE = 1e-05      , _SCREAMING_SNAKE_CASE=None      , _SCREAMING_SNAKE_CASE=False      , _SCREAMING_SNAKE_CASE=0.0      , **_SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							vocab_size
      lowerCAmelCase 						=							hidden_size
      lowerCAmelCase 						=							num_hidden_layers
      lowerCAmelCase 						=							num_attention_heads
      lowerCAmelCase 						=							intermediate_size
      lowerCAmelCase 						=							hidden_act
      lowerCAmelCase 						=							hidden_dropout_prob
      lowerCAmelCase 						=							attention_probs_dropout_prob
      lowerCAmelCase 						=							max_position_embeddings
      lowerCAmelCase 						=							initializer_range
      lowerCAmelCase 						=							layer_norm_eps
      lowerCAmelCase 						=							classifier_dropout
      lowerCAmelCase 						=							is_decoder
      lowerCAmelCase 						=							act_dropout








'''simple docstring'''



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						:  Optional[int]							       =				logging.get_logger(__name__)



@add_end_docstrings(a_			)
class 				_snake_case					(   a_			):

  def __init__(				self      , *_SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      super().__init__(*_SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	)
      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      , _SCREAMING_SNAKE_CASE=None	):





      '''simple docstring'''

      lowerCAmelCase 						=							{}
      if top_k is not None:
          lowerCAmelCase 						=							top_k
      return {}, {}, postprocess_params

  def __call__(				self      , _SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

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

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							load_image(_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							self.image_processor(images=_SCREAMING_SNAKE_CASE      , return_tensors=self.framework	)
      return model_inputs

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model(**_SCREAMING_SNAKE_CASE	)
      return model_outputs


  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE=5	):





      '''simple docstring'''

      if top_k > self.model.config.num_labels:
          lowerCAmelCase 						=							self.model.config.num_labels

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

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





