

'''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        lowercase_	(		__SCREAMING_SNAKE_CASE ):

  '''simple docstring'''





  __lowerCAmelCase					: str      =   "MarkupLMFeatureExtractor"
  __lowerCAmelCase					: Optional[Any]      =   ("MarkupLMTokenizer", "MarkupLMTokenizerFast")
  __lowerCAmelCase					: str      =   True
  def __call__(     self   , a_=None   , a_=None   , a_=None   , a_=None   , a_=None   , a_ = True   , a_ = False   , a_ = None   , a_ = None   , a_ = 0   , a_ = None   , a_ = None   , a_ = None   , a_ = False   , a_ = False   , a_ = False   , a_ = False   , a_ = True   , a_ = None   , **a_   , )							->				BatchEncoding:


    """simple docstring"""







    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`'     )

      UpperCAmelCase													=	self.feature_extractor(UpperCamelCase__     )
      UpperCAmelCase													=	features['''nodes''']
      UpperCAmelCase													=	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(UpperCamelCase__   , UpperCamelCase__     ):
        UpperCAmelCase													=	[questions]  # add batch dimension (as the feature extractor always adds a batch dimension)

    UpperCAmelCase													=	self.tokenizer(
        text=questions if questions is not None else nodes   , text_pair=nodes if questions is not None else None   , xpaths=UpperCamelCase__   , node_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__   , )

    return encoded_inputs
  def 						snake_case_			(     self   , *a_   , **a_     )							->				int:


    """simple docstring"""
    return self.tokenizer.batch_decode(*UpperCamelCase__   , **UpperCamelCase__     )
  def 						snake_case_			(     self   , *a_   , **a_     )							->				Optional[int]:


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





  @property
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    UpperCAmelCase													=	self.tokenizer.model_input_names
    return tokenizer_input_names








'''simple docstring'''




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import MobileNetVaImageProcessor



class        lowercase_	(		unittest.TestCase ):

  '''simple docstring'''





  def __init__(     self   , a_   , a_=7   , a_=3   , a_=1_8   , a_=3_0   , a_=4_0_0   , a_=True   , a_=None   , a_=True   , a_=None   , )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase													=	size if size is not None else {'shortest_edge': 2_0}
    UpperCAmelCase													=	crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8}
    UpperCAmelCase													=	parent
    UpperCAmelCase													=	batch_size
    UpperCAmelCase													=	num_channels
    UpperCAmelCase													=	image_size
    UpperCAmelCase													=	min_resolution
    UpperCAmelCase													=	max_resolution
    UpperCAmelCase													=	do_resize
    UpperCAmelCase													=	size
    UpperCAmelCase													=	do_center_crop
    UpperCAmelCase													=	crop_size





  def 						snake_case_			(     self     )							->				Optional[int]:


    """simple docstring"""
    return {
        "do_resize": self.do_resize,
        "size": self.size,
        "do_center_crop": self.do_center_crop,
        "crop_size": self.crop_size,
    }




@require_torch
@require_vision
class        lowercase_	(		a ,       unittest.TestCase ):

  '''simple docstring'''





  __lowerCAmelCase					: Union[str, Any]      =   MobileNetVaImageProcessor if is_vision_available() else None
  def 						snake_case_			(     self     )							->				Dict:


    """simple docstring"""
    UpperCAmelCase													=	MobileNetVaImageProcessingTester(self     )
  @property
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    return self.image_processor_tester.prepare_image_processor_dict()
  def 						snake_case_			(     self     )							->				Optional[int]:


    """simple docstring"""
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    self.assertTrue(hasattr(a_   , 'do_resize'     )     )
    self.assertTrue(hasattr(a_   , 'size'     )     )
    self.assertTrue(hasattr(a_   , 'do_center_crop'     )     )
    self.assertTrue(hasattr(a_   , 'center_crop'     )     )
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    UpperCAmelCase													=	self.image_processing_class.from_dict(self.image_processor_dict     )
    self.assertEqual(image_processor.size   , {'shortest_edge': 2_0}     )
    self.assertEqual(image_processor.crop_size   , {'height': 1_8, 'width': 1_8}     )

    UpperCAmelCase													=	self.image_processing_class.from_dict(self.image_processor_dict   , size=4_2   , crop_size=8_4     )
    self.assertEqual(image_processor.size   , {'shortest_edge': 4_2}     )
    self.assertEqual(image_processor.crop_size   , {'height': 8_4, 'width': 8_4}     )
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    pass
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    # Initialize image_processing
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    # create random PIL images
    UpperCAmelCase													=	prepare_image_inputs(self.image_processor_tester   , equal_resolution=a_     )
    for image in image_inputs:
      self.assertIsInstance(a_   , Image.Image     )

    # Test not batched input
    UpperCAmelCase													=	image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )

    # Test batched
    UpperCAmelCase													=	image_processing(a_   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    # Initialize image_processing
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    # create random numpy tensors
    UpperCAmelCase													=	prepare_image_inputs(self.image_processor_tester   , equal_resolution=a_   , numpify=a_     )
    for image in image_inputs:
      self.assertIsInstance(a_   , np.ndarray     )

    # Test not batched input
    UpperCAmelCase													=	image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )

    # Test batched
    UpperCAmelCase													=	image_processing(a_   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )





  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    # Initialize image_processing
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    # create random PyTorch tensors
    UpperCAmelCase													=	prepare_image_inputs(self.image_processor_tester   , equal_resolution=a_   , torchify=a_     )
    for image in image_inputs:
      self.assertIsInstance(a_   , torch.Tensor     )

    # Test not batched input
    UpperCAmelCase													=	image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )

    # Test batched
    UpperCAmelCase													=	image_processing(a_   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )


