codingutf8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license processor class for markuplm featureextractorclass markuplmfeatureextractor tokenizerclass markuplmtokenizer markuplmtokenizerfast parsehtml true def call self htmlstringsnone nodesnone xpathsnone nodelabelsnone questionsnone addspecialtokens bool true padding unionbool str paddingstrategy false truncation unionbool str truncationstrategy none maxlength optionalint none stride int 0 padtomultipleof optionalint none returntokentypeids optionalbool none returnattentionmask optionalbool none returnoverflowingtokens bool false returnspecialtokensmask bool false returnoffsetsmapping bool false returnlength bool false verbose bool true returntensors optionalunionstr tensortype none kwargs batchencoding first create nodes and xpaths if self parsehtml if htmlstrings is none raise valueerrormake sure to pass html strings in case parsehtml is set to true if nodes is not none or xpaths is not none or nodelabels is not none raise valueerror please don t pass nodes xpaths nor node labels in case parsehtml is set to true features self featureextractorhtmlstrings nodes featuresnodes xpaths featuresxpaths else if htmlstrings is not none raise valueerroryou have passed html strings but parsehtml is set to false if nodes is none or xpaths is none raise valueerrormake sure to pass nodes and xpaths in case parsehtml is set to false second apply the tokenizer if questions is not none and self parsehtml if isinstancequestions str questions questions add batch dimension as the feature extractor always adds a batch dimension encodedinputs self tokenizer textquestions if questions is not none else nodes textpairnodes if questions is not none else none xpathsxpaths nodelabelsnodelabels addspecialtokensaddspecialtokens paddingpadding truncationtruncation maxlengthmaxlength stridestride padtomultipleofpadtomultipleof returntokentypeidsreturntokentypeids returnattentionmaskreturnattentionmask returnoverflowingtokensreturnoverflowingtokens returnspecialtokensmaskreturnspecialtokensmask returnoffsetsmappingreturnoffsetsmapping returnlengthreturnlength verboseverbose returntensorsreturntensors kwargs return encodedinputs def batchdecodeself args kwargs return self tokenizer batchdecodeargs kwargs def decodeself args kwargs return self tokenizer decodeargs kwargs property def modelinputnamesself tokenizerinputnames self tokenizer modelinputnames return tokenizerinputnames coding utf 8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license processor class for markuplm constructs a markuplm processor which combines a markuplm feature extractor and a markuplm tokenizer into a single processor markuplmprocessor offers all the functionalities you need to prepare data for the model it first uses markuplmfeatureextractor to extract nodes and corresponding xpaths from one or more html strings next these are provided to markuplmtokenizer or markuplmtokenizerfast which turns them into token level input_ids attention_mask token_type_ids xpath_tags_seq and xpath_subs_seq args feature_extractor markuplmfeatureextractor an instance of markuplmfeatureextractor the feature extractor is a required input tokenizer markuplmtokenizer or markuplmtokenizerfast an instance of markuplmtokenizer or markuplmtokenizerfast the tokenizer is a required input parse_html bool optional defaults to true whether or not to use markuplmfeatureextractor to parse html strings into nodes and corresponding xpaths this method first forwards the html_strings argument to markuplmfeatureextractor __call__ next it passes the nodes and xpaths along with the additional arguments to markuplmtokenizer __call__ and returns the output optionally one can also provide a text argument which is passed along as first sequence please refer to the docstring of the above two methods for more information first create nodes and xpaths second apply the tokenizer add batch dimension as the feature extractor always adds a batch dimension this method forwards all its arguments to trocrtokenizer s pretrainedtokenizer batch_decode please refer to the docstring of this method for more information this method forwards all its arguments to trocrtokenizer s pretrainedtokenizer decode please refer to the docstring of this method for more information
from typing import Optional, Union

from ...file_utils import TensorType
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, TruncationStrategy


class MarkupLMProcessor(ProcessorMixin):
    r

    feature_extractor_class = "MarkupLMFeatureExtractor"
    tokenizer_class = ("MarkupLMTokenizer", "MarkupLMTokenizerFast")
    parse_html = True

    def __call__(
        self,
        html_strings=None,
        nodes=None,
        xpaths=None,
        node_labels=None,
        questions=None,
        add_special_tokens: bool = True,
        padding: Union[bool, str, PaddingStrategy] = False,
        truncation: Union[bool, str, TruncationStrategy] = None,
        max_length: Optional[int] = None,
        stride: int = 0,
        pad_to_multiple_of: Optional[int] = None,
        return_token_type_ids: Optional[bool] = None,
        return_attention_mask: Optional[bool] = None,
        return_overflowing_tokens: bool = False,
        return_special_tokens_mask: bool = False,
        return_offsets_mapping: bool = False,
        return_length: bool = False,
        verbose: bool = True,
        return_tensors: Optional[Union[str, TensorType]] = None,
        **kwargs,
    ) -> BatchEncoding:
        
        
        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`"
                )

            features = self.feature_extractor(html_strings)
            nodes = features["nodes"]
            xpaths = 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`")

        
        if questions is not None and self.parse_html:
            if isinstance(questions, str):
                questions = [questions]  

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

        return encoded_inputs

    def batch_decode(self, *args, **kwargs):
        
        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):
        
        return self.tokenizer.decode(*args, **kwargs)

    @property
    def model_input_names(self):
        tokenizer_input_names = self.tokenizer.model_input_names
        return tokenizer_input_names