


"""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 			SCREAMING_SNAKE_CASE_  (   unittest.TestCase):




							'''simple docstring'''





							__magic_name__					:     int				  =  MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
							__magic_name__					:     int				  =  TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING




							def        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__	,					lowerCamelCase__)   ->	Optional[Any]:







								'''simple docstring'''




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




							def        UpperCAmelCase (						self	,					lowerCamelCase__	,					lowerCamelCase__)   ->	Dict:







								'''simple docstring'''




								snake_case__  :						Union[str, Any]		=      translator("Some string")
								self.assertEqual(lowerCamelCase__	,					[{"translation_text": ANY(lowerCamelCase__)}])

								snake_case__  :						Union[str, Any]		=      translator(["Some string"])
								self.assertEqual(lowerCamelCase__	,					[{"translation_text": ANY(lowerCamelCase__)}])

								snake_case__  :						Optional[Any]		=      translator(["Some string", "other string"])
								self.assertEqual(lowerCamelCase__	,					[{"translation_text": ANY(lowerCamelCase__)}, {"translation_text": ANY(lowerCamelCase__)}])




							@require_torch
							def        UpperCAmelCase (						self)   ->	List[Any]:







								'''simple docstring'''




								snake_case__  :						Dict		=      pipeline("translation_en_to_ro"	,					model="patrickvonplaten/t5-tiny-random"	,					framework="pt")
								snake_case__  :						Optional[Any]		=      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)   ->	Union[str, Any]:







								'''simple docstring'''




								snake_case__  :						str		=      pipeline("translation_en_to_ro"	,					model="patrickvonplaten/t5-tiny-random"	,					framework="tf")
								snake_case__  :						Union[str, Any]		=      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)   ->	int:







								'''simple docstring'''




								snake_case__  :						Optional[int]		=      pipeline("translation_en_to_de"	,					model="patrickvonplaten/t5-tiny-random"	,					framework="pt")
								snake_case__  :						List[str]		=      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)   ->	Optional[Any]:







								'''simple docstring'''




								snake_case__  :						int		=      pipeline("translation_en_to_de"	,					model="patrickvonplaten/t5-tiny-random"	,					framework="tf")
								snake_case__  :						List[str]		=      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 			SCREAMING_SNAKE_CASE_  (   unittest.TestCase):




							'''simple docstring'''





							@require_torch
							@slow
							def        UpperCAmelCase (						self)   ->	Optional[int]:







								'''simple docstring'''




								with self.assertRaises(lowerCamelCase__):
									pipeline(task="translation_cn_to_ar")

								# but we do for this one
								snake_case__  :						str		=      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)   ->	Union[str, Any]:







								'''simple docstring'''




								snake_case__  :						Optional[int]		=      MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
								snake_case__  :						Any		=      MBartaaTokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")

								snake_case__  :						Union[str, Any]		=      pipeline(task="translation"	,					model=lowerCamelCase__	,					tokenizer=lowerCamelCase__)
								# Missing src_lang, tgt_lang
								with self.assertRaises(lowerCamelCase__):
									translator("This is a test")

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

								snake_case__  :						int		=      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
								snake_case__  :						Optional[Any]		=      pipeline(task="translation"	,					model=lowerCamelCase__	,					tokenizer=lowerCamelCase__	,					src_lang="en_XX"	,					tgt_lang="ar_AR")
								snake_case__  :						Union[str, Any]		=      translator("This is a test")
								self.assertEqual(lowerCamelCase__	,					[{"translation_text": "هذا إختبار"}])




							@require_torch
							def        UpperCAmelCase (						self)   ->	Dict:







								'''simple docstring'''




								snake_case__  :						List[Any]		=      "patrickvonplaten/t5-tiny-random"
								snake_case__  :						Optional[Any]		=      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)   ->	Dict:







								'''simple docstring'''




								snake_case__  :						List[Any]		=      "patrickvonplaten/t5-tiny-random"
								with pytest.warns(lowerCamelCase__	,					match=R".*translation_en_to_de.*"):
									snake_case__  :						int		=      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)   ->	Tuple:







								'''simple docstring'''




								with self.assertRaises(lowerCamelCase__):
									pipeline(task="translation")









"""simple docstring"""






from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class 			SCREAMING_SNAKE_CASE_  (   _lowercase):




							'''simple docstring'''





							__magic_name__					:     Union[str, Any]				  =  ['''image_processor''', '''tokenizer''']
							__magic_name__					:     Optional[int]				  =  '''EfficientNetImageProcessor'''
							__magic_name__					:     Optional[Any]				  =  ('''BertTokenizer''', '''BertTokenizerFast''')




							def __init__(						self	,					lowerCamelCase__	,					lowerCamelCase__)   ->	str:







								'''simple docstring'''




								super().__init__(lowerCamelCase__	,					lowerCamelCase__)




							def __call__(						self	,					lowerCamelCase__=None	,					lowerCamelCase__=None	,					lowerCamelCase__="max_length"	,					lowerCamelCase__=64	,					lowerCamelCase__=None	,					**lowerCamelCase__)   ->	List[str]:







								'''simple docstring'''




								if text is None and images is None:
									raise ValueError("You have to specify either text or images. Both cannot be none.")

								if text is not None:
									snake_case__  :						int		=      self.tokenizer(
									    lowerCamelCase__	,					padding=lowerCamelCase__	,					max_length=lowerCamelCase__	,					return_tensors=lowerCamelCase__	,					**lowerCamelCase__)

								if images is not None:
									snake_case__  :						int		=      self.image_processor(lowerCamelCase__	,					return_tensors=lowerCamelCase__	,					**lowerCamelCase__)

								if text is not None and images is not None:
									snake_case__  :						List[str]		=      image_features.pixel_values
									return encoding
								elif text is not None:
									return encoding
								else:
									return BatchEncoding(data=dict(**lowerCamelCase__)	,					tensor_type=lowerCamelCase__)




							def        UpperCAmelCase (						self	,					*lowerCamelCase__	,					**lowerCamelCase__)   ->	int:







								'''simple docstring'''




								return self.tokenizer.batch_decode(*lowerCamelCase__	,					**lowerCamelCase__)




							def        UpperCAmelCase (						self	,					*lowerCamelCase__	,					**lowerCamelCase__)   ->	Optional[Any]:







								'''simple docstring'''




								return self.tokenizer.decode(*lowerCamelCase__	,					**lowerCamelCase__)







							@property
							def        UpperCAmelCase (						self)   ->	Tuple:







								'''simple docstring'''




								snake_case__  :						Dict		=      self.tokenizer.model_input_names
								snake_case__  :						Optional[int]		=      self.image_processor.model_input_names
								return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))





