

"""simple docstring"""




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


A_				:      int           =			logging.get_logger(__name__)





class  lowerCamelCase    (A__      ):

			def __init__(       self					:    List[Any]     ,					**__UpperCAmelCase					:    int							) ->     Dict:
						requires_backends(self     ,					["""bs4"""]							)
						super().__init__(**__UpperCAmelCase							)

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

			def        SCREAMING_SNAKE_CASE					(       self					:    Any     ,					__UpperCAmelCase					:    Union[str, Any]							) ->     Dict:
						SCREAMING_SNAKE_CASE__										=   BeautifulSoup(__UpperCAmelCase     ,					"""html.parser"""							)

						SCREAMING_SNAKE_CASE__										=   []
						SCREAMING_SNAKE_CASE__										=   []
						SCREAMING_SNAKE_CASE__										=   []

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

												SCREAMING_SNAKE_CASE__										=   html.unescape(__UpperCAmelCase							).strip()
												if not text_in_this_tag:
															continue

												all_doc_strings.append(__UpperCAmelCase							)

												SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__										=   self.xpath_soup(__UpperCAmelCase							)
												stringaxtag_seq.append(__UpperCAmelCase							)
												stringaxsubs_seq.append(__UpperCAmelCase							)

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

						return all_doc_strings, stringaxtag_seq, stringaxsubs_seq

			def        SCREAMING_SNAKE_CASE					(       self					:    Any     ,					__UpperCAmelCase					:    Dict     ,					__UpperCAmelCase					:    Dict							) ->     Union[str, Any]:
						SCREAMING_SNAKE_CASE__										=   """"""
						for tagname, subs in zip(__UpperCAmelCase     ,					__UpperCAmelCase							):
									xpath += F"""/{tagname}"""
									if subs != 0:
												xpath += F"""[{subs}]"""
						return xpath





			def __call__(       self					:    Tuple     ,					__UpperCAmelCase					:    Optional[Any]							) ->     BatchFeature:
						SCREAMING_SNAKE_CASE__										=   False

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

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

						SCREAMING_SNAKE_CASE__										=   bool(isinstance(__UpperCAmelCase     ,					(list, tuple)							) and (isinstance(html_strings[0]     ,					__UpperCAmelCase							))							)

						if not is_batched:
									SCREAMING_SNAKE_CASE__										=   [html_strings]

						# Get nodes + xpaths
						SCREAMING_SNAKE_CASE__										=   []
						SCREAMING_SNAKE_CASE__										=   []
						for html_string in html_strings:
									SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__										=   self.get_three_from_single(__UpperCAmelCase							)
									nodes.append(__UpperCAmelCase							)
									SCREAMING_SNAKE_CASE__										=   []
									for node, tag_list, sub_list in zip(__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase							):
												SCREAMING_SNAKE_CASE__										=   self.construct_xpath(__UpperCAmelCase     ,					__UpperCAmelCase							)
												xpath_strings.append(__UpperCAmelCase							)
									xpaths.append(__UpperCAmelCase							)

						# return as Dict
						SCREAMING_SNAKE_CASE__										=   {"""nodes""": nodes, """xpaths""": xpaths}
						SCREAMING_SNAKE_CASE__										=   BatchFeature(data=__UpperCAmelCase     ,					tensor_type=__UpperCAmelCase							)

						return encoded_inputs





"""simple docstring"""




A_				:      Any           =			"0.21.0"

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
						from .utils import rich


