





"""simple docstring"""
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 lowerCamelCase__  (    __UpperCamelCase				):
   __UpperCAmelCase        =					["""image_processor""", """tokenizer"""]
   __UpperCAmelCase        =					"""LayoutLMv2ImageProcessor"""
   __UpperCAmelCase        =					("""LayoutLMv2Tokenizer""", """LayoutLMv2TokenizerFast""")



   def __init__(       self      ,   snake_case=None      ,   snake_case=None      ,   **snake_case	) ->    List[Any]:







      """simple docstring"""
      lowercase    :			Union[str, Any]  =       None
      if "feature_extractor" in kwargs:
         warnings.warn(
             """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
             """ instead."""      ,   snake_case      ,   )
         lowercase    :			Union[str, Any]  =       kwargs.pop("""feature_extractor"""	)

      lowercase    :			List[Any]  =       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__(snake_case      ,   snake_case	)



   def __call__(       self      ,   snake_case      ,   snake_case = None      ,   snake_case = None      ,   snake_case = None      ,   snake_case = None      ,   snake_case = True      ,   snake_case = False      ,   snake_case = False      ,   snake_case = None      ,   snake_case = 0      ,   snake_case = None      ,   snake_case = None      ,   snake_case = None      ,   snake_case = False      ,   snake_case = False      ,   snake_case = False      ,   snake_case = False      ,   snake_case = True      ,   snake_case = None      ,   **snake_case      ,   ) ->    BatchEncoding:







      """simple docstring"""
      # verify input
      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
      lowercase    :			Any  =       self.image_processor(images=snake_case      ,   return_tensors=snake_case	)

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

      lowercase    :			Optional[Any]  =       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=snake_case      ,   add_special_tokens=snake_case      ,   padding=snake_case      ,   truncation=snake_case      ,   max_length=snake_case      ,   stride=snake_case      ,   pad_to_multiple_of=snake_case      ,   return_token_type_ids=snake_case      ,   return_attention_mask=snake_case      ,   return_overflowing_tokens=snake_case      ,   return_special_tokens_mask=snake_case      ,   return_offsets_mapping=snake_case      ,   return_length=snake_case      ,   verbose=snake_case      ,   return_tensors=snake_case      ,   **snake_case      ,   )

      # add pixel values
      lowercase    :			Tuple  =       features.pop("""pixel_values"""	)
      if return_overflowing_tokens is True:
         lowercase    :			Optional[Any]  =       self.get_overflowing_images(snake_case      ,   encoded_inputs["""overflow_to_sample_mapping"""]	)
      lowercase    :			str  =       images

      return encoded_inputs



   def    _UpperCAmelCase	(       self      ,   snake_case      ,   snake_case	) ->    str:







      """simple docstring"""
      # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
      lowercase    :			Tuple  =       []
      for sample_idx in overflow_to_sample_mapping:
         images_with_overflow.append(images[sample_idx]	)

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

      return images_with_overflow



   def    _UpperCAmelCase	(       self      ,   *snake_case      ,   **snake_case	) ->    Dict:







      """simple docstring"""
      return self.tokenizer.batch_decode(*snake_case      ,   **snake_case	)



   def    _UpperCAmelCase	(       self      ,   *snake_case      ,   **snake_case	) ->    Optional[Any]:







      """simple docstring"""
      return self.tokenizer.decode(*snake_case      ,   **snake_case	)



   @property
   def    _UpperCAmelCase	(       self	) ->    Tuple:







      """simple docstring"""
      return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]



   @property
   def    _UpperCAmelCase	(       self	) ->    str:







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





   @property
   def    _UpperCAmelCase	(       self	) ->    Any:







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









"""simple docstring"""
from typing import Optional, Union

from ...file_utils import TensorType
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, TruncationStrategy
class lowerCamelCase__  (    __UpperCamelCase				):
   __UpperCAmelCase        =					"""MarkupLMFeatureExtractor"""
   __UpperCAmelCase        =					("""MarkupLMTokenizer""", """MarkupLMTokenizerFast""")
   __UpperCAmelCase        =					True



   def __call__(       self      ,   snake_case=None      ,   snake_case=None      ,   snake_case=None      ,   snake_case=None      ,   snake_case=None      ,   snake_case = True      ,   snake_case = False      ,   snake_case = None      ,   snake_case = None      ,   snake_case = 0      ,   snake_case = None      ,   snake_case = None      ,   snake_case = None      ,   snake_case = False      ,   snake_case = False      ,   snake_case = False      ,   snake_case = False      ,   snake_case = True      ,   snake_case = None      ,   **snake_case      ,   ) ->    BatchEncoding:







      """simple docstring"""
      # first, create nodes and xpaths
      if self.parse_html:
         if html_strings is None:
            raise ValueError("""Make sure to pass HTML strings in case `parse_html` is set to `True`"""	)

         if nodes is not None or xpaths is not None or node_labels is not None:
            raise ValueError(
                """Please don't pass nodes, xpaths nor node labels in case `parse_html` is set to `True`"""	)

         lowercase    :			List[Any]  =       self.feature_extractor(snake_case	)
         lowercase    :			Union[str, Any]  =       features["""nodes"""]
         lowercase    :			List[Any]  =       features["""xpaths"""]
      else:
         if html_strings is not None:
            raise ValueError("""You have passed HTML strings but `parse_html` is set to `False`."""	)
         if nodes is None or xpaths is None:
            raise ValueError("""Make sure to pass nodes and xpaths in case `parse_html` is set to `False`"""	)

        # # second, apply the tokenizer
      if questions is not None and self.parse_html:
         if isinstance(snake_case      ,   snake_case	):
            lowercase    :			Dict  =       [questions]  # add batch dimension (as the feature extractor always adds a batch dimension)

      lowercase    :			Dict  =       self.tokenizer(
          text=questions if questions is not None else nodes      ,   text_pair=nodes if questions is not None else None      ,   xpaths=snake_case      ,   node_labels=snake_case      ,   add_special_tokens=snake_case      ,   padding=snake_case      ,   truncation=snake_case      ,   max_length=snake_case      ,   stride=snake_case      ,   pad_to_multiple_of=snake_case      ,   return_token_type_ids=snake_case      ,   return_attention_mask=snake_case      ,   return_overflowing_tokens=snake_case      ,   return_special_tokens_mask=snake_case      ,   return_offsets_mapping=snake_case      ,   return_length=snake_case      ,   verbose=snake_case      ,   return_tensors=snake_case      ,   **snake_case      ,   )

      return encoded_inputs



   def    _UpperCAmelCase	(       self      ,   *snake_case      ,   **snake_case	) ->    Optional[Any]:







      """simple docstring"""
      return self.tokenizer.batch_decode(*snake_case      ,   **snake_case	)



   def    _UpperCAmelCase	(       self      ,   *snake_case      ,   **snake_case	) ->    Tuple:







      """simple docstring"""
      return self.tokenizer.decode(*snake_case      ,   **snake_case	)





   @property
   def    _UpperCAmelCase	(       self	) ->    Dict:







      """simple docstring"""
      lowercase    :			Any  =       self.tokenizer.model_input_names
      return tokenizer_input_names


