



'''simple docstring'''

from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
		from PIL import Image

		from ..image_utils import load_image

if is_torch_available():
		import torch

		from transformers.modeling_outputs import BaseModelOutput

		from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

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



@add_end_docstrings(SCREAMING_SNAKE_CASE__     )
class 		lowerCamelCase__      (						SCREAMING_SNAKE_CASE__     ):




					'''simple docstring'''



					def __init__(  self				:	Dict     ,  **UpperCamelCase_				:	Dict					)  ->       Optional[int]:



						'''simple docstring'''





						super().__init__(**snake_case__					)

						if self.framework == "tf":
							raise ValueError(F'''The {self.__class__} is only available in PyTorch.'''					)

						requires_backends(self     ,  'vision'					)
						self.check_model_type(snake_case__					)




					def __call__(  self				:	str     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	str = None     ,  **UpperCamelCase_				:	List[Any]     ,  )  ->       Optional[int]:



						'''simple docstring'''





						if "text_queries" in kwargs:
							_lowercase					:       List[str]       =		kwargs.pop('text_queries'					)

						if isinstance(snake_case__     ,  (str, Image.Image)					):
							_lowercase					:       Tuple       =		{"image": image, "candidate_labels": candidate_labels}
						else:
							_lowercase					:       Dict       =		image
						_lowercase					:       Any       =		super().__call__(snake_case__     ,  **snake_case__					)
						return results




					def 						__UpperCAmelCase     (  self				:	Dict     ,  **UpperCamelCase_				:	List[Any]					)  ->       List[str]:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		{}
						if "threshold" in kwargs:
							_lowercase					:       int       =		kwargs["threshold"]
						if "top_k" in kwargs:
							_lowercase					:       List[str]       =		kwargs["top_k"]
						return {}, {}, postprocess_params




					def 						__UpperCAmelCase     (  self				:	Optional[Any]     ,  UpperCamelCase_				:	Optional[Any]					)  ->       Any:



						'''simple docstring'''





						_lowercase					:       Tuple       =		load_image(inputs['image']					)
						_lowercase					:       List[str]       =		inputs["candidate_labels"]
						if isinstance(snake_case__     ,  snake_case__					):
							_lowercase					:       Union[str, Any]       =		candidate_labels.split(','					)

						_lowercase					:       List[Any]       =		torch.tensor([[image.height, image.width]]     ,  dtype=torch.intaa					)
						for i, candidate_label in enumerate(snake_case__					):
							_lowercase					:       List[str]       =		self.tokenizer(snake_case__     ,  return_tensors=self.framework					)
							_lowercase					:       Union[str, Any]       =		self.image_processor(snake_case__     ,  return_tensors=self.framework					)
							yield {
							    "is_last": i == len(snake_case__					) - 1,
							    "target_size": target_size,
							    "candidate_label": candidate_label,
							    **text_inputs,
							    **image_features,
							}




					def 						__UpperCAmelCase     (  self				:	Optional[Any]     ,  UpperCamelCase_				:	Union[str, Any]					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       List[str]       =		model_inputs.pop('target_size'					)
						_lowercase					:       Tuple       =		model_inputs.pop('candidate_label'					)
						_lowercase					:       int       =		model_inputs.pop('is_last'					)

						_lowercase					:       Union[str, Any]       =		self.model(**snake_case__					)

						_lowercase					:       Union[str, Any]       =		{"target_size": target_size, "candidate_label": candidate_label, "is_last": is_last, **outputs}
						return model_outputs




					def 						__UpperCAmelCase     (  self				:	int     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	int=0.1     ,  UpperCamelCase_				:	Optional[Any]=None					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		[]
						for model_output in model_outputs:
							_lowercase					:       Optional[int]       =		model_output["candidate_label"]
							_lowercase					:       List[str]       =		BaseModelOutput(snake_case__					)
							_lowercase					:       str       =		self.image_processor.post_process_object_detection(
							    outputs=snake_case__     ,  threshold=snake_case__     ,  target_sizes=model_output['target_size']					)[0]

							for index in outputs["scores"].nonzero():
								_lowercase					:       Dict       =		outputs["scores"][index].item()
								_lowercase					:       List[Any]       =		self._get_bounding_box(outputs['boxes'][index][0]					)

								_lowercase					:       List[Any]       =		{"score": score, "label": label, "box": box}
								results.append(snake_case__					)

						_lowercase					:       Dict       =		sorted(snake_case__     ,  key=lambda UpperCamelCase_					: x["score"]     ,  reverse=snake_case__					)
						if top_k:
							_lowercase					:       List[str]       =		results[:top_k]

						return results




					def 						__UpperCAmelCase     (  self				:	Union[str, Any]     ,  UpperCamelCase_				:	int					)  ->       Optional[int]:



						'''simple docstring'''





						if self.framework != "pt":
							raise ValueError('The ZeroShotObjectDetectionPipeline is only available in PyTorch.'					)
						_lowercase					:       List[str]       =		box.int().tolist()
						_lowercase					:       Any       =		{
						    "xmin": xmin,
						    "ymin": ymin,
						    "xmax": xmax,
						    "ymax": ymax,
						}
						return bbox








'''simple docstring'''

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


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

_A				:    Optional[int]     						={
    '''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 		lowerCamelCase__      (						A     ):




					'''simple docstring'''



					A_						    =						"""markuplm"""




					def __init__(  self				:	int     ,  UpperCamelCase_				:	Optional[Any]=3_0522     ,  UpperCamelCase_				:	Optional[Any]=768     ,  UpperCamelCase_				:	Tuple=12     ,  UpperCamelCase_				:	Union[str, Any]=12     ,  UpperCamelCase_				:	Tuple=3072     ,  UpperCamelCase_				:	Union[str, Any]="gelu"     ,  UpperCamelCase_				:	List[Any]=0.1     ,  UpperCamelCase_				:	List[Any]=0.1     ,  UpperCamelCase_				:	Dict=512     ,  UpperCamelCase_				:	Optional[int]=2     ,  UpperCamelCase_				:	Any=0.02     ,  UpperCamelCase_				:	Optional[Any]=1E-12     ,  UpperCamelCase_				:	List[str]=0     ,  UpperCamelCase_				:	Optional[int]=0     ,  UpperCamelCase_				:	Tuple=2     ,  UpperCamelCase_				:	str=256     ,  UpperCamelCase_				:	Optional[Any]=1024     ,  UpperCamelCase_				:	Union[str, Any]=216     ,  UpperCamelCase_				:	int=1001     ,  UpperCamelCase_				:	int=32     ,  UpperCamelCase_				:	int=50     ,  UpperCamelCase_				:	str="absolute"     ,  UpperCamelCase_				:	Union[str, Any]=True     ,  UpperCamelCase_				:	Optional[int]=None     ,  **UpperCamelCase_				:	Any     ,  )  ->       Optional[int]:



						'''simple docstring'''





						super().__init__(
						    pad_token_id=UpperCamelCase_     ,  bos_token_id=UpperCamelCase_     ,  eos_token_id=UpperCamelCase_     ,  **UpperCamelCase_     ,  )
						_lowercase					:       List[Any]       =		vocab_size
						_lowercase					:       Union[str, Any]       =		hidden_size
						_lowercase					:       Dict       =		num_hidden_layers
						_lowercase					:       Optional[Any]       =		num_attention_heads
						_lowercase					:       Dict       =		hidden_act
						_lowercase					:       Optional[Any]       =		intermediate_size
						_lowercase					:       Optional[int]       =		hidden_dropout_prob
						_lowercase					:       Union[str, Any]       =		attention_probs_dropout_prob
						_lowercase					:       Any       =		max_position_embeddings
						_lowercase					:       List[Any]       =		type_vocab_size
						_lowercase					:       Union[str, Any]       =		initializer_range
						_lowercase					:       Optional[int]       =		layer_norm_eps
						_lowercase					:       Optional[Any]       =		position_embedding_type
						_lowercase					:       str       =		use_cache
						_lowercase					:       str       =		classifier_dropout
						# additional properties
						_lowercase					:       int       =		max_depth
						_lowercase					:       Dict       =		max_xpath_tag_unit_embeddings
						_lowercase					:       str       =		max_xpath_subs_unit_embeddings
						_lowercase					:       List[str]       =		tag_pad_id
						_lowercase					:       Optional[int]       =		subs_pad_id
						_lowercase					:       Any       =		xpath_unit_hidden_size
