Kevin Fink commited on
Commit
c0d76c2
·
1 Parent(s): 0958d38
Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -1,11 +1,24 @@
1
  import spaces
2
  import gradio as gr
3
- from transformers import Trainer, TrainingArguments, AutoTokenizer, AutoModelForSeq2SeqLM
4
  from datasets import load_dataset
5
  import traceback
6
  from huggingface_hub import login
7
  from peft import get_peft_model, LoraConfig
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  @spaces.GPU
10
  def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
11
  try:
@@ -21,7 +34,7 @@ def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch
21
 
22
  # Load the model and tokenizer
23
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name.strip(), num_labels=2)
24
- #model = get_peft_model(model, lora_config)
25
  tokenizer = AutoTokenizer.from_pretrained(model_name)
26
 
27
  # Tokenize the dataset
@@ -37,9 +50,9 @@ def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch
37
  eval_strategy="epoch",
38
  save_strategy='epoch',
39
  learning_rate=lr*0.00001,
40
- per_device_train_batch_size=batch_size,
41
- per_device_eval_batch_size=batch_size,
42
- num_train_epochs=num_epochs,
43
  weight_decay=0.01,
44
  gradient_accumulation_steps=grad*0.1,
45
  load_best_model_at_end=True,
@@ -59,6 +72,7 @@ def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch
59
  args=training_args,
60
  train_dataset=tokenized_datasets['train'],
61
  eval_dataset=tokenized_datasets['validation'],
 
62
  )
63
 
64
  # Fine-tune the model
 
1
  import spaces
2
  import gradio as gr
3
+ from transformers import Trainer, TrainingArguments, AutoTokenizer, AutoModelForSeq2SeqLM, TrainerCallback
4
  from datasets import load_dataset
5
  import traceback
6
  from huggingface_hub import login
7
  from peft import get_peft_model, LoraConfig
8
 
9
+ class LoggingCallback(TrainerCallback):
10
+ def on_step_end(self, args, state, control, kwargs):
11
+ # Log the learning rate
12
+ current_lr = state.optimizer.param_groups[0]['lr']
13
+ print(f"Current Learning Rate: {current_lr}")
14
+
15
+ def on_epoch_end(self, args, state, control, kwargs):
16
+ # Log the error rate (assuming you have a metric to calculate it)
17
+ # Here we assume you have a way to get the validation loss
18
+ if state.best_metric is not None:
19
+ error_rate = 1 - state.best_metric # Assuming best_metric is accuracy
20
+ print(f"Current Error Rate: {error_rate:.4f}")
21
+
22
  @spaces.GPU
23
  def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
24
  try:
 
34
 
35
  # Load the model and tokenizer
36
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name.strip(), num_labels=2)
37
+ model = get_peft_model(model, lora_config)
38
  tokenizer = AutoTokenizer.from_pretrained(model_name)
39
 
40
  # Tokenize the dataset
 
50
  eval_strategy="epoch",
51
  save_strategy='epoch',
52
  learning_rate=lr*0.00001,
53
+ per_device_train_batch_size=int(batch_size),
54
+ per_device_eval_batch_size=int(batch_size),
55
+ num_train_epochs=int(num_epochs),
56
  weight_decay=0.01,
57
  gradient_accumulation_steps=grad*0.1,
58
  load_best_model_at_end=True,
 
72
  args=training_args,
73
  train_dataset=tokenized_datasets['train'],
74
  eval_dataset=tokenized_datasets['validation'],
75
+ callbacks=[LoggingCallback()],
76
  )
77
 
78
  # Fine-tune the model