




import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
     import tensorflow as tf

     from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

_UpperCAmelCase	:						int			=     logging.get_logger(__name__)




class    a__       (							enum.Enum):
       _a 					=						0
       _a 					=						1







@add_end_docstrings(lowerCAmelCase)
class    a__       (							lowerCAmelCase):
       _a 					=						'''generated'''






       def __init__(   self:					str       ,					*_lowerCAmelCase:					Union[str, Any]       ,					**_lowerCAmelCase:					Optional[int]				):
         super().__init__(*UpperCAmelCase__       ,					**UpperCAmelCase__				)

         self.check_model_type(
             TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
             if self.framework == "tf"
             else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					_lowerCAmelCase:					List[Any]=None       ,					_lowerCAmelCase:					Tuple=None       ,					_lowerCAmelCase:					int=None       ,					_lowerCAmelCase:					Dict=None       ,					_lowerCAmelCase:					List[str]=None       ,					_lowerCAmelCase:					str=None       ,					**_lowerCAmelCase:					List[str]       ,					):
         lowercase    :Optional[int]      	=						{}
         if truncation is not None:
           lowercase    :List[Any]      	=						truncation

         lowercase    :List[Any]      	=						generate_kwargs

         lowercase    :List[str]      	=						{}
         if return_tensors is not None and return_type is None:
           lowercase    :str      	=						ReturnType.TENSORS if return_tensors else ReturnType.TEXT
         if return_type is not None:
           lowercase    :Union[str, Any]      	=						return_type

         if clean_up_tokenization_spaces is not None:
           lowercase    :Union[str, Any]      	=						clean_up_tokenization_spaces

         if stop_sequence is not None:
           lowercase    :Dict      	=						self.tokenizer.encode(UpperCAmelCase__       ,					add_special_tokens=UpperCAmelCase__				)
           if len(UpperCAmelCase__				) > 1:
             warnings.warn(
                 "Stopping on a multiple token sequence is not yet supported on transformers. The first token of"
                 " the stop sequence will be used as the stop sequence string in the interim."				)
           lowercase    :Dict      	=						stop_sequence_ids[0]

         return preprocess_params, forward_params, postprocess_params






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					int				):
         return True






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					*_lowerCAmelCase:					Dict       ,					_lowerCAmelCase:					Tuple				):
         lowercase    :List[str]      	=						self.model.config.prefix if self.model.config.prefix is not None else ""
         if isinstance(args[0]       ,					UpperCAmelCase__				):
           if self.tokenizer.pad_token_id is None:
             raise ValueError("Please make sure that the tokenizer has a pad_token_id when using a batch input"				)
           lowercase    :str      	=						([prefix + arg for arg in args[0]],)
           lowercase    :int      	=						True

         elif isinstance(args[0]       ,					UpperCAmelCase__				):
           lowercase    :Optional[Any]      	=						(prefix + args[0],)
           lowercase    :List[Any]      	=						False
         else:
           raise ValueError(
               F" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"				)
         lowercase    :Optional[Any]      	=						self.tokenizer(*UpperCAmelCase__       ,					padding=UpperCAmelCase__       ,					truncation=UpperCAmelCase__       ,					return_tensors=self.framework				)
         # This is produced by tokenizers but is an invalid generate kwargs
         if "token_type_ids" in inputs:
           del inputs["token_type_ids"]
         return inputs






       def __call__(   self:					Optional[int]       ,					*_lowerCAmelCase:					Dict       ,					**_lowerCAmelCase:					Optional[Any]				):
         lowercase    :str      	=						super().__call__(*UpperCAmelCase__       ,					**UpperCAmelCase__				)
         if (
             isinstance(args[0]       ,					UpperCAmelCase__				)
             and all(isinstance(UpperCAmelCase__       ,					UpperCAmelCase__				) for el in args[0]				)
             and all(len(UpperCAmelCase__				) == 1 for res in result				)
         ):
           return [res[0] for res in result]
         return result






       def 					SCREAMING_SNAKE_CASE    (   self:					int       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					Union[str, Any]=TruncationStrategy.DO_NOT_TRUNCATE       ,					**_lowerCAmelCase:					List[str]				):
         lowercase    :List[str]      	=						self._parse_and_tokenize(UpperCAmelCase__       ,					truncation=UpperCAmelCase__       ,					**UpperCAmelCase__				)
         return inputs






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]       ,					_lowerCAmelCase:					Dict       ,					**_lowerCAmelCase:					Any				):
         if self.framework == "pt":
           lowercase	,    lowercase    :str      	=						model_inputs["input_ids"].shape
         elif self.framework == "tf":
           lowercase	,    lowercase    :List[str]      	=						tf.shape(model_inputs["input_ids"]				).numpy()

         lowercase    :Dict      	=						generate_kwargs.get("min_length"       ,					self.model.config.min_length				)
         lowercase    :Union[str, Any]      	=						generate_kwargs.get("max_length"       ,					self.model.config.max_length				)
         self.check_inputs(UpperCAmelCase__       ,					generate_kwargs["min_length"]       ,					generate_kwargs["max_length"]				)
         lowercase    :Dict      	=						self.model.generate(**UpperCAmelCase__       ,					**UpperCAmelCase__				)
         lowercase    :str      	=						output_ids.shape[0]
         if self.framework == "pt":
           lowercase    :int      	=						output_ids.reshape(UpperCAmelCase__       ,					out_b // in_b       ,					*output_ids.shape[1:]				)
         elif self.framework == "tf":
           lowercase    :int      	=						tf.reshape(UpperCAmelCase__       ,					(in_b, out_b // in_b, *output_ids.shape[1:])				)
         return {"output_ids": output_ids}






       def 					SCREAMING_SNAKE_CASE    (   self:					int       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					Optional[int]=ReturnType.TEXT       ,					_lowerCAmelCase:					Dict=False				):
         lowercase    :Union[str, Any]      	=						[]
         for output_ids in model_outputs["output_ids"][0]:
           if return_type == ReturnType.TENSORS:
             lowercase    :List[str]      	=						{F"{self.return_name}_token_ids": output_ids}
           elif return_type == ReturnType.TEXT:
             lowercase    :Optional[Any]      	=						{
                 F"{self.return_name}_text": self.tokenizer.decode(
                     UpperCAmelCase__       ,					skip_special_tokens=UpperCAmelCase__       ,					clean_up_tokenization_spaces=UpperCAmelCase__       ,					)
             }
           records.append(UpperCAmelCase__				)
         return records







@add_end_docstrings(lowerCAmelCase)
class    a__       (							lowerCAmelCase):
       _a 					=						'''summary'''






       def __call__(   self:					Union[str, Any]       ,					*_lowerCAmelCase:					Optional[int]       ,					**_lowerCAmelCase:					Union[str, Any]				):
         return super().__call__(*UpperCAmelCase__       ,					**UpperCAmelCase__				)






       def 					SCREAMING_SNAKE_CASE    (   self:					str       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					int				):
         if max_length < min_length:
           logger.warning(F"Your min_length={min_length} must be inferior than your max_length={max_length}."				)

         if input_length < max_length:
           logger.warning(
               F"Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is "
               "a summarization task, where outputs shorter than the input are typically wanted, you might "
               F"consider decreasing max_length manually, e.g. summarizer('...', max_length={input_length//2})"				)







@add_end_docstrings(lowerCAmelCase)
class    a__       (							lowerCAmelCase):
       _a 					=						'''translation'''






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					int				):
         if input_length > 0.9 * max_length:
           logger.warning(
               F"Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider "
               "increasing your max_length manually, e.g. translator(\'...\', max_length=400)"				)
         return True






       def 					SCREAMING_SNAKE_CASE    (   self:					int       ,					*_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					Any=TruncationStrategy.DO_NOT_TRUNCATE       ,					_lowerCAmelCase:					Optional[Any]=None       ,					_lowerCAmelCase:					List[Any]=None				):
         if getattr(self.tokenizer       ,					"_build_translation_inputs"       ,					UpperCAmelCase__				):
           return self.tokenizer._build_translation_inputs(
               *UpperCAmelCase__       ,					return_tensors=self.framework       ,					truncation=UpperCAmelCase__       ,					src_lang=UpperCAmelCase__       ,					tgt_lang=UpperCAmelCase__				)
         else:
           return super()._parse_and_tokenize(*UpperCAmelCase__       ,					truncation=UpperCAmelCase__				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					Dict=None       ,					_lowerCAmelCase:					Optional[int]=None       ,					**_lowerCAmelCase:					int				):
         lowercase	,    lowercase	,    lowercase    :str      	=						super()._sanitize_parameters(**UpperCAmelCase__				)
         if src_lang is not None:
           lowercase    :Dict      	=						src_lang
         if tgt_lang is not None:
           lowercase    :List[str]      	=						tgt_lang
         if src_lang is None and tgt_lang is None:
           # Backward compatibility, direct arguments use is preferred.
           lowercase    :List[Any]      	=						kwargs.get("task"       ,					self.task				)
           lowercase    :Union[str, Any]      	=						task.split("_"				)
           if task and len(UpperCAmelCase__				) == 4:
             # translation, XX, to YY
             lowercase    :str      	=						items[1]
             lowercase    :Optional[Any]      	=						items[3]
         return preprocess_params, forward_params, postprocess_params






       def __call__(   self:					List[str]       ,					*_lowerCAmelCase:					List[str]       ,					**_lowerCAmelCase:					Any				):
         return super().__call__(*UpperCAmelCase__       ,					**UpperCAmelCase__				)









import warnings
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    __lowerCAmelCase       (							lowerCAmelCase):
       _a 					=						['''image_processor''', '''tokenizer''']
       _a 					=						'''LayoutLMv2ImageProcessor'''
       _a 					=						('''LayoutXLMTokenizer''', '''LayoutXLMTokenizerFast''')






       def __init__(   self:					int       ,					_lowerCAmelCase:					Any=None       ,					_lowerCAmelCase:					List[Any]=None       ,					**_lowerCAmelCase:					Optional[Any]				):
         if "feature_extractor" in kwargs:
           warnings.warn(
               "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
               " instead."       ,					_lowerCAmelCase       ,					)
           lowercase    :Optional[Any]      	=						kwargs.pop("feature_extractor"				)

         lowercase    :Union[str, Any]      	=						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__(_lowerCAmelCase       ,					_lowerCAmelCase				)






       def __call__(   self:					List[str]       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None       ,					_lowerCAmelCase:					Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None       ,					_lowerCAmelCase:					Union[List[List[int]], List[List[List[int]]]] = None       ,					_lowerCAmelCase:					Optional[Union[List[int], List[List[int]]]] = None       ,					_lowerCAmelCase:					bool = True       ,					_lowerCAmelCase:					Union[bool, str, PaddingStrategy] = False       ,					_lowerCAmelCase:					Union[bool, str, TruncationStrategy] = None       ,					_lowerCAmelCase:					Optional[int] = None       ,					_lowerCAmelCase:					int = 0       ,					_lowerCAmelCase:					Optional[int] = None       ,					_lowerCAmelCase:					Optional[bool] = None       ,					_lowerCAmelCase:					Optional[bool] = None       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = False       ,					_lowerCAmelCase:					bool = True       ,					_lowerCAmelCase:					Optional[Union[str, TensorType]] = None       ,					**_lowerCAmelCase:					Tuple       ,					):
         # verify input
         if self.image_processor.apply_ocr and (boxes is not None):
           raise ValueError(
               "You cannot provide bounding boxes "
               "if you initialized the image processor with apply_ocr set to True."				)

         if self.image_processor.apply_ocr and (word_labels is not None):
           raise ValueError(
               "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."				)

         if return_overflowing_tokens is True and return_offsets_mapping is False:
           raise ValueError("You cannot return overflowing tokens without returning the offsets mapping."				)

         # first, apply the image processor
         lowercase    :int      	=						self.image_processor(images=_lowerCAmelCase       ,					return_tensors=_lowerCAmelCase				)

         # second, apply the tokenizer
         if text is not None and self.image_processor.apply_ocr and text_pair is None:
           if isinstance(_lowerCAmelCase       ,					_lowerCAmelCase				):
             lowercase    :int      	=						[text]  # add batch dimension (as the image processor always adds a batch dimension)
           lowercase    :int      	=						features["words"]

         lowercase    :Optional[Any]      	=						self.tokenizer(
             text=text if text is not None else features["words"]       ,					text_pair=text_pair if text_pair is not None else None       ,					boxes=boxes if boxes is not None else features["boxes"]       ,					word_labels=_lowerCAmelCase       ,					add_special_tokens=_lowerCAmelCase       ,					padding=_lowerCAmelCase       ,					truncation=_lowerCAmelCase       ,					max_length=_lowerCAmelCase       ,					stride=_lowerCAmelCase       ,					pad_to_multiple_of=_lowerCAmelCase       ,					return_token_type_ids=_lowerCAmelCase       ,					return_attention_mask=_lowerCAmelCase       ,					return_overflowing_tokens=_lowerCAmelCase       ,					return_special_tokens_mask=_lowerCAmelCase       ,					return_offsets_mapping=_lowerCAmelCase       ,					return_length=_lowerCAmelCase       ,					verbose=_lowerCAmelCase       ,					return_tensors=_lowerCAmelCase       ,					**_lowerCAmelCase       ,					)

         # add pixel values
         lowercase    :Any      	=						features.pop("pixel_values"				)
         if return_overflowing_tokens is True:
           lowercase    :Any      	=						self.get_overflowing_images(_lowerCAmelCase       ,					encoded_inputs["overflow_to_sample_mapping"]				)
         lowercase    :Optional[int]      	=						images

         return encoded_inputs






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					List[Any]				):
         # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
         lowercase    :Dict      	=						[]
         for sample_idx in overflow_to_sample_mapping:
           images_with_overflow.append(images[sample_idx]				)

         if len(_lowerCAmelCase				) != len(_lowerCAmelCase				):
           raise ValueError(
               "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
               F" {len(_lowerCAmelCase				)} and {len(_lowerCAmelCase				)}"				)

         return images_with_overflow






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict       ,					*_lowerCAmelCase:					str       ,					**_lowerCAmelCase:					List[Any]				):
         return self.tokenizer.batch_decode(*_lowerCAmelCase       ,					**_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					str       ,					*_lowerCAmelCase:					Tuple       ,					**_lowerCAmelCase:					Union[str, Any]				):
         return self.tokenizer.decode(*_lowerCAmelCase       ,					**_lowerCAmelCase				)






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         return ["input_ids", "bbox", "attention_mask", "image"]






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         warnings.warn(
             "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."       ,					_lowerCAmelCase       ,					)
         return self.image_processor_class






       @property
       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         warnings.warn(
             "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."       ,					_lowerCAmelCase       ,					)
         return self.image_processor



