





import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''
 lowercase_		=      ["image_processor", "tokenizer"]
 lowercase_		=      "LayoutLMv2ImageProcessor"
 lowercase_		=      ("LayoutXLMTokenizer", "LayoutXLMTokenizerFast")

 def __init__(self    :       Any	,			UpperCAmelCase_    :       List[str]=None	,			UpperCAmelCase_    :       List[Any]=None	,			**UpperCAmelCase_    :       Optional[int])	->str:



   '''simple docstring'''



   if "feature_extractor" in kwargs:
     warnings.warn(
         "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
         " instead."	,			UpperCAmelCase_	,			)
     lowerCamelCase__:				List[str]        =kwargs.pop("feature_extractor")

   lowerCamelCase__:				Optional[int]        =image_processor if image_processor is not None else feature_extractor
   if image_processor is None:
     raise ValueError("You need to specify an `image_processor`.")
   if tokenizer is None:
     raise ValueError("You need to specify a `tokenizer`.")

   super().__init__(UpperCAmelCase_	,			UpperCAmelCase_)

 def __call__(self    :       List[str]	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None	,			UpperCAmelCase_    :       Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None	,			UpperCAmelCase_    :       Union[List[List[int]], List[List[List[int]]]] = None	,			UpperCAmelCase_    :       Optional[Union[List[int], List[List[int]]]] = None	,			UpperCAmelCase_    :       bool = True	,			UpperCAmelCase_    :       Union[bool, str, PaddingStrategy] = False	,			UpperCAmelCase_    :       Union[bool, str, TruncationStrategy] = None	,			UpperCAmelCase_    :       Optional[int] = None	,			UpperCAmelCase_    :       int = 0	,			UpperCAmelCase_    :       Optional[int] = None	,			UpperCAmelCase_    :       Optional[bool] = None	,			UpperCAmelCase_    :       Optional[bool] = None	,			UpperCAmelCase_    :       bool = False	,			UpperCAmelCase_    :       bool = False	,			UpperCAmelCase_    :       bool = False	,			UpperCAmelCase_    :       bool = False	,			UpperCAmelCase_    :       bool = True	,			UpperCAmelCase_    :       Optional[Union[str, TensorType]] = None	,			**UpperCAmelCase_    :       int	,			)	->BatchEncoding:



   '''simple docstring'''



   if self.image_processor.apply_ocr and (boxes is not None):
     raise ValueError(
         "You cannot provide bounding boxes "
         "if you initialized the image processor with apply_ocr set to True.")

   if self.image_processor.apply_ocr and (word_labels is not None):
     raise ValueError(
         "You cannot provide word labels if you initialized the image processor with apply_ocr set to True.")

   if return_overflowing_tokens is True and return_offsets_mapping is False:
     raise ValueError("You cannot return overflowing tokens without returning the offsets mapping.")

   # first, apply the image processor
   lowerCamelCase__:				List[str]        =self.image_processor(images=UpperCAmelCase_	,			return_tensors=UpperCAmelCase_)

   # second, apply the tokenizer
   if text is not None and self.image_processor.apply_ocr and text_pair is None:
     if isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
       lowerCamelCase__:				int        =[text]  # add batch dimension (as the image processor always adds a batch dimension)
     lowerCamelCase__:				Union[str, Any]        =features["words"]

   lowerCamelCase__:				int        =self.tokenizer(
       text=text if text is not None else features["words"]	,			text_pair=text_pair if text_pair is not None else None	,			boxes=boxes if boxes is not None else features["boxes"]	,			word_labels=UpperCAmelCase_	,			add_special_tokens=UpperCAmelCase_	,			padding=UpperCAmelCase_	,			truncation=UpperCAmelCase_	,			max_length=UpperCAmelCase_	,			stride=UpperCAmelCase_	,			pad_to_multiple_of=UpperCAmelCase_	,			return_token_type_ids=UpperCAmelCase_	,			return_attention_mask=UpperCAmelCase_	,			return_overflowing_tokens=UpperCAmelCase_	,			return_special_tokens_mask=UpperCAmelCase_	,			return_offsets_mapping=UpperCAmelCase_	,			return_length=UpperCAmelCase_	,			verbose=UpperCAmelCase_	,			return_tensors=UpperCAmelCase_	,			**UpperCAmelCase_	,			)

   # add pixel values
   lowerCamelCase__:				Dict        =features.pop("pixel_values")
   if return_overflowing_tokens is True:
     lowerCamelCase__:				Optional[int]        =self.get_overflowing_images(UpperCAmelCase_	,			encoded_inputs["overflow_to_sample_mapping"])
   lowerCamelCase__:				Tuple        =images

   return encoded_inputs

 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict	,			UpperCAmelCase_    :       Tuple	,			UpperCAmelCase_    :       Any)	->List[str]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =[]
   for sample_idx in overflow_to_sample_mapping:
     images_with_overflow.append(images[sample_idx])

   if len(UpperCAmelCase_) != len(UpperCAmelCase_):
     raise ValueError(
         "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
         F""" {len(UpperCAmelCase_)} and {len(UpperCAmelCase_)}""")

   return images_with_overflow

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any]	,			*UpperCAmelCase_    :       List[Any]	,			**UpperCAmelCase_    :       Dict)	->Tuple:



   '''simple docstring'''



   return self.tokenizer.batch_decode(*UpperCAmelCase_	,			**UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int]	,			*UpperCAmelCase_    :       List[str]	,			**UpperCAmelCase_    :       int)	->List[str]:



   '''simple docstring'''



   return self.tokenizer.decode(*UpperCAmelCase_	,			**UpperCAmelCase_)

 @property
 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->Any:



   '''simple docstring'''



   return ["input_ids", "bbox", "attention_mask", "image"]

 @property
 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int])	->Optional[Any]:



   '''simple docstring'''



   warnings.warn(
       "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."	,			UpperCAmelCase_	,			)
   return self.image_processor_class







 @property
 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple)	->Optional[int]:



   '''simple docstring'''



   warnings.warn(
       "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."	,			UpperCAmelCase_	,			)
   return self.image_processor








