



'''simple docstring'''





import copy
import os
from typing import Union

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


_lowerCAmelCase      						=	logging.get_logger(__name__)

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






class        lowerCAmelCase_(				SCREAMING_SNAKE_CASE_     ):
  '''simple docstring'''




  __lowercase							:      Optional[int]				=      '''pix2struct_text_model'''
  __lowercase							:      str				=      ['''past_key_values''']
  __lowercase							:      Dict				=      {
      '''hidden_size''': '''hidden_size''',
      '''num_attention_heads''': '''num_heads''',
      '''num_hidden_layers''': '''num_layers''',
  }




  def __init__(    self  ,__UpperCAmelCase=5_0244  ,__UpperCAmelCase=768  ,__UpperCAmelCase=64  ,__UpperCAmelCase=2048  ,__UpperCAmelCase=12  ,__UpperCAmelCase=12  ,__UpperCAmelCase=32  ,__UpperCAmelCase=128  ,__UpperCAmelCase=0.1  ,__UpperCAmelCase=1E-6  ,__UpperCAmelCase=1.0  ,__UpperCAmelCase="gelu_new"  ,__UpperCAmelCase=0  ,__UpperCAmelCase=False  ,__UpperCAmelCase=0  ,__UpperCAmelCase=1  ,__UpperCAmelCase=False  ,__UpperCAmelCase=True  ,**__UpperCAmelCase  ,)    ->		Tuple:
      lowerCAmelCase__ :  Optional[Any]							      =					vocab_size
      lowerCAmelCase__ :  List[Any]							      =					hidden_size
      lowerCAmelCase__ :  List[Any]							      =					d_kv
      lowerCAmelCase__ :  Optional[Any]							      =					d_ff
      lowerCAmelCase__ :  Optional[int]							      =					num_layers
      lowerCAmelCase__ :  Optional[Any]							      =					num_heads
      lowerCAmelCase__ :  str							      =					relative_attention_num_buckets
      lowerCAmelCase__ :  Dict							      =					relative_attention_max_distance
      lowerCAmelCase__ :  Optional[Any]							      =					dropout_rate
      lowerCAmelCase__ :  Optional[int]							      =					layer_norm_epsilon
      lowerCAmelCase__ :  List[str]							      =					initializer_factor
      lowerCAmelCase__ :  Dict							      =					use_cache

      lowerCAmelCase__ :  int							      =					eos_token_id
      lowerCAmelCase__ :  Optional[int]							      =					decoder_start_token_id

      # for backwards compatibility
      lowerCAmelCase__ :  Optional[int]							      =					dense_act_fn

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






  @classmethod
  def 							UpperCAmelCase_	(    cls  ,__UpperCAmelCase  ,**__UpperCAmelCase      )    ->		"PretrainedConfig":
      cls._set_token_in_kwargs(__UpperCAmelCase      )

      lowerCAmelCase__      ,   lowerCAmelCase__ :  Dict							      =					cls.get_config_dict(__UpperCAmelCase  ,**__UpperCAmelCase      )

      # get the text config dict if we are loading from Pix2StructConfig
      if config_dict.get("""model_type"""      ) == "pix2struct":
          lowerCAmelCase__ :  List[str]							      =					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(__UpperCAmelCase  ,**__UpperCAmelCase      )







class        lowerCAmelCase_(				SCREAMING_SNAKE_CASE_     ):
  '''simple docstring'''




  __lowercase							:      Tuple				=      '''pix2struct_vision_model'''




  def __init__(    self  ,__UpperCAmelCase=768  ,__UpperCAmelCase=768  ,__UpperCAmelCase=2048  ,__UpperCAmelCase=64  ,__UpperCAmelCase=12  ,__UpperCAmelCase=12  ,__UpperCAmelCase="gelu_new"  ,__UpperCAmelCase=1E-6  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=1E-10  ,__UpperCAmelCase=1.0  ,__UpperCAmelCase=4096  ,__UpperCAmelCase=32  ,__UpperCAmelCase=128  ,**__UpperCAmelCase  ,)    ->		Union[str, Any]:
      super().__init__(**__UpperCAmelCase      )

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






  @classmethod
  def 							UpperCAmelCase_	(    cls  ,__UpperCAmelCase  ,**__UpperCAmelCase      )    ->		"PretrainedConfig":
      cls._set_token_in_kwargs(__UpperCAmelCase      )

      lowerCAmelCase__      ,   lowerCAmelCase__ :  Any							      =					cls.get_config_dict(__UpperCAmelCase  ,**__UpperCAmelCase      )

      # 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(__UpperCAmelCase  ,**__UpperCAmelCase      )







