Upload 3 files
Browse files- app.py +18 -0
- requirements.txt +6 -0
- train.py +60 -0
app.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
|
4 |
+
def run_training():
|
5 |
+
# Execute train.py and capture its output
|
6 |
+
result = subprocess.run(["python", "train.py"], capture_output=True, text=True)
|
7 |
+
return result.stdout + "\n" + result.stderr
|
8 |
+
|
9 |
+
# Create a Gradio interface with no inputs and a text output for logs
|
10 |
+
iface = gr.Interface(
|
11 |
+
fn=run_training,
|
12 |
+
inputs=[],
|
13 |
+
outputs="text",
|
14 |
+
title="LLaMA LoRA Fine-Tuning",
|
15 |
+
description="Click the button below to start fine-tuning the LLaMA 3.2 3B Instruct model using PEFT/LoRA."
|
16 |
+
)
|
17 |
+
|
18 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
unsloth
|
2 |
+
transformers
|
3 |
+
datasets
|
4 |
+
pandas
|
5 |
+
torch
|
6 |
+
trl
|
train.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 # ✅ now works because we added 'trl'
|
8 |
+
|
9 |
+
|
10 |
+
# Load and format your dataset
|
11 |
+
df = pd.read_csv("data.csv")
|
12 |
+
df["text"] = df.apply(lambda row: f"### Instruction:\n{row['instruction']}\n\n### Response:\n{row['response']}\n", axis=1)
|
13 |
+
dataset = Dataset.from_pandas(df[["text"]])
|
14 |
+
|
15 |
+
# Load Unsloth model
|
16 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
17 |
+
model_name = "unsloth/Llama-3.2-3B-Instruct",
|
18 |
+
max_seq_length = 2048,
|
19 |
+
dtype = torch.float16,
|
20 |
+
load_in_4bit = True,
|
21 |
+
)
|
22 |
+
|
23 |
+
# Apply LoRA without task_type
|
24 |
+
model = FastLanguageModel.get_peft_model(
|
25 |
+
model,
|
26 |
+
r = 8,
|
27 |
+
lora_alpha = 32,
|
28 |
+
lora_dropout = 0.05,
|
29 |
+
bias = "none",
|
30 |
+
)
|
31 |
+
|
32 |
+
# Tokenize text
|
33 |
+
def tokenize(example):
|
34 |
+
return tokenizer(example["text"], truncation=True, padding="max_length", max_length=512)
|
35 |
+
|
36 |
+
tokenized_dataset = dataset.map(tokenize, batched=True)
|
37 |
+
|
38 |
+
# Set up training
|
39 |
+
training_args = TrainingArguments(
|
40 |
+
output_dir = "./lora-finetuned",
|
41 |
+
per_device_train_batch_size = 2,
|
42 |
+
num_train_epochs = 3,
|
43 |
+
learning_rate = 2e-4,
|
44 |
+
logging_steps = 10,
|
45 |
+
save_steps = 100,
|
46 |
+
fp16 = True,
|
47 |
+
)
|
48 |
+
|
49 |
+
# Train
|
50 |
+
trainer = SFTTrainer(
|
51 |
+
model = model,
|
52 |
+
tokenizer = tokenizer,
|
53 |
+
args = training_args,
|
54 |
+
train_dataset = tokenized_dataset,
|
55 |
+
)
|
56 |
+
|
57 |
+
trainer.train()
|
58 |
+
|
59 |
+
# Save the fine-tuned LoRA adapter
|
60 |
+
model.save_pretrained("./lora-finetuned")
|