Update train.py
Browse files
train.py
CHANGED
@@ -1,12 +1,14 @@
|
|
|
|
1 |
import unsloth # must be first
|
2 |
import pandas as pd
|
3 |
import torch
|
4 |
from datasets import Dataset
|
5 |
from transformers import TrainingArguments
|
6 |
from unsloth import FastLanguageModel
|
7 |
-
from trl import SFTTrainer
|
8 |
import os
|
9 |
import shutil
|
|
|
10 |
|
11 |
# Load and format your dataset
|
12 |
df = pd.read_csv("data.csv")
|
@@ -38,7 +40,7 @@ tokenized_dataset = dataset.map(tokenize, batched=True)
|
|
38 |
|
39 |
# Set up training
|
40 |
training_args = TrainingArguments(
|
41 |
-
output_dir = "./
|
42 |
per_device_train_batch_size = 2,
|
43 |
num_train_epochs = 3,
|
44 |
learning_rate = 2e-4,
|
@@ -57,15 +59,20 @@ trainer = SFTTrainer(
|
|
57 |
|
58 |
trainer.train()
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
os.makedirs(
|
|
|
63 |
|
|
|
|
|
64 |
try:
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
70 |
except Exception as e:
|
71 |
-
print(f"β Failed to
|
|
|
1 |
+
# β
Final train.py with ZIP logic added
|
2 |
import unsloth # must be first
|
3 |
import pandas as pd
|
4 |
import torch
|
5 |
from datasets import Dataset
|
6 |
from transformers import TrainingArguments
|
7 |
from unsloth import FastLanguageModel
|
8 |
+
from trl import SFTTrainer
|
9 |
import os
|
10 |
import shutil
|
11 |
+
import zipfile
|
12 |
|
13 |
# Load and format your dataset
|
14 |
df = pd.read_csv("data.csv")
|
|
|
40 |
|
41 |
# Set up training
|
42 |
training_args = TrainingArguments(
|
43 |
+
output_dir = "./output_model",
|
44 |
per_device_train_batch_size = 2,
|
45 |
num_train_epochs = 3,
|
46 |
learning_rate = 2e-4,
|
|
|
59 |
|
60 |
trainer.train()
|
61 |
|
62 |
+
# Save the fine-tuned LoRA adapter
|
63 |
+
output_dir = "./output_model"
|
64 |
+
os.makedirs(output_dir, exist_ok=True)
|
65 |
+
model.save_pretrained(output_dir)
|
66 |
|
67 |
+
# β
Zip it for download
|
68 |
+
zip_path = "/home/user/app/model.zip"
|
69 |
try:
|
70 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
71 |
+
for root, _, files in os.walk(output_dir):
|
72 |
+
for file in files:
|
73 |
+
full_path = os.path.join(root, file)
|
74 |
+
rel_path = os.path.relpath(full_path, output_dir)
|
75 |
+
zipf.write(full_path, rel_path)
|
76 |
+
print(f"β
Zipped model to {zip_path}")
|
77 |
except Exception as e:
|
78 |
+
print(f"β Failed to zip model: {e}")
|