Spaces:
Runtime error
Runtime error
Update fine_tune.py
Browse files- fine_tune.py +29 -8
fine_tune.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
from datasets import load_dataset
|
|
@@ -17,18 +20,20 @@ import uvicorn
|
|
| 17 |
base_model_name = "unsloth/llama-3-8b-Instruct-bnb-4bit"
|
| 18 |
output_dir = "/data/fine_tuning"
|
| 19 |
dataset_path = "dataset.jsonl"
|
| 20 |
-
hub_model_id = "Nutnell/direct-ed-finetune-job"
|
| 21 |
|
| 22 |
# --- Initialize model and tokenizer variables ---
|
| 23 |
model = None
|
| 24 |
tokenizer = None
|
| 25 |
|
| 26 |
# --- Training Logic ---
|
|
|
|
| 27 |
if not os.path.exists(os.path.join(output_dir, 'adapter_config.json')):
|
| 28 |
print("No fine-tuned model found. Starting training...")
|
| 29 |
|
|
|
|
| 30 |
dataset = load_dataset("json", data_files=dataset_path, split="train")
|
| 31 |
|
|
|
|
| 32 |
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
base_model_name,
|
| 34 |
device_map="auto",
|
|
@@ -51,6 +56,7 @@ if not os.path.exists(os.path.join(output_dir, 'adapter_config.json')):
|
|
| 51 |
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
training_arguments = TrainingArguments(
|
| 55 |
output_dir=output_dir,
|
| 56 |
num_train_epochs=1,
|
|
@@ -66,42 +72,52 @@ if not os.path.exists(os.path.join(output_dir, 'adapter_config.json')):
|
|
| 66 |
group_by_length=True,
|
| 67 |
lr_scheduler_type="linear",
|
| 68 |
push_to_hub=True,
|
| 69 |
-
hub_model_id=
|
| 70 |
)
|
| 71 |
|
|
|
|
| 72 |
trainer = SFTTrainer(
|
| 73 |
model=model,
|
| 74 |
train_dataset=dataset,
|
| 75 |
peft_config=peft_config,
|
| 76 |
-
dataset_text_field="text",
|
| 77 |
args=training_arguments,
|
| 78 |
)
|
| 79 |
|
|
|
|
| 80 |
trainer.train()
|
|
|
|
|
|
|
| 81 |
trainer.model.save_pretrained(output_dir)
|
| 82 |
print(f"Fine-tuned model adapter saved to {output_dir}")
|
| 83 |
|
| 84 |
-
# Push trained model to Hub
|
| 85 |
-
trainer.push_to_hub()
|
| 86 |
-
|
| 87 |
model = trainer.model
|
| 88 |
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
print("Found existing fine-tuned model. Loading for inference...")
|
|
|
|
|
|
|
| 91 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 92 |
base_model_name,
|
| 93 |
device_map="auto",
|
| 94 |
trust_remote_code=True,
|
| 95 |
)
|
|
|
|
| 96 |
model = PeftModel.from_pretrained(base_model, output_dir)
|
| 97 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
|
| 98 |
|
| 99 |
-
|
|
|
|
| 100 |
print("Setting up inference pipeline...")
|
| 101 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device_map="auto")
|
| 102 |
print("Inference pipeline ready.")
|
| 103 |
|
|
|
|
| 104 |
# --- FastAPI App ---
|
|
|
|
|
|
|
| 105 |
class GenerateRequest(BaseModel):
|
| 106 |
prompt: str
|
| 107 |
|
|
@@ -113,6 +129,11 @@ def home():
|
|
| 113 |
|
| 114 |
@app.post("/generate")
|
| 115 |
def generate(request: GenerateRequest):
|
|
|
|
| 116 |
formatted_prompt = f"<|start_header_id|>user<|end_header_id|>\n\n{request.prompt}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
|
|
|
| 117 |
outputs = pipe(formatted_prompt, max_new_tokens=200, do_sample=True, temperature=0.7)
|
| 118 |
-
return {"response": outputs[0]["generated_text"]}
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
import os
|
| 5 |
import torch
|
| 6 |
from datasets import load_dataset
|
|
|
|
| 20 |
base_model_name = "unsloth/llama-3-8b-Instruct-bnb-4bit"
|
| 21 |
output_dir = "/data/fine_tuning"
|
| 22 |
dataset_path = "dataset.jsonl"
|
|
|
|
| 23 |
|
| 24 |
# --- Initialize model and tokenizer variables ---
|
| 25 |
model = None
|
| 26 |
tokenizer = None
|
| 27 |
|
| 28 |
# --- Training Logic ---
|
| 29 |
+
# Check if a fine-tuned model adapter already exists
|
| 30 |
if not os.path.exists(os.path.join(output_dir, 'adapter_config.json')):
|
| 31 |
print("No fine-tuned model found. Starting training...")
|
| 32 |
|
| 33 |
+
# Load dataset
|
| 34 |
dataset = load_dataset("json", data_files=dataset_path, split="train")
|
| 35 |
|
| 36 |
+
# Load base model for training
|
| 37 |
model = AutoModelForCausalLM.from_pretrained(
|
| 38 |
base_model_name,
|
| 39 |
device_map="auto",
|
|
|
|
| 56 |
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Training args
|
| 60 |
training_arguments = TrainingArguments(
|
| 61 |
output_dir=output_dir,
|
| 62 |
num_train_epochs=1,
|
|
|
|
| 72 |
group_by_length=True,
|
| 73 |
lr_scheduler_type="linear",
|
| 74 |
push_to_hub=True,
|
| 75 |
+
hub_model_id = "Nutnell/direct-ed-finetune-job"
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# Initialize Trainer
|
| 79 |
trainer = SFTTrainer(
|
| 80 |
model=model,
|
| 81 |
train_dataset=dataset,
|
| 82 |
peft_config=peft_config,
|
| 83 |
+
dataset_text_field="text", # Ensure your dataset has a 'text' column
|
| 84 |
args=training_arguments,
|
| 85 |
)
|
| 86 |
|
| 87 |
+
# Train the model
|
| 88 |
trainer.train()
|
| 89 |
+
|
| 90 |
+
# Save the trained adapter
|
| 91 |
trainer.model.save_pretrained(output_dir)
|
| 92 |
print(f"Fine-tuned model adapter saved to {output_dir}")
|
| 93 |
|
|
|
|
|
|
|
|
|
|
| 94 |
model = trainer.model
|
| 95 |
|
| 96 |
+
# --- Inference Logic ---
|
| 97 |
+
# If training did not run, load the existing model
|
| 98 |
else:
|
| 99 |
print("Found existing fine-tuned model. Loading for inference...")
|
| 100 |
+
|
| 101 |
+
# Load the base model
|
| 102 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 103 |
base_model_name,
|
| 104 |
device_map="auto",
|
| 105 |
trust_remote_code=True,
|
| 106 |
)
|
| 107 |
+
# Apply the PEFT adapter
|
| 108 |
model = PeftModel.from_pretrained(base_model, output_dir)
|
| 109 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
|
| 110 |
|
| 111 |
+
|
| 112 |
+
# --- Create Inference Pipeline ---
|
| 113 |
print("Setting up inference pipeline...")
|
| 114 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device_map="auto")
|
| 115 |
print("Inference pipeline ready.")
|
| 116 |
|
| 117 |
+
|
| 118 |
# --- FastAPI App ---
|
| 119 |
+
|
| 120 |
+
# PYDANTIC MODEL FOR THE REQUEST BODY
|
| 121 |
class GenerateRequest(BaseModel):
|
| 122 |
prompt: str
|
| 123 |
|
|
|
|
| 129 |
|
| 130 |
@app.post("/generate")
|
| 131 |
def generate(request: GenerateRequest):
|
| 132 |
+
# Access the prompt from the request object
|
| 133 |
formatted_prompt = f"<|start_header_id|>user<|end_header_id|>\n\n{request.prompt}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 134 |
+
|
| 135 |
outputs = pipe(formatted_prompt, max_new_tokens=200, do_sample=True, temperature=0.7)
|
| 136 |
+
return {"response": outputs[0]["generated_text"]}
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|