




import unittest

import pytest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    MBartaaTokenizerFast,
    MBartConfig,
    MBartForConditionalGeneration,
    TranslationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch, slow

from .test_pipelines_common import ANY






@is_pipeline_test
class 						lowercase     (   unittest.TestCase):
      __lowercase 		=  MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
      __lowercase 		=  TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING






      def    A__(self ,  a__ ,  a__ ,  a__					)       ->   Optional[Any]:
         if isinstance(model.config ,  __SCREAMING_SNAKE_CASE					):
            SCREAMING_SNAKE_CASE__						,				SCREAMING_SNAKE_CASE__			:				Any       						= list(tokenizer.lang_code_to_id.keys()					)[:2]
            SCREAMING_SNAKE_CASE__			:				Optional[int]       						= TranslationPipeline(model=__SCREAMING_SNAKE_CASE ,  tokenizer=__SCREAMING_SNAKE_CASE ,  src_lang=__SCREAMING_SNAKE_CASE ,  tgt_lang=__SCREAMING_SNAKE_CASE					)
         else:
            SCREAMING_SNAKE_CASE__			:				int       						= TranslationPipeline(model=__SCREAMING_SNAKE_CASE ,  tokenizer=__SCREAMING_SNAKE_CASE					)
         return translator, ["Some string", "Some other text"]






      def    A__(self ,  a__ ,  a__					)       ->   Optional[Any]:
         SCREAMING_SNAKE_CASE__			:				Tuple       						= translator('''Some string'''					)
         self.assertEqual(__SCREAMING_SNAKE_CASE ,  [{'''translation_text''': ANY(__SCREAMING_SNAKE_CASE					)}]					)

         SCREAMING_SNAKE_CASE__			:				List[str]       						= translator(['''Some string''']					)
         self.assertEqual(__SCREAMING_SNAKE_CASE ,  [{'''translation_text''': ANY(__SCREAMING_SNAKE_CASE					)}]					)

         SCREAMING_SNAKE_CASE__			:				List[str]       						= translator(['''Some string''', '''other string''']					)
         self.assertEqual(__SCREAMING_SNAKE_CASE ,  [{'''translation_text''': ANY(__SCREAMING_SNAKE_CASE					)}, {'''translation_text''': ANY(__SCREAMING_SNAKE_CASE					)}]					)






      @require_torch
      def    A__(self					)       ->   Any:
         SCREAMING_SNAKE_CASE__			:				Dict       						= pipeline('''translation_en_to_ro''' ,  model='''patrickvonplaten/t5-tiny-random''' ,  framework='''pt'''					)
         SCREAMING_SNAKE_CASE__			:				List[Any]       						= translator('''This is a test string''' ,  max_length=2_0					)
         self.assertEqual(
             __SCREAMING_SNAKE_CASE ,  [
                 {
                     '''translation_text''': (
                         '''Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'''
                         ''' Beide Beide'''
                     )
                 }
             ] ,  )






      @require_tf
      def    A__(self					)       ->   str:
         SCREAMING_SNAKE_CASE__			:				int       						= pipeline('''translation_en_to_ro''' ,  model='''patrickvonplaten/t5-tiny-random''' ,  framework='''tf'''					)
         SCREAMING_SNAKE_CASE__			:				Dict       						= translator('''This is a test string''' ,  max_length=2_0					)
         self.assertEqual(
             __SCREAMING_SNAKE_CASE ,  [
                 {
                     '''translation_text''': (
                         '''Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'''
                         ''' Beide Beide'''
                     )
                 }
             ] ,  )






      @require_torch
      def    A__(self					)       ->   List[str]:
         SCREAMING_SNAKE_CASE__			:				List[Any]       						= pipeline('''translation_en_to_de''' ,  model='''patrickvonplaten/t5-tiny-random''' ,  framework='''pt'''					)
         SCREAMING_SNAKE_CASE__			:				List[str]       						= translator('''This is a test string''' ,  max_length=2_0					)
         self.assertEqual(
             __SCREAMING_SNAKE_CASE ,  [
                 {
                     '''translation_text''': (
                         '''monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'''
                         ''' urine urine urine urine urine urine urine'''
                     )
                 }
             ] ,  )






      @require_tf
      def    A__(self					)       ->   Dict:
         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= pipeline('''translation_en_to_de''' ,  model='''patrickvonplaten/t5-tiny-random''' ,  framework='''tf'''					)
         SCREAMING_SNAKE_CASE__			:				List[Any]       						= translator('''This is a test string''' ,  max_length=2_0					)
         self.assertEqual(
             __SCREAMING_SNAKE_CASE ,  [
                 {
                     '''translation_text''': (
                         '''monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'''
                         ''' urine urine urine urine urine urine urine'''
                     )
                 }
             ] ,  )






