





"""simple docstring"""



import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
       import torch






@is_pipeline_test
class 		__snake_case (		unittest.TestCase					):





 """simple docstring"""


 _lowerCamelCase				=					MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
 _lowerCamelCase				=					TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Tuple									=					TextaTextGenerationPipeline(model=__lowerCamelCase ,						tokenizer=__lowerCamelCase  )
     return generator, ["Something to write", "Something else"]

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						str									=					generator('''Something there'''  )
     self.assertEqual(__lowerCamelCase ,						[{'''generated_text''': ANY(__lowerCamelCase  )}]  )
     # These are encoder decoder, they don't just append to incoming string
     self.assertFalse(outputs[0]['''generated_text'''].startswith('''Something there'''  )  )

     __A   :						Union[str, Any]									=					generator(['''This is great !''', '''Something else'''] ,						num_return_sequences=2 ,						do_sample=__lowerCamelCase  )
     self.assertEqual(
         __lowerCamelCase ,						[
             [{'''generated_text''': ANY(__lowerCamelCase  )}, {'''generated_text''': ANY(__lowerCamelCase  )}],
             [{'''generated_text''': ANY(__lowerCamelCase  )}, {'''generated_text''': ANY(__lowerCamelCase  )}],
         ] ,						)

     __A   :						Any									=					generator(
         ['''This is great !''', '''Something else'''] ,						num_return_sequences=2 ,						batch_size=2 ,						do_sample=__lowerCamelCase  )
     self.assertEqual(
         __lowerCamelCase ,						[
             [{'''generated_text''': ANY(__lowerCamelCase  )}, {'''generated_text''': ANY(__lowerCamelCase  )}],
             [{'''generated_text''': ANY(__lowerCamelCase  )}, {'''generated_text''': ANY(__lowerCamelCase  )}],
         ] ,						)

     with self.assertRaises(__lowerCamelCase  ):
         generator(4  )

 @require_torch
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						int									=					pipeline('''text2text-generation''' ,						model='''patrickvonplaten/t5-tiny-random''' ,						framework='''pt'''  )
     # do_sample=False necessary for reproducibility
     __A   :						Optional[Any]									=					generator('''Something there''' ,						do_sample=__lowerCamelCase  )
     self.assertEqual(__lowerCamelCase ,						[{'''generated_text''': ''''''}]  )

     __A   :						Optional[int]									=					3
     __A   :						Tuple									=					generator(
         '''Something there''' ,						num_return_sequences=__lowerCamelCase ,						num_beams=__lowerCamelCase ,						)
     __A   :						Any									=					[
         {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide Beide'''},
         {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide'''},
         {'''generated_text''': ''''''},
     ]
     self.assertEqual(__lowerCamelCase ,						__lowerCamelCase  )

     __A   :						int									=					generator('''This is a test''' ,						do_sample=__lowerCamelCase ,						num_return_sequences=2 ,						return_tensors=__lowerCamelCase  )
     self.assertEqual(
         __lowerCamelCase ,						[
             {'''generated_token_ids''': ANY(torch.Tensor  )},
             {'''generated_token_ids''': ANY(torch.Tensor  )},
         ] ,						)
     __A   :						List[Any]									=					generator.model.config.eos_token_id
     __A   :						List[str]									=					'''<pad>'''
     __A   :						int									=					generator(
         ['''This is a test''', '''This is a second test'''] ,						do_sample=__lowerCamelCase ,						num_return_sequences=2 ,						batch_size=2 ,						return_tensors=__lowerCamelCase ,						)
     self.assertEqual(
         __lowerCamelCase ,						[
             [
                 {'''generated_token_ids''': ANY(torch.Tensor  )},
                 {'''generated_token_ids''': ANY(torch.Tensor  )},
             ],
             [
                 {'''generated_token_ids''': ANY(torch.Tensor  )},
                 {'''generated_token_ids''': ANY(torch.Tensor  )},
             ],
         ] ,						)







 @require_tf
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						int									=					pipeline('''text2text-generation''' ,						model='''patrickvonplaten/t5-tiny-random''' ,						framework='''tf'''  )
     # do_sample=False necessary for reproducibility
     __A   :						List[Any]									=					generator('''Something there''' ,						do_sample=__lowerCamelCase  )
     self.assertEqual(__lowerCamelCase ,						[{'''generated_text''': ''''''}]  )











"""simple docstring"""



import json
import os
import unittest

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin






class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							unittest.TestCase					):





 """simple docstring"""


 _lowerCamelCase				=					XLMTokenizer
 _lowerCamelCase				=					False

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     super().setUp()

     # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
     __A   :						Tuple									=					[
         '''l''',
         '''o''',
         '''w''',
         '''e''',
         '''r''',
         '''s''',
         '''t''',
         '''i''',
         '''d''',
         '''n''',
         '''w</w>''',
         '''r</w>''',
         '''t</w>''',
         '''lo''',
         '''low''',
         '''er</w>''',
         '''low</w>''',
         '''lowest</w>''',
         '''newer</w>''',
         '''wider</w>''',
         '''<unk>''',
     ]
     __A   :						Dict									=					dict(zip(__lowerCamelCase ,						range(len(__lowerCamelCase  )  )  )  )
     __A   :						Union[str, Any]									=					['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', '''''']

     __A   :						Optional[Any]									=					os.path.join(self.tmpdirname ,						VOCAB_FILES_NAMES['''vocab_file''']  )
     __A   :						Optional[Any]									=					os.path.join(self.tmpdirname ,						VOCAB_FILES_NAMES['''merges_file''']  )
     with open(self.vocab_file ,						'''w'''  ) as fp:
         fp.write(json.dumps(__lowerCamelCase  )  )
     with open(self.merges_file ,						'''w'''  ) as fp:
         fp.write('''\n'''.join(__lowerCamelCase  )  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					'''lower newer'''
     __A   :						int									=					'''lower newer'''
     return input_text, output_text

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					XLMTokenizer(self.vocab_file ,						self.merges_file  )

     __A   :						Optional[Any]									=					'''lower'''
     __A   :						Any									=					['''low''', '''er</w>''']
     __A   :						Tuple									=					tokenizer.tokenize(__lowerCamelCase  )
     self.assertListEqual(__lowerCamelCase ,						__lowerCamelCase  )

     __A   :						str									=					tokens + ['''<unk>''']
     __A   :						List[str]									=					[14, 15, 20]
     self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowerCamelCase  ) ,						__lowerCamelCase  )







 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					XLMTokenizer.from_pretrained('''xlm-mlm-en-2048'''  )

     __A   :						Union[str, Any]									=					tokenizer.encode('''sequence builders''' ,						add_special_tokens=__lowerCamelCase  )
     __A   :						int									=					tokenizer.encode('''multi-sequence build''' ,						add_special_tokens=__lowerCamelCase  )

     __A   :						int									=					tokenizer.build_inputs_with_special_tokens(__lowerCamelCase  )
     __A   :						List[Any]									=					tokenizer.build_inputs_with_special_tokens(__lowerCamelCase ,						__lowerCamelCase  )

     assert encoded_sentence == [0] + text + [1]
     assert encoded_pair == [0] + text + [1] + text_a + [1]




