Spaces:
Sleeping
Sleeping
Update train.py
Browse files
train.py
CHANGED
|
@@ -3,6 +3,8 @@ 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 |
|
| 7 |
# Load dataset (StackOverflow Python dataset as an example)
|
| 8 |
dataset = load_dataset("stackoverflow", "python")
|
|
@@ -16,7 +18,7 @@ def format_data(example):
|
|
| 16 |
dataset = dataset.map(format_data)
|
| 17 |
|
| 18 |
# Load the Mistral-7B model and tokenizer
|
| 19 |
-
model_name = "mistralai/Mistral-7B-v0.1" #
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(model_name, load_in_8bit=True)
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
|
|
@@ -48,6 +50,15 @@ trainer = Trainer(
|
|
| 48 |
# Start the fine-tuning process
|
| 49 |
trainer.train()
|
| 50 |
|
| 51 |
-
# Save the model
|
| 52 |
model.save_pretrained("./tuned_model")
|
| 53 |
tokenizer.save_pretrained("./tuned_model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
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 |
|
|
|
|
| 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 = "user06/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!")
|