



"""simple docstring"""






from typing import Dict, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends


if is_vision_available():
	import PIL

# soft dependency
if is_pytesseract_available():
	import pytesseract

__snake_case				:  List[str] =       logging.get_logger(__name__)


def      _lowercase							(    __snake_case   ,__snake_case   ,__snake_case      )		->  Optional[int]:
			return [
			    int(1_000 * (box[0] / width)      ),
			    int(1_000 * (box[1] / height)      ),
			    int(1_000 * (box[2] / width)      ),
			    int(1_000 * (box[3] / height)      ),
			]


def      _lowercase							(    __snake_case   ,__snake_case   ,__snake_case = None      )		->  Optional[Any]:
			__lowerCAmelCase :    Tuple				=			tesseract_config if tesseract_config is not None else ""

			# apply OCR
			__lowerCAmelCase :    Dict				=			to_pil_image(__snake_case      )
			__lowerCAmelCase		,      __lowerCAmelCase :    int				=			pil_image.size
			__lowerCAmelCase :    Union[str, Any]				=			pytesseract.image_to_data(__snake_case   ,lang=__snake_case   ,output_type="dict"   ,config=__snake_case      )
			__lowerCAmelCase		,      __lowerCAmelCase		,      __lowerCAmelCase		,      __lowerCAmelCase		,      __lowerCAmelCase :    str				=			data["text"], data["left"], data["top"], data["width"], data["height"]

			# filter empty words and corresponding coordinates
			__lowerCAmelCase :    List[str]				=			[idx for idx, word in enumerate(__snake_case      ) if not word.strip()]
			__lowerCAmelCase :    Any				=			[word for idx, word in enumerate(__snake_case      ) if idx not in irrelevant_indices]
			__lowerCAmelCase :    List[str]				=			[coord for idx, coord in enumerate(__snake_case      ) if idx not in irrelevant_indices]
			__lowerCAmelCase :    str				=			[coord for idx, coord in enumerate(__snake_case      ) if idx not in irrelevant_indices]
			__lowerCAmelCase :    str				=			[coord for idx, coord in enumerate(__snake_case      ) if idx not in irrelevant_indices]
			__lowerCAmelCase :    Union[str, Any]				=			[coord for idx, coord in enumerate(__snake_case      ) if idx not in irrelevant_indices]

			# turn coordinates into (left, top, left+width, top+height) format
			__lowerCAmelCase :    Tuple				=			[]
			for x, y, w, h in zip(__snake_case   ,__snake_case   ,__snake_case   ,__snake_case      ):
						__lowerCAmelCase :    Optional[int]				=			[x, y, x + w, y + h]
						actual_boxes.append(__snake_case      )

			# finally, normalize the bounding boxes
			__lowerCAmelCase :    Any				=			[]
			for box in actual_boxes:
						normalized_boxes.append(normalize_box(__snake_case   ,__snake_case   ,__snake_case      )      )

			assert len(__snake_case      ) == len(__snake_case      ), "Not as many words as there are bounding boxes"

			return words, normalized_boxes





