'''simple docstring'''





from ...configuration_utils import PretrainedConfig


lowerCAmelCase			:							str     ={
    "google/tapas-base-finetuned-sqa": (
        "https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json"
    ),
    "google/tapas-base-finetuned-wtq": (
        "https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json"
    ),
    "google/tapas-base-finetuned-wikisql-supervised": (
        "https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json"
    ),
    "google/tapas-base-finetuned-tabfact": (
        "https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json"
    ),
}




class 	__snake_case	( __lowerCAmelCase		):





      '''simple docstring'''






      _snake_case    		=       'tapas'



      def __init__(					self     :				Optional[Any]    ,  _UpperCamelCase     :				Dict=3_0522    ,  _UpperCamelCase     :				int=768    ,  _UpperCamelCase     :				int=12    ,  _UpperCamelCase     :				List[Any]=12    ,  _UpperCamelCase     :				List[Any]=3072    ,  _UpperCamelCase     :				int="gelu"    ,  _UpperCamelCase     :				Tuple=0.1    ,  _UpperCamelCase     :				Union[str, Any]=0.1    ,  _UpperCamelCase     :				Tuple=1024    ,  _UpperCamelCase     :				str=[3, 256, 256, 2, 256, 256, 10]    ,  _UpperCamelCase     :				Tuple=0.0_2    ,  _UpperCamelCase     :				Union[str, Any]=1E-1_2    ,  _UpperCamelCase     :				Dict=0    ,  _UpperCamelCase     :				int=10.0    ,  _UpperCamelCase     :				Dict=0    ,  _UpperCamelCase     :				List[Any]=1.0    ,  _UpperCamelCase     :				Optional[int]=None    ,  _UpperCamelCase     :				str=1.0    ,  _UpperCamelCase     :				Union[str, Any]=False    ,  _UpperCamelCase     :				Any=None    ,  _UpperCamelCase     :				Optional[Any]=1.0    ,  _UpperCamelCase     :				Any=1.0    ,  _UpperCamelCase     :				Tuple=False    ,  _UpperCamelCase     :				List[Any]=False    ,  _UpperCamelCase     :				Any="ratio"    ,  _UpperCamelCase     :				Optional[Any]=None    ,  _UpperCamelCase     :				int=None    ,  _UpperCamelCase     :				Dict=64    ,  _UpperCamelCase     :				int=32    ,  _UpperCamelCase     :				List[Any]=False    ,  _UpperCamelCase     :				List[str]=True    ,  _UpperCamelCase     :				int=False    ,  _UpperCamelCase     :				Dict=False    ,  _UpperCamelCase     :				List[str]=True    ,  _UpperCamelCase     :				List[str]=False    ,  _UpperCamelCase     :				str=None    ,  _UpperCamelCase     :				List[Any]=None    ,  **_UpperCamelCase     :				int    ,  )				->Optional[Any]:


          """simple docstring"""


          super().__init__(pad_token_id=_UpperCamelCase    ,  **_UpperCamelCase)

          # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes)
          _lowerCamelCase     :		List[str] 						=						vocab_size
          _lowerCamelCase     :		Any 						=						hidden_size
          _lowerCamelCase     :		Dict 						=						num_hidden_layers
          _lowerCamelCase     :		Any 						=						num_attention_heads
          _lowerCamelCase     :		Union[str, Any] 						=						hidden_act
          _lowerCamelCase     :		int 						=						intermediate_size
          _lowerCamelCase     :		Tuple 						=						hidden_dropout_prob
          _lowerCamelCase     :		str 						=						attention_probs_dropout_prob
          _lowerCamelCase     :		Union[str, Any] 						=						max_position_embeddings
          _lowerCamelCase     :		Optional[Any] 						=						type_vocab_sizes
          _lowerCamelCase     :		int 						=						initializer_range
          _lowerCamelCase     :		Union[str, Any] 						=						layer_norm_eps

          # Fine-tuning task hyperparameters
          _lowerCamelCase     :		List[Any] 						=						positive_label_weight
          _lowerCamelCase     :		int 						=						num_aggregation_labels
          _lowerCamelCase     :		Any 						=						aggregation_loss_weight
          _lowerCamelCase     :		str 						=						use_answer_as_supervision
          _lowerCamelCase     :		int 						=						answer_loss_importance
          _lowerCamelCase     :		List[Any] 						=						use_normalized_answer_loss
          _lowerCamelCase     :		int 						=						huber_loss_delta
          _lowerCamelCase     :		Dict 						=						temperature
          _lowerCamelCase     :		List[str] 						=						aggregation_temperature
          _lowerCamelCase     :		Tuple 						=						use_gumbel_for_cells
          _lowerCamelCase     :		Optional[int] 						=						use_gumbel_for_aggregation
          _lowerCamelCase     :		Optional[int] 						=						average_approximation_function
          _lowerCamelCase     :		Optional[Any] 						=						cell_selection_preference
          _lowerCamelCase     :		Optional[Any] 						=						answer_loss_cutoff
          _lowerCamelCase     :		List[str] 						=						max_num_rows
          _lowerCamelCase     :		Optional[int] 						=						max_num_columns
          _lowerCamelCase     :		List[Any] 						=						average_logits_per_cell
          _lowerCamelCase     :		Dict 						=						select_one_column
          _lowerCamelCase     :		Optional[int] 						=						allow_empty_column_selection
          _lowerCamelCase     :		List[Any] 						=						init_cell_selection_weights_to_zero
          _lowerCamelCase     :		List[Any] 						=						reset_position_index_per_cell
          _lowerCamelCase     :		Tuple 						=						disable_per_token_loss

          # Aggregation hyperparameters
          _lowerCamelCase     :		Optional[int] 						=						aggregation_labels
          _lowerCamelCase     :		Any 						=						no_aggregation_label_index

          if isinstance(self.aggregation_labels    ,  _UpperCamelCase):
              _lowerCamelCase     :		int 						=						{int(_UpperCamelCase): v for k, v in aggregation_labels.items()}





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




@add_end_docstrings(__lowerCAmelCase		)
class 	__snake_case	( __lowerCAmelCase		):





      '''simple docstring'''






      def __init__(					self     :				str    ,  *_UpperCamelCase     :				int    ,  **_UpperCamelCase     :				List[str])				->Tuple:


          """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 	_SCREAMING_SNAKE_CASE    (					self     :				Dict    ,  _UpperCamelCase     :				List[str]=None)				->Optional[int]:


          """simple docstring"""


          _lowerCamelCase     :		Optional[int] 						=						{}
          if top_k is not None:
              _lowerCamelCase     :		str 						=						top_k
          return {}, {}, postprocess_params



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


          """simple docstring"""


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



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[int]    ,  _UpperCamelCase     :				Optional[int])				->str:


          """simple docstring"""


          _lowerCamelCase     :		Tuple 						=						load_image(_UpperCamelCase)
          _lowerCamelCase     :		Any 						=						self.image_processor(images=_UpperCamelCase    ,  return_tensors=self.framework)
          return model_inputs



      def 	_SCREAMING_SNAKE_CASE    (					self     :				str    ,  _UpperCamelCase     :				Union[str, Any])				->List[str]:


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						self.model(**_UpperCamelCase)
          return model_outputs




      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any]    ,  _UpperCamelCase     :				List[str]    ,  _UpperCamelCase     :				List[str]=5)				->str:


          """simple docstring"""


          if top_k > self.model.config.num_labels:
              _lowerCamelCase     :		Union[str, Any] 						=						self.model.config.num_labels

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

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