import enum
import os
from hashlib import shaaaa
from typing import Optional

from .. import config
from .logging import get_logger


__A     =    get_logger(__name__)



class        _SCREAMING_SNAKE_CASE						(     enum.Enum   ):







 '''simple docstring'''
 lowercase_		=      "all_checks"
 lowercase_		=      "basic_checks"
 lowercase_		=      "no_checks"



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''





def 					lowerCAmelCase_			(	__a     ,   __a     ,   __a=None		)					->		Optional[int]:







  """simple docstring"""





  if expected_checksums is None:
    logger.info("Unable to verify checksums."		)
    return
  if len(set(__a		) - set(__a		)		) > 0:
    raise ExpectedMoreDownloadedFiles(str(set(__a		) - set(__a		)		)		)
  if len(set(__a		) - set(__a		)		) > 0:
    raise UnexpectedDownloadedFile(str(set(__a		) - set(__a		)		)		)
  lowerCamelCase__:				List[Any]        =[url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]]
  lowerCamelCase__:				Union[str, Any]        =" for " + verification_name if verification_name is not None else ""
  if len(__a		) > 0:
    raise NonMatchingChecksumError(
        F"""Checksums didn't match{for_verification_name}:\n"""
        F"""{bad_urls}\n"""
        "Set `verification_mode='no_checks'` to skip checksums verification and ignore this error"		)
  logger.info("All the checksums matched successfully" + for_verification_name		)



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''





def 					lowerCAmelCase_			(	__a     ,   __a		)					->		Any:







  """simple docstring"""





  if expected_splits is None:
    logger.info("Unable to verify splits sizes."		)
    return
  if len(set(__a		) - set(__a		)		) > 0:
    raise ExpectedMoreSplits(str(set(__a		) - set(__a		)		)		)
  if len(set(__a		) - set(__a		)		) > 0:
    raise UnexpectedSplits(str(set(__a		) - set(__a		)		)		)
  lowerCamelCase__:				Optional[int]        =[
      {"expected": expected_splits[name], "recorded": recorded_splits[name]}
      for name in expected_splits
      if expected_splits[name].num_examples != recorded_splits[name].num_examples
  ]
  if len(__a		) > 0:
    raise NonMatchingSplitsSizesError(str(__a		)		)
  logger.info("All the splits matched successfully."		)





def 					lowerCAmelCase_			(	__a     ,   __a = True		)					->		dict:







  """simple docstring"""





  if record_checksum:
    lowerCamelCase__:				str        =shaaaa()
    with open(__a     ,   "rb"		) as f:
      for chunk in iter(lambda: f.read(1 << 20		)     ,   b""		):
        m.update(__a		)
      lowerCamelCase__:				Dict        =m.hexdigest()
  else:
    lowerCamelCase__:				List[str]        =None
  return {"num_bytes": os.path.getsize(__a		), "checksum": checksum}






def 					lowerCAmelCase_			(	__a		)					->		int:







  """simple docstring"""





  if dataset_size and config.IN_MEMORY_MAX_SIZE:
    return dataset_size < config.IN_MEMORY_MAX_SIZE
  else:
    return False

