


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase__        =				{
    '''configuration_dpr''': ['''DPR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPRConfig'''],
    '''tokenization_dpr''': [
        '''DPRContextEncoderTokenizer''',
        '''DPRQuestionEncoderTokenizer''',
        '''DPRReaderOutput''',
        '''DPRReaderTokenizer''',
    ],
}


try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__        =				[
      '''DPRContextEncoderTokenizerFast''',
      '''DPRQuestionEncoderTokenizerFast''',
      '''DPRReaderTokenizerFast''',
  ]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__        =				[
      '''DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''DPRContextEncoder''',
      '''DPRPretrainedContextEncoder''',
      '''DPRPreTrainedModel''',
      '''DPRPretrainedQuestionEncoder''',
      '''DPRPretrainedReader''',
      '''DPRQuestionEncoder''',
      '''DPRReader''',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__        =				[
      '''TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''TFDPRContextEncoder''',
      '''TFDPRPretrainedContextEncoder''',
      '''TFDPRPretrainedQuestionEncoder''',
      '''TFDPRPretrainedReader''',
      '''TFDPRQuestionEncoder''',
      '''TFDPRReader''',
  ]


if TYPE_CHECKING:
  from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
  from .tokenization_dpr import (
      DPRContextEncoderTokenizer,
      DPRQuestionEncoderTokenizer,
      DPRReaderOutput,
      DPRReaderTokenizer,
  )

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_dpr_fast import (
        DPRContextEncoderTokenizerFast,
        DPRQuestionEncoderTokenizerFast,
        DPRReaderTokenizerFast,
    )

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_dpr import (
        DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPRContextEncoder,
        DPRPretrainedContextEncoder,
        DPRPreTrainedModel,
        DPRPretrainedQuestionEncoder,
        DPRPretrainedReader,
        DPRQuestionEncoder,
        DPRReader,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_dpr import (
        TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFDPRContextEncoder,
        TFDPRPretrainedContextEncoder,
        TFDPRPretrainedQuestionEncoder,
        TFDPRPretrainedReader,
        TFDPRQuestionEncoder,
        TFDPRReader,
    )

else:
  import sys

  UpperCAmelCase__        =				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





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





     def 	__A   (self						,	UpperCAmelCase						,	UpperCAmelCase						,	UpperCAmelCase			)	->  Tuple:
      if isinstance(model.config						,	UpperCAmelCase			):
       _lowercase ,       _lowercase				 =list(tokenizer.lang_code_to_id.keys()			)[:2]
       _lowercase				 =TranslationPipeline(model=UpperCAmelCase						,	tokenizer=UpperCAmelCase						,	src_lang=UpperCAmelCase						,	tgt_lang=UpperCAmelCase			)
      else:
       _lowercase				 =TranslationPipeline(model=UpperCAmelCase						,	tokenizer=UpperCAmelCase			)
      return translator, ["Some string", "Some other text"]





     def 	__A   (self						,	UpperCAmelCase						,	UpperCAmelCase			)	->  List[Any]:
      _lowercase				 =translator('''Some string'''			)
      self.assertEqual(UpperCAmelCase						,	[{'''translation_text''': ANY(UpperCAmelCase			)}]			)

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

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





     @require_torch
     def 	__A   (self			)	->  Optional[Any]:
      _lowercase				 =pipeline('''translation_en_to_ro'''						,	model='''patrickvonplaten/t5-tiny-random'''						,	framework='''pt'''			)
      _lowercase				 =translator('''This is a test string'''						,	max_length=2_0			)
      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 	__A   (self			)	->  Optional[Any]:
      _lowercase				 =pipeline('''translation_en_to_ro'''						,	model='''patrickvonplaten/t5-tiny-random'''						,	framework='''tf'''			)
      _lowercase				 =translator('''This is a test string'''						,	max_length=2_0			)
      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 	__A   (self			)	->  str:
      _lowercase				 =pipeline('''translation_en_to_de'''						,	model='''patrickvonplaten/t5-tiny-random'''						,	framework='''pt'''			)
      _lowercase				 =translator('''This is a test string'''						,	max_length=2_0			)
      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 	__A   (self			)	->  Optional[int]:
      _lowercase				 =pipeline('''translation_en_to_de'''						,	model='''patrickvonplaten/t5-tiny-random'''						,	framework='''tf'''			)
      _lowercase				 =translator('''This is a test string'''						,	max_length=2_0			)
      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  lowerCamelCase__  (			unittest.TestCase):





     @require_torch
     @slow
     def 	__A   (self			)	->  Optional[Any]:
      # 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
      _lowercase				 =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]:
      _lowercase				 =MBartForConditionalGeneration.from_pretrained('''facebook/mbart-large-50-many-to-many-mmt'''			)
      _lowercase				 =MBartaaTokenizerFast.from_pretrained('''facebook/mbart-large-50-many-to-many-mmt'''			)

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

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

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





     @require_torch
     def 	__A   (self			)	->  Tuple:
      _lowercase				 ='''patrickvonplaten/t5-tiny-random'''
      _lowercase				 =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 	__A   (self			)	->  str:
      _lowercase				 ='''patrickvonplaten/t5-tiny-random'''
      with pytest.warns(UpperCAmelCase						,	match=R'''.*translation_en_to_de.*'''			):
       _lowercase				 =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 	__A   (self			)	->  List[str]:
      with self.assertRaises(UpperCAmelCase			):
       pipeline(task='''translation'''			)

