


'''simple docstring'''






import argparse
from copy import deepcopy

import numpy as np
from datasets import ClassLabel, DatasetDict, load_dataset
from evaluate import load

from transformers import (
    AutoModelForSequenceClassification,
    AutoTokenizer,
    DataCollatorWithPadding,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    set_seed,
)





def lowerCamelCase__     (		):







   '''simple docstring'''




   _UpperCAmelCase  :  int				       =argparse.ArgumentParser()
   parser.add_argument('--model_ckpt'			,       type=__lowerCamelCase			,       default='microsoft/unixcoder-base-nine'				)
   parser.add_argument('--num_epochs'			,       type=__lowerCamelCase			,       default=5				)
   parser.add_argument('--batch_size'			,       type=__lowerCamelCase			,       default=6				)
   parser.add_argument('--gradient_accumulation_steps'			,       type=__lowerCamelCase			,       default=1				)
   parser.add_argument('--freeze'			,       type=__lowerCamelCase			,       default=__lowerCamelCase				)
   parser.add_argument('--learning_rate'			,       type=__lowerCamelCase			,       default=5e-4				)
   parser.add_argument('--seed'			,       type=__lowerCamelCase			,       default=0				)
   parser.add_argument('--lr_scheduler_type'			,       type=__lowerCamelCase			,       default='cosine'				)
   parser.add_argument('--num_warmup_steps'			,       type=__lowerCamelCase			,       default=1_0				)
   parser.add_argument('--weight_decay'			,       type=__lowerCamelCase			,       default=0.01				)
   parser.add_argument('--output_dir'			,       type=__lowerCamelCase			,       default='./results'				)
   return parser.parse_args()


lowercase				=load('accuracy')





def lowerCamelCase__     (		__lowerCamelCase			:				Union[str, Any]				):







   '''simple docstring'''




   _UpperCAmelCase       ,			_UpperCAmelCase  :  List[Any]				       =eval_pred
   _UpperCAmelCase  :  int				       =np.argmax(__lowerCamelCase			,       axis=1				)
   return metric.compute(predictions=__lowerCamelCase			,       references=__lowerCamelCase				)






class    __magic_name__				(						lowerCAmelCase							):






       def __init__(     self    ,				snake_case)		->    None:




          '''simple docstring'''
          super().__init__()
          _UpperCAmelCase  :  List[Any]				       =trainer






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				**snake_case)		->    Union[str, Any]:




          '''simple docstring'''
          if control.should_evaluate:
             _UpperCAmelCase  :  Any				       =deepcopy(snake_case)
             self._trainer.evaluate(eval_dataset=self._trainer.train_dataset    ,				metric_key_prefix='train')
             return control_copy






def lowerCamelCase__     (		):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[int]				       =get_args()
   set_seed(args.seed				)

   _UpperCAmelCase  :  Any				       =load_dataset('codeparrot/codecomplex'			,       split='train'				)
   _UpperCAmelCase  :  List[Any]				       =dataset.train_test_split(test_size=0.2				)
   _UpperCAmelCase  :  Union[str, Any]				       =train_test['test'].train_test_split(test_size=0.5				)
   _UpperCAmelCase  :  Dict				       =DatasetDict(
       {
           'train': train_test['train'],
           'test': test_validation['train'],
           'valid': test_validation['test'],
       }				)

   print('Loading tokenizer and model'				)
   _UpperCAmelCase  :  Optional[Any]				       =AutoTokenizer.from_pretrained(args.model_ckpt				)
   _UpperCAmelCase  :  Tuple				       =tokenizer.eos_token
   _UpperCAmelCase  :  str				       =AutoModelForSequenceClassification.from_pretrained(args.model_ckpt			,       num_labels=7				)
   _UpperCAmelCase  :  List[str]				       =model.config.eos_token_id

   if args.freeze:
      for param in model.roberta.parameters():
         _UpperCAmelCase  :  int				       =False

   _UpperCAmelCase  :  str				       =ClassLabel(num_classes=7			,       names=list(set(train_test_validation['train']['complexity']				)				)				)

   def tokenize(__lowerCamelCase			:				Union[str, Any]				):
      _UpperCAmelCase  :  Any				       =tokenizer(example['src']			,       truncation=__lowerCamelCase			,       max_length=1_0_2_4				)
      _UpperCAmelCase  :  List[Any]				       =labels.straint(example['complexity']				)
      return {
          "input_ids": inputs["input_ids"],
          "attention_mask": inputs["attention_mask"],
          "label": label,
      }

   _UpperCAmelCase  :  int				       =train_test_validation.map(
       __lowerCamelCase			,       batched=__lowerCamelCase			,       remove_columns=train_test_validation['train'].column_names			,       )
   _UpperCAmelCase  :  List[Any]				       =DataCollatorWithPadding(tokenizer=__lowerCamelCase				)

   _UpperCAmelCase  :  Optional[Any]				       =TrainingArguments(
       output_dir=args.output_dir			,       learning_rate=args.learning_rate			,       lr_scheduler_type=args.lr_scheduler_type			,       evaluation_strategy='epoch'			,       save_strategy='epoch'			,       logging_strategy='epoch'			,       per_device_train_batch_size=args.batch_size			,       per_device_eval_batch_size=args.batch_size			,       num_train_epochs=args.num_epochs			,       gradient_accumulation_steps=args.gradient_accumulation_steps			,       weight_decay=0.01			,       metric_for_best_model='accuracy'			,       run_name='complexity-java'			,       report_to='wandb'			,       )

   _UpperCAmelCase  :  List[str]				       =Trainer(
       model=__lowerCamelCase			,       args=__lowerCamelCase			,       train_dataset=tokenized_datasets['train']			,       eval_dataset=tokenized_datasets['valid']			,       tokenizer=__lowerCamelCase			,       data_collator=__lowerCamelCase			,       compute_metrics=__lowerCamelCase			,       )

   print('Training...'				)
   trainer.add_callback(CustomCallback(__lowerCamelCase				)				)
   trainer.train()