class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      ['pixel_values']

	def __init__(	self:						str			,     _SCREAMING_SNAKE_CASE:						bool = True			,     _SCREAMING_SNAKE_CASE:						Dict[str, int] = None			,     _SCREAMING_SNAKE_CASE:						PILImageResampling = PILImageResampling.BILINEAR			,     _SCREAMING_SNAKE_CASE:						bool = True			,     _SCREAMING_SNAKE_CASE:						Optional[str] = None			,     _SCREAMING_SNAKE_CASE:						Optional[str] = ""			,     **_SCREAMING_SNAKE_CASE:						Tuple			,     )							->			None:

				"""simple docstring"""

				super().__init__(**_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    Any				=			size if size is not None else {"height": 224, "width": 224}
				__lowerCAmelCase :    Optional[Any]				=			get_size_dict(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase :    List[str]				=			do_resize
				__lowerCAmelCase :    Optional[int]				=			size
				__lowerCAmelCase :    Optional[Any]				=			resample
				__lowerCAmelCase :    Tuple				=			apply_ocr
				__lowerCAmelCase :    Tuple				=			ocr_lang
				__lowerCAmelCase :    List[Any]				=			tesseract_config

	def 				_SCREAMING_SNAKE_CASE  (	self:						Dict			,     _SCREAMING_SNAKE_CASE:						np.ndarray			,     _SCREAMING_SNAKE_CASE:						Dict[str, int]			,     _SCREAMING_SNAKE_CASE:						PILImageResampling = PILImageResampling.BILINEAR			,     _SCREAMING_SNAKE_CASE:						Optional[Union[str, ChannelDimension]] = None			,     **_SCREAMING_SNAKE_CASE:						Tuple			,     )							->			np.ndarray:

				"""simple docstring"""

				__lowerCAmelCase :    Tuple				=			get_size_dict(_SCREAMING_SNAKE_CASE)
				if "height" not in size or "width" not in size:
							raise ValueError(F"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}""")
				__lowerCAmelCase :    str				=			(size["height"], size["width"])
				return resize(_SCREAMING_SNAKE_CASE			,     size=_SCREAMING_SNAKE_CASE			,     resample=_SCREAMING_SNAKE_CASE			,     data_format=_SCREAMING_SNAKE_CASE			,     **_SCREAMING_SNAKE_CASE)







	def 				_SCREAMING_SNAKE_CASE  (	self:						Union[str, Any]			,     _SCREAMING_SNAKE_CASE:						ImageInput			,     _SCREAMING_SNAKE_CASE:						bool = None			,     _SCREAMING_SNAKE_CASE:						Dict[str, int] = None			,     _SCREAMING_SNAKE_CASE:						PILImageResampling = None			,     _SCREAMING_SNAKE_CASE:						bool = None			,     _SCREAMING_SNAKE_CASE:						Optional[str] = None			,     _SCREAMING_SNAKE_CASE:						Optional[str] = None			,     _SCREAMING_SNAKE_CASE:						Optional[Union[str, TensorType]] = None			,     _SCREAMING_SNAKE_CASE:						ChannelDimension = ChannelDimension.FIRST			,     **_SCREAMING_SNAKE_CASE:						Union[str, Any]			,     )							->			PIL.Image.Image:

				"""simple docstring"""

				__lowerCAmelCase :    str				=			do_resize if do_resize is not None else self.do_resize
				__lowerCAmelCase :    str				=			size if size is not None else self.size
				__lowerCAmelCase :    Any				=			get_size_dict(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    List[Any]				=			resample if resample is not None else self.resample
				__lowerCAmelCase :    Tuple				=			apply_ocr if apply_ocr is not None else self.apply_ocr
				__lowerCAmelCase :    List[Any]				=			ocr_lang if ocr_lang is not None else self.ocr_lang
				__lowerCAmelCase :    List[str]				=			tesseract_config if tesseract_config is not None else self.tesseract_config

				__lowerCAmelCase :    int				=			make_list_of_images(_SCREAMING_SNAKE_CASE)

				if not valid_images(_SCREAMING_SNAKE_CASE):
							raise ValueError(
							    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
							    "torch.Tensor, tf.Tensor or jax.ndarray.")

				if do_resize and size is None:
							raise ValueError("Size must be specified if do_resize is True.")

				# All transformations expect numpy arrays.
				__lowerCAmelCase :    Any				=			[to_numpy_array(_SCREAMING_SNAKE_CASE) for image in images]

				if apply_ocr:
							requires_backends(self			,     "pytesseract")
							__lowerCAmelCase :    Optional[Any]				=			[]
							__lowerCAmelCase :    Any				=			[]
							for image in images:
										__lowerCAmelCase		,      __lowerCAmelCase :    List[str]				=			apply_tesseract(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
										words_batch.append(_SCREAMING_SNAKE_CASE)
										boxes_batch.append(_SCREAMING_SNAKE_CASE)

				if do_resize:
							__lowerCAmelCase :    List[Any]				=			[self.resize(image=_SCREAMING_SNAKE_CASE			,     size=_SCREAMING_SNAKE_CASE			,     resample=_SCREAMING_SNAKE_CASE) for image in images]

				# flip color channels from RGB to BGR (as Detectron2 requires this)
				__lowerCAmelCase :    str				=			[flip_channel_order(_SCREAMING_SNAKE_CASE) for image in images]
				__lowerCAmelCase :    Dict				=			[to_channel_dimension_format(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE) for image in images]

				__lowerCAmelCase :    Tuple				=			BatchFeature(data={"pixel_values": images}			,     tensor_type=_SCREAMING_SNAKE_CASE)

				if apply_ocr:
							__lowerCAmelCase :    Union[str, Any]				=			words_batch
							__lowerCAmelCase :    Optional[int]				=			boxes_batch
				return data




"""simple docstring"""






import warnings

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


__snake_case				:  Tuple =       logging.get_logger(__name__)

__snake_case				:  Tuple =       {
    'xlnet-base-cased': 'https://huggingface.co/xlnet-base-cased/resolve/main/config.json',
    'xlnet-large-cased': 'https://huggingface.co/xlnet-large-cased/resolve/main/config.json',
}



class      A__  (    __SCREAMING_SNAKE_CASE				):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      'xlnet'
	SCREAMING_SNAKE_CASE        =      ['mems']
	SCREAMING_SNAKE_CASE        =      {
	    'n_token': 'vocab_size',  # Backward compatibility
	    'hidden_size': 'd_model',
	    'num_attention_heads': 'n_head',
	    'num_hidden_layers': 'n_layer',
	}

	def __init__(	self:						Tuple			,     _SCREAMING_SNAKE_CASE:						List[str]=3_2000			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]=1024			,     _SCREAMING_SNAKE_CASE:						Dict=24			,     _SCREAMING_SNAKE_CASE:						Optional[int]=16			,     _SCREAMING_SNAKE_CASE:						List[str]=4096			,     _SCREAMING_SNAKE_CASE:						Tuple="gelu"			,     _SCREAMING_SNAKE_CASE:						int=True			,     _SCREAMING_SNAKE_CASE:						List[Any]="bi"			,     _SCREAMING_SNAKE_CASE:						Any=0.02			,     _SCREAMING_SNAKE_CASE:						List[str]=1e-12			,     _SCREAMING_SNAKE_CASE:						Any=0.1			,     _SCREAMING_SNAKE_CASE:						Any=512			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]=None			,     _SCREAMING_SNAKE_CASE:						Any=True			,     _SCREAMING_SNAKE_CASE:						Any=False			,     _SCREAMING_SNAKE_CASE:						Tuple=False			,     _SCREAMING_SNAKE_CASE:						List[Any]=-1			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]=False			,     _SCREAMING_SNAKE_CASE:						Optional[int]="last"			,     _SCREAMING_SNAKE_CASE:						Tuple=True			,     _SCREAMING_SNAKE_CASE:						Dict="tanh"			,     _SCREAMING_SNAKE_CASE:						int=0.1			,     _SCREAMING_SNAKE_CASE:						Tuple=5			,     _SCREAMING_SNAKE_CASE:						List[str]=5			,     _SCREAMING_SNAKE_CASE:						Optional[Any]=5			,     _SCREAMING_SNAKE_CASE:						Optional[int]=1			,     _SCREAMING_SNAKE_CASE:						Tuple=2			,     **_SCREAMING_SNAKE_CASE:						str			,     )							->			Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    Dict				=			vocab_size
				__lowerCAmelCase :    Optional[int]				=			d_model
				__lowerCAmelCase :    Union[str, Any]				=			n_layer
				__lowerCAmelCase :    List[Any]				=			n_head
				if d_model % n_head != 0:
							raise ValueError(F"""'d_model % n_head' ({d_model % n_head}) should be equal to 0""")
				if "d_head" in kwargs:
							if kwargs["d_head"] != d_model // n_head:
										raise ValueError(
										    F"""`d_head` ({kwargs['d_head']}) should be equal to `d_model // n_head` ({d_model // n_head})""")
				__lowerCAmelCase :    Optional[Any]				=			d_model // n_head
				__lowerCAmelCase :    Tuple				=			ff_activation
				__lowerCAmelCase :    Union[str, Any]				=			d_inner
				__lowerCAmelCase :    Any				=			untie_r
				__lowerCAmelCase :    int				=			attn_type

				__lowerCAmelCase :    Any				=			initializer_range
				__lowerCAmelCase :    List[Any]				=			layer_norm_eps

				__lowerCAmelCase :    int				=			dropout
				__lowerCAmelCase :    List[Any]				=			mem_len
				__lowerCAmelCase :    str				=			reuse_len
				__lowerCAmelCase :    Any				=			bi_data
				__lowerCAmelCase :    Optional[int]				=			clamp_len
				__lowerCAmelCase :    Union[str, Any]				=			same_length

				__lowerCAmelCase :    List[str]				=			summary_type
				__lowerCAmelCase :    Optional[int]				=			summary_use_proj
				__lowerCAmelCase :    Tuple				=			summary_activation
				__lowerCAmelCase :    Dict				=			summary_last_dropout
				__lowerCAmelCase :    str				=			start_n_top
				__lowerCAmelCase :    List[str]				=			end_n_top

				__lowerCAmelCase :    str				=			bos_token_id
				__lowerCAmelCase :    Optional[Any]				=			pad_token_id
				__lowerCAmelCase :    Optional[Any]				=			eos_token_id

				if "use_cache" in kwargs:
							warnings.warn(
							    "The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`"
							    " instead."			,     _SCREAMING_SNAKE_CASE			,     )
							__lowerCAmelCase :    Optional[int]				=			kwargs["use_cache"]

				__lowerCAmelCase :    Optional[int]				=			use_mems_eval
				__lowerCAmelCase :    Any				=			use_mems_train
				super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE			,     bos_token_id=_SCREAMING_SNAKE_CASE			,     eos_token_id=_SCREAMING_SNAKE_CASE			,     **_SCREAMING_SNAKE_CASE)

	@property
	def 				_SCREAMING_SNAKE_CASE  (	self:						Any)							->			int:

				"""simple docstring"""

				logger.info(F"""The model {self.model_type} is one of the few models that has no sequence length limit.""")
				return -1







	@max_position_embeddings.setter
	def 				_SCREAMING_SNAKE_CASE  (	self:						List[str]			,     _SCREAMING_SNAKE_CASE:						Optional[int])							->			Optional[int]:

				"""simple docstring"""

				raise NotImplementedError(
				    F"""The model {self.model_type} is one of the few models that has no sequence length limit.""")