

'''simple docstring'''
import json
import os
import tempfile

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


if is_torch_available():
     import numpy as np
     import torch

if is_vision_available():
     from PIL import Image


def  _lowerCAmelCase      (		_UpperCamelCase		:  int							,				_UpperCamelCase		:  Tuple=False							,				_UpperCamelCase		:  str=False							,				_UpperCamelCase		:  int=False       )   ->    List[str]:
     """simple docstring"""




     assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

     _SCREAMING_SNAKE_CASE												=[]
     for i in range(image_processor_tester.batch_size       ):
          if equal_resolution:
               _SCREAMING_SNAKE_CASE												=_SCREAMING_SNAKE_CASE												=image_processor_tester.max_resolution
          else:
               # To avoid getting image width/height 0
               _SCREAMING_SNAKE_CASE												=image_processor_tester.min_resolution
               if getattr(_A							,				'size_divisor'							,				_A       ):
                    # If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
                    _SCREAMING_SNAKE_CASE												=max(image_processor_tester.size_divisor							,				_A       )
               _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE												=np.random.choice(np.arange(_A							,				image_processor_tester.max_resolution       )							,				2       )
          image_inputs.append(
              np.random.randint(2_55							,				size=(image_processor_tester.num_channels, width, height)							,				dtype=np.uinta       )       )

     if not numpify and not torchify:
          # PIL expects the channel dimension as last dimension
          _SCREAMING_SNAKE_CASE												=[Image.fromarray(np.moveaxis(_A							,				0							,				-1       )       ) for image in image_inputs]

     if torchify:
          _SCREAMING_SNAKE_CASE												=[torch.from_numpy(_A       ) for image in image_inputs]

     return image_inputs


def  _lowerCAmelCase      (		_UpperCamelCase		:  Union[str, Any]							,				_UpperCamelCase		:  int=10							,				_UpperCamelCase		:  Optional[Any]=10							,				_UpperCamelCase		:  Dict=False							,				_UpperCamelCase		:  List[str]=False       )   ->    List[Any]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE												=[]
     for i in range(image_processor_tester.num_frames       ):
          video.append(np.random.randint(2_55							,				size=(image_processor_tester.num_channels, width, height)							,				dtype=np.uinta       )       )

     if not numpify and not torchify:
          # PIL expects the channel dimension as last dimension
          _SCREAMING_SNAKE_CASE												=[Image.fromarray(np.moveaxis(_A							,				0							,				-1       )       ) for frame in video]

     if torchify:
          _SCREAMING_SNAKE_CASE												=[torch.from_numpy(_A       ) for frame in video]

     return video


def  _lowerCAmelCase      (		_UpperCamelCase		:  List[Any]							,				_UpperCamelCase		:  Any=False							,				_UpperCamelCase		:  Any=False							,				_UpperCamelCase		:  Dict=False       )   ->    Optional[Any]:
     """simple docstring"""




     assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

     _SCREAMING_SNAKE_CASE												=[]
     for i in range(image_processor_tester.batch_size       ):
          if equal_resolution:
               _SCREAMING_SNAKE_CASE												=_SCREAMING_SNAKE_CASE												=image_processor_tester.max_resolution
          else:
               _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE												=np.random.choice(
                   np.arange(image_processor_tester.min_resolution							,				image_processor_tester.max_resolution       )							,				2       )
               _SCREAMING_SNAKE_CASE												=prepare_video(
                   image_processor_tester=_A							,				width=_A							,				height=_A							,				numpify=_A							,				torchify=_A							,				)
          video_inputs.append(_A       )

     return video_inputs







