codingutf8 2022 meta platforms s and the huggingface 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 imagetext processor class for flava attributes imageprocessor tokenizer imageprocessorclass flavaimageprocessor tokenizerclass berttokenizer berttokenizerfast def initself imageprocessornone tokenizernone kwargs featureextractor none if featureextractor in kwargs warnings warn the featureextractor argument is deprecated and will be removed in v5 use imageprocessor instead futurewarning featureextractor kwargs popfeatureextractor imageprocessor imageprocessor if imageprocessor is not none else featureextractor if imageprocessor is none raise valueerroryou need to specify an imageprocessor if tokenizer is none raise valueerroryou need to specify a tokenizer super initimageprocessor tokenizer self currentprocessor self imageprocessor def call self images optionalimageinput none text optionaluniontextinput pretokenizedinput listtextinput listpretokenizedinput none addspecialtokens bool true padding unionbool str paddingstrategy false truncation unionbool str truncationstrategy false maxlength optionalint none stride int 0 padtomultipleof optionalint none returnimagemask optionalbool none returncodebookpixels optionalbool 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 if text is none and images is none raise valueerroryou have to specify either text or images both cannot be none if text is not none encoding self tokenizer texttext addspecialtokensaddspecialtokens paddingpadding truncationtruncation maxlengthmaxlength stridestride padtomultipleofpadtomultipleof returntokentypeidsreturntokentypeids returnattentionmaskreturnattentionmask returnoverflowingtokensreturnoverflowingtokens returnspecialtokensmaskreturnspecialtokensmask returnoffsetsmappingreturnoffsetsmapping returnlengthreturnlength verboseverbose returntensorsreturntensors kwargs if images is not none imagefeatures self imageprocessor images returnimagemaskreturnimagemask returncodebookpixelsreturncodebookpixels returntensorsreturntensors kwargs if text is not none and images is not none encoding updateimagefeatures return encoding elif text is not none return encoding else return batchencodingdatadictimagefeatures tensortypereturntensors 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 property def featureextractorclassself warnings warn featureextractorclass is deprecated and will be removed in v5 use imageprocessorclass instead futurewarning return self imageprocessorclass property def featureextractorself warnings warn featureextractor is deprecated and will be removed in v5 use imageprocessor instead futurewarning return self imageprocessor coding utf 8 2022 meta platforms s and the huggingface 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 image text processor class for flava constructs a flava processor which wraps a flava image processor and a flava tokenizer into a single processor flavaprocessor offers all the functionalities of flavaimageprocessor and berttokenizerfast see the flavaprocessor __call__ and flavaprocessor decode for more information args image_processor flavaimageprocessor optional the image processor is a required input tokenizer berttokenizerfast optional the tokenizer is a required input this method uses flavaimageprocessor __call__ method to prepare image s for the model and berttokenizerfast __call__ to prepare text for the model please refer to the docstring of the above two methods for more information this method forwards all its arguments to berttokenizerfast s pretrainedtokenizer batch_decode please refer to the docstring of this method for more information this method forwards all its arguments to berttokenizerfast s pretrainedtokenizer decode please refer to the docstring of this method for more information
import warnings
from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType


class FlavaProcessor(ProcessorMixin):
    r

    attributes = ["image_processor", "tokenizer"]
    image_processor_class = "FlavaImageProcessor"
    tokenizer_class = ("BertTokenizer", "BertTokenizerFast")

    def __init__(self, image_processor=None, tokenizer=None, **kwargs):
        feature_extractor = None
        if "feature_extractor" in kwargs:
            warnings.warn(
                "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                " instead.",
                FutureWarning,
            )
            feature_extractor = kwargs.pop("feature_extractor")

        image_processor = image_processor if image_processor is not None else feature_extractor
        if image_processor is None:
            raise ValueError("You need to specify an `image_processor`.")
        if tokenizer is None:
            raise ValueError("You need to specify a `tokenizer`.")

        super().__init__(image_processor, tokenizer)
        self.current_processor = self.image_processor

    def __call__(
        self,
        images: Optional[ImageInput] = None,
        text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None,
        add_special_tokens: bool = True,
        padding: Union[bool, str, PaddingStrategy] = False,
        truncation: Union[bool, str, TruncationStrategy] = False,
        max_length: Optional[int] = None,
        stride: int = 0,
        pad_to_multiple_of: Optional[int] = None,
        return_image_mask: Optional[bool] = None,
        return_codebook_pixels: Optional[bool] = 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,
    ):
        

        if text is None and images is None:
            raise ValueError("You have to specify either text or images. Both cannot be none.")

        if text is not None:
            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_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,
            )
        if images is not None:
            image_features = self.image_processor(
                images,
                return_image_mask=return_image_mask,
                return_codebook_pixels=return_codebook_pixels,
                return_tensors=return_tensors,
                **kwargs,
            )

        if text is not None and images is not None:
            encoding.update(image_features)
            return encoding
        elif text is not None:
            return encoding
        else:
            return BatchEncoding(data=dict(**image_features), tensor_type=return_tensors)

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

    @property
    def feature_extractor_class(self):
        warnings.warn(
            "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.",
            FutureWarning,
        )
        return self.image_processor_class

    @property
    def feature_extractor(self):
        warnings.warn(
            "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.",
            FutureWarning,
        )
        return self.image_processor