'''simple docstring'''





import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase			:							Dict     =logging.get_logger(__name__)

lowerCAmelCase			:							Tuple     ={
    """google/pix2struct-textcaps-base""": (
        """https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"""
    ),
}




class 	__snake_case	( _snake_case		):





      '''simple docstring'''






      _snake_case    		=       """pix2struct_text_model"""
      _snake_case    		=       ["""past_key_values"""]
      _snake_case    		=       {
          """hidden_size""": """hidden_size""",
          """num_attention_heads""": """num_heads""",
          """num_hidden_layers""": """num_layers""",
      }



      def __init__(					self     :				List[str]    ,  _UpperCamelCase     :				List[str]=5_0244    ,  _UpperCamelCase     :				str=768    ,  _UpperCamelCase     :				Union[str, Any]=64    ,  _UpperCamelCase     :				Optional[int]=2048    ,  _UpperCamelCase     :				Optional[int]=12    ,  _UpperCamelCase     :				Dict=12    ,  _UpperCamelCase     :				Union[str, Any]=32    ,  _UpperCamelCase     :				Dict=128    ,  _UpperCamelCase     :				int=0.1    ,  _UpperCamelCase     :				Any=1E-6    ,  _UpperCamelCase     :				List[Any]=1.0    ,  _UpperCamelCase     :				Any="gelu_new"    ,  _UpperCamelCase     :				Union[str, Any]=0    ,  _UpperCamelCase     :				int=False    ,  _UpperCamelCase     :				Tuple=0    ,  _UpperCamelCase     :				Tuple=1    ,  _UpperCamelCase     :				List[str]=False    ,  _UpperCamelCase     :				List[str]=True    ,  **_UpperCamelCase     :				str    ,  )				->str:


          """simple docstring"""


          _lowerCamelCase     :		int 						=						vocab_size
          _lowerCamelCase     :		List[str] 						=						hidden_size
          _lowerCamelCase     :		int 						=						d_kv
          _lowerCamelCase     :		Any 						=						d_ff
          _lowerCamelCase     :		int 						=						num_layers
          _lowerCamelCase     :		List[str] 						=						num_heads
          _lowerCamelCase     :		Any 						=						relative_attention_num_buckets
          _lowerCamelCase     :		Union[str, Any] 						=						relative_attention_max_distance
          _lowerCamelCase     :		str 						=						dropout_rate
          _lowerCamelCase     :		str 						=						layer_norm_epsilon
          _lowerCamelCase     :		Optional[Any] 						=						initializer_factor
          _lowerCamelCase     :		Any 						=						use_cache

          _lowerCamelCase     :		Dict 						=						eos_token_id
          _lowerCamelCase     :		Dict 						=						decoder_start_token_id

          # for backwards compatibility
          _lowerCamelCase     :		List[str] 						=						dense_act_fn

          super().__init__(
              pad_token_id=snake_case_    ,  eos_token_id=snake_case_    ,  decoder_start_token_id=snake_case_    ,  tie_word_embeddings=snake_case_    ,  is_decoder=snake_case_    ,  **snake_case_    ,  )




      @classmethod
      def 	_SCREAMING_SNAKE_CASE    (					cls     :				Dict    ,  _UpperCamelCase     :				Any    ,  **_UpperCamelCase     :				Optional[int])				->Tuple:


          """simple docstring"""


          cls._set_token_in_kwargs(snake_case_)

          _lowerCamelCase     :		Dict 						=						cls.get_config_dict(snake_case_    ,  **snake_case_)

          # get the text config dict if we are loading from Pix2StructConfig
          if config_dict.get("""model_type""") == "pix2struct":
              _lowerCamelCase     :		int 						=						config_dict["text_config"]

          if "model_type" in config_dict and hasattr(cls    ,  """model_type""") and config_dict["model_type"] != cls.model_type:
              logger.warning(
                  F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                  F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""")

          return cls.from_dict(snake_case_    ,  **snake_case_)





class 	__snake_case	( _snake_case		):





      '''simple docstring'''






      _snake_case    		=       """pix2struct_vision_model"""



      def __init__(					self     :				Optional[int]    ,  _UpperCamelCase     :				List[Any]=768    ,  _UpperCamelCase     :				str=768    ,  _UpperCamelCase     :				Optional[Any]=2048    ,  _UpperCamelCase     :				Union[str, Any]=64    ,  _UpperCamelCase     :				Optional[int]=12    ,  _UpperCamelCase     :				Tuple=12    ,  _UpperCamelCase     :				List[str]="gelu_new"    ,  _UpperCamelCase     :				str=1E-6    ,  _UpperCamelCase     :				Union[str, Any]=0.0    ,  _UpperCamelCase     :				List[Any]=0.0    ,  _UpperCamelCase     :				List[Any]=1E-1_0    ,  _UpperCamelCase     :				Any=1.0    ,  _UpperCamelCase     :				str=4096    ,  _UpperCamelCase     :				Optional[int]=32    ,  _UpperCamelCase     :				int=128    ,  **_UpperCamelCase     :				Any    ,  )				->Dict:


          """simple docstring"""


          super().__init__(**snake_case_)

          _lowerCamelCase     :		Optional[Any] 						=						hidden_size
          _lowerCamelCase     :		Dict 						=						patch_embed_hidden_size
          _lowerCamelCase     :		Tuple 						=						d_ff
          _lowerCamelCase     :		Tuple 						=						dropout_rate
          _lowerCamelCase     :		int 						=						num_hidden_layers
          _lowerCamelCase     :		Optional[int] 						=						num_attention_heads
          _lowerCamelCase     :		Any 						=						initializer_range
          _lowerCamelCase     :		List[str] 						=						initializer_factor
          _lowerCamelCase     :		Tuple 						=						attention_dropout
          _lowerCamelCase     :		Optional[int] 						=						layer_norm_eps
          _lowerCamelCase     :		int 						=						dense_act_fn
          _lowerCamelCase     :		int 						=						seq_len
          _lowerCamelCase     :		Optional[Any] 						=						relative_attention_num_buckets
          _lowerCamelCase     :		str 						=						relative_attention_max_distance
          _lowerCamelCase     :		List[Any] 						=						d_kv




      @classmethod
      def 	_SCREAMING_SNAKE_CASE    (					cls     :				str    ,  _UpperCamelCase     :				Tuple    ,  **_UpperCamelCase     :				str)				->int:


          """simple docstring"""


          cls._set_token_in_kwargs(snake_case_)

          _lowerCamelCase     :		str 						=						cls.get_config_dict(snake_case_    ,  **snake_case_)

          # get the vision config dict if we are loading from Pix2StructConfig
          if config_dict.get("""model_type""") == "pix2struct":
              _lowerCamelCase     :		Optional[Any] 						=						config_dict["vision_config"]

          if "model_type" in config_dict and hasattr(cls    ,  """model_type""") and config_dict["model_type"] != cls.model_type:
              logger.warning(
                  F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                  F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""")

          return cls.from_dict(snake_case_    ,  **snake_case_)





