krisha06 commited on
Commit
a54c990
·
verified ·
1 Parent(s): a9279c3

Update train.py

Browse files
Files changed (1) hide show
  1. train.py +5 -16
train.py CHANGED
@@ -3,8 +3,6 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
3
  from peft import LoraConfig, get_peft_model
4
  from datasets import load_dataset
5
  from transformers import TrainingArguments, Trainer
6
- from huggingface_hub import Repository
7
- import os
8
 
9
  # Load dataset (StackOverflow Python dataset as an example)
10
  dataset = load_dataset("stackoverflow", "python")
@@ -18,7 +16,7 @@ def format_data(example):
18
  dataset = dataset.map(format_data)
19
 
20
  # Load the Mistral-7B model and tokenizer
21
- model_name = "mistralai/Mistral-7B-v0.1" # You can also use Phi-2
22
  model = AutoModelForCausalLM.from_pretrained(model_name, load_in_8bit=True)
23
  tokenizer = AutoTokenizer.from_pretrained(model_name)
24
 
@@ -33,11 +31,11 @@ model = get_peft_model(model, lora_config)
33
 
34
  # Training arguments
35
  training_args = TrainingArguments(
36
- output_dir="./tuned_model",
37
  per_device_train_batch_size=4,
38
  num_train_epochs=3,
39
- save_strategy="epoch",
40
- save_total_limit=2
41
  )
42
 
43
  # Trainer setup
@@ -50,15 +48,6 @@ trainer = Trainer(
50
  # Start the fine-tuning process
51
  trainer.train()
52
 
53
- # Save the model locally
54
  model.save_pretrained("./tuned_model")
55
  tokenizer.save_pretrained("./tuned_model")
56
-
57
- # Hugging Face Repo setup for uploading the model
58
- repo_name = "krisha06/Python_tutor" # Replace with your repo name
59
- repo = Repository(local_dir="./tuned_model", clone_from=repo_name)
60
-
61
- # Upload the model to Hugging Face Hub
62
- repo.push_to_hub()
63
-
64
- print("Model fine-tuned and uploaded to Hugging Face Hub successfully!")
 
3
  from peft import LoraConfig, get_peft_model
4
  from datasets import load_dataset
5
  from transformers import TrainingArguments, Trainer
 
 
6
 
7
  # Load dataset (StackOverflow Python dataset as an example)
8
  dataset = load_dataset("stackoverflow", "python")
 
16
  dataset = dataset.map(format_data)
17
 
18
  # Load the Mistral-7B model and tokenizer
19
+ model_name = "mistralai/Mistral-7B-v0.1" # or use Phi-2
20
  model = AutoModelForCausalLM.from_pretrained(model_name, load_in_8bit=True)
21
  tokenizer = AutoTokenizer.from_pretrained(model_name)
22
 
 
31
 
32
  # Training arguments
33
  training_args = TrainingArguments(
34
+ output_dir="./tuned_model", # Directory to save the fine-tuned model
35
  per_device_train_batch_size=4,
36
  num_train_epochs=3,
37
+ save_strategy="epoch", # Save model after each epoch
38
+ save_total_limit=2 # Keep only the last 2 saved models
39
  )
40
 
41
  # Trainer setup
 
48
  # Start the fine-tuning process
49
  trainer.train()
50
 
51
+ # Save the model and tokenizer
52
  model.save_pretrained("./tuned_model")
53
  tokenizer.save_pretrained("./tuned_model")