





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

lowercase    : Optional[Any]    	=  logging.get_logger(__name__)

class  __A(						enum.Enum ):
					__A	  =       0
					__A	  =       1






@add_end_docstrings(__A )
class  __A(						__A ):
					__A	  =       """generated"""

					def __init__(      self,       *A,       **A      ):





												"""simple docstring"""

												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 	_UpperCamelCase     (      self,       A=None,       A=None,       A=None,       A=None,       A=None,       A=None,       **A,       ):





												"""simple docstring"""

												_UpperCamelCase              =						{}
												if truncation is not None:
																			_UpperCamelCase              =						truncation

												_UpperCamelCase              =						generate_kwargs

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

												if clean_up_tokenization_spaces is not None:
																			_UpperCamelCase              =						clean_up_tokenization_spaces

												if stop_sequence is not None:
																			_UpperCamelCase              =						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.'''      )
																			_UpperCamelCase              =						stop_sequence_ids[0]

												return preprocess_params, forward_params, postprocess_params

					def 	_UpperCamelCase     (      self,       A,       A,       A      ):





												"""simple docstring"""

												return True

					def 	_UpperCamelCase     (      self,       *A,       A      ):





												"""simple docstring"""

												_UpperCamelCase              =						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'''      )
																			_UpperCamelCase              =						([prefix + arg for arg in args[0]],)
																			_UpperCamelCase              =						True

												elif isinstance(args[0],       UpperCamelCase__      ):
																			_UpperCamelCase              =						(prefix + args[0],)
																			_UpperCamelCase              =						False
												else:
																			raise ValueError(
																			    F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`'''      )
												_UpperCamelCase              =						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,       *A,       **A      ):





												"""simple docstring"""

												_UpperCamelCase              =						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 	_UpperCamelCase     (      self,       A,       A=TruncationStrategy.DO_NOT_TRUNCATE,       **A      ):





												"""simple docstring"""

												_UpperCamelCase              =						self._parse_and_tokenize(UpperCamelCase__,       truncation=UpperCamelCase__,       **UpperCamelCase__      )
												return inputs

					def 	_UpperCamelCase     (      self,       A,       **A      ):





												"""simple docstring"""

												if self.framework == "pt":
																			_UpperCamelCase              =						model_inputs['input_ids'].shape
												elif self.framework == "tf":
																			_UpperCamelCase              =						tf.shape(model_inputs['''input_ids''']      ).numpy()

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



					def 	_UpperCamelCase     (      self,       A,       A=ReturnType.TEXT,       A=False      ):





												"""simple docstring"""

												_UpperCamelCase              =						[]
												for output_ids in model_outputs["output_ids"][0]:
																			if return_type == ReturnType.TENSORS:
																										_UpperCamelCase              =						{F'''{self.return_name}_token_ids''': output_ids}
																			elif return_type == ReturnType.TEXT:
																										_UpperCamelCase              =						{
																										    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(__A )
class  __A(						__A ):
					__A	  =       """summary"""

					def __call__(      self,       *A,       **A      ):





												"""simple docstring"""

												return super().__call__(*UpperCamelCase__,       **UpperCamelCase__      )



					def 	_UpperCamelCase     (      self,       A,       A,       A      ):





												"""simple docstring"""

												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(__A )
class  __A(						__A ):
					__A	  =       """translation"""

					def 	_UpperCamelCase     (      self,       A,       A,       A      ):





												"""simple docstring"""

												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,       *A,       A=TruncationStrategy.DO_NOT_TRUNCATE,       A=None,       A=None      ):





												"""simple docstring"""

												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 	_UpperCamelCase     (      self,       A=None,       A=None,       **A      ):





												"""simple docstring"""

												_UpperCamelCase              =						super()._sanitize_parameters(**UpperCamelCase__      )
												if src_lang is not None:
																			_UpperCamelCase              =						src_lang
												if tgt_lang is not None:
																			_UpperCamelCase              =						tgt_lang
												if src_lang is None and tgt_lang is None:
																			# Backward compatibility, direct arguments use is preferred.
																			_UpperCamelCase              =						kwargs.get('''task''',       self.task      )
																			_UpperCamelCase              =						task.split('''_'''      )
																			if task and len(UpperCamelCase__      ) == 4:
																										# translation, XX, to YY
																										_UpperCamelCase              =						items[1]
																										_UpperCamelCase              =						items[3]
												return preprocess_params, forward_params, postprocess_params



					def __call__(      self,       *A,       **A      ):





												"""simple docstring"""

												return super().__call__(*UpperCamelCase__,       **UpperCamelCase__      )













import os
import sys
import unittest


lowercase    : Any    	=  os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, """utils"""))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
lowercase    : int    	=  os.path.join(git_repo_path, """src""", """transformers""")

lowercase    : str    	=  """
{0} = None
"""

lowercase    : Union[str, Any]    	=  """
class {0}(metaclass=DummyObject):
    _backends = {1}

    def __init__(self, *args, **kwargs):
        requires_backends(self, {1})
"""


lowercase    : Optional[Any]    	=  """
def {0}(*args, **kwargs):
    requires_backends({0}, {1})
"""

class  __A(						unittest.TestCase ):

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						find_backend('''    _import_structure["models.albert"].append("AlbertTokenizerFast")'''      )
            self.assertIsNone(A      )

            _UpperCamelCase              =						find_backend('''    if not is_tokenizers_available():'''      )
            self.assertEqual(A,       '''tokenizers'''      )

            _UpperCamelCase              =						find_backend('''    if not is_tensorflow_text_available():'''      )
            self.assertEqual(A,       '''tensorflow_text'''      )

            _UpperCamelCase              =						find_backend('''    if not (is_sentencepiece_available() and is_tokenizers_available()):'''      )
            self.assertEqual(A,       '''sentencepiece_and_tokenizers'''      )

            _UpperCamelCase              =						find_backend(
                '''    if not (is_sentencepiece_available() and is_tensorflow_text_available()):'''      )
            self.assertEqual(A,       '''sentencepiece_and_tensorflow_text'''      )

            _UpperCamelCase              =						find_backend(
                '''    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):'''      )
            self.assertEqual(A,       '''sentencepiece_and_tokenizers_and_vision'''      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						read_init()
            # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
            self.assertIn('''torch''',       A      )
            self.assertIn('''tensorflow_text''',       A      )
            self.assertIn('''sentencepiece_and_tokenizers''',       A      )

            # Likewise, we can't assert on the exact content of a key
            self.assertIn('''BertModel''',       objects['''torch''']      )
            self.assertIn('''TFBertModel''',       objects['''tf''']      )
            self.assertIn('''FlaxBertModel''',       objects['''flax''']      )
            self.assertIn('''BertModel''',       objects['''torch''']      )
            self.assertIn('''TFBertTokenizer''',       objects['''tensorflow_text''']      )
            self.assertIn('''convert_slow_tokenizer''',       objects['''sentencepiece_and_tokenizers''']      )

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						create_dummy_object('''CONSTANT''',       '''\'torch\''''      )
            self.assertEqual(A,       '''\nCONSTANT = None\n'''      )

            _UpperCamelCase              =						create_dummy_object('''function''',       '''\'torch\''''      )
            self.assertEqual(
                A,       '''\ndef function(*args, **kwargs):\n    requires_backends(function, \'torch\')\n'''      )

            _UpperCamelCase              =						'''
class FakeClass(metaclass=DummyObject):
    _backends = \'torch\'

    def __init__(self, *args, **kwargs):
        requires_backends(self, \'torch\')
'''
            _UpperCamelCase              =						create_dummy_object('''FakeClass''',       '''\'torch\''''      )
            self.assertEqual(A,       A      )



     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						'''# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, ["torch"])


class FakeClass(metaclass=DummyObject):
    _backends = ["torch"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, ["torch"])
'''
            _UpperCamelCase              =						create_dummy_files({'''torch''': ['''CONSTANT''', '''function''', '''FakeClass''']}      )
            self.assertEqual(dummy_files['''torch'''],       A      )






