






"""simple docstring"""






import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_gpta import GPTaTokenizer


if TYPE_CHECKING:
  from transformers.pipelines.conversational import Conversation


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

UpperCAmelCase_   : Dict   			= {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

UpperCAmelCase_   : Dict   			= {
    """vocab_file""": {
        """gpt2""": """https://huggingface.co/gpt2/resolve/main/vocab.json""",
        """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/vocab.json""",
        """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/vocab.json""",
        """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/vocab.json""",
        """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """gpt2""": """https://huggingface.co/gpt2/resolve/main/merges.txt""",
        """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/merges.txt""",
        """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/merges.txt""",
        """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/merges.txt""",
        """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """gpt2""": """https://huggingface.co/gpt2/resolve/main/tokenizer.json""",
        """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/tokenizer.json""",
        """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/tokenizer.json""",
        """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/tokenizer.json""",
        """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/tokenizer.json""",
    },
}

UpperCAmelCase_   : List[str]   			= {
    """gpt2""": 1024,
    """gpt2-medium""": 1024,
    """gpt2-large""": 1024,
    """gpt2-xl""": 1024,
    """distilgpt2""": 1024,
}




class 							lowerCAmelCase__				(			UpperCAmelCase__ ):






      '''simple docstring'''

      __UpperCamelCase	 =  VOCAB_FILES_NAMES
      __UpperCamelCase	 =  PRETRAINED_VOCAB_FILES_MAP
      __UpperCamelCase	 =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __UpperCamelCase	 =  ["input_ids", "attention_mask"]
      __UpperCamelCase	 =  GPTaTokenizer
      def __init__(						self     :  Optional[int]						,	lowercase_     :  int=None						,	lowercase_     :  List[str]=None						,	lowercase_     :  Union[str, Any]=None						,	lowercase_     :  Tuple="<|endoftext|>"						,	lowercase_     :  str="<|endoftext|>"						,	lowercase_     :  Dict="<|endoftext|>"						,	lowercase_     :  Tuple=False						,	**lowercase_     :  Optional[int]						,	):




          '''simple docstring'''


          super().__init__(
              lowercase_						,	lowercase_						,	tokenizer_file=lowercase_						,	unk_token=lowercase_						,	bos_token=lowercase_						,	eos_token=lowercase_						,	add_prefix_space=lowercase_						,	**lowercase_						,	)

          SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		kwargs.pop('''add_bos_token'''						,	lowercase_)

          SCREAMING_SNAKE_CASE_      :  Dict    						=		json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__())
          if pre_tok_state.get('''add_prefix_space'''						,	lowercase_) != add_prefix_space:
              SCREAMING_SNAKE_CASE_      :  int    						=		getattr(lowercase_						,	pre_tok_state.pop('''type'''))
              SCREAMING_SNAKE_CASE_      :  str    						=		add_prefix_space
              SCREAMING_SNAKE_CASE_      :  Dict    						=		pre_tok_class(**lowercase_)

          SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		add_prefix_space
      def _SCREAMING_SNAKE_CASE (						self     :  str						,	*lowercase_     :  List[Any]						,	**lowercase_     :  Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		kwargs.get('''is_split_into_words'''						,	lowercase_)
          assert self.add_prefix_space or not is_split_into_words, (
              F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
              "to use it with pretokenized inputs."
          )

          return super()._batch_encode_plus(*lowercase_						,	**lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[int]						,	*lowercase_     :  List[str]						,	**lowercase_     :  List[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[str]    						=		kwargs.get('''is_split_into_words'''						,	lowercase_)

          assert self.add_prefix_space or not is_split_into_words, (
              F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
              "to use it with pretokenized inputs."
          )

          return super()._encode_plus(*lowercase_						,	**lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  int						,	lowercase_     :  str						,	lowercase_     :  Optional[str] = None):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[str]    						=		self._tokenizer.model.save(lowercase_						,	name=lowercase_)
          return tuple(lowercase_)



      def _SCREAMING_SNAKE_CASE (						self     :  int						,	lowercase_     :  "Conversation"):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Dict    						=		[]
          for is_user, text in conversation.iter_texts():
              input_ids.extend(self.encode(lowercase_						,	add_special_tokens=lowercase_) + [self.eos_token_id])

          if len(lowercase_) > self.model_max_length:
              SCREAMING_SNAKE_CASE_      :  Any    						=		input_ids[-self.model_max_length :]
          return input_ids










"""simple docstring"""






import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_gpta import GPTaTokenizer


if TYPE_CHECKING:
  from transformers.pipelines.conversational import Conversation


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

UpperCAmelCase_   : Dict   			= {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

UpperCAmelCase_   : Dict   			= {
    """vocab_file""": {
        """gpt2""": """https://huggingface.co/gpt2/resolve/main/vocab.json""",
        """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/vocab.json""",
        """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/vocab.json""",
        """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/vocab.json""",
        """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """gpt2""": """https://huggingface.co/gpt2/resolve/main/merges.txt""",
        """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/merges.txt""",
        """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/merges.txt""",
        """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/merges.txt""",
        """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """gpt2""": """https://huggingface.co/gpt2/resolve/main/tokenizer.json""",
        """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/tokenizer.json""",
        """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/tokenizer.json""",
        """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/tokenizer.json""",
        """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/tokenizer.json""",
    },
}

UpperCAmelCase_   : List[str]   			= {
    """gpt2""": 1024,
    """gpt2-medium""": 1024,
    """gpt2-large""": 1024,
    """gpt2-xl""": 1024,
    """distilgpt2""": 1024,
}




class 							lowerCAmelCase__				(			UpperCAmelCase__ ):






      '''simple docstring'''

      __UpperCamelCase	 =  VOCAB_FILES_NAMES
      __UpperCamelCase	 =  PRETRAINED_VOCAB_FILES_MAP
      __UpperCamelCase	 =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __UpperCamelCase	 =  ["input_ids", "attention_mask"]
      __UpperCamelCase	 =  GPTaTokenizer
      def __init__(						self     :  Optional[int]						,	lowercase_     :  int=None						,	lowercase_     :  List[str]=None						,	lowercase_     :  Union[str, Any]=None						,	lowercase_     :  Tuple="<|endoftext|>"						,	lowercase_     :  str="<|endoftext|>"						,	lowercase_     :  Dict="<|endoftext|>"						,	lowercase_     :  Tuple=False						,	**lowercase_     :  Optional[int]						,	):




          '''simple docstring'''


          super().__init__(
              lowercase_						,	lowercase_						,	tokenizer_file=lowercase_						,	unk_token=lowercase_						,	bos_token=lowercase_						,	eos_token=lowercase_						,	add_prefix_space=lowercase_						,	**lowercase_						,	)

          SCREAMING_SNAKE_CASE_      :  Union[str, Any]    						=		kwargs.pop('''add_bos_token'''						,	lowercase_)

          SCREAMING_SNAKE_CASE_      :  Dict    						=		json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__())
          if pre_tok_state.get('''add_prefix_space'''						,	lowercase_) != add_prefix_space:
              SCREAMING_SNAKE_CASE_      :  int    						=		getattr(lowercase_						,	pre_tok_state.pop('''type'''))
              SCREAMING_SNAKE_CASE_      :  str    						=		add_prefix_space
              SCREAMING_SNAKE_CASE_      :  Dict    						=		pre_tok_class(**lowercase_)

          SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		add_prefix_space
      def _SCREAMING_SNAKE_CASE (						self     :  str						,	*lowercase_     :  List[Any]						,	**lowercase_     :  Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[Any]    						=		kwargs.get('''is_split_into_words'''						,	lowercase_)
          assert self.add_prefix_space or not is_split_into_words, (
              F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
              "to use it with pretokenized inputs."
          )

          return super()._batch_encode_plus(*lowercase_						,	**lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[int]						,	*lowercase_     :  List[str]						,	**lowercase_     :  List[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[str]    						=		kwargs.get('''is_split_into_words'''						,	lowercase_)

          assert self.add_prefix_space or not is_split_into_words, (
              F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
              "to use it with pretokenized inputs."
          )

          return super()._encode_plus(*lowercase_						,	**lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  int						,	lowercase_     :  str						,	lowercase_     :  Optional[str] = None):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  List[str]    						=		self._tokenizer.model.save(lowercase_						,	name=lowercase_)
          return tuple(lowercase_)



      def _SCREAMING_SNAKE_CASE (						self     :  int						,	lowercase_     :  "Conversation"):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Dict    						=		[]
          for is_user, text in conversation.iter_texts():
              input_ids.extend(self.encode(lowercase_						,	add_special_tokens=lowercase_) + [self.eos_token_id])

          if len(lowercase_) > self.model_max_length:
              SCREAMING_SNAKE_CASE_      :  Any    						=		input_ids[-self.model_max_length :]
          return input_ids


