
    sg                         d dl Z d dlmZmZ ddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ  e
       s e       s ed	      d
Zd
ZdefdZ G d de      Zy)    N)ArgumentParser	Namespace   )%SingleSentenceClassificationProcessor)TextClassificationPipeline)is_tf_availableis_torch_availablelogging   )BaseTransformersCLICommandzRAt least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI trainingFargsc                     t        |       S )z
    Factory function used to instantiate training command from provided command line arguments.

    Returns: TrainCommand
    )TrainCommand)r   s    N/var/www/html/venv/lib/python3.12/site-packages/transformers/commands/train.pytrain_command_factoryr       s         c                   @    e Zd Zedefd       ZdefdZd Zd Z	d Z
y)	r   parserc                    | j                  dd      }|j                  dt        dd       |j                  dt        d	d
       |j                  dt        dd       |j                  dt        dd       |j                  ddd       |j                  dt        dd       |j                  dt        dd       |j                  dt        dd       |j                  dt        d d!       |j                  d"t        d#d$       |j                  d%t        d&d'       |j                  d(t        d)d*       |j                  d+t        d,d-       |j                  d.t        d/d0       |j                  t        1       y2)3z
        Register this command to argparse so it's available for the transformer-cli

        Args:
            parser: Root parser to register command-specific arguments
        trainz$CLI tool to train a model on a task.)helpz--train_dataTzcpath to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences.)typerequiredr   z--column_labelr   z3Column of the dataset csv file with example labels.)r   defaultr   z--column_textr   z2Column of the dataset csv file with example texts.z--column_idr   z0Column of the dataset csv file with example ids.z--skip_first_row
store_truez-Skip the first row of the csv file (headers).)actionr   z--validation_data zpath to validation dataset.z--validation_splitg?z^if validation dataset is not provided, fraction of train dataset to use as validation dataset.z--outputz./z path to saved the trained model.z--tasktext_classificationzTask to train the model on.z--modelzgoogle-bert/bert-base-uncasedz%Model's name or path to stored model.z--train_batch_size    zBatch size for training.z--valid_batch_size@   zBatch size for validation.z--learning_rategiUMu>zLearning rate.z--adam_epsilong:0yE>zEpsilon for Adam optimizer.)funcN)
add_parseradd_argumentstrintfloatset_defaultsr   )r   train_parsers     r   register_subcommandz TrainCommand.register_subcommand*   s    ((7](^!!v	 	" 	
 	!!38m 	" 	
 	!!#q7k 	" 	
 	!!Q5g 	" 	
 	!!|:i 	" 	
 	!!"5CRo!p!! q	 	" 	
 	!!*3Km!n!!3(=Da 	" 	
 	!!C)HOv 	" 	
 	!!"6S"Sm!n!!"6S"So!p!!"3%Td!e!!"2Tq!r!!'<!=r   r   c                    t        j                  d      | _        t               rdnd| _        t        j                  |j                  d       |j                  | _        |j                  | _        |j                  | _	        |j                  | _
        | j                  j                  d|j                   d|j                          |j                  dk(  r%t        j                  |j                        | _        n*|j                  d	k(  rt"        |j                  d
k(  rt"        | j                  j                  d|j$                          t'        j(                  |j$                  |j                  |j                  |j                  |j*                        | _        d | _        |j0                  ry| j                  j                  d|j0                          t'        j(                  |j0                  |j                  |j                  |j                  |j*                        | _        |j2                  | _        |j4                  | _        |j6                  | _        |j8                  | _        |j:                  | _        y )Nztransformers-cli/trainingtftorchT)exist_okzLoading z pipeline for r   token_classificationquestion_answeringzLoading dataset from )column_labelcolumn_text	column_idskip_first_rowz Loading validation dataset from )r
   
get_loggerloggerr   	frameworkosmakedirsoutputr0   r1   r2   infotaskmodelr   from_pretrainedpipelineNotImplementedError
train_data	Processorcreate_from_csvr3   train_datasetvalid_datasetvalidation_datavalidation_splittrain_batch_sizevalid_batch_sizelearning_rateadam_epsilon)selfr   s     r   __init__zTrainCommand.__init__]   s   (()DE!0!2
DKK$/kk --++8DII;nTZZLIJ99--6FFtzzRDMYY00%%YY..%%00ABC&66OO**((nn..
 "KK?@T@T?UVW!*!:!:$$!.. ,,..#22"D !% 5 5 $ 5 5 $ 5 5!// --r   c                 `    | j                   dk(  r| j                         S | j                         S )Nr+   )r6   run_tf	run_torchrK   s    r   runzTrainCommand.run   s'    >>T!;;= ~~r   c                     t         )N)r?   rP   s    r   rO   zTrainCommand.run_torch   s    !!r   c           	         | j                   j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                         | j                   j                  | j                         y )N)rE   rF   rI   rJ   rG   rH   )r>   fitrC   rD   rF   rI   rJ   rG   rH   save_pretrainedr9   rP   s    r   rN   zTrainCommand.run_tf   sp     ..!22,,**!22!22 	 	
 	%%dkk2r   N)__name__
__module____qualname__staticmethodr   r)   r   rL   rQ   rO   rN    r   r   r   r   )   s8    0>N 0> 0>d+.Y +.Z 
"3r   r   )r7   argparser   r   datar   rA   	pipelinesr   utilsr   r	   r
   r   r   RuntimeErrorUSE_XLAUSE_AMPr   r   rZ   r   r   <module>rb      s]    
 . E 2 @ @ ( !3!5
k
ll 
	 u3- u3r   