






"""simple docstring"""






from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


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




class 							lowerCAmelCase__				(			UpperCAmelCase__ ):






      '''simple docstring'''

      __UpperCamelCase	 =  ["pixel_values"]
      def __init__(						self     :  Tuple						,	lowercase_     :  bool = True						,	lowercase_     :  Optional[Dict[str, int]] = None						,	lowercase_     :  PILImageResampling = PILImageResampling.BILINEAR						,	lowercase_     :  bool = True						,	lowercase_     :  Dict[str, int] = None						,	lowercase_     :  bool = True						,	lowercase_     :  Union[int, float] = 1 / 255						,	lowercase_     :  bool = True						,	lowercase_     :  Optional[Union[float, List[float]]] = None						,	lowercase_     :  Optional[Union[float, List[float]]] = None						,	**lowercase_     :  str						,	):




          '''simple docstring'''


          super().__init__(**lowercase_)
          SCREAMING_SNAKE_CASE_      :  Dict    						=		size if size is not None else {'''shortest_edge''': 256}
          SCREAMING_SNAKE_CASE_      :  str    						=		get_size_dict(lowercase_						,	default_to_square=lowercase_)
          SCREAMING_SNAKE_CASE_      :  Dict    						=		crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
          SCREAMING_SNAKE_CASE_      :  int    						=		get_size_dict(lowercase_)
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		do_resize
          SCREAMING_SNAKE_CASE_      :  int    						=		size
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		resample
          SCREAMING_SNAKE_CASE_      :  int    						=		do_center_crop
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		crop_size
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		do_rescale
          SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		rescale_factor
          SCREAMING_SNAKE_CASE_      :  str    						=		do_normalize
          SCREAMING_SNAKE_CASE_      :  int    						=		image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		image_std if image_std is not None else IMAGENET_STANDARD_STD
      def _SCREAMING_SNAKE_CASE (						self     :  str						,	lowercase_     :  np.ndarray						,	lowercase_     :  Dict[str, int]						,	lowercase_     :  PILImageResampling = PILImageResampling.BICUBIC						,	lowercase_     :  Optional[Union[str, ChannelDimension]] = None						,	**lowercase_     :  Optional[int]						,	):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		get_size_dict(lowercase_						,	default_to_square=lowercase_)
          if "shortest_edge" not in size:
              raise ValueError(F'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}')
          SCREAMING_SNAKE_CASE_      :  int    						=		get_resize_output_image_size(lowercase_						,	size=size['''shortest_edge''']						,	default_to_square=lowercase_)
          return resize(lowercase_						,	size=lowercase_						,	resample=lowercase_						,	data_format=lowercase_						,	**lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[int]						,	lowercase_     :  np.ndarray						,	lowercase_     :  Dict[str, int]						,	lowercase_     :  Optional[Union[str, ChannelDimension]] = None						,	**lowercase_     :  int						,	):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[str]    						=		get_size_dict(lowercase_)
          return center_crop(lowercase_						,	size=(size['''height'''], size['''width'''])						,	data_format=lowercase_						,	**lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  Union[str, Any]						,	lowercase_     :  np.ndarray						,	lowercase_     :  float						,	lowercase_     :  Optional[Union[str, ChannelDimension]] = None						,	**lowercase_     :  List[str]):




          '''simple docstring'''


          return rescale(lowercase_						,	scale=lowercase_						,	data_format=lowercase_						,	**lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  int						,	lowercase_     :  np.ndarray						,	lowercase_     :  Union[float, List[float]]						,	lowercase_     :  Union[float, List[float]]						,	lowercase_     :  Optional[Union[str, ChannelDimension]] = None						,	**lowercase_     :  str						,	):




          '''simple docstring'''


          return normalize(lowercase_						,	mean=lowercase_						,	std=lowercase_						,	data_format=lowercase_						,	**lowercase_)



      def _SCREAMING_SNAKE_CASE (						self     :  int						,	lowercase_     :  ImageInput						,	lowercase_     :  Optional[bool] = None						,	lowercase_     :  Dict[str, int] = None						,	lowercase_     :  PILImageResampling = None						,	lowercase_     :  bool = None						,	lowercase_     :  Dict[str, int] = None						,	lowercase_     :  Optional[bool] = None						,	lowercase_     :  Optional[float] = None						,	lowercase_     :  Optional[bool] = None						,	lowercase_     :  Optional[Union[float, List[float]]] = None						,	lowercase_     :  Optional[Union[float, List[float]]] = None						,	lowercase_     :  Optional[Union[str, TensorType]] = None						,	lowercase_     :  Union[str, ChannelDimension] = ChannelDimension.FIRST						,	**lowercase_     :  Dict						,	):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Tuple    						=		do_resize if do_resize is not None else self.do_resize
          SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		size if size is not None else self.size
          SCREAMING_SNAKE_CASE_      :  int    						=		get_size_dict(lowercase_						,	default_to_square=lowercase_)
          SCREAMING_SNAKE_CASE_      :  Dict    						=		resample if resample is not None else self.resample
          SCREAMING_SNAKE_CASE_      :  Dict    						=		do_center_crop if do_center_crop is not None else self.do_center_crop
          SCREAMING_SNAKE_CASE_      :  List[str]    						=		crop_size if crop_size is not None else self.crop_size
          SCREAMING_SNAKE_CASE_      :  Any    						=		get_size_dict(lowercase_)
          SCREAMING_SNAKE_CASE_      :  Dict    						=		do_rescale if do_rescale is not None else self.do_rescale
          SCREAMING_SNAKE_CASE_      :  Dict    						=		rescale_factor if rescale_factor is not None else self.rescale_factor
          SCREAMING_SNAKE_CASE_      :  str    						=		do_normalize if do_normalize is not None else self.do_normalize
          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		image_mean if image_mean is not None else self.image_mean
          SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		image_std if image_std is not None else self.image_std

          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		make_list_of_images(lowercase_)

          if not valid_images(lowercase_):
              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.''')

          if do_center_crop and crop_size is None:
              raise ValueError('''Crop size must be specified if do_center_crop is True.''')

          if do_rescale and rescale_factor is None:
              raise ValueError('''Rescale factor must be specified if do_rescale is True.''')

          if do_normalize and (image_mean is None or image_std is None):
              raise ValueError('''Image mean and std must be specified if do_normalize is True.''')

          # All transformations expect numpy arrays.
          SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		[to_numpy_array(lowercase_) for image in images]

          if do_resize:
              SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		[self.resize(image=lowercase_						,	size=lowercase_						,	resample=lowercase_) for image in images]

          if do_center_crop:
              SCREAMING_SNAKE_CASE_      :  Tuple    						=		[self.center_crop(image=lowercase_						,	size=lowercase_) for image in images]

          if do_rescale:
              SCREAMING_SNAKE_CASE_      :  int    						=		[self.rescale(image=lowercase_						,	scale=lowercase_) for image in images]

          if do_normalize:
              SCREAMING_SNAKE_CASE_      :  Optional[int]    						=		[self.normalize(image=lowercase_						,	mean=lowercase_						,	std=lowercase_) for image in images]

          SCREAMING_SNAKE_CASE_      :  Any    						=		[to_channel_dimension_format(lowercase_						,	lowercase_) for image in images]

          SCREAMING_SNAKE_CASE_      :  Dict    						=		{'''pixel_values''': images}
          return BatchFeature(data=lowercase_						,	tensor_type=lowercase_)










import html

from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from ...utils import is_bsa_available, logging, requires_backends


if is_bsa_available():
	import bsa
	from bsa import BeautifulSoup


UpperCAmelCase_ :						Any      =      logging.get_logger(__name__)

class     _SCREAMING_SNAKE_CASE       (			_a      ):





	def __init__(		self				:					Optional[int] ,  **__lowerCamelCase				:					Optional[int]	):
		requires_backends(self ,  ["""bs4"""]	)
		super().__init__(**__lowerCamelCase	)





	def 						_A (		self				:					List[str] ,  __lowerCamelCase				:					Any	):
		UpperCamelCase     :Optional[int]          =	[]
		UpperCamelCase     :List[str]          =	[]
		UpperCamelCase     :Union[str, Any]          =	element if element.name else element.parent
		for parent in child.parents:  # type: bs4.element.Tag
			UpperCamelCase     :Optional[Any]          =	parent.find_all(child.name ,  recursive=__lowerCamelCase	)
			xpath_tags.append(child.name	)
			xpath_subscripts.append(
			    0 if 1 == len(__lowerCamelCase	) else next(i for i, s in enumerate(__lowerCamelCase ,  1	) if s is child	)	)
			UpperCamelCase     :Any          =	parent
		xpath_tags.reverse()
		xpath_subscripts.reverse()
		return xpath_tags, xpath_subscripts





	def 						_A (		self				:					Any ,  __lowerCamelCase				:					Tuple	):
		UpperCamelCase     :Any          =	BeautifulSoup(__lowerCamelCase ,  """html.parser"""	)

		UpperCamelCase     :Union[str, Any]          =	[]
		UpperCamelCase     :Tuple          =	[]
		UpperCamelCase     :Tuple          =	[]

		for element in html_code.descendants:
			if type(__lowerCamelCase	) == bsa.element.NavigableString:
				if type(element.parent	) != bsa.element.Tag:
					continue

				UpperCamelCase     :Any          =	html.unescape(__lowerCamelCase	).strip()
				if not text_in_this_tag:
					continue

				all_doc_strings.append(__lowerCamelCase	)

				UpperCamelCase     ,      UpperCamelCase     :Optional[Any]          =	self.xpath_soup(__lowerCamelCase	)
				stringaxtag_seq.append(__lowerCamelCase	)
				stringaxsubs_seq.append(__lowerCamelCase	)

		if len(__lowerCamelCase	) != len(__lowerCamelCase	):
			raise ValueError("""Number of doc strings and xtags does not correspond"""	)
		if len(__lowerCamelCase	) != len(__lowerCamelCase	):
			raise ValueError("""Number of doc strings and xsubs does not correspond"""	)

		return all_doc_strings, stringaxtag_seq, stringaxsubs_seq





	def 						_A (		self				:					int ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					List[str]	):
		UpperCamelCase     :Tuple          =	""""""
		for tagname, subs in zip(__lowerCamelCase ,  __lowerCamelCase	):
			xpath += F"""/{tagname}"""
			if subs != 0:
				xpath += F"""[{subs}]"""
		return xpath





	def __call__(		self				:					Any ,  __lowerCamelCase				:					Dict	):
		UpperCamelCase     :Any          =	False

		# Check that strings has a valid type
		if isinstance(__lowerCamelCase ,  __lowerCamelCase	):
			UpperCamelCase     :List[Any]          =	True
		elif isinstance(__lowerCamelCase ,  (list, tuple)	):
			if len(__lowerCamelCase	) == 0 or isinstance(html_strings[0] ,  __lowerCamelCase	):
				UpperCamelCase     :Any          =	True

		if not valid_strings:
			raise ValueError(
			    """HTML strings must of type `str`, `List[str]` (batch of examples), """
			    F"""but is of type {type(__lowerCamelCase	)}."""	)

		UpperCamelCase     :str          =	bool(isinstance(__lowerCamelCase ,  (list, tuple)	) and (isinstance(html_strings[0] ,  __lowerCamelCase	))	)

		if not is_batched:
			UpperCamelCase     :Any          =	[html_strings]

		# Get nodes + xpaths
		UpperCamelCase     :Union[str, Any]          =	[]
		UpperCamelCase     :str          =	[]
		for html_string in html_strings:
			UpperCamelCase     ,      UpperCamelCase     ,      UpperCamelCase     :int          =	self.get_three_from_single(__lowerCamelCase	)
			nodes.append(__lowerCamelCase	)
			UpperCamelCase     :int          =	[]
			for node, tag_list, sub_list in zip(__lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase	):
				UpperCamelCase     :str          =	self.construct_xpath(__lowerCamelCase ,  __lowerCamelCase	)
				xpath_strings.append(__lowerCamelCase	)
			xpaths.append(__lowerCamelCase	)

		# return as Dict
		UpperCamelCase     :Optional[int]          =	{"""nodes""": nodes, """xpaths""": xpaths}
		UpperCamelCase     :Any          =	BatchFeature(data=__lowerCamelCase ,  tensor_type=__lowerCamelCase	)

		return encoded_inputs
