





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


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


				def _snake_case							(		self ,			UpperCAmelCase_ ,			UpperCAmelCase_				):
								lowerCamelCase									=translator("""Some string"""				)
								self.assertEqual(UpperCAmelCase_ ,			[{"""translation_text""": ANY(UpperCAmelCase_				)}]				)

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

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


				@require_torch
				def _snake_case							(		self				):
								lowerCamelCase									=pipeline("""translation_en_to_ro""" ,			model="""patrickvonplaten/t5-tiny-random""" ,			framework="""pt"""				)
								lowerCamelCase									=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 _snake_case							(		self				):
								lowerCamelCase									=pipeline("""translation_en_to_ro""" ,			model="""patrickvonplaten/t5-tiny-random""" ,			framework="""tf"""				)
								lowerCamelCase									=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 _snake_case							(		self				):
								lowerCamelCase									=pipeline("""translation_en_to_de""" ,			model="""patrickvonplaten/t5-tiny-random""" ,			framework="""pt"""				)
								lowerCamelCase									=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 _snake_case							(		self				):
								lowerCamelCase									=pipeline("""translation_en_to_de""" ,			model="""patrickvonplaten/t5-tiny-random""" ,			framework="""tf"""				)
								lowerCamelCase									=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    __A  (    unittest.TestCase     ):


				@require_torch
				@slow
				def _snake_case							(		self				):
								# We don't provide a default for this pair
								with self.assertRaises(UpperCAmelCase_				):
												pipeline(task="""translation_cn_to_ar"""				)

								# but we do for this one
								lowerCamelCase									=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 _snake_case							(		self				):
								lowerCamelCase									=MBartForConditionalGeneration.from_pretrained("""facebook/mbart-large-50-many-to-many-mmt"""				)
								lowerCamelCase									=MBartaaTokenizerFast.from_pretrained("""facebook/mbart-large-50-many-to-many-mmt"""				)

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

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

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


				@require_torch
				def _snake_case							(		self				):
								lowerCamelCase									="""patrickvonplaten/t5-tiny-random"""
								lowerCamelCase									=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 _snake_case							(		self				):
								lowerCamelCase									="""patrickvonplaten/t5-tiny-random"""
								with pytest.warns(UpperCAmelCase_ ,			match=r""".*translation_en_to_de.*"""				):
												lowerCamelCase									=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 _snake_case							(		self				):
								with self.assertRaises(UpperCAmelCase_				):
												pipeline(task="""translation"""				)












import argparse
import os


UpperCAmelCase__    :      Any       					={
    '''pytorch''': {
        '''1.13''': {
            '''torch''': '''1.13.1''',
            '''torchvision''': '''0.14.1''',
            '''torchaudio''': '''0.13.1''',
            '''python''': 3.9,
            '''cuda''': '''cu116''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu116'''
            ),
            '''base_image''': '''nvidia/cuda:11.6.2-cudnn8-devel-ubuntu20.04''',
        },
        '''1.12''': {
            '''torch''': '''1.12.1''',
            '''torchvision''': '''0.13.1''',
            '''torchaudio''': '''0.12.1''',
            '''python''': 3.9,
            '''cuda''': '''cu113''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu113'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''1.11''': {
            '''torch''': '''1.11.0''',
            '''torchvision''': '''0.12.0''',
            '''torchaudio''': '''0.11.0''',
            '''python''': 3.9,
            '''cuda''': '''cu113''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu113'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''1.10''': {
            '''torch''': '''1.10.2''',
            '''torchvision''': '''0.11.3''',
            '''torchaudio''': '''0.10.2''',
            '''python''': 3.9,
            '''cuda''': '''cu113''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.10.2 torchvision==0.11.3 torchaudio==0.10.2'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu113'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        # torchaudio < 0.10 has no CUDA-enabled binary distributions
        '''1.9''': {
            '''torch''': '''1.9.1''',
            '''torchvision''': '''0.10.1''',
            '''torchaudio''': '''0.9.1''',
            '''python''': 3.9,
            '''cuda''': '''cu111''',
            '''install''': (
                '''python3 -m pip install --no-cache-dir -U torch==1.9.1 torchvision==0.10.1 torchaudio==0.9.1'''
                ''' --extra-index-url https://download.pytorch.org/whl/cu111'''
            ),
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
    },
    '''tensorflow''': {
        '''2.11''': {
            '''tensorflow''': '''2.11.1''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.11.1''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.10''': {
            '''tensorflow''': '''2.10.1''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.10.1''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.9''': {
            '''tensorflow''': '''2.9.3''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.9.3''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.8''': {
            '''tensorflow''': '''2.8.2''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.8.2''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.7''': {
            '''tensorflow''': '''2.7.3''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.7.3''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.6''': {
            '''tensorflow''': '''2.6.5''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.6.5''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
        '''2.5''': {
            '''tensorflow''': '''2.5.3''',
            '''install''': '''python3 -m pip install --no-cache-dir -U tensorflow==2.5.3''',
            '''base_image''': '''nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04''',
        },
    },
}


if __name__ == "__main__":
							UpperCAmelCase__    :      Tuple       					=argparse.ArgumentParser('''Choose the framework and version to install''')
							parser.add_argument(
							    '''--framework''', help='''The framework to install. Should be `torch` or `tensorflow`''', type=str, required=True
							)
							parser.add_argument('''--version''', help='''The version of the framework to install.''', type=str, required=True)
							UpperCAmelCase__    :      List[str]       					=parser.parse_args()

							UpperCAmelCase__    :      Dict       					=past_versions_testing[args.framework][args.version]

							os.system(F"echo \"export INSTALL_CMD=\'{info['install']}\'\" >> ~/.profile")
							print(F"echo \"export INSTALL_CMD=\'{info['install']}\'\" >> ~/.profile")

							UpperCAmelCase__    :      List[Any]       					=''''''
							if args.framework == "pytorch":
														UpperCAmelCase__    :      Optional[int]       					=info['''cuda''']
							os.system(F"echo \"export CUDA='{cuda}'\" >> ~/.profile")
							print(F"echo \"export CUDA='{cuda}'\" >> ~/.profile")





