






'''simple docstring'''
import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch






class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase				):




 @require_torch
 def      __magic_name__	(      self	:							Tuple      )	->						Optional[Any]:
       # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
       # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
       # while running an external program

       # python one-liner segments

       # this must be loaded before socket.socket is monkey-patched
       SCREAMING_SNAKE_CASE__     :				str									='''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

       SCREAMING_SNAKE_CASE__     :				Any									='''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

       SCREAMING_SNAKE_CASE__     :				Dict									='''
import socket
def offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")
socket.socket = offline_socket
        '''

       # Force fetching the files so that we can use the cache
       SCREAMING_SNAKE_CASE__     :				Dict									='''hf-internal-testing/tiny-random-bert'''
       BertConfig.from_pretrained(__lowercase      )
       BertModel.from_pretrained(__lowercase      )
       BertTokenizer.from_pretrained(__lowercase      )
       pipeline(task='''fill-mask'''					,      model=__lowercase      )

       # baseline - just load from_pretrained with normal network
       SCREAMING_SNAKE_CASE__     :				Dict									=[sys.executable, '''-c''', '''\n'''.join([load, run, mock]      )]

       # should succeed
       SCREAMING_SNAKE_CASE__     :				Any									=self.get_env()
       # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
       SCREAMING_SNAKE_CASE__     :				Any									='''1'''
       SCREAMING_SNAKE_CASE__     :				str									=subprocess.run(__lowercase					,      env=__lowercase					,      check=__lowercase					,      capture_output=__lowercase      )
       self.assertEqual(result.returncode					,      0					,      result.stderr      )
       self.assertIn('''success'''					,      result.stdout.decode()      )




 @require_torch
 def      __magic_name__	(      self	:							str      )	->						List[Any]:
       # python one-liner segments
       # this must be loaded before socket.socket is monkey-patched
       SCREAMING_SNAKE_CASE__     :				int									='''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

       SCREAMING_SNAKE_CASE__     :				List[Any]									='''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

       SCREAMING_SNAKE_CASE__     :				List[Any]									='''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")
socket.socket = offline_socket
        '''

       # Force fetching the files so that we can use the cache
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									='''hf-internal-testing/tiny-random-bert'''
       BertConfig.from_pretrained(__lowercase      )
       BertModel.from_pretrained(__lowercase      )
       BertTokenizer.from_pretrained(__lowercase      )
       pipeline(task='''fill-mask'''					,      model=__lowercase      )

       # baseline - just load from_pretrained with normal network
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=[sys.executable, '''-c''', '''\n'''.join([load, run, mock]      )]

       # should succeed
       SCREAMING_SNAKE_CASE__     :				str									=self.get_env()
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=subprocess.run(__lowercase					,      env=__lowercase					,      check=__lowercase					,      capture_output=__lowercase      )
       self.assertEqual(result.returncode					,      0					,      result.stderr      )
       self.assertIn('''success'''					,      result.stdout.decode()      )




 @require_torch
 def      __magic_name__	(      self	:							Any      )	->						Any:
       # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
       # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
       # while running an external program

       # python one-liner segments

       # this must be loaded before socket.socket is monkey-patched
       SCREAMING_SNAKE_CASE__     :				Dict									='''
from transformers import BertConfig, BertModel, BertTokenizer
        '''

       SCREAMING_SNAKE_CASE__     :				List[Any]									='''
mname = "hf-internal-testing/tiny-random-bert-sharded"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print("success")
        '''

       SCREAMING_SNAKE_CASE__     :				List[Any]									='''
import socket
def offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")
socket.socket = offline_socket
        '''

       # baseline - just load from_pretrained with normal network
       SCREAMING_SNAKE_CASE__     :				str									=[sys.executable, '''-c''', '''\n'''.join([load, run]      )]

       # should succeed
       SCREAMING_SNAKE_CASE__     :				List[Any]									=self.get_env()
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=subprocess.run(__lowercase					,      env=__lowercase					,      check=__lowercase					,      capture_output=__lowercase      )
       self.assertEqual(result.returncode					,      0					,      result.stderr      )
       self.assertIn('''success'''					,      result.stdout.decode()      )

       # next emulate no network
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=[sys.executable, '''-c''', '''\n'''.join([load, mock, run]      )]

       # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
       # env["TRANSFORMERS_OFFLINE"] = "0"
       # result = subprocess.run(cmd, env=env, check=False, capture_output=True)
       # self.assertEqual(result.returncode, 1, result.stderr)

       # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
       SCREAMING_SNAKE_CASE__     :				Any									='''1'''
       SCREAMING_SNAKE_CASE__     :				int									=subprocess.run(__lowercase					,      env=__lowercase					,      check=__lowercase					,      capture_output=__lowercase      )
       self.assertEqual(result.returncode					,      0					,      result.stderr      )
       self.assertIn('''success'''					,      result.stdout.decode()      )




 @require_torch
 def      __magic_name__	(      self	:							Optional[Any]      )	->						str:
       SCREAMING_SNAKE_CASE__     :				List[Any]									='''
from transformers import pipeline
        '''
       SCREAMING_SNAKE_CASE__     :				int									='''
mname = "hf-internal-testing/tiny-random-bert"
pipe = pipeline(model=mname)
        '''

       SCREAMING_SNAKE_CASE__     :				Optional[int]									='''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")
socket.socket = offline_socket
        '''
       SCREAMING_SNAKE_CASE__     :				Any									=self.get_env()
       SCREAMING_SNAKE_CASE__     :				Tuple									='''1'''
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=[sys.executable, '''-c''', '''\n'''.join([load, mock, run]      )]
       SCREAMING_SNAKE_CASE__     :				Tuple									=subprocess.run(__lowercase					,      env=__lowercase					,      check=__lowercase					,      capture_output=__lowercase      )
       self.assertEqual(result.returncode					,      1					,      result.stderr      )
       self.assertIn(
           '''You cannot infer task automatically within `pipeline` when using offline mode'''					,      result.stderr.decode().replace('''\n'''					,      ''''''      )					,      )




 @require_torch
 def      __magic_name__	(      self	:							Optional[int]      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				List[str]									='''
from transformers import AutoModel
        '''
       SCREAMING_SNAKE_CASE__     :				List[Any]									='''
mname = "hf-internal-testing/test_dynamic_model"
AutoModel.from_pretrained(mname, trust_remote_code=True)
print("success")
        '''

       # baseline - just load from_pretrained with normal network
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=[sys.executable, '''-c''', '''\n'''.join([load, run]      )]

       # should succeed
       SCREAMING_SNAKE_CASE__     :				str									=self.get_env()
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=subprocess.run(__lowercase					,      env=__lowercase					,      check=__lowercase					,      capture_output=__lowercase      )
       self.assertEqual(result.returncode					,      0					,      result.stderr      )
       self.assertIn('''success'''					,      result.stdout.decode()      )

       # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
       SCREAMING_SNAKE_CASE__     :				Dict									='''1'''
       SCREAMING_SNAKE_CASE__     :				Dict									=subprocess.run(__lowercase					,      env=__lowercase					,      check=__lowercase					,      capture_output=__lowercase      )
       self.assertEqual(result.returncode					,      0					,      result.stderr      )
       self.assertIn('''success'''					,      result.stdout.decode()      )







'''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
a_					= False
a_					= False





def  _a(  UpperCamelCase__				:						Namespace							):




      '''simple docstring'''






      return TrainCommand(UpperCamelCase__							)







class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase				):




 @staticmethod
 def      __magic_name__	(      __lowercase	:							ArgumentParser      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Union[str, 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-08					,      help='''Epsilon for Adam optimizer.'''      )
       train_parser.set_defaults(func=__lowercase      )




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

       SCREAMING_SNAKE_CASE__     :				int									='''tf''' if is_tf_available() else '''torch'''

       os.makedirs(args.output					,      exist_ok=__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Any									=args.output

       SCREAMING_SNAKE_CASE__     :				str									=args.column_label
       SCREAMING_SNAKE_CASE__     :				List[Any]									=args.column_text
       SCREAMING_SNAKE_CASE__     :				Tuple									=args.column_id

       self.logger.info(F"Loading {args.task} pipeline for {args.model}"      )
       if args.task == "text_classification":
             SCREAMING_SNAKE_CASE__     :				List[str]									=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}"      )
       SCREAMING_SNAKE_CASE__     :				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					,      )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=None
       if args.validation_data:
             self.logger.info(F"Loading validation dataset from {args.validation_data}"      )
             SCREAMING_SNAKE_CASE__     :				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					,      )

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




 def      __magic_name__	(      self	:							Any      )	->						str:
       if self.framework == "tf":
             return self.run_tf()
       return self.run_torch()




 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       raise NotImplementedError




 def      __magic_name__	(      self	:							Dict      )	->						List[Any]:
       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      )