





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers
from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roformer import RoFormerTokenizer
from .tokenization_utils import JiebaPreTokenizer


lowercase    : Optional[int]    	=  logging.get_logger(__name__)

lowercase    : Union[str, Any]    	=  {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

lowercase    : Union[str, Any]    	=  {
    """vocab_file""": {
        """junnyu/roformer_chinese_small""": """https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt""",
        """junnyu/roformer_chinese_base""": """https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt""",
        """junnyu/roformer_chinese_char_small""": (
            """https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt"""
        ),
        """junnyu/roformer_chinese_char_base""": (
            """https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt"""
        ),
        """junnyu/roformer_small_discriminator""": (
            """https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt"""
        ),
        """junnyu/roformer_small_generator""": (
            """https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt"""
        ),
    }
}

lowercase    : str    	=  {
    """junnyu/roformer_chinese_small""": 1536,
    """junnyu/roformer_chinese_base""": 1536,
    """junnyu/roformer_chinese_char_small""": 512,
    """junnyu/roformer_chinese_char_base""": 512,
    """junnyu/roformer_small_discriminator""": 128,
    """junnyu/roformer_small_generator""": 128,
}


lowercase    : int    	=  {
    """junnyu/roformer_chinese_small""": {"""do_lower_case""": True},
    """junnyu/roformer_chinese_base""": {"""do_lower_case""": True},
    """junnyu/roformer_chinese_char_small""": {"""do_lower_case""": True},
    """junnyu/roformer_chinese_char_base""": {"""do_lower_case""": True},
    """junnyu/roformer_small_discriminator""": {"""do_lower_case""": True},
    """junnyu/roformer_small_generator""": {"""do_lower_case""": True},
}

class  __A(						__UpperCAmelCase ):
     __A	  =       VOCAB_FILES_NAMES
     __A	  =       PRETRAINED_VOCAB_FILES_MAP
     __A	  =       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     __A	  =       PRETRAINED_INIT_CONFIGURATION
     __A	  =       RoFormerTokenizer

     def __init__(      self,       A=None,       A=None,       A=True,       A="[UNK]",       A="[SEP]",       A="[PAD]",       A="[CLS]",       A="[MASK]",       A=True,       A=None,       **A,       ):





            """simple docstring"""

            super().__init__(
                A,       tokenizer_file=A,       do_lower_case=A,       unk_token=A,       sep_token=A,       pad_token=A,       cls_token=A,       mask_token=A,       tokenize_chinese_chars=A,       strip_accents=A,       **A,       )

            _UpperCamelCase              =						json.loads(self.backend_tokenizer.normalizer.__getstate__()      )
            if (
                pre_tok_state.get('''lowercase''',       A      ) != do_lower_case
                or pre_tok_state.get('''strip_accents''',       A      ) != strip_accents
            ):
                   _UpperCamelCase              =						getattr(A,       pre_tok_state.pop('''type'''      )      )
                   _UpperCamelCase              =						do_lower_case
                   _UpperCamelCase              =						strip_accents
                   _UpperCamelCase              =						pre_tok_class(**A      )

            _UpperCamelCase              =						do_lower_case

     def __getstate__(      self      ):





            """simple docstring"""

            _UpperCamelCase              =						self.__dict__.copy()
            _UpperCamelCase              =						BertPreTokenizer()
            return state

     def __setstate__(      self,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						d
            _UpperCamelCase              =						self.__dict__['''_tokenizer'''].get_vocab()
            _UpperCamelCase              =						PreTokenizer.custom(JiebaPreTokenizer(A      )      )

     def 	_UpperCamelCase     (      self,       A,       A=None      ):





            """simple docstring"""

            _UpperCamelCase              =						[self.cls_token_id] + token_ids_a + [self.sep_token_id]

            if token_ids_a:
                   output += token_ids_a + [self.sep_token_id]

            return output

     def 	_UpperCamelCase     (      self,       A,       A = None      ):





            """simple docstring"""

            _UpperCamelCase              =						[self.sep_token_id]
            _UpperCamelCase              =						[self.cls_token_id]
            if token_ids_a is None:
                   return len(cls + token_ids_a + sep      ) * [0]
            return len(cls + token_ids_a + sep      ) * [0] + len(token_ids_a + sep      ) * [1]

     def 	_UpperCamelCase     (      self,       A,       A = None      ):





            """simple docstring"""

            _UpperCamelCase              =						self._tokenizer.model.save(A,       name=A      )
            return tuple(A      )



     def 	_UpperCamelCase     (      self,       A,       A=None,       A=None,       A=False,       **A,       ):





            """simple docstring"""

            _UpperCamelCase              =						BertPreTokenizer()
            return super().save_pretrained(A,       A,       A,       A,       **A      )













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

     def 	_UpperCamelCase     (      self,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						TextaTextGenerationPipeline(model=A,       tokenizer=A      )
            return generator, ["Something to write", "Something else"]

     def 	_UpperCamelCase     (      self,       A,       A      ):





            """simple docstring"""

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

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

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

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

     @require_torch
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

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

            _UpperCamelCase              =						3
            _UpperCamelCase              =						generator(
                '''Something there''',       num_return_sequences=A,       num_beams=A,       )
            _UpperCamelCase              =						[
                {'''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(A,       A      )

            _UpperCamelCase              =						generator('''This is a test''',       do_sample=A,       num_return_sequences=2,       return_tensors=A      )
            self.assertEqual(
                A,       [
                    {'''generated_token_ids''': ANY(torch.Tensor      )},
                    {'''generated_token_ids''': ANY(torch.Tensor      )},
                ],       )
            _UpperCamelCase              =						generator.model.config.eos_token_id
            _UpperCamelCase              =						'''<pad>'''
            _UpperCamelCase              =						generator(
                ['''This is a test''', '''This is a second test'''],       do_sample=A,       num_return_sequences=2,       batch_size=2,       return_tensors=A,       )
            self.assertEqual(
                A,       [
                    [
                        {'''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"""

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






