






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


							def   _UpperCamelCase				(	self					:  Tuple	,			snake_case					:  str	,			snake_case					:  List[Any]	,			snake_case					:  Tuple							):
													'''simple docstring'''





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


							def   _UpperCamelCase				(	self					:  Union[str, Any]	,			snake_case					:  Union[str, Any]	,			snake_case					:  Union[str, Any]							):
													'''simple docstring'''





													A__  :     str						     =     translator("""Some string"""							)
													self.assertEqual(UpperCAmelCase_	,			[{"""translation_text""": ANY(UpperCAmelCase_							)}]							)

													A__  :     Dict						     =     translator(["""Some string"""]							)
													self.assertEqual(UpperCAmelCase_	,			[{"""translation_text""": ANY(UpperCAmelCase_							)}]							)

													A__  :     str						     =     translator(["""Some string""", """other string"""]							)
													self.assertEqual(UpperCAmelCase_	,			[{"""translation_text""": ANY(UpperCAmelCase_							)}, {"""translation_text""": ANY(UpperCAmelCase_							)}]							)


							@require_torch
							def   _UpperCamelCase				(	self					:  List[str]							):
													'''simple docstring'''





													A__  :     Union[str, Any]						     =     pipeline("""translation_en_to_ro"""	,			model="""patrickvonplaten/t5-tiny-random"""	,			framework="""pt"""							)
													A__  :     Dict						     =     translator("""This is a test string"""	,			max_length=20							)
													self.assertEqual(
													    UpperCAmelCase_	,			[
													        {
													            """translation_text""": (
													                """Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide"""
													                """ Beide Beide"""
													            )
													        }
													    ]	,			)


							@require_tf
							def   _UpperCamelCase				(	self					:  Optional[Any]							):
													'''simple docstring'''





													A__  :     List[str]						     =     pipeline("""translation_en_to_ro"""	,			model="""patrickvonplaten/t5-tiny-random"""	,			framework="""tf"""							)
													A__  :     Union[str, Any]						     =     translator("""This is a test string"""	,			max_length=20							)
													self.assertEqual(
													    UpperCAmelCase_	,			[
													        {
													            """translation_text""": (
													                """Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide"""
													                """ Beide Beide"""
													            )
													        }
													    ]	,			)


							@require_torch
							def   _UpperCamelCase				(	self					:  str							):
													'''simple docstring'''





													A__  :     List[Any]						     =     pipeline("""translation_en_to_de"""	,			model="""patrickvonplaten/t5-tiny-random"""	,			framework="""pt"""							)
													A__  :     Optional[int]						     =     translator("""This is a test string"""	,			max_length=20							)
													self.assertEqual(
													    UpperCAmelCase_	,			[
													        {
													            """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'''





													A__  :     Any						     =     pipeline("""translation_en_to_de"""	,			model="""patrickvonplaten/t5-tiny-random"""	,			framework="""tf"""							)
													A__  :     Dict						     =     translator("""This is a test string"""	,			max_length=20							)
													self.assertEqual(
													    UpperCAmelCase_	,			[
													        {
													            """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						):


							@require_torch
							@slow
							def   _UpperCamelCase				(	self					:  List[Any]							):
													'''simple docstring'''





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

													# but we do for this one
													A__  :     Optional[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   _UpperCamelCase				(	self					:  List[Any]							):
													'''simple docstring'''





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

													A__  :     Dict						     =     pipeline(task="""translation"""	,			model=UpperCAmelCase_	,			tokenizer=UpperCAmelCase_							)
													# Missing src_lang, tgt_lang
													with self.assertRaises(UpperCAmelCase_							):
																			translator("""This is a test"""							)

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

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

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


							@require_torch
							def   _UpperCamelCase				(	self					:  Optional[Any]							):
													'''simple docstring'''





													A__  :     Any						     =     "patrickvonplaten/t5-tiny-random"
													A__  :     Union[str, Any]						     =     pipeline(task="""translation_cn_to_ar"""	,			model=UpperCAmelCase_							)
													self.assertEqual(translator._preprocess_params["""src_lang"""]	,			"""cn"""							)
													self.assertEqual(translator._preprocess_params["""tgt_lang"""]	,			"""ar"""							)


							@require_torch
							def   _UpperCamelCase				(	self					:  Any							):
													'''simple docstring'''





													A__  :     Dict						     =     "patrickvonplaten/t5-tiny-random"
													with pytest.warns(UpperCAmelCase_	,			match=r""".*translation_en_to_de.*"""							):
																			A__  :     Dict						     =     pipeline(task="""translation"""	,			model=UpperCAmelCase_							)
													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					:  List[Any]							):
													'''simple docstring'''





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














import re


def      lowerCamelCase__	(   lowercase ):

  """simple docstring"""






  def docstring_decorator(lowercase ):
    SCREAMING_SNAKE_CASE				:					List[Any]		  =			fn.__doc__
    SCREAMING_SNAKE_CASE				:					Tuple		  =			func_doc.split("\n" )
    SCREAMING_SNAKE_CASE				:					Tuple		  =			0
    while i < len(lowercase ) and re.search(R"^\s*Examples?:\s*$"  ,   lines[i] ) is None:
      i += 1
    if i < len(lowercase ):
      SCREAMING_SNAKE_CASE				:					int		  =			example_docstring
      SCREAMING_SNAKE_CASE				:					Tuple		  =			"\n".join(lowercase )
    else:
      raise ValueError(
          F'''The function {fn} should have an empty \'Examples:\' in its docstring as placeholder, '''
          F'''current docstring is:\n{func_doc}''' )
    SCREAMING_SNAKE_CASE				:					int		  =			func_doc
    return fn

  return docstring_decorator