class 						lowercase     (   unittest.TestCase):






      @require_torch
      @slow
      def    A__(self					)       ->   List[Any]:
         # We don't provide a default for this pair
         with self.assertRaises(__SCREAMING_SNAKE_CASE					):
            pipeline(task='''translation_cn_to_ar'''					)

         # but we do for this one
         SCREAMING_SNAKE_CASE__			:				List[Any]       						= pipeline(task='''translation_en_to_de'''					)
         self.assertEqual(translator._preprocess_params['''src_lang'''] ,  '''en'''					)
         self.assertEqual(translator._preprocess_params['''tgt_lang'''] ,  '''de'''					)






      @require_torch
      @slow
      def    A__(self					)       ->   str:
         SCREAMING_SNAKE_CASE__			:				Dict       						= MBartForConditionalGeneration.from_pretrained('''facebook/mbart-large-50-many-to-many-mmt'''					)
         SCREAMING_SNAKE_CASE__			:				Dict       						= MBartaaTokenizerFast.from_pretrained('''facebook/mbart-large-50-many-to-many-mmt'''					)

         SCREAMING_SNAKE_CASE__			:				Any       						= pipeline(task='''translation''' ,  model=__SCREAMING_SNAKE_CASE ,  tokenizer=__SCREAMING_SNAKE_CASE					)
         # Missing src_lang, tgt_lang
         with self.assertRaises(__SCREAMING_SNAKE_CASE					):
            translator('''This is a test'''					)

         SCREAMING_SNAKE_CASE__			:				Tuple       						= translator('''This is a test''' ,  src_lang='''en_XX''' ,  tgt_lang='''ar_AR'''					)
         self.assertEqual(__SCREAMING_SNAKE_CASE ,  [{'''translation_text''': '''هذا إختبار'''}]					)

         SCREAMING_SNAKE_CASE__			:				List[Any]       						= translator('''This is a test''' ,  src_lang='''en_XX''' ,  tgt_lang='''hi_IN'''					)
         self.assertEqual(__SCREAMING_SNAKE_CASE ,  [{'''translation_text''': '''यह एक परीक्षण है'''}]					)

         # src_lang, tgt_lang can be defined at pipeline call time
         SCREAMING_SNAKE_CASE__			:				List[str]       						= pipeline(task='''translation''' ,  model=__SCREAMING_SNAKE_CASE ,  tokenizer=__SCREAMING_SNAKE_CASE ,  src_lang='''en_XX''' ,  tgt_lang='''ar_AR'''					)
         SCREAMING_SNAKE_CASE__			:				Any       						= translator('''This is a test'''					)
         self.assertEqual(__SCREAMING_SNAKE_CASE ,  [{'''translation_text''': '''هذا إختبار'''}]					)






      @require_torch
      def    A__(self					)       ->   Dict:
         SCREAMING_SNAKE_CASE__			:				int       						= '''patrickvonplaten/t5-tiny-random'''
         SCREAMING_SNAKE_CASE__			:				List[Any]       						= pipeline(task='''translation_cn_to_ar''' ,  model=__SCREAMING_SNAKE_CASE					)
         self.assertEqual(translator._preprocess_params['''src_lang'''] ,  '''cn'''					)
         self.assertEqual(translator._preprocess_params['''tgt_lang'''] ,  '''ar'''					)






      @require_torch
      def    A__(self					)       ->   Optional[int]:
         SCREAMING_SNAKE_CASE__			:				Tuple       						= '''patrickvonplaten/t5-tiny-random'''
         with pytest.warns(__SCREAMING_SNAKE_CASE ,  match=R'''.*translation_en_to_de.*'''					):
            SCREAMING_SNAKE_CASE__			:				Any       						= pipeline(task='''translation''' ,  model=__SCREAMING_SNAKE_CASE					)
         self.assertEqual(translator.task ,  '''translation_en_to_de'''					)
         self.assertEqual(translator._preprocess_params['''src_lang'''] ,  '''en'''					)
         self.assertEqual(translator._preprocess_params['''tgt_lang'''] ,  '''de'''					)






      @require_torch
      def    A__(self					)       ->   str:
         with self.assertRaises(__SCREAMING_SNAKE_CASE					):
            pipeline(task='''translation'''					)







