
"""simple docstring"""






import os
from argparse import ArgumentParser, Namespace

from ..data import SingleSentenceClassificationProcessor as Processor
from ..pipelines import TextClassificationPipeline
from ..utils import is_tf_available, is_torch_available, logging
from . import BaseTransformersCLICommand


if not is_tf_available() and not is_torch_available():
				raise RuntimeError('At least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI training')

# TF training parameters
__UpperCAmelCase       =  False
__UpperCAmelCase       =  False






def 						_snake_case       (	lowercase__		:  Namespace		)  ->				str:


			'''simple docstring'''





			return TrainCommand(lowercase__		)




class 			_SCREAMING_SNAKE_CASE					(			A__							):






			@staticmethod
			def  __lowerCAmelCase (					__A       )			->     int:
						lowerCAmelCase_    :List[str]			     =  parser.add_parser("""train"""       ,   help="""CLI tool to train a model on a task."""       )

						train_parser.add_argument(
						    """--train_data"""       ,   type=__A       ,   required=__A       ,   help="""path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences."""       ,   )
						train_parser.add_argument(
						    """--column_label"""       ,   type=__A       ,   default=0       ,   help="""Column of the dataset csv file with example labels."""       )
						train_parser.add_argument(
						    """--column_text"""       ,   type=__A       ,   default=1       ,   help="""Column of the dataset csv file with example texts."""       )
						train_parser.add_argument(
						    """--column_id"""       ,   type=__A       ,   default=2       ,   help="""Column of the dataset csv file with example ids."""       )
						train_parser.add_argument(
						    """--skip_first_row"""       ,   action="""store_true"""       ,   help="""Skip the first row of the csv file (headers)."""       )

						train_parser.add_argument("""--validation_data"""       ,   type=__A       ,   default=""""""       ,   help="""path to validation dataset."""       )
						train_parser.add_argument(
						    """--validation_split"""       ,   type=__A       ,   default=0.1       ,   help="""if validation dataset is not provided, fraction of train dataset to use as validation dataset."""       ,   )

						train_parser.add_argument("""--output"""       ,   type=__A       ,   default="""./"""       ,   help="""path to saved the trained model."""       )

						train_parser.add_argument(
						    """--task"""       ,   type=__A       ,   default="""text_classification"""       ,   help="""Task to train the model on."""       )
						train_parser.add_argument(
						    """--model"""       ,   type=__A       ,   default="""bert-base-uncased"""       ,   help="""Model's name or path to stored model."""       )
						train_parser.add_argument("""--train_batch_size"""       ,   type=__A       ,   default=32       ,   help="""Batch size for training."""       )
						train_parser.add_argument("""--valid_batch_size"""       ,   type=__A       ,   default=64       ,   help="""Batch size for validation."""       )
						train_parser.add_argument("""--learning_rate"""       ,   type=__A       ,   default=3E-5       ,   help="""Learning rate."""       )
						train_parser.add_argument("""--adam_epsilon"""       ,   type=__A       ,   default=1E-08       ,   help="""Epsilon for Adam optimizer."""       )
						train_parser.set_defaults(func=__A       )






			def __init__(					self       ,   __A       )			->     Dict:
						lowerCAmelCase_    :List[Any]			     =  logging.get_logger("""transformers-cli/training"""       )

						lowerCAmelCase_    :int			     =  """tf""" if is_tf_available() else """torch"""

						os.makedirs(args.output       ,   exist_ok=__A       )
						lowerCAmelCase_    :List[Any]			     =  args.output

						lowerCAmelCase_    :int			     =  args.column_label
						lowerCAmelCase_    :int			     =  args.column_text
						lowerCAmelCase_    :Union[str, Any]			     =  args.column_id

						self.logger.info(f"""Loading {args.task} pipeline for {args.model}"""       )
						if args.task == "text_classification":
									lowerCAmelCase_    :Tuple			     =  TextClassificationPipeline.from_pretrained(args.model       )
						elif args.task == "token_classification":
									raise NotImplementedError
						elif args.task == "question_answering":
									raise NotImplementedError

						self.logger.info(f"""Loading dataset from {args.train_data}"""       )
						lowerCAmelCase_    :Any			     =  Processor.create_from_csv(
						    args.train_data       ,   column_label=args.column_label       ,   column_text=args.column_text       ,   column_id=args.column_id       ,   skip_first_row=args.skip_first_row       ,   )
						lowerCAmelCase_    :str			     =  None
						if args.validation_data:
									self.logger.info(f"""Loading validation dataset from {args.validation_data}"""       )
									lowerCAmelCase_    :List[Any]			     =  Processor.create_from_csv(
									    args.validation_data       ,   column_label=args.column_label       ,   column_text=args.column_text       ,   column_id=args.column_id       ,   skip_first_row=args.skip_first_row       ,   )

						lowerCAmelCase_    :Optional[Any]			     =  args.validation_split
						lowerCAmelCase_    :str			     =  args.train_batch_size
						lowerCAmelCase_    :str			     =  args.valid_batch_size
						lowerCAmelCase_    :Optional[int]			     =  args.learning_rate
						lowerCAmelCase_    :Union[str, Any]			     =  args.adam_epsilon






			def  __lowerCAmelCase (					self       )			->     Optional[int]:
						if self.framework == "tf":
									return self.run_tf()
						return self.run_torch()






			def  __lowerCAmelCase (					self       )			->     Tuple:
						raise NotImplementedError






			def  __lowerCAmelCase (					self       )			->     Optional[int]:
						self.pipeline.fit(
						    self.train_dataset       ,   validation_data=self.valid_dataset       ,   validation_split=self.validation_split       ,   learning_rate=self.learning_rate       ,   adam_epsilon=self.adam_epsilon       ,   train_batch_size=self.train_batch_size       ,   valid_batch_size=self.valid_batch_size       ,   )

						# Save trained pipeline
						self.pipeline.save_pretrained(self.output       )














"""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


