

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   ):
    __UpperCamelCase         =MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
    __UpperCamelCase         =TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

    def UpperCamelCase			(			self :      str							,    snake_case__ :      List[Any]							,    snake_case__ :      List[str]							,    snake_case__ :      Tuple    ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			TextaTextGenerationPipeline(model=snake_case__							,    tokenizer=snake_case__    )
     return generator, ["Something to write", "Something else"]

    def UpperCamelCase			(			self :      Dict							,    snake_case__ :      int							,    snake_case__ :      int    ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE								=			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'    )    )

     SCREAMING_SNAKE_CASE								=			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__    )}],
         ]							,    )

     SCREAMING_SNAKE_CASE								=			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 UpperCamelCase			(			self :      str    ):

     """simple docstring"""






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

     SCREAMING_SNAKE_CASE								=			3
     SCREAMING_SNAKE_CASE								=			generator(
         'Something there'							,    num_return_sequences=snake_case__							,    num_beams=snake_case__							,    )
     SCREAMING_SNAKE_CASE								=			[
         {'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__    )

     SCREAMING_SNAKE_CASE								=			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    )},
         ]							,    )
     SCREAMING_SNAKE_CASE								=			generator.model.config.eos_token_id
     SCREAMING_SNAKE_CASE								=			'<pad>'
     SCREAMING_SNAKE_CASE								=			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 UpperCamelCase			(			self :      List[str]    ):

     """simple docstring"""






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




# limitations under the License.

# NOTE: This file is deprecated and will be removed in a future version.
# It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works

from .pipelines import DiffusionPipeline, ImagePipelineOutput  # noqa: F401
from .utils import deprecate


deprecate(
    "pipelines_utils",
    "0.22.0",
    "Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.",
    standard_warn=False,
    stacklevel=3,
)

