





"""simple docstring"""




import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
   import torch

@is_pipeline_test
class  _UpperCAmelCase  (   unittest.TestCase		):



      '''simple docstring'''
      lowercase_      : Union[str, Any]	    =   MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
      lowercase_      : str	    =   TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       Optional[int]										=  TextaTextGenerationPipeline(model=snake_case_					,      tokenizer=snake_case_  )
        return generator, ["Something to write", "Something else"]

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       List[Any]										=  generator('Something there'  )
        self.assertEqual(snake_case_					,      [{'generated_text': ANY(snake_case_  )}]  )
        # These are encoder decoder, they don't just append to incoming string
        self.assertFalse(outputs[0]['generated_text'].startswith('Something there'  )  )

        A_  :       Optional[int]										=  generator(['This is great !', 'Something else']					,      num_return_sequences=2					,      do_sample=snake_case_  )
        self.assertEqual(
            snake_case_					,      [
                [{'generated_text': ANY(snake_case_  )}, {'generated_text': ANY(snake_case_  )}],
                [{'generated_text': ANY(snake_case_  )}, {'generated_text': ANY(snake_case_  )}],
            ]					,      )

        A_  :       str										=  generator(
            ['This is great !', 'Something else']					,      num_return_sequences=2					,      batch_size=2					,      do_sample=snake_case_  )
        self.assertEqual(
            snake_case_					,      [
                [{'generated_text': ANY(snake_case_  )}, {'generated_text': ANY(snake_case_  )}],
                [{'generated_text': ANY(snake_case_  )}, {'generated_text': ANY(snake_case_  )}],
            ]					,      )

        with self.assertRaises(snake_case_  ):
          generator(4  )

      @require_torch
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       List[str]										=  pipeline('text2text-generation'					,      model='patrickvonplaten/t5-tiny-random'					,      framework='pt'  )
        # do_sample=False necessary for reproducibility
        A_  :       Optional[Any]										=  generator('Something there'					,      do_sample=snake_case_  )
        self.assertEqual(snake_case_					,      [{'generated_text': ''}]  )

        A_  :       List[Any]										=  3
        A_  :       Tuple										=  generator(
            'Something there'					,      num_return_sequences=snake_case_					,      num_beams=snake_case_					,      )
        A_  :       str										=  [
            {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide Beide'},
            {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide'},
            {'generated_text': ''},
        ]
        self.assertEqual(snake_case_					,      snake_case_  )

        A_  :       List[Any]										=  generator('This is a test'					,      do_sample=snake_case_					,      num_return_sequences=2					,      return_tensors=snake_case_  )
        self.assertEqual(
            snake_case_					,      [
                {'generated_token_ids': ANY(torch.Tensor  )},
                {'generated_token_ids': ANY(torch.Tensor  )},
            ]					,      )
        A_  :       List[Any]										=  generator.model.config.eos_token_id
        A_  :       Optional[Any]										=  '<pad>'
        A_  :       List[Any]										=  generator(
            ['This is a test', 'This is a second test']					,      do_sample=snake_case_					,      num_return_sequences=2					,      batch_size=2					,      return_tensors=snake_case_					,      )
        self.assertEqual(
            snake_case_					,      [
                [
                    {'generated_token_ids': ANY(torch.Tensor  )},
                    {'generated_token_ids': ANY(torch.Tensor  )},
                ],
                [
                    {'generated_token_ids': ANY(torch.Tensor  )},
                    {'generated_token_ids': ANY(torch.Tensor  )},
                ],
            ]					,      )

      @require_tf
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       int										=  pipeline('text2text-generation'					,      model='patrickvonplaten/t5-tiny-random'					,      framework='tf'  )
        # do_sample=False necessary for reproducibility
        A_  :       List[Any]										=  generator('Something there'					,      do_sample=snake_case_  )
        self.assertEqual(snake_case_					,      [{'generated_text': ''}]  )






"""simple docstring"""




from typing import TYPE_CHECKING

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


lowerCamelCase_     :						List[str]						=					{'configuration_xlnet': ['XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLNetConfig']}

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

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

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase_     :						int						=					[
       '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:
   lowerCamelCase_     :						Union[str, Any]						=					[
       '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

   lowerCamelCase_     :						List[Any]						=					_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)