



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


a_      :      Dict    =     logging.get_logger(__name__)

a_      :      str    =     {
    'microsoft/markuplm-base': 'https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json',
    'microsoft/markuplm-large': 'https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json',
}
class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    Union[str, Any]      						=					'''markuplm'''

 def __init__(	self		,				SCREAMING_SNAKE_CASE=3_0_5_2_2		,				SCREAMING_SNAKE_CASE=7_6_8		,				SCREAMING_SNAKE_CASE=1_2		,				SCREAMING_SNAKE_CASE=1_2		,				SCREAMING_SNAKE_CASE=3_0_7_2		,				SCREAMING_SNAKE_CASE="gelu"		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=5_1_2		,				SCREAMING_SNAKE_CASE=2		,				SCREAMING_SNAKE_CASE=0.02		,				SCREAMING_SNAKE_CASE=1e-1_2		,				SCREAMING_SNAKE_CASE=0		,				SCREAMING_SNAKE_CASE=0		,				SCREAMING_SNAKE_CASE=2		,				SCREAMING_SNAKE_CASE=2_5_6		,				SCREAMING_SNAKE_CASE=1_0_2_4		,				SCREAMING_SNAKE_CASE=2_1_6		,				SCREAMING_SNAKE_CASE=1_0_0_1		,				SCREAMING_SNAKE_CASE=3_2		,				SCREAMING_SNAKE_CASE=5_0		,				SCREAMING_SNAKE_CASE="absolute"		,				SCREAMING_SNAKE_CASE=True		,				SCREAMING_SNAKE_CASE=None		,				**SCREAMING_SNAKE_CASE		,				) ->			List[str]:
  super().__init__(
      pad_token_id=SCREAMING_SNAKE_CASE		,				bos_token_id=SCREAMING_SNAKE_CASE		,				eos_token_id=SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE		,				)
  a__												= vocab_size
  a__												= hidden_size
  a__												= num_hidden_layers
  a__												= num_attention_heads
  a__												= hidden_act
  a__												= intermediate_size
  a__												= hidden_dropout_prob
  a__												= attention_probs_dropout_prob
  a__												= max_position_embeddings
  a__												= type_vocab_size
  a__												= initializer_range
  a__												= layer_norm_eps
  a__												= position_embedding_type
  a__												= use_cache
  a__												= classifier_dropout
  # additional properties
  a__												= max_depth
  a__												= max_xpath_tag_unit_embeddings
  a__												= max_xpath_subs_unit_embeddings
  a__												= tag_pad_id
  a__												= subs_pad_id
  a__												= xpath_unit_hidden_size










from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
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_VISUAL_QUESTION_ANSWERING_MAPPING

a_      :      str    =     logging.get_logger(__name__)
@add_end_docstrings(_lowercase				)
class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 def __init__(	self		,				*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			int:
  super().__init__(*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )
  self.check_model_type(SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				**SCREAMING_SNAKE_CASE      ) ->			Union[str, Any]:
  a__							,			a__												= {}, {}
  if padding is not None:
   a__												= padding
  if truncation is not None:
   a__												= truncation
  if top_k is not None:
   a__												= top_k
  return preprocess_params, {}, postprocess_params

 def __call__(	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE = None		,				**SCREAMING_SNAKE_CASE      ) ->			Optional[int]:
  if isinstance(SCREAMING_SNAKE_CASE		,				(Image.Image, str)      ) and isinstance(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ):
   a__												= {'''image''': image, '''question''': question}
  else:
   a__												= image
  a__												= super().__call__(SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )
  return results

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=False		,				SCREAMING_SNAKE_CASE=False      ) ->			Dict:
  a__												= load_image(inputs['''image''']      )
  a__												= self.tokenizer(
      inputs['''question''']		,				return_tensors=self.framework		,				padding=SCREAMING_SNAKE_CASE		,				truncation=SCREAMING_SNAKE_CASE      )
  a__												= self.image_processor(images=SCREAMING_SNAKE_CASE		,				return_tensors=self.framework      )
  model_inputs.update(SCREAMING_SNAKE_CASE      )
  return model_inputs

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE      ) ->			Union[str, Any]:
  a__												= self.model(**SCREAMING_SNAKE_CASE      )
  return model_outputs



 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=5      ) ->			Optional[Any]:
  if top_k > self.model.config.num_labels:
   a__												= self.model.config.num_labels

  if self.framework == "pt":
   a__												= model_outputs.logits.sigmoid()[0]
   a__							,			a__												= probs.topk(SCREAMING_SNAKE_CASE      )
  else:
   raise ValueError(f"Unsupported framework: {self.framework}"      )

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





