codingutf8 2023 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 pix2struct attributes imageprocessor tokenizer imageprocessorclass pix2structimageprocessor tokenizerclass t5tokenizer t5tokenizerfast def initself imageprocessor tokenizer tokenizer returntokentypeids false super initimageprocessor tokenizer def call self imagesnone text uniontextinput pretokenizedinput listtextinput listpretokenizedinput none addspecialtokens bool true padding unionbool str paddingstrategy false truncation unionbool str truncationstrategy none maxlength optionalint none maxpatches optionalint 2048 stride int 0 padtomultipleof optionalint none returnattentionmask optionalbool none returnoverflowingtokens bool false returnspecialtokensmask bool false returnoffsetsmapping bool false returntokentypeids bool false returnlength bool false verbose bool true returntensors optionalunionstr tensortype none kwargs batchencoding if images is none and text is none raise valueerroryou have to specify either images or text get only text if images is none and not self imageprocessor isvqa self currentprocessor self tokenizer textencoding self tokenizer texttext addspecialtokensaddspecialtokens paddingpadding truncationtruncation maxlengthmaxlength stridestride padtomultipleofpadtomultipleof returnattentionmaskreturnattentionmask returnoverflowingtokensreturnoverflowingtokens returnspecialtokensmaskreturnspecialtokensmask returnoffsetsmappingreturnoffsetsmapping returntokentypeidsreturntokentypeids returnlengthreturnlength verboseverbose returntensorsreturntensors kwargs return textencoding if not self imageprocessor isvqa add pixelvalues encodingimageprocessor self imageprocessor images returntensorsreturntensors maxpatchesmaxpatches kwargs else add pixelvalues and bbox encodingimageprocessor self imageprocessor images returntensorsreturntensors maxpatchesmaxpatches headertexttext kwargs if text is not none and not self imageprocessor isvqa textencoding self tokenizer texttext addspecialtokensaddspecialtokens paddingpadding truncationtruncation maxlengthmaxlength stridestride padtomultipleofpadtomultipleof returnattentionmaskreturnattentionmask returnoverflowingtokensreturnoverflowingtokens returnspecialtokensmaskreturnspecialtokensmask returnoffsetsmappingreturnoffsetsmapping returntokentypeidsreturntokentypeids returnlengthreturnlength verboseverbose returntensorsreturntensors kwargs if attentionmask in textencoding textencodingdecoderattentionmask textencoding popattentionmask if inputids in textencoding textencodingdecoderinputids textencoding popinputids else textencoding none if textencoding is not none encodingimageprocessor updatetextencoding return encodingimageprocessor 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 imageprocessorinputnames self imageprocessor modelinputnames return listdict fromkeystokenizerinputnames imageprocessorinputnames coding utf 8 2023 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 pix2struct constructs a pix2struct processor which wraps a bert tokenizer and pix2struct image processor into a single processor pix2structprocessor offers all the functionalities of pix2structimageprocessor and t5tokenizerfast see the docstring of pix2structprocessor __call__ and pix2structprocessor decode for more information args image_processor pix2structimageprocessor an instance of pix2structimageprocessor the image processor is a required input tokenizer union t5tokenizerfast t5tokenizer an instance of t5tokenizerfast or t5tokenizer the tokenizer is a required input this method uses pix2structimageprocessor preprocess method to prepare image s for the model and t5tokenizerfast __call__ to prepare text for the model please refer to the docstring of the above two methods for more information get only text add pixel_values add pixel_values and bbox this method forwards all its arguments to pix2structtokenizerfast s pretrainedtokenizer batch_decode please refer to the docstring of this method for more information this method forwards all its arguments to pix2structtokenizerfast s pretrainedtokenizer decode please refer to the docstring of this method for more information
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 Pix2StructProcessor(ProcessorMixin):
    r

    attributes = ["image_processor", "tokenizer"]
    image_processor_class = "Pix2StructImageProcessor"
    tokenizer_class = ("T5Tokenizer", "T5TokenizerFast")

    def __init__(self, image_processor, tokenizer):
        tokenizer.return_token_type_ids = False
        super().__init__(image_processor, tokenizer)

    def __call__(
        self,
        images=None,
        text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,
        add_special_tokens: bool = True,
        padding: Union[bool, str, PaddingStrategy] = False,
        truncation: Union[bool, str, TruncationStrategy] = None,
        max_length: Optional[int] = None,
        max_patches: Optional[int] = 2048,
        stride: int = 0,
        pad_to_multiple_of: Optional[int] = None,
        return_attention_mask: Optional[bool] = None,
        return_overflowing_tokens: bool = False,
        return_special_tokens_mask: bool = False,
        return_offsets_mapping: bool = False,
        return_token_type_ids: bool = False,
        return_length: bool = False,
        verbose: bool = True,
        return_tensors: Optional[Union[str, TensorType]] = None,
        **kwargs,
    ) -> BatchEncoding:
        
        if images is None and text is None:
            raise ValueError("You have to specify either images or text.")

        
        if images is None and not self.image_processor.is_vqa:
            self.current_processor = self.tokenizer
            text_encoding = self.tokenizer(
                text=text,
                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_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_token_type_ids=return_token_type_ids,
                return_length=return_length,
                verbose=verbose,
                return_tensors=return_tensors,
                **kwargs,
            )
            return text_encoding

        if not self.image_processor.is_vqa:
            
            encoding_image_processor = self.image_processor(
                images, return_tensors=return_tensors, max_patches=max_patches, **kwargs
            )
        else:
            
            encoding_image_processor = self.image_processor(
                images, return_tensors=return_tensors, max_patches=max_patches, header_text=text, **kwargs
            )

        if text is not None and not self.image_processor.is_vqa:
            text_encoding = self.tokenizer(
                text=text,
                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_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_token_type_ids=return_token_type_ids,
                return_length=return_length,
                verbose=verbose,
                return_tensors=return_tensors,
                **kwargs,
            )

            if "attention_mask" in text_encoding:
                text_encoding["decoder_attention_mask"] = text_encoding.pop("attention_mask")
            if "input_ids" in text_encoding:
                text_encoding["decoder_input_ids"] = text_encoding.pop("input_ids")
        else:
            text_encoding = None

        if text_encoding is not None:
            encoding_image_processor.update(text_encoding)

        return encoding_image_processor

    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
        image_processor_input_names = self.image_processor.model_input_names
        return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))