'''simple docstring'''





import unittest

import requests

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

from .test_pipelines_common import ANY


if is_torch_available():
       from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
       lowercase__			=	False


if is_vision_available():
       from PIL import Image
else:






       class 						lowercase     :






             @staticmethod
             def    A__(*a__ ,  **a__					)       ->   Optional[Any]:
                pass






@is_pipeline_test
@require_vision
class 						lowercase     (   unittest.TestCase):
      __lowercase 		=  MODEL_FOR_VISION_2_SEQ_MAPPING
      __lowercase 		=  TF_MODEL_FOR_VISION_2_SEQ_MAPPING






      def    A__(self ,  a__ ,  a__ ,  a__					)       ->   Optional[Any]:
         SCREAMING_SNAKE_CASE__			:				Tuple       						= pipeline('''image-to-text''' ,  model=a__ ,  tokenizer=a__ ,  image_processor=a__					)
         SCREAMING_SNAKE_CASE__			:				List[Any]       						= [
             Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					),
             '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
         ]
         return pipe, examples






      def    A__(self ,  a__ ,  a__					)       ->   Any:
         SCREAMING_SNAKE_CASE__			:				Tuple       						= pipe(a__					)
         self.assertEqual(
             a__ ,  [
                 [{'''generated_text''': ANY(a__					)}],
                 [{'''generated_text''': ANY(a__					)}],
             ] ,  )






      @require_tf
      def    A__(self					)       ->   List[str]:
         SCREAMING_SNAKE_CASE__			:				List[str]       						= pipeline('''image-to-text''' ,  model='''hf-internal-testing/tiny-random-vit-gpt2''' ,  framework='''tf'''					)
         SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

         SCREAMING_SNAKE_CASE__			:				str       						= pipe(a__					)
         self.assertEqual(
             a__ ,  [
                 {
                     '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                 },
             ] ,  )

         SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= pipe([image, image]					)
         self.assertEqual(
             a__ ,  [
                 [
                     {
                         '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                     }
                 ],
                 [
                     {
                         '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                     }
                 ],
             ] ,  )

         SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= pipe(a__ ,  max_new_tokens=1					)
         self.assertEqual(
             a__ ,  [{'''generated_text''': '''growth'''}] ,  )






      @require_torch
      def    A__(self					)       ->   Optional[Any]:
         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= pipeline('''image-to-text''' ,  model='''hf-internal-testing/tiny-random-vit-gpt2'''					)
         SCREAMING_SNAKE_CASE__			:				Any       						= '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

         SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= pipe(a__					)
         self.assertEqual(
             a__ ,  [
                 {
                     '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                 },
             ] ,  )

         SCREAMING_SNAKE_CASE__			:				Any       						= pipe([image, image]					)
         self.assertEqual(
             a__ ,  [
                 [
                     {
                         '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                     }
                 ],
                 [
                     {
                         '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                     }
                 ],
             ] ,  )






      @require_torch
      def    A__(self					)       ->   Any:
         SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= pipeline('''image-to-text''' ,  model='''hf-internal-testing/tiny-random-BlipForConditionalGeneration'''					)
         SCREAMING_SNAKE_CASE__			:				int       						= '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
         SCREAMING_SNAKE_CASE__			:				Tuple       						= '''a photo of'''

         SCREAMING_SNAKE_CASE__			:				List[Any]       						= pipe(a__ ,  prompt=a__					)
         self.assertTrue(outputs[0]['''generated_text'''].startswith(a__					)					)






      @slow
      @require_torch
      def    A__(self					)       ->   List[Any]:
         SCREAMING_SNAKE_CASE__			:				Tuple       						= pipeline('''image-to-text''' ,  model='''ydshieh/vit-gpt2-coco-en'''					)
         SCREAMING_SNAKE_CASE__			:				List[str]       						= '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

         SCREAMING_SNAKE_CASE__			:				List[Any]       						= pipe(a__					)
         self.assertEqual(a__ ,  [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]					)

         SCREAMING_SNAKE_CASE__			:				Tuple       						= pipe([image, image]					)
         self.assertEqual(
             a__ ,  [
                 [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}],
                 [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}],
             ] ,  )






      @slow
      @require_torch
      def    A__(self					)       ->   Tuple:
         SCREAMING_SNAKE_CASE__			:				Dict       						= pipeline('''image-to-text''' ,  model='''Salesforce/blip-image-captioning-base'''					)
         SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= '''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= Image.open(requests.get(a__ ,  stream=a__					).raw					)

         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= pipe(a__					)
         self.assertEqual(a__ ,  [{'''generated_text''': '''a pink pokemon pokemon with a blue shirt and a blue shirt'''}]					)






      @slow
      @require_torch
      def    A__(self					)       ->   int:
         SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= pipeline('''image-to-text''' ,  model='''microsoft/git-base-coco'''					)
         SCREAMING_SNAKE_CASE__			:				str       						= '''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
         SCREAMING_SNAKE_CASE__			:				List[str]       						= Image.open(requests.get(a__ ,  stream=a__					).raw					)

         SCREAMING_SNAKE_CASE__			:				str       						= pipe(a__					)
         self.assertEqual(a__ ,  [{'''generated_text''': '''a cartoon of a purple character.'''}]					)






      @slow
      @require_torch
      def    A__(self					)       ->   Dict:
         SCREAMING_SNAKE_CASE__			:				str       						= pipeline('''image-to-text''' ,  model='''Salesforce/blip-image-captioning-base'''					)
         SCREAMING_SNAKE_CASE__			:				List[Any]       						= '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= Image.open(requests.get(a__ ,  stream=a__					).raw					)

         SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= '''a photography of'''

         SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= pipe(a__ ,  prompt=a__					)
         self.assertEqual(a__ ,  [{'''generated_text''': '''a photography of a volcano'''}]					)

         with self.assertRaises(a__					):
            SCREAMING_SNAKE_CASE__			:				Any       						= pipe([image, image] ,  prompt=[prompt, prompt]					)






      @slow
      @require_torch
      def    A__(self					)       ->   Dict:
         SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= pipeline('''image-to-text''' ,  model='''microsoft/git-base-coco'''					)
         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
         SCREAMING_SNAKE_CASE__			:				Dict       						= Image.open(requests.get(a__ ,  stream=a__					).raw					)

         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= '''a photo of a'''

         SCREAMING_SNAKE_CASE__			:				str       						= pipe(a__ ,  prompt=a__					)
         self.assertEqual(a__ ,  [{'''generated_text''': '''a photo of a tent with a tent and a tent in the background.'''}]					)

         with self.assertRaises(a__					):
            SCREAMING_SNAKE_CASE__			:				Dict       						= pipe([image, image] ,  prompt=[prompt, prompt]					)






      @unittest.skipIf(
          not is_torch_greater_or_equal_than_1_11 ,  reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''					)
      @slow
      @require_torch
      def    A__(self					)       ->   Union[str, Any]:
         SCREAMING_SNAKE_CASE__			:				List[str]       						= pipeline('''image-to-text''' ,  model='''google/pix2struct-ai2d-base'''					)
         SCREAMING_SNAKE_CASE__			:				int       						= '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
         SCREAMING_SNAKE_CASE__			:				Any       						= Image.open(requests.get(a__ ,  stream=a__					).raw					)

         SCREAMING_SNAKE_CASE__			:				int       						= '''What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'''

         SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= pipe(a__ ,  prompt=a__					)
         self.assertEqual(a__ ,  [{'''generated_text''': '''ash cloud'''}]					)

         with self.assertRaises(a__					):
            SCREAMING_SNAKE_CASE__			:				Any       						= pipe([image, image] ,  prompt=[prompt, prompt]					)






      @slow
      @require_tf
      def    A__(self					)       ->   str:
         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= pipeline('''image-to-text''' ,  model='''ydshieh/vit-gpt2-coco-en''' ,  framework='''tf'''					)
         SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

         SCREAMING_SNAKE_CASE__			:				Dict       						= pipe(a__					)
         self.assertEqual(a__ ,  [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]					)

         SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= pipe([image, image]					)
         self.assertEqual(
             a__ ,  [
                 [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}],
                 [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}],
             ] ,  )

