
import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
  import tensorflow as tf

  from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
  from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

lowercase  :					List[Any]						    =							logging.get_logger(__name__)

class __snake_case		(      enum.Enum     ):
    _a		:						Any=				0
    _a		:						Optional[Any]=				1






@add_end_docstrings(lowerCAmelCase     )
class __snake_case		(      lowerCAmelCase     ):
    _a		:						int=				"generated"
    def __init__(  self		,*snake_case		,**snake_case      ):



           '''simple docstring'''







           super().__init__(*snake_case		,**snake_case      )

           self.check_model_type(
               TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
               if self.framework == """tf"""
               else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case=None		,snake_case=None		,snake_case=None		,snake_case=None		,snake_case=None		,snake_case=None		,**snake_case		,):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      {}
           if truncation is not None:
                  lowercase							:     int       	=      truncation

           lowercase							:     Union[str, Any]       	=      generate_kwargs

           lowercase							:     List[Any]       	=      {}
           if return_tensors is not None and return_type is None:
                  lowercase							:     Any       	=      ReturnType.TENSORS if return_tensors else ReturnType.TEXT
           if return_type is not None:
                  lowercase							:     Dict       	=      return_type

           if clean_up_tokenization_spaces is not None:
                  lowercase							:     Optional[Any]       	=      clean_up_tokenization_spaces

           if stop_sequence is not None:
                  lowercase							:     Union[str, Any]       	=      self.tokenizer.encode(snake_case		,add_special_tokens=snake_case      )
                  if len(snake_case      ) > 1:
                         warnings.warn(
                             """Stopping on a multiple token sequence is not yet supported on transformers. The first token of"""
                             """ the stop sequence will be used as the stop sequence string in the interim."""      )
                  lowercase							:     List[Any]       	=      stop_sequence_ids[0]

           return preprocess_params, forward_params, postprocess_params
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           return True
    def       _SCREAMING_SNAKE_CASE       (  self		,*snake_case		,snake_case      ):



           '''simple docstring'''







           lowercase							:     Union[str, Any]       	=      self.model.config.prefix if self.model.config.prefix is not None else """"""
           if isinstance(args[0]		,snake_case      ):
                  if self.tokenizer.pad_token_id is None:
                         raise ValueError("""Please make sure that the tokenizer has a pad_token_id when using a batch input"""      )
                  lowercase							:     int       	=      ([prefix + arg for arg in args[0]],)
                  lowercase							:     str       	=      True

           elif isinstance(args[0]		,snake_case      ):
                  lowercase							:     Optional[int]       	=      (prefix + args[0],)
                  lowercase							:     Union[str, Any]       	=      False
           else:
                  raise ValueError(
                      f" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"      )
           lowercase							:     Optional[int]       	=      self.tokenizer(*snake_case		,padding=snake_case		,truncation=snake_case		,return_tensors=self.framework      )
           # This is produced by tokenizers but is an invalid generate kwargs
           if "token_type_ids" in inputs:
                  del inputs["token_type_ids"]
           return inputs
    def __call__(  self		,*snake_case		,**snake_case      ):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      super().__call__(*snake_case		,**snake_case      )
           if (
               isinstance(args[0]		,snake_case      )
               and all(isinstance(snake_case		,snake_case      ) for el in args[0]      )
               and all(len(snake_case      ) == 1 for res in result      )
           ):
                  return [res[0] for res in result]
           return result
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case=TruncationStrategy.DO_NOT_TRUNCATE		,**snake_case      ):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      self._parse_and_tokenize(snake_case		,truncation=snake_case		,**snake_case      )
           return inputs
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,**snake_case      ):



           '''simple docstring'''







           if self.framework == "pt":
                  lowercase	,			lowercase							:     Any       	=      model_inputs["""input_ids"""].shape
           elif self.framework == "tf":
                  lowercase	,			lowercase							:     Optional[Any]       	=      tf.shape(model_inputs["""input_ids"""]      ).numpy()

           lowercase							:     Union[str, Any]       	=      generate_kwargs.get("""min_length"""		,self.model.config.min_length      )
           lowercase							:     Tuple       	=      generate_kwargs.get("""max_length"""		,self.model.config.max_length      )
           self.check_inputs(snake_case		,generate_kwargs["""min_length"""]		,generate_kwargs["""max_length"""]      )
           lowercase							:     int       	=      self.model.generate(**snake_case		,**snake_case      )
           lowercase							:     Union[str, Any]       	=      output_ids.shape[0]
           if self.framework == "pt":
                  lowercase							:     Any       	=      output_ids.reshape(snake_case		,out_b // in_b		,*output_ids.shape[1:]      )
           elif self.framework == "tf":
                  lowercase							:     List[Any]       	=      tf.reshape(snake_case		,(in_b, out_b // in_b, *output_ids.shape[1:])      )
           return {"output_ids": output_ids}
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case=ReturnType.TEXT		,snake_case=False      ):



           '''simple docstring'''







           lowercase							:     int       	=      []
           for output_ids in model_outputs["output_ids"][0]:
                  if return_type == ReturnType.TENSORS:
                         lowercase							:     Union[str, Any]       	=      {f"{self.return_name}_token_ids": output_ids}
                  elif return_type == ReturnType.TEXT:
                         lowercase							:     Tuple       	=      {
                             f"{self.return_name}_text": self.tokenizer.decode(
                                 snake_case		,skip_special_tokens=snake_case		,clean_up_tokenization_spaces=snake_case		,)
                         }
                  records.append(snake_case      )
           return records






@add_end_docstrings(lowerCAmelCase     )
class __snake_case		(      lowerCAmelCase     ):
    _a		:						Optional[int]=				"summary"
    def __call__(  self		,*snake_case		,**snake_case      ):



           '''simple docstring'''







           return super().__call__(*snake_case		,**snake_case      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           if max_length < min_length:
                  logger.warning(f"Your min_length={min_length} must be inferior than your max_length={max_length}."      )

           if input_length < max_length:
                  logger.warning(
                      f"Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is "
                      """a summarization task, where outputs shorter than the input are typically wanted, you might """
                      f"consider decreasing max_length manually, e.g. summarizer('...', max_length={input_length//2})"      )






@add_end_docstrings(lowerCAmelCase     )
class __snake_case		(      lowerCAmelCase     ):
    _a		:						Tuple=				"translation"
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           if input_length > 0.9 * max_length:
                  logger.warning(
                      f"Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider "
                      """increasing your max_length manually, e.g. translator('...', max_length=400)"""      )
           return True
    def       _SCREAMING_SNAKE_CASE       (  self		,*snake_case		,snake_case=TruncationStrategy.DO_NOT_TRUNCATE		,snake_case=None		,snake_case=None      ):



           '''simple docstring'''







           if getattr(self.tokenizer		,"""_build_translation_inputs"""		,snake_case      ):
                  return self.tokenizer._build_translation_inputs(
                      *snake_case		,return_tensors=self.framework		,truncation=snake_case		,src_lang=snake_case		,tgt_lang=snake_case      )
           else:
                  return super()._parse_and_tokenize(*snake_case		,truncation=snake_case      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case=None		,snake_case=None		,**snake_case      ):



           '''simple docstring'''







           lowercase	,			lowercase	,			lowercase							:     List[Any]       	=      super()._sanitize_parameters(**snake_case      )
           if src_lang is not None:
                  lowercase							:     int       	=      src_lang
           if tgt_lang is not None:
                  lowercase							:     str       	=      tgt_lang
           if src_lang is None and tgt_lang is None:
                  # Backward compatibility, direct arguments use is preferred.
                  lowercase							:     List[str]       	=      kwargs.get("""task"""		,self.task      )
                  lowercase							:     Tuple       	=      task.split("""_"""      )
                  if task and len(snake_case      ) == 4:
                         # translation, XX, to YY
                         lowercase							:     Any       	=      items[1]
                         lowercase							:     List[str]       	=      items[3]
           return preprocess_params, forward_params, postprocess_params
    def __call__(  self		,*snake_case		,**snake_case      ):



           '''simple docstring'''







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


'''simple docstring'''







import copy
import os
from typing import Union

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


_lowercase :  Optional[Any]    							=       logging.get_logger(__name__)

_lowercase :  List[str]    							=       {
    "google/pix2struct-textcaps-base": (
        "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"
    ),
}






class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_text_model'''
					lowerCAmelCase_        =     ['''past_key_values''']
					lowerCAmelCase_        =     {
					    '''hidden_size''': '''hidden_size''',
					    '''num_attention_heads''': '''num_heads''',
					    '''num_hidden_layers''': '''num_layers''',
					}






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=5_02_44				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							__SCREAMING_SNAKE_CASE=0.1				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=1				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





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

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

											# for backwards compatibility
											lowercase_	:     Any													=       dense_act_fn

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






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     Optional[int]													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

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







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_vision_model'''






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=1E-1_0				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=40_96				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(**__SCREAMING_SNAKE_CASE		)

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






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     str													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											# get the vision config dict if we are loading from Pix2StructConfig
											if config_dict.get('''model_type'''		) == "pix2struct":
																	lowercase_	:     Optional[int]													=       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(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct'''
					lowerCAmelCase_        =     True






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=0.02				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





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

											if text_config is None:
																	lowercase_	:     Optional[Any]													=       {}
																	logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''		)

											if vision_config is None:
																	lowercase_	:     Dict													=       {}
																	logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''		)

											lowercase_	:     str													=       PixaStructTextConfig(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Tuple													=       PixaStructVisionConfig(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       self.text_config.decoder_start_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.pad_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.eos_token_id

											lowercase_	:     int													=       initializer_factor
											lowercase_	:     Any													=       initializer_range

											lowercase_	:     str													=       self.initializer_range
											lowercase_	:     str													=       self.initializer_range

											lowercase_	:     int													=       is_vqa






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       copy.deepcopy(self.__dict__		)
											lowercase_	:     Any													=       self.text_config.to_dict()
											lowercase_	:     Optional[Any]													=       self.vision_config.to_dict()
											lowercase_	:     Optional[int]													=       self.__class__.model_type
											return output



