





"""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 					lowerCAmelCase_      (   SCREAMING_SNAKE_CASE							:				Namespace ):


							'''simple docstring'''







							return TrainCommand(SCREAMING_SNAKE_CASE )
class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
				"""simple docstring"""




				@staticmethod
				def        __lowercase	(  __lowercase	:ArgumentParser      ):
											__lowerCamelCase   :   Any       =parser.add_parser('''train'''    ,     help='''CLI tool to train a model on a task.'''      )

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

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




				def __init__(  self	:List[str]    ,     __lowercase	:Namespace      ):
											__lowerCamelCase   :   List[Any]       =logging.get_logger('''transformers-cli/training'''      )

											__lowerCamelCase   :   Any       ='''tf''' if is_tf_available() else '''torch'''

											os.makedirs(args.output    ,     exist_ok=__lowercase      )
											__lowerCamelCase   :   Optional[Any]       =args.output

											__lowerCamelCase   :   Optional[Any]       =args.column_label
											__lowerCamelCase   :   Dict       =args.column_text
											__lowerCamelCase   :   Tuple       =args.column_id

											self.logger.info(f'Loading {args.task} pipeline for {args.model}'      )
											if args.task == "text_classification":
																		__lowerCamelCase   :   List[Any]       =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   :   Union[str, 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   :   List[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   :   List[str]       =args.validation_split
											__lowerCamelCase   :   Optional[int]       =args.train_batch_size
											__lowerCamelCase   :   Union[str, Any]       =args.valid_batch_size
											__lowerCamelCase   :   Optional[int]       =args.learning_rate
											__lowerCamelCase   :   List[Any]       =args.adam_epsilon




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




				def        __lowercase	(  self	:str      ):
											raise NotImplementedError




				def        __lowercase	(  self	:str      ):
											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 contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
							from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
							from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
							from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


_UpperCamelCase					      =   DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

_UpperCamelCase					      =   'main'
# Default branch name
_UpperCamelCase					      =   'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'
# One particular commit (not the top of `main`)
_UpperCamelCase					      =   'aaaaaaa'
# This commit does not exist, so we should 404.

_UpperCamelCase					      =   'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684'
# Sha-1 of config.json on the top of `main`, for checking purposes
_UpperCamelCase					      =   '4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'



@contextlib.contextmanager
def 					lowerCAmelCase_      (   ):


							'''simple docstring'''







							print('''Welcome!''' )
							yield
							print('''Bye!''' )



@contextlib.contextmanager
def 					lowerCAmelCase_      (   ):


							'''simple docstring'''







							print('''Bonjour!''' )
							yield
							print('''Au revoir!''' )
class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				def        __lowercase	(  self	:str      ):
											# If the spec is missing, importlib would not be able to import the module dynamically.
											assert transformers.__spec__ is not None
											assert importlib.util.find_spec('''transformers'''      ) is not None

class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				@unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO      )
				def        __lowercase	(  self	:str    ,     __lowercase	:str      ):
											with ContextManagers([]      ):
																		print('''Transformers are awesome!'''      )
											# The print statement adds a new line at the end of the output
											self.assertEqual(mock_stdout.getvalue()    ,     '''Transformers are awesome!\n'''      )




				@unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO      )
				def        __lowercase	(  self	:Any    ,     __lowercase	:Tuple      ):
											with ContextManagers([context_en()]      ):
																		print('''Transformers are awesome!'''      )
											# The output should be wrapped with an English welcome and goodbye
											self.assertEqual(mock_stdout.getvalue()    ,     '''Welcome!\nTransformers are awesome!\nBye!\n'''      )




				@unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO      )
				def        __lowercase	(  self	:List[Any]    ,     __lowercase	:Optional[Any]      ):
											with ContextManagers([context_fr(), context_en()]      ):
																		print('''Transformers are awesome!'''      )
											# The output should be wrapped with an English and French welcome and goodbye
											self.assertEqual(mock_stdout.getvalue()    ,     '''Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n'''      )




				@require_torch
				def        __lowercase	(  self	:Tuple      ):
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''', '''next_sentence_label''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''start_positions''', '''end_positions''']      )
											class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
															"""simple docstring"""
															pass

											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )




				@require_tf
				def        __lowercase	(  self	:str      ):
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''', '''next_sentence_label''']      )
											self.assertEqual(find_labels(__lowercase      )    ,     ['''start_positions''', '''end_positions''']      )
											class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
															"""simple docstring"""
															pass

											self.assertEqual(find_labels(__lowercase      )    ,     ['''labels''']      )




				@require_flax
				def        __lowercase	(  self	:str      ):
											# Flax models don't have labels
											self.assertEqual(find_labels(__lowercase      )    ,     []      )
											self.assertEqual(find_labels(__lowercase      )    ,     []      )
											self.assertEqual(find_labels(__lowercase      )    ,     []      )
											class 		SCREAMING_SNAKE_CASE_				(     snake_case__ ):
															"""simple docstring"""
															pass

											self.assertEqual(find_labels(__lowercase      )    ,     []      )




