
"""simple docstring"""

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 __lowerCAmelCase						( unittest.TestCase     ):
     lowercase		  =		MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
     lowercase		  =		TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase ):

      '''simple docstring'''




      if isinstance(model.config      ,	_lowerCamelCase ):
       __UpperCamelCase							    =			list(tokenizer.lang_code_to_id.keys() )[:2]
       __UpperCamelCase							    =			TranslationPipeline(model=_lowerCamelCase      ,	tokenizer=_lowerCamelCase      ,	src_lang=_lowerCamelCase      ,	tgt_lang=_lowerCamelCase )
      else:
       __UpperCamelCase							    =			TranslationPipeline(model=_lowerCamelCase      ,	tokenizer=_lowerCamelCase )
      return translator, ["Some string", "Some other text"]





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase ):

      '''simple docstring'''




      __UpperCamelCase							    =			translator('Some string' )
      self.assertEqual(_lowerCamelCase      ,	[{'translation_text': ANY(_lowerCamelCase )}] )

      __UpperCamelCase							    =			translator(['Some string'] )
      self.assertEqual(_lowerCamelCase      ,	[{'translation_text': ANY(_lowerCamelCase )}] )

      __UpperCamelCase							    =			translator(['Some string', 'other string'] )
      self.assertEqual(_lowerCamelCase      ,	[{'translation_text': ANY(_lowerCamelCase )}, {'translation_text': ANY(_lowerCamelCase )}] )





     @require_torch
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			pipeline('translation_en_to_ro'      ,	model='patrickvonplaten/t5-tiny-random'      ,	framework='pt' )
      __UpperCamelCase							    =			translator('This is a test string'      ,	max_length=20 )
      self.assertEqual(
          _lowerCamelCase      ,	[
              {
                  'translation_text': (
                      'Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'
                      ' Beide Beide'
                  )
              }
          ]      ,	)





     @require_tf
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			pipeline('translation_en_to_ro'      ,	model='patrickvonplaten/t5-tiny-random'      ,	framework='tf' )
      __UpperCamelCase							    =			translator('This is a test string'      ,	max_length=20 )
      self.assertEqual(
          _lowerCamelCase      ,	[
              {
                  'translation_text': (
                      'Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'
                      ' Beide Beide'
                  )
              }
          ]      ,	)





     @require_torch
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			pipeline('translation_en_to_de'      ,	model='patrickvonplaten/t5-tiny-random'      ,	framework='pt' )
      __UpperCamelCase							    =			translator('This is a test string'      ,	max_length=20 )
      self.assertEqual(
          _lowerCamelCase      ,	[
              {
                  'translation_text': (
                      'monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'
                      ' urine urine urine urine urine urine urine'
                  )
              }
          ]      ,	)







     @require_tf
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			pipeline('translation_en_to_de'      ,	model='patrickvonplaten/t5-tiny-random'      ,	framework='tf' )
      __UpperCamelCase							    =			translator('This is a test string'      ,	max_length=20 )
      self.assertEqual(
          _lowerCamelCase      ,	[
              {
                  'translation_text': (
                      'monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'
                      ' urine urine urine urine urine urine urine'
                  )
              }
          ]      ,	)


class __lowerCAmelCase						( unittest.TestCase     ):





     @require_torch
     @slow
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      with self.assertRaises(_lowerCamelCase ):
       pipeline(task='translation_cn_to_ar' )

      # but we do for this one
      __UpperCamelCase							    =			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 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			MBartForConditionalGeneration.from_pretrained('facebook/mbart-large-50-many-to-many-mmt' )
      __UpperCamelCase							    =			MBartaaTokenizerFast.from_pretrained('facebook/mbart-large-50-many-to-many-mmt' )

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

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

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

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





     @require_torch
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			"patrickvonplaten/t5-tiny-random"
      __UpperCamelCase							    =			pipeline(task='translation_cn_to_ar'      ,	model=_lowerCamelCase )
      self.assertEqual(translator._preprocess_params['src_lang']      ,	'cn' )
      self.assertEqual(translator._preprocess_params['tgt_lang']      ,	'ar' )





     @require_torch
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			"patrickvonplaten/t5-tiny-random"
      with pytest.warns(_lowerCamelCase      ,	match=R'.*translation_en_to_de.*' ):
       __UpperCamelCase							    =			pipeline(task='translation'      ,	model=_lowerCamelCase )
      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 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      with self.assertRaises(_lowerCamelCase ):
       pipeline(task='translation' )





import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
				from PIL import Image
class    _UpperCamelCase(   unittest.TestCase		,						SCREAMING_SNAKE_CASE				):

						def 						a__ (					self		:			Optional[int]			):
													_UpperCAmelCase  :			Any	       =		load_tool("image-captioning"			)
													self.tool.setup()
													_UpperCAmelCase  :			Optional[Any]	       =		load_tool("image-captioning"	,						remote=_lowerCamelCase			)

						def 						a__ (					self		:			int			):
													_UpperCAmelCase  :			Optional[int]	       =		Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
													_UpperCAmelCase  :			Optional[int]	       =		self.tool(_lowerCamelCase			)
													self.assertEqual(_lowerCamelCase	,						"two cats sleeping on a couch"			)

						def 						a__ (					self		:			str			):
													_UpperCAmelCase  :			List[str]	       =		Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
													_UpperCAmelCase  :			Optional[Any]	       =		self.remote_tool(_lowerCamelCase			)
													self.assertEqual(_lowerCamelCase	,						"two cats sleeping on a couch"			)

						def 						a__ (					self		:			Union[str, Any]			):
													_UpperCAmelCase  :			Any	       =		Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
													_UpperCAmelCase  :			Union[str, Any]	       =		self.tool(image=_lowerCamelCase			)
													self.assertEqual(_lowerCamelCase	,						"two cats sleeping on a couch"			)






						def 						a__ (					self		:			Optional[int]			):
													_UpperCAmelCase  :			str	       =		Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"			)			) / "000000039769.png"			)
													_UpperCAmelCase  :			str	       =		self.remote_tool(image=_lowerCamelCase			)
													self.assertEqual(_lowerCamelCase	,						"two cats sleeping on a couch"			)




