




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

__lowerCAmelCase		:				Any       			=		logging.get_logger(__name__)




class     snake_case__							(enum.Enum   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Dict							 =					0
				SCREAMING_SNAKE_CASE_       :							Optional[Any]							 =					1




@add_end_docstrings(_UpperCamelCase   )
class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							int							 =					"""generated"""





				def __init__(      self				:     Dict					,      *__lowerCamelCase				:     List[Any]					,      **__lowerCamelCase				:     Union[str, Any]							)					->     List[Any]:
							super().__init__(*__lowerCamelCase					,      **__lowerCamelCase							)

							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 							__UpperCAmelCase							(      self				:     List[Any]					,      __lowerCamelCase				:     List[str]=None					,      __lowerCamelCase				:     List[str]=None					,      __lowerCamelCase				:     Any=None					,      __lowerCamelCase				:     Union[str, Any]=None					,      __lowerCamelCase				:     Union[str, Any]=None					,      __lowerCamelCase				:     str=None					,      **__lowerCamelCase				:     str					,      )					->     Tuple:
							a    =			{}
							if truncation is not None:
										a    =			truncation

							a    =			generate_kwargs

							a    =			{}
							if return_tensors is not None and return_type is None:
										a    =			ReturnType.TENSORS if return_tensors else ReturnType.TEXT
							if return_type is not None:
										a    =			return_type

							if clean_up_tokenization_spaces is not None:
										a    =			clean_up_tokenization_spaces

							if stop_sequence is not None:
										a    =			self.tokenizer.encode(__lowerCamelCase					,      add_special_tokens=__lowerCamelCase							)
										if len(__lowerCamelCase							) > 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."							)
										a    =			stop_sequence_ids[0]

							return preprocess_params, forward_params, postprocess_params





				def 							__UpperCAmelCase							(      self				:     List[Any]					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int							)					->     Union[str, Any]:
							return True





				def 							__UpperCAmelCase							(      self				:     Optional[int]					,      *__lowerCamelCase				:     str					,      __lowerCamelCase				:     Optional[int]							)					->     Union[str, Any]:
							a    =			self.model.config.prefix if self.model.config.prefix is not None else ""
							if isinstance(args[0]					,      __lowerCamelCase							):
										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"							)
										a    =			([prefix + arg for arg in args[0]],)
										a    =			True

							elif isinstance(args[0]					,      __lowerCamelCase							):
										a    =			(prefix + args[0],)
										a    =			False
							else:
										raise ValueError(
										    f""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"""							)
							a    =			self.tokenizer(*__lowerCamelCase					,      padding=__lowerCamelCase					,      truncation=__lowerCamelCase					,      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				:     Any					,      *__lowerCamelCase				:     List[Any]					,      **__lowerCamelCase				:     int							)					->     Optional[int]:
							a    =			super().__call__(*__lowerCamelCase					,      **__lowerCamelCase							)
							if (
							    isinstance(args[0]					,      __lowerCamelCase							)
							    and all(isinstance(__lowerCamelCase					,      __lowerCamelCase							) for el in args[0]							)
							    and all(len(__lowerCamelCase							) == 1 for res in result							)
							):
										return [res[0] for res in result]
							return result





				def 							__UpperCAmelCase							(      self				:     str					,      __lowerCamelCase				:     Any					,      __lowerCamelCase				:     List[Any]=TruncationStrategy.DO_NOT_TRUNCATE					,      **__lowerCamelCase				:     str							)					->     Optional[int]:
							a    =			self._parse_and_tokenize(__lowerCamelCase					,      truncation=__lowerCamelCase					,      **__lowerCamelCase							)
							return inputs





				def 							__UpperCAmelCase							(      self				:     List[Any]					,      __lowerCamelCase				:     Optional[int]					,      **__lowerCamelCase				:     Dict							)					->     Optional[int]:
							if self.framework == "pt":
										a    ,		a    =			model_inputs["input_ids"].shape
							elif self.framework == "tf":
										a    ,		a    =			tf.shape(model_inputs["input_ids"]							).numpy()

							a    =			generate_kwargs.get("min_length"					,      self.model.config.min_length							)
							a    =			generate_kwargs.get("max_length"					,      self.model.config.max_length							)
							self.check_inputs(__lowerCamelCase					,      generate_kwargs["min_length"]					,      generate_kwargs["max_length"]							)
							a    =			self.model.generate(**__lowerCamelCase					,      **__lowerCamelCase							)
							a    =			output_ids.shape[0]
							if self.framework == "pt":
										a    =			output_ids.reshape(__lowerCamelCase					,      out_b // in_b					,      *output_ids.shape[1:]							)
							elif self.framework == "tf":
										a    =			tf.reshape(__lowerCamelCase					,      (in_b, out_b // in_b, *output_ids.shape[1:])							)
							return {"output_ids": output_ids}





				def 							__UpperCAmelCase							(      self				:     Optional[int]					,      __lowerCamelCase				:     List[Any]					,      __lowerCamelCase				:     List[Any]=ReturnType.TEXT					,      __lowerCamelCase				:     List[Any]=False							)					->     int:
							a    =			[]
							for output_ids in model_outputs["output_ids"][0]:
										if return_type == ReturnType.TENSORS:
													a    =			{f"""{self.return_name}_token_ids""": output_ids}
										elif return_type == ReturnType.TEXT:
													a    =			{
													    f"""{self.return_name}_text""": self.tokenizer.decode(
													        __lowerCamelCase					,      skip_special_tokens=__lowerCamelCase					,      clean_up_tokenization_spaces=__lowerCamelCase					,      )
													}
										records.append(__lowerCamelCase							)
							return records




@add_end_docstrings(_UpperCamelCase   )
class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Union[str, Any]							 =					"""summary"""





				def __call__(      self				:     Dict					,      *__lowerCamelCase				:     List[Any]					,      **__lowerCamelCase				:     str							)					->     Optional[Any]:
							return super().__call__(*__lowerCamelCase					,      **__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Tuple					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int							)					->     bool:
							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(_UpperCamelCase   )
class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Dict							 =					"""translation"""





				def 							__UpperCAmelCase							(      self				:     Optional[int]					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int					,      __lowerCamelCase				:     int							)					->     Dict:
							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 							__UpperCAmelCase							(      self				:     Union[str, Any]					,      *__lowerCamelCase				:     Optional[int]					,      __lowerCamelCase				:     Any=TruncationStrategy.DO_NOT_TRUNCATE					,      __lowerCamelCase				:     Optional[int]=None					,      __lowerCamelCase				:     str=None							)					->     Optional[Any]:
							if getattr(self.tokenizer					,      "_build_translation_inputs"					,      __lowerCamelCase							):
										return self.tokenizer._build_translation_inputs(
										    *__lowerCamelCase					,      return_tensors=self.framework					,      truncation=__lowerCamelCase					,      src_lang=__lowerCamelCase					,      tgt_lang=__lowerCamelCase							)
							else:
										return super()._parse_and_tokenize(*__lowerCamelCase					,      truncation=__lowerCamelCase							)





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





				def __call__(      self				:     List[str]					,      *__lowerCamelCase				:     int					,      **__lowerCamelCase				:     List[str]							)					->     Dict:
							return super().__call__(*__lowerCamelCase					,      **__lowerCamelCase							)












import warnings

from ...utils import logging
from .image_processing_donut import DonutImageProcessor


__lowerCAmelCase		:				List[Any]       			=		logging.get_logger(__name__)




class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				def __init__(      self				:     Union[str, Any]					,      *__lowerCamelCase				:     Optional[Any]					,      **__lowerCamelCase				:     Dict							)					->     None:
							warnings.warn(
							    "The class DonutFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"
							    " use DonutImageProcessor instead."					,      __lowerCamelCase					,      )
							super().__init__(*__lowerCamelCase					,      **__lowerCamelCase							)