class 	__snake_case	( _snake_case		):





      '''simple docstring'''






      _snake_case    		=       """pix2struct"""
      _snake_case    		=       True



      def __init__(					self     :				List[Any]    ,  _UpperCamelCase     :				Optional[int]=None    ,  _UpperCamelCase     :				Optional[int]=None    ,  _UpperCamelCase     :				Dict=1.0    ,  _UpperCamelCase     :				Dict=0.0_2    ,  _UpperCamelCase     :				Optional[int]=False    ,  _UpperCamelCase     :				Optional[int]=False    ,  _UpperCamelCase     :				Optional[int]=True    ,  **_UpperCamelCase     :				Optional[Any]    ,  )				->int:


          """simple docstring"""


          super().__init__(tie_word_embeddings=snake_case_    ,  is_encoder_decoder=snake_case_    ,  **snake_case_)

          if text_config is None:
              _lowerCamelCase     :		List[str] 						=						{}
              logger.info("""text_config is None. Initializing the Pix2StructTextConfig with default values.""")

          if vision_config is None:
              _lowerCamelCase     :		Optional[Any] 						=						{}
              logger.info("""vision_config is None. Initializing the Pix2StructVisionConfig with default values.""")

          _lowerCamelCase     :		Tuple 						=						PixaStructTextConfig(**snake_case_)
          _lowerCamelCase     :		Optional[int] 						=						PixaStructVisionConfig(**snake_case_)

          _lowerCamelCase     :		List[str] 						=						self.text_config.decoder_start_token_id
          _lowerCamelCase     :		List[str] 						=						self.text_config.pad_token_id
          _lowerCamelCase     :		Dict 						=						self.text_config.eos_token_id

          _lowerCamelCase     :		Union[str, Any] 						=						initializer_factor
          _lowerCamelCase     :		str 						=						initializer_range

          _lowerCamelCase     :		Any 						=						self.initializer_range
          _lowerCamelCase     :		Optional[int] 						=						self.initializer_range

          _lowerCamelCase     :		List[str] 						=						is_vqa



      @classmethod
      def 	_SCREAMING_SNAKE_CASE    (					cls     :				Optional[Any]    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				Any    ,  **_UpperCamelCase     :				Optional[Any])				->Dict:


          """simple docstring"""


          return cls(text_config=text_config.to_dict()    ,  vision_config=vision_config.to_dict()    ,  **snake_case_)




      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->Any:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						copy.deepcopy(self.__dict__)
          _lowerCamelCase     :		str 						=						self.text_config.to_dict()
          _lowerCamelCase     :		Optional[int] 						=						self.vision_config.to_dict()
          _lowerCamelCase     :		Optional[int] 						=						self.__class__.model_type
          return output





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)]




