

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_									=   logging.get_logger(__name__)





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





@add_end_docstrings(SCREAMING_SNAKE_CASE							)
class  __lowerCAmelCase     (					SCREAMING_SNAKE_CASE							):
  _a									=  """generated"""




  def __init__(				self							,   *lowerCAmelCase							,   **lowerCAmelCase					)  -> List[Any]:
        '''simple docstring'''






        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   A__     (				self							,   lowerCAmelCase=None							,   lowerCAmelCase=None							,   lowerCAmelCase=None							,   lowerCAmelCase=None							,   lowerCAmelCase=None							,   lowerCAmelCase=None							,   **lowerCAmelCase							,   )  -> Any:
        '''simple docstring'''






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

        _lowercase	       =generate_kwargs

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

        if clean_up_tokenization_spaces is not None:
              _lowercase	       =clean_up_tokenization_spaces

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

        return preprocess_params, forward_params, postprocess_params




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase					)  -> Optional[int]:
        '''simple docstring'''






        return True




  def   A__     (				self							,   *lowerCAmelCase							,   lowerCAmelCase					)  -> List[str]:
        '''simple docstring'''






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

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






        _lowercase	       =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   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase=TruncationStrategy.DO_NOT_TRUNCATE							,   **lowerCAmelCase					)  -> List[Any]:
        '''simple docstring'''






        _lowercase	       =self._parse_and_tokenize(lowerCAmelCase							,   truncation=lowerCAmelCase							,   **lowerCAmelCase					)
        return inputs




  def   A__     (				self							,   lowerCAmelCase							,   **lowerCAmelCase					)  -> str:
        '''simple docstring'''






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

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




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase=ReturnType.TEXT							,   lowerCAmelCase=False					)  -> List[Any]:
        '''simple docstring'''






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




  def __call__(				self							,   *lowerCAmelCase							,   **lowerCAmelCase					)  -> Optional[Any]:
        '''simple docstring'''






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




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase					)  -> bool:
        '''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(SCREAMING_SNAKE_CASE							)
class  __lowerCAmelCase     (					SCREAMING_SNAKE_CASE							):
  _a									=  """translation"""




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase					)  -> str:
        '''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   A__     (				self							,   *lowerCAmelCase							,   lowerCAmelCase=TruncationStrategy.DO_NOT_TRUNCATE							,   lowerCAmelCase=None							,   lowerCAmelCase=None					)  -> Tuple:
        '''simple docstring'''






        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   A__     (				self							,   lowerCAmelCase=None							,   lowerCAmelCase=None							,   **lowerCAmelCase					)  -> Dict:
        '''simple docstring'''






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




  def __call__(				self							,   *lowerCAmelCase							,   **lowerCAmelCase					)  -> Dict:
        '''simple docstring'''






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





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowercase_									=   {'configuration_xlnet': ['XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLNetConfig']}

try:
  if not is_sentencepiece_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase_									=   ['XLNetTokenizer']

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase_									=   ['XLNetTokenizerFast']

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase_									=   [
      'XLNET_PRETRAINED_MODEL_ARCHIVE_LIST',
      'XLNetForMultipleChoice',
      'XLNetForQuestionAnswering',
      'XLNetForQuestionAnsweringSimple',
      'XLNetForSequenceClassification',
      'XLNetForTokenClassification',
      'XLNetLMHeadModel',
      'XLNetModel',
      'XLNetPreTrainedModel',
      'load_tf_weights_in_xlnet',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase_									=   [
      'TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST',
      'TFXLNetForMultipleChoice',
      'TFXLNetForQuestionAnsweringSimple',
      'TFXLNetForSequenceClassification',
      'TFXLNetForTokenClassification',
      'TFXLNetLMHeadModel',
      'TFXLNetMainLayer',
      'TFXLNetModel',
      'TFXLNetPreTrainedModel',
  ]


if TYPE_CHECKING:
  from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig

  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet import XLNetTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet_fast import XLNetTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_xlnet import (
        XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        XLNetForMultipleChoice,
        XLNetForQuestionAnswering,
        XLNetForQuestionAnsweringSimple,
        XLNetForSequenceClassification,
        XLNetForTokenClassification,
        XLNetLMHeadModel,
        XLNetModel,
        XLNetPreTrainedModel,
        load_tf_weights_in_xlnet,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_xlnet import (
        TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXLNetForMultipleChoice,
        TFXLNetForQuestionAnsweringSimple,
        TFXLNetForSequenceClassification,
        TFXLNetForTokenClassification,
        TFXLNetLMHeadModel,
        TFXLNetMainLayer,
        TFXLNetModel,
        TFXLNetPreTrainedModel,
    )

else:
  import sys

  lowercase_									=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