class        lowerCAmelCase_(				SCREAMING_SNAKE_CASE_     ):
  '''simple docstring'''




  __lowercase							:      Union[str, Any]				=      '''pix2struct'''
  __lowercase							:      str				=      True




  def __init__(    self  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,__UpperCAmelCase=1.0  ,__UpperCAmelCase=0.0_2  ,__UpperCAmelCase=False  ,__UpperCAmelCase=False  ,__UpperCAmelCase=True  ,**__UpperCAmelCase  ,)    ->		Tuple:
      super().__init__(tie_word_embeddings=__UpperCAmelCase  ,is_encoder_decoder=__UpperCAmelCase  ,**__UpperCAmelCase      )

      if text_config is None:
          lowerCAmelCase__ :  Union[str, Any]							      =					{}
          logger.info("""text_config is None. Initializing the Pix2StructTextConfig with default values."""      )

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

      lowerCAmelCase__ :  Optional[Any]							      =					PixaStructTextConfig(**__UpperCAmelCase      )
      lowerCAmelCase__ :  Any							      =					PixaStructVisionConfig(**__UpperCAmelCase      )

      lowerCAmelCase__ :  Tuple							      =					self.text_config.decoder_start_token_id
      lowerCAmelCase__ :  List[str]							      =					self.text_config.pad_token_id
      lowerCAmelCase__ :  Any							      =					self.text_config.eos_token_id

      lowerCAmelCase__ :  Dict							      =					initializer_factor
      lowerCAmelCase__ :  List[str]							      =					initializer_range

      lowerCAmelCase__ :  int							      =					self.initializer_range
      lowerCAmelCase__ :  Tuple							      =					self.initializer_range

      lowerCAmelCase__ :  Optional[int]							      =					is_vqa




  @classmethod
  def 							UpperCAmelCase_	(    cls  ,__UpperCAmelCase  ,__UpperCAmelCase  ,**__UpperCAmelCase      )    ->		Optional[int]:
      return cls(text_config=text_config.to_dict()  ,vision_config=vision_config.to_dict()  ,**__UpperCAmelCase      )






  def 							UpperCAmelCase_	(    self      )    ->		str:
      lowerCAmelCase__ :  str							      =					copy.deepcopy(self.__dict__      )
      lowerCAmelCase__ :  Any							      =					self.text_config.to_dict()
      lowerCAmelCase__ :  Optional[Any]							      =					self.vision_config.to_dict()
      lowerCAmelCase__ :  Any							      =					self.__class__.model_type
      return output







'''simple docstring'''





from collections import UserDict
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_torch_available():
 from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
 from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
 from ..tf_utils import stable_softmax

_lowerCAmelCase      						=	logging.get_logger(__name__)






@add_end_docstrings(SCREAMING_SNAKE_CASE_     )
class        lowerCAmelCase_(				SCREAMING_SNAKE_CASE_     ):
  '''simple docstring'''




  def __init__(    self  ,**__UpperCAmelCase      )    ->		Tuple:
      super().__init__(**__UpperCAmelCase      )

      requires_backends(self  ,"""vision"""      )
      self.check_model_type(
          TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
          if self.framework == """tf"""
          else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING      )




  def __call__(    self  ,__UpperCAmelCase  ,**__UpperCAmelCase      )    ->		str:
      return super().__call__(__UpperCAmelCase  ,**__UpperCAmelCase      )




  def 							UpperCAmelCase_	(    self  ,**__UpperCAmelCase      )    ->		str:
      lowerCAmelCase__ :  List[Any]							      =					{}
      if "candidate_labels" in kwargs:
          lowerCAmelCase__ :  int							      =					kwargs["""candidate_labels"""]
      if "hypothesis_template" in kwargs:
          lowerCAmelCase__ :  Optional[int]							      =					kwargs["""hypothesis_template"""]

      return preprocess_params, {}, {}




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase=None  ,__UpperCAmelCase="This is a photo of {}."      )    ->		int:
      lowerCAmelCase__ :  str							      =					load_image(__UpperCAmelCase      )
      lowerCAmelCase__ :  Dict							      =					self.image_processor(images=[image]  ,return_tensors=self.framework      )
      lowerCAmelCase__ :  List[Any]							      =					candidate_labels
      lowerCAmelCase__ :  List[str]							      =					[hypothesis_template.format(__UpperCAmelCase      ) for x in candidate_labels]
      lowerCAmelCase__ :  Optional[Any]							      =					self.tokenizer(__UpperCAmelCase  ,return_tensors=self.framework  ,padding=__UpperCAmelCase      )
      lowerCAmelCase__ :  Tuple							      =					[text_inputs]
      return inputs




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  Tuple							      =					model_inputs.pop("""candidate_labels"""      )
      lowerCAmelCase__ :  Union[str, Any]							      =					model_inputs.pop("""text_inputs"""      )
      if isinstance(text_inputs[0]  ,__UpperCAmelCase      ):
          lowerCAmelCase__ :  int							      =					text_inputs[0]
      else:
          # Batching case.
          lowerCAmelCase__ :  Dict							      =					text_inputs[0][0]

      lowerCAmelCase__ :  Any							      =					self.model(**__UpperCAmelCase  ,**__UpperCAmelCase      )

      lowerCAmelCase__ :  Union[str, Any]							      =					{
          """candidate_labels""": candidate_labels,
          """logits""": outputs.logits_per_image,
      }
      return model_outputs






  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase      )    ->		Any:
      lowerCAmelCase__ :  Union[str, Any]							      =					model_outputs.pop("""candidate_labels"""      )
      lowerCAmelCase__ :  List[str]							      =					model_outputs["""logits"""][0]
      if self.framework == "pt":
          lowerCAmelCase__ :  List[str]							      =					logits.softmax(dim=-1      ).squeeze(-1      )
          lowerCAmelCase__ :  Optional[Any]							      =					probs.tolist()
          if not isinstance(__UpperCAmelCase  ,__UpperCAmelCase      ):
              lowerCAmelCase__ :  Dict							      =					[scores]
      elif self.framework == "tf":
          lowerCAmelCase__ :  Any							      =					stable_softmax(__UpperCAmelCase  ,axis=-1      )
          lowerCAmelCase__ :  List[Any]							      =					probs.numpy().tolist()
      else:
          raise ValueError(F"""Unsupported framework: {self.framework}"""      )

      lowerCAmelCase__ :  Tuple							      =					[
          {"""score""": score, """label""": candidate_label}
          for score, candidate_label in sorted(zip(__UpperCAmelCase  ,__UpperCAmelCase      )  ,key=lambda __UpperCAmelCase      : -x[0]      )
      ]
      return result