class 							A__					:
  A__					=       None





  def 		A						(	self   :			Dict				)			->					int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.image_processing_class(**self.image_processor_dict				)
       _SCREAMING_SNAKE_CASE												=json.loads(image_processor.to_json_string()				)
       for key, value in self.image_processor_dict.items():
            self.assertEqual(obj[key]							,      lowercase_				)





  def 		A						(	self   :			List[Any]				)			->					List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.image_processing_class(**self.image_processor_dict				)

       with tempfile.TemporaryDirectory() as tmpdirname:
            _SCREAMING_SNAKE_CASE												=os.path.join(lowercase_							,      'image_processor.json'				)
            image_processor_first.to_json_file(lowercase_				)
            _SCREAMING_SNAKE_CASE												=self.image_processing_class.from_json_file(lowercase_				)

       self.assertEqual(image_processor_second.to_dict()							,      image_processor_first.to_dict()				)





  def 		A						(	self   :			Any				)			->					Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.image_processing_class(**self.image_processor_dict				)

       with tempfile.TemporaryDirectory() as tmpdirname:
            _SCREAMING_SNAKE_CASE												=image_processor_first.save_pretrained(lowercase_				)[0]
            check_json_file_has_correct_format(lowercase_				)
            _SCREAMING_SNAKE_CASE												=self.image_processing_class.from_pretrained(lowercase_				)

       self.assertEqual(image_processor_second.to_dict()							,      image_processor_first.to_dict()				)





  def 		A						(	self   :			List[Any]				)			->					Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=self.image_processing_class()
       self.assertIsNotNone(lowercase_				)





  @require_torch
  @require_vision
  def 		A						(	self   :			Optional[Any]				)			->					Dict:





       '''simple docstring'''






       if self.test_cast_dtype is not None:
            # Initialize image_processor
            _SCREAMING_SNAKE_CASE												=self.image_processing_class(**self.image_processor_dict				)

            # create random PyTorch tensors
            _SCREAMING_SNAKE_CASE												=prepare_image_inputs(self.image_processor_tester							,      equal_resolution=lowercase_							,      torchify=lowercase_				)

            _SCREAMING_SNAKE_CASE												=image_processor(lowercase_							,      return_tensors='pt'				)
            # for layoutLM compatiblity
            self.assertEqual(encoding.pixel_values.device							,      torch.device('cpu'				)				)
            self.assertEqual(encoding.pixel_values.dtype							,      torch.floataa				)

            _SCREAMING_SNAKE_CASE												=image_processor(lowercase_							,      return_tensors='pt'				).to(torch.floataa				)
            self.assertEqual(encoding.pixel_values.device							,      torch.device('cpu'				)				)
            self.assertEqual(encoding.pixel_values.dtype							,      torch.floataa				)

            _SCREAMING_SNAKE_CASE												=image_processor(lowercase_							,      return_tensors='pt'				).to('cpu'							,      torch.bfloataa				)
            self.assertEqual(encoding.pixel_values.device							,      torch.device('cpu'				)				)
            self.assertEqual(encoding.pixel_values.dtype							,      torch.bfloataa				)

            with self.assertRaises(lowercase_				):
                 _SCREAMING_SNAKE_CASE												=image_processor(lowercase_							,      return_tensors='pt'				).to(torch.bfloataa							,      'cpu'				)

            # Try with text + image feature
            _SCREAMING_SNAKE_CASE												=image_processor(lowercase_							,      return_tensors='pt'				)
            encoding.update({'input_ids': torch.LongTensor([[1, 2, 3], [4, 5, 6]]				)}				)
            _SCREAMING_SNAKE_CASE												=encoding.to(torch.floataa				)

            self.assertEqual(encoding.pixel_values.device							,      torch.device('cpu'				)				)
            self.assertEqual(encoding.pixel_values.dtype							,      torch.floataa				)
            self.assertEqual(encoding.input_ids.dtype							,      torch.long				)









'''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__					=       MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
  A__					=       TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING





  def 		A						(	self   :			Optional[Any]							,      _a   :			Tuple							,      _a   :			Union[str, Any]							,      _a   :			str				)			->					List[Any]:





       '''simple docstring'''






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





  def 		A						(	self   :			Union[str, Any]							,      _a   :			List[Any]							,      _a   :			Optional[Any]				)			->					List[Any]:





       '''simple docstring'''






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

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

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





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





       '''simple docstring'''






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





  @require_tf
  def 		A						(	self   :			Tuple				)			->					Union[str, Any]:





       '''simple docstring'''






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





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





       '''simple docstring'''






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





  @require_tf
  def 		A						(	self   :			Dict				)			->					Tuple:





       '''simple docstring'''






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





       '''simple docstring'''






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

       # but we do for this one
       _SCREAMING_SNAKE_CASE												=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 		A						(	self   :			Optional[Any]				)			->					List[Any]:





       '''simple docstring'''






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

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

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

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

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





  @require_torch
  def 		A						(	self   :			int				)			->					int:





       '''simple docstring'''






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





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												='patrickvonplaten/t5-tiny-random'
       with pytest.warns(_a							,      match=r'.*translation_en_to_de.*'				):
            _SCREAMING_SNAKE_CASE												=pipeline(task='translation'							,      model=_a				)
       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 		A						(	self   :			Any				)			->					Dict:





       '''simple docstring'''






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