


'''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
lowerCamelCase							:   Tuple  						=	False
lowerCamelCase							:   str  						=	False

def _SCREAMING_SNAKE_CASE      (A       )       ->       List[Any]:






       """simple docstring"""







       return TrainCommand(snake_case__       )

class   __lowerCAmelCase						(__snake_case     ):





      '''simple docstring'''






      @staticmethod
      def       UpperCamelCase__		(UpperCamelCase    :    int ):

             '''simple docstring'''







             lowercase__      			= parser.add_parser('''train'''   ,						help='''CLI tool to train a model on a task.''' )

             train_parser.add_argument(
                 '''--train_data'''   ,						type=UpperCamelCase   ,						required=UpperCamelCase   ,						help='''path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences.'''   ,						)
             train_parser.add_argument(
                 '''--column_label'''   ,						type=UpperCamelCase   ,						default=0   ,						help='''Column of the dataset csv file with example labels.''' )
             train_parser.add_argument(
                 '''--column_text'''   ,						type=UpperCamelCase   ,						default=1   ,						help='''Column of the dataset csv file with example texts.''' )
             train_parser.add_argument(
                 '''--column_id'''   ,						type=UpperCamelCase   ,						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=UpperCamelCase   ,						default=''''''   ,						help='''path to validation dataset.''' )
             train_parser.add_argument(
                 '''--validation_split'''   ,						type=UpperCamelCase   ,						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=UpperCamelCase   ,						default='''./'''   ,						help='''path to saved the trained model.''' )

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



      def __init__(self    :    Optional[Any]   ,						UpperCamelCase    :    int ):

             '''simple docstring'''







             lowercase__      			= logging.get_logger('''transformers-cli/training''' )

             lowercase__      			= '''tf''' if is_tf_available() else '''torch'''

             os.makedirs(args.output   ,						exist_ok=UpperCamelCase )
             lowercase__      			= args.output

             lowercase__      			= args.column_label
             lowercase__      			= args.column_text
             lowercase__      			= args.column_id

             self.logger.info(f"Loading {args.task} pipeline for {args.model}" )
             if args.task == "text_classification":
                    lowercase__      			= 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}" )
             lowercase__      			= 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   ,						)
             lowercase__      			= None
             if args.validation_data:
                    self.logger.info(f"Loading validation dataset from {args.validation_data}" )
                    lowercase__      			= 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   ,						)

             lowercase__      			= args.validation_split
             lowercase__      			= args.train_batch_size
             lowercase__      			= args.valid_batch_size
             lowercase__      			= args.learning_rate
             lowercase__      			= args.adam_epsilon



      def       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







             if self.framework == "tf":
                    return self.run_tf()
             return self.run_torch()



      def       UpperCamelCase__		(self    :    Tuple ):

             '''simple docstring'''







             raise NotImplementedError






      def       UpperCamelCase__		(self    :    List[Any] ):

             '''simple docstring'''







             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 unittest

from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow


if is_flax_available():
      import jax

      from transformers.models.auto.modeling_flax_auto import FlaxAutoModel
      from transformers.models.bert.modeling_flax_bert import FlaxBertModel
      from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel





@require_flax
class  A				(							unittest.TestCase						):





     @slow
     def 				__lowerCAmelCase							(							self	) ->				List[Any]:






         """simple docstring"""






         for model_name in ["bert-base-cased", "bert-large-uncased"]:
             with self.subTest(SCREAMING_SNAKE_CASE	):
                 A       :   int					=						AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE	)
                 self.assertIsNotNone(SCREAMING_SNAKE_CASE	)
                 self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

                 A       :   List[str]					=						FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE	)
                 self.assertIsNotNone(SCREAMING_SNAKE_CASE	)
                 self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     @slow
     def 				__lowerCAmelCase							(							self	) ->				int:






         """simple docstring"""






         for model_name in ["roberta-base", "roberta-large"]:
             with self.subTest(SCREAMING_SNAKE_CASE	):
                 A       :   Any					=						AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE	)
                 self.assertIsNotNone(SCREAMING_SNAKE_CASE	)
                 self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

                 A       :   Any					=						FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE	)
                 self.assertIsNotNone(SCREAMING_SNAKE_CASE	)
                 self.assertIsInstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)





     @slow
     def 				__lowerCAmelCase							(							self	) ->				Any:






         """simple docstring"""






         for model_name in ["bert-base-cased", "bert-large-uncased"]:
             A       :   Optional[int]					=						AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE	)
             A       :   List[str]					=						FlaxBertModel.from_pretrained(SCREAMING_SNAKE_CASE	)
             A       :   Optional[Any]					=						tokenizer('''Do you support jax jitted function?'''						,			return_tensors=TensorType.JAX	)

             @jax.jit
             def eval(**SCREAMING_SNAKE_CASE	):
                 return model(**SCREAMING_SNAKE_CASE	)

             eval(**SCREAMING_SNAKE_CASE	).block_until_ready()





     @slow
     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         for model_name in ["roberta-base", "roberta-large"]:
             A       :   List[str]					=						AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE	)
             A       :   Union[str, Any]					=						FlaxRobertaModel.from_pretrained(SCREAMING_SNAKE_CASE	)
             A       :   int					=						tokenizer('''Do you support jax jitted function?'''						,			return_tensors=TensorType.JAX	)

             @jax.jit
             def eval(**SCREAMING_SNAKE_CASE	):
                 return model(**SCREAMING_SNAKE_CASE	)

             eval(**SCREAMING_SNAKE_CASE	).block_until_ready()





     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         with self.assertRaisesRegex(
             SCREAMING_SNAKE_CASE						,			'''bert-base is not a local folder and is not a valid model identifier'''	):
             A       :   List[Any]					=						FlaxAutoModel.from_pretrained('''bert-base'''	)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         with self.assertRaisesRegex(
             SCREAMING_SNAKE_CASE						,			R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''	):
             A       :   Optional[int]					=						FlaxAutoModel.from_pretrained(SCREAMING_SNAKE_CASE						,			revision='''aaaaaa'''	)





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






         with self.assertRaisesRegex(
             SCREAMING_SNAKE_CASE						,			'''hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack'''						,			):
             A       :   List[str]					=						FlaxAutoModel.from_pretrained('''hf-internal-testing/config-no-model'''	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[Any]:






         """simple docstring"""






         with self.assertRaisesRegex(SCREAMING_SNAKE_CASE						,			'''Use `from_pt=True` to load this model'''	):
             A       :   Any					=						FlaxAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only'''	)