if __name__ == "__main__":
 main()





'''simple docstring'''






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class    __magic_name__				(						lowerCAmelCase							):
       UpperCAmelCase		 =["image_processor", "tokenizer"]
       UpperCAmelCase		 ="CLIPImageProcessor"
       UpperCAmelCase		 =("CLIPTokenizer", "CLIPTokenizerFast")






       def __init__(     self    ,				snake_case=None    ,				snake_case=None    ,				**snake_case)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =None
          if "feature_extractor" in kwargs:
             warnings.warn(
                 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                 ' instead.'    ,				snake_case    ,				)
             _UpperCAmelCase  :  str				       =kwargs.pop('feature_extractor')

          _UpperCAmelCase  :  List[str]				       =image_processor if image_processor is not None else feature_extractor
          if image_processor is None:
             raise ValueError('You need to specify an `image_processor`.')
          if tokenizer is None:
             raise ValueError('You need to specify a `tokenizer`.')

          super().__init__(snake_case    ,				snake_case)






       def __call__(     self    ,				snake_case=None    ,				snake_case=None    ,				snake_case=None    ,				**snake_case)		->    Optional[Any]:




          '''simple docstring'''

          if text is None and images is None:
             raise ValueError('You have to specify either text or images. Both cannot be none.')

          if text is not None:
             _UpperCAmelCase  :  List[Any]				       =self.tokenizer(snake_case    ,				return_tensors=snake_case    ,				**snake_case)

          if images is not None:
             _UpperCAmelCase  :  List[str]				       =self.image_processor(snake_case    ,				return_tensors=snake_case    ,				**snake_case)

          if text is not None and images is not None:
             _UpperCAmelCase  :  Any				       =image_features.pixel_values
             return encoding
          elif text is not None:
             return encoding
          else:
             return BatchEncoding(data=dict(**snake_case)    ,				tensor_type=snake_case)






       def      lowerCAmelCase						(     self    ,				*snake_case    ,				**snake_case)		->    int:




          '''simple docstring'''
          return self.tokenizer.batch_decode(*snake_case    ,				**snake_case)






       def      lowerCAmelCase						(     self    ,				*snake_case    ,				**snake_case)		->    Optional[int]:




          '''simple docstring'''
          return self.tokenizer.decode(*snake_case    ,				**snake_case)






       @property
       def      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[Any]				       =self.tokenizer.model_input_names
          _UpperCAmelCase  :  Any				       =self.image_processor.model_input_names
          return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))






       @property
       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          warnings.warn(
              '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'    ,				snake_case    ,				)
          return self.image_processor_class






       @property
       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          warnings.warn(
              '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'    ,				snake_case    ,				)
          return self.image_processor

