






'''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      __SCREAMING_SNAKE_CASE      (			unittest.TestCase				):
 snake_case_  							=							MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
 snake_case_  							=							TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING




 def      __magic_name__	(      self	:							int					,      __lowercase	:							List[Any]					,      __lowercase	:							Optional[int]					,      __lowercase	:							List[Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=TextaTextGenerationPipeline(model=__lowercase					,      tokenizer=__lowercase      )
       return generator, ["Something to write", "Something else"]




 def      __magic_name__	(      self	:							Union[str, Any]					,      __lowercase	:							Optional[Any]					,      __lowercase	:							Dict      )	->						Union[str, Any]:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=generator('''Something there'''      )
       self.assertEqual(__lowercase					,      [{'''generated_text''': ANY(__lowercase      )}]      )
       # These are encoder decoder, they don't just append to incoming string
       self.assertFalse(outputs[0]['''generated_text'''].startswith('''Something there'''      )      )

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

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

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




 @require_torch
 def      __magic_name__	(      self	:							Dict      )	->						List[str]:
       SCREAMING_SNAKE_CASE__     :				List[str]									=pipeline('''text2text-generation'''					,      model='''patrickvonplaten/t5-tiny-random'''					,      framework='''pt'''      )
       # do_sample=False necessary for reproducibility
       SCREAMING_SNAKE_CASE__     :				Tuple									=generator('''Something there'''					,      do_sample=__lowercase      )
       self.assertEqual(__lowercase					,      [{'''generated_text''': ''''''}]      )

       SCREAMING_SNAKE_CASE__     :				int									=3
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=generator(
           '''Something there'''					,      num_return_sequences=__lowercase					,      num_beams=__lowercase					,      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									=[
           {'''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(__lowercase					,      __lowercase      )

       SCREAMING_SNAKE_CASE__     :				List[str]									=generator('''This is a test'''					,      do_sample=__lowercase					,      num_return_sequences=2					,      return_tensors=__lowercase      )
       self.assertEqual(
           __lowercase					,      [
               {'''generated_token_ids''': ANY(torch.Tensor      )},
               {'''generated_token_ids''': ANY(torch.Tensor      )},
           ]					,      )
       SCREAMING_SNAKE_CASE__     :				Any									=generator.model.config.eos_token_id
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									='''<pad>'''
       SCREAMING_SNAKE_CASE__     :				Tuple									=generator(
           ['''This is a test''', '''This is a second test''']					,      do_sample=__lowercase					,      num_return_sequences=2					,      batch_size=2					,      return_tensors=__lowercase					,      )
       self.assertEqual(
           __lowercase					,      [
               [
                   {'''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      __magic_name__	(      self	:							Any      )	->						List[str]:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=pipeline('''text2text-generation'''					,      model='''patrickvonplaten/t5-tiny-random'''					,      framework='''tf'''      )
       # do_sample=False necessary for reproducibility
       SCREAMING_SNAKE_CASE__     :				int									=generator('''Something there'''					,      do_sample=__lowercase      )
       self.assertEqual(__lowercase					,      [{'''generated_text''': ''''''}]      )







'''simple docstring'''
import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
  from PIL import Image
else:






  class      __SCREAMING_SNAKE_CASE      :




   @staticmethod
   def      __magic_name__	(      *__lowercase	:							int					,      **__lowercase	:							Optional[Any]      )	->						Optional[Any]:
         pass






@is_pipeline_test
@require_vision
class      __SCREAMING_SNAKE_CASE      (			unittest.TestCase				):




 @require_torch
 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      candidate_labels=['''a''', '''b''', '''c''']      )

       # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
       # python and torch versions.
       self.assertIn(
           nested_simplify(__lowercase      )					,      [
               [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}],
               [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''c'''}, {'''score''': 0.333, '''label''': '''b'''}],
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				int									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''					,      framework='''tf'''      )
       SCREAMING_SNAKE_CASE__     :				Dict									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Tuple									=image_classifier(__lowercase					,      candidate_labels=['''a''', '''b''', '''c''']      )

       self.assertEqual(
           nested_simplify(__lowercase      )					,      [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}]					,      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_classifier([image] * 5					,      candidate_labels=['''A''', '''B''', '''C''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
                   {'''score''': 0.333, '''label''': ANY(__lowercase      )},
               ],
           ]					,      )




 @slow
 @require_torch
 def      __magic_name__	(      self	:							List[Any]      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=pipeline(
           task='''zero-shot-image-classification'''					,      model='''openai/clip-vit-base-patch32'''					,      )
       # This is an image of 2 cats with remotes and no planes
       SCREAMING_SNAKE_CASE__     :				Any									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )

       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=image_classifier([image] * 5					,      candidate_labels=['''cat''', '''plane''', '''remote''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5					,      )




 @slow
 @require_tf
 def      __magic_name__	(      self	:							Union[str, Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				str									=pipeline(
           task='''zero-shot-image-classification'''					,      model='''openai/clip-vit-base-patch32'''					,      framework='''tf'''      )
       # This is an image of 2 cats with remotes and no planes
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=image_classifier(__lowercase					,      candidate_labels=['''cat''', '''plane''', '''remote''']      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]					,      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_classifier([image] * 5					,      candidate_labels=['''cat''', '''plane''', '''remote''']					,      batch_size=2      )
       self.assertEqual(
           nested_simplify(__lowercase      )					,      [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5					,      )