





'''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    __a (unittest.TestCase       ):
							__a   :				Union[str, Any]       =    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
							__a   :				Tuple       =    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

							def       UpperCAmelCase__		(							self							:       Optional[int]    ,       __magic_name__							:       Union[str, Any]    ,       __magic_name__							:       Dict    ,       __magic_name__							:       List[Any]				)   ->    Union[str, Any]:







													"""simple docstring"""
													if isinstance(model.config    ,       __magic_name__				):
																			UpperCAmelCase_     : str         =					list(tokenizer.lang_code_to_id.keys()				)[:2]
																			UpperCAmelCase_     : Optional[int]         =					TranslationPipeline(model=__magic_name__    ,       tokenizer=__magic_name__    ,       src_lang=__magic_name__    ,       tgt_lang=__magic_name__				)
													else:
																			UpperCAmelCase_     : str         =					TranslationPipeline(model=__magic_name__    ,       tokenizer=__magic_name__				)
													return translator, ["Some string", "Some other text"]

							def       UpperCAmelCase__		(							self							:       Dict    ,       __magic_name__							:       int    ,       __magic_name__							:       Dict				)   ->    Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_     : List[Any]         =					translator('''Some string'''				)
													self.assertEqual(__magic_name__    ,       [{'''translation_text''': ANY(__magic_name__				)}]				)

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

													UpperCAmelCase_     : int         =					translator(['''Some string''', '''other string''']				)
													self.assertEqual(__magic_name__    ,       [{'''translation_text''': ANY(__magic_name__				)}, {'''translation_text''': ANY(__magic_name__				)}]				)

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







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







													"""simple docstring"""
													UpperCAmelCase_     : Tuple         =					pipeline('''translation_en_to_ro'''    ,       model='''patrickvonplaten/t5-tiny-random'''    ,       framework='''tf'''				)
													UpperCAmelCase_     : Dict         =					translator('''This is a test string'''    ,       max_length=20				)
													self.assertEqual(
													    __magic_name__    ,       [
													        {
													            '''translation_text''': (
													                '''Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'''
													                ''' Beide Beide'''
													            )
													        }
													    ]    ,       )

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







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







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







													"""simple docstring"""
													# We don't provide a default for this pair
													with self.assertRaises(__magic_name__				):
																			pipeline(task='''translation_cn_to_ar'''				)

													# but we do for this one
													UpperCAmelCase_     : 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							:       str				)   ->    Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_     : Union[str, Any]         =					MBartForConditionalGeneration.from_pretrained('''facebook/mbart-large-50-many-to-many-mmt'''				)
													UpperCAmelCase_     : Any         =					MBartaaTokenizerFast.from_pretrained('''facebook/mbart-large-50-many-to-many-mmt'''				)

													UpperCAmelCase_     : List[str]         =					pipeline(task='''translation'''    ,       model=__magic_name__    ,       tokenizer=__magic_name__				)
													# Missing src_lang, tgt_lang
													with self.assertRaises(__magic_name__				):
																			translator('''This is a test'''				)

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

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

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

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







													"""simple docstring"""
													UpperCAmelCase_     : Any         =					'''patrickvonplaten/t5-tiny-random'''
													UpperCAmelCase_     : List[Any]         =					pipeline(task='''translation_cn_to_ar'''    ,       model=__magic_name__				)
													self.assertEqual(translator._preprocess_params['''src_lang''']    ,       '''cn'''				)
													self.assertEqual(translator._preprocess_params['''tgt_lang''']    ,       '''ar'''				)

							@require_torch
							def       UpperCAmelCase__		(							self							:       Optional[Any]				)   ->    Optional[Any]:







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







													"""simple docstring"""
													with self.assertRaises(__magic_name__				):
																			pipeline(task='''translation'''				)








'''simple docstring'''







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import ConvNextImageProcessor


class    __a (unittest.TestCase       ):

							def __init__(							self							:       Dict    ,       __magic_name__							:       List[str]    ,       __magic_name__							:       str=7    ,       __magic_name__							:       Optional[int]=3    ,       __magic_name__							:       Union[str, Any]=18    ,       __magic_name__							:       Dict=30    ,       __magic_name__							:       List[Any]=4_00    ,       __magic_name__							:       Dict=True    ,       __magic_name__							:       Union[str, Any]=None    ,       __magic_name__							:       Optional[Any]=0.8_7_5    ,       __magic_name__							:       List[Any]=True    ,       __magic_name__							:       Optional[int]=[0.5, 0.5, 0.5]    ,       __magic_name__							:       Optional[int]=[0.5, 0.5, 0.5]    ,       )   ->    Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_     : str         =					size if size is not None else {'''shortest_edge''': 20}
													UpperCAmelCase_     : Optional[Any]         =					parent
													UpperCAmelCase_     : Any         =					batch_size
													UpperCAmelCase_     : Optional[Any]         =					num_channels
													UpperCAmelCase_     : Optional[Any]         =					image_size
													UpperCAmelCase_     : Optional[int]         =					min_resolution
													UpperCAmelCase_     : Optional[Any]         =					max_resolution
													UpperCAmelCase_     : Optional[int]         =					do_resize
													UpperCAmelCase_     : Optional[Any]         =					size
													UpperCAmelCase_     : int         =					crop_pct
													UpperCAmelCase_     : Optional[int]         =					do_normalize
													UpperCAmelCase_     : Dict         =					image_mean
													UpperCAmelCase_     : int         =					image_std



							def       UpperCAmelCase__		(							self							:       Optional[int]				)   ->    int:







													"""simple docstring"""
													return {
													    "image_mean": self.image_mean,
													    "image_std": self.image_std,
													    "do_normalize": self.do_normalize,
													    "do_resize": self.do_resize,
													    "size": self.size,
													    "crop_pct": self.crop_pct,
													}





@require_torch
@require_vision
class    __a (lowerCamelCase    ,						unittest.TestCase       ):
							__a   :				Dict       =    ConvNextImageProcessor if is_vision_available() else None

							def       UpperCAmelCase__		(							self							:       Optional[int]				)   ->    Dict:







													"""simple docstring"""
													UpperCAmelCase_     : str         =					ConvNextImageProcessingTester(self				)

							@property
							def       UpperCAmelCase__		(							self							:       List[Any]				)   ->    Any:







													"""simple docstring"""
													return self.image_processor_tester.prepare_image_processor_dict()

							def       UpperCAmelCase__		(							self							:       List[Any]				)   ->    Tuple:







													"""simple docstring"""
													UpperCAmelCase_     : Dict         =					self.image_processing_class(**self.image_processor_dict				)
													self.assertTrue(hasattr(__magic_name__    ,       '''do_resize'''				)				)
													self.assertTrue(hasattr(__magic_name__    ,       '''size'''				)				)
													self.assertTrue(hasattr(__magic_name__    ,       '''crop_pct'''				)				)
													self.assertTrue(hasattr(__magic_name__    ,       '''do_normalize'''				)				)
													self.assertTrue(hasattr(__magic_name__    ,       '''image_mean'''				)				)
													self.assertTrue(hasattr(__magic_name__    ,       '''image_std'''				)				)

							def       UpperCAmelCase__		(							self							:       Dict				)   ->    List[Any]:







													"""simple docstring"""
													UpperCAmelCase_     : List[str]         =					self.image_processing_class.from_dict(self.image_processor_dict				)
													self.assertEqual(image_processor.size    ,       {'''shortest_edge''': 20}				)

													UpperCAmelCase_     : List[Any]         =					self.image_processing_class.from_dict(self.image_processor_dict    ,       size=42				)
													self.assertEqual(image_processor.size    ,       {'''shortest_edge''': 42}				)

							def       UpperCAmelCase__		(							self							:       str				)   ->    Optional[int]:







													"""simple docstring"""
													pass

							def       UpperCAmelCase__		(							self							:       List[str]				)   ->    int:







													"""simple docstring"""
													# Initialize image_processing
													UpperCAmelCase_     : int         =					self.image_processing_class(**self.image_processor_dict				)
													# create random PIL images
													UpperCAmelCase_     : List[Any]         =					prepare_image_inputs(self.image_processor_tester    ,       equal_resolution=__magic_name__				)
													for image in image_inputs:
																			self.assertIsInstance(__magic_name__    ,       Image.Image				)

													# Test not batched input
													UpperCAmelCase_     : int         =					image_processing(image_inputs[0]    ,       return_tensors='''pt'''				).pixel_values
													self.assertEqual(
													    encoded_images.shape    ,       (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''shortest_edge'''],
													        self.image_processor_tester.size['''shortest_edge'''],
													    )    ,       )

													# Test batched
													UpperCAmelCase_     : Tuple         =					image_processing(__magic_name__    ,       return_tensors='''pt'''				).pixel_values
													self.assertEqual(
													    encoded_images.shape    ,       (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''shortest_edge'''],
													        self.image_processor_tester.size['''shortest_edge'''],
													    )    ,       )

							def       UpperCAmelCase__		(							self							:       str				)   ->    Optional[int]:







													"""simple docstring"""
													# Initialize image_processing
													UpperCAmelCase_     : Optional[Any]         =					self.image_processing_class(**self.image_processor_dict				)
													# create random numpy tensors
													UpperCAmelCase_     : Tuple         =					prepare_image_inputs(self.image_processor_tester    ,       equal_resolution=__magic_name__    ,       numpify=__magic_name__				)
													for image in image_inputs:
																			self.assertIsInstance(__magic_name__    ,       np.ndarray				)

													# Test not batched input
													UpperCAmelCase_     : int         =					image_processing(image_inputs[0]    ,       return_tensors='''pt'''				).pixel_values
													self.assertEqual(
													    encoded_images.shape    ,       (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''shortest_edge'''],
													        self.image_processor_tester.size['''shortest_edge'''],
													    )    ,       )

													# Test batched
													UpperCAmelCase_     : Union[str, Any]         =					image_processing(__magic_name__    ,       return_tensors='''pt'''				).pixel_values
													self.assertEqual(
													    encoded_images.shape    ,       (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''shortest_edge'''],
													        self.image_processor_tester.size['''shortest_edge'''],
													    )    ,       )



							def       UpperCAmelCase__		(							self							:       str				)   ->    int:







													"""simple docstring"""
													# Initialize image_processing
													UpperCAmelCase_     : Union[str, Any]         =					self.image_processing_class(**self.image_processor_dict				)
													# create random PyTorch tensors
													UpperCAmelCase_     : List[Any]         =					prepare_image_inputs(self.image_processor_tester    ,       equal_resolution=__magic_name__    ,       torchify=__magic_name__				)
													for image in image_inputs:
																			self.assertIsInstance(__magic_name__    ,       torch.Tensor				)

													# Test not batched input
													UpperCAmelCase_     : List[Any]         =					image_processing(image_inputs[0]    ,       return_tensors='''pt'''				).pixel_values
													self.assertEqual(
													    encoded_images.shape    ,       (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''shortest_edge'''],
													        self.image_processor_tester.size['''shortest_edge'''],
													    )    ,       )

													# Test batched
													UpperCAmelCase_     : str         =					image_processing(__magic_name__    ,       return_tensors='''pt'''				).pixel_values
													self.assertEqual(
													    encoded_images.shape    ,       (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''shortest_edge'''],
													        self.image_processor_tester.size['''shortest_edge'''],
													    )    ,       )





