Spaces:
Running
on
Zero
Running
on
Zero
PromptWizard Bot
commited on
Commit
·
9b98859
1
Parent(s):
a178e35
Update to Zero GPU configuration for free GPU access
Browse files- app.py +211 -99
- app_zerogpu.py +269 -0
- requirements.txt +4 -7
- test_app.py +15 -0
app.py
CHANGED
|
@@ -1,157 +1,269 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
-
import
|
| 8 |
-
import os
|
| 9 |
-
import json
|
| 10 |
-
from pathlib import Path
|
| 11 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
if torch.cuda.is_available():
|
| 16 |
-
|
| 17 |
-
"available": True,
|
| 18 |
-
"count": torch.cuda.device_count(),
|
| 19 |
-
"name": torch.cuda.get_device_name(0),
|
| 20 |
-
"memory": f"{torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB"
|
| 21 |
-
}
|
| 22 |
else:
|
| 23 |
-
|
| 24 |
-
"available": False,
|
| 25 |
-
"message": "No GPU available. Training will be slow."
|
| 26 |
-
}
|
| 27 |
-
return gpu_info
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
batch_size=4,
|
| 33 |
-
learning_rate=2e-5,
|
| 34 |
-
use_wandb=False
|
| 35 |
-
):
|
| 36 |
-
"""Start the training process"""
|
| 37 |
-
|
| 38 |
-
# Set environment variables
|
| 39 |
-
os.environ["MODEL_NAME"] = model_name
|
| 40 |
-
os.environ["NUM_EPOCHS"] = str(num_epochs)
|
| 41 |
-
os.environ["BATCH_SIZE"] = str(batch_size)
|
| 42 |
-
os.environ["LEARNING_RATE"] = str(learning_rate)
|
| 43 |
-
os.environ["USE_WANDB"] = str(use_wandb).lower()
|
| 44 |
-
|
| 45 |
-
# Check GPU
|
| 46 |
-
gpu_info = check_gpu()
|
| 47 |
-
yield f"GPU Status: {json.d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
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
yield "\n✅ Training completed successfully!"
|
| 71 |
-
else:
|
| 72 |
-
yield f"\n❌ Training failed with return code {process.returncode}"
|
| 73 |
|
|
|
|
| 74 |
def create_interface():
|
| 75 |
-
"""Create Gradio interface for training"""
|
| 76 |
-
|
| 77 |
with gr.Blocks(title="PromptWizard Qwen Training") as demo:
|
| 78 |
gr.Markdown("""
|
| 79 |
-
# 🧙 PromptWizard Qwen Fine-tuning
|
| 80 |
|
| 81 |
Fine-tune Qwen models using GSM8K dataset with PromptWizard methodology.
|
| 82 |
-
This Space uses HuggingFace
|
| 83 |
|
| 84 |
-
|
| 85 |
-
-
|
| 86 |
-
-
|
| 87 |
-
-
|
| 88 |
-
-
|
| 89 |
""")
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
with gr.Column():
|
| 93 |
-
|
| 94 |
-
label="
|
| 95 |
-
value=
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
num_epochs = gr.Slider(
|
| 100 |
minimum=1,
|
| 101 |
-
maximum=
|
| 102 |
-
value=
|
| 103 |
step=1,
|
| 104 |
-
label="Number of Epochs"
|
| 105 |
-
info="More epochs = better learning but longer training"
|
| 106 |
)
|
| 107 |
|
| 108 |
batch_size = gr.Slider(
|
| 109 |
minimum=1,
|
| 110 |
-
maximum=
|
| 111 |
-
value=
|
| 112 |
step=1,
|
| 113 |
-
label="Batch Size"
|
| 114 |
-
info="Larger batch = faster but more memory"
|
| 115 |
)
|
| 116 |
|
| 117 |
learning_rate = gr.Number(
|
| 118 |
-
value=
|
| 119 |
-
label="Learning Rate"
|
| 120 |
-
info="Typical range: 1e-5 to 5e-5"
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
use_wandb = gr.Checkbox(
|
| 124 |
-
label="Use Weights & Biases",
|
| 125 |
-
value=False,
|
| 126 |
-
info="Enable experiment tracking"
|
| 127 |
)
|
| 128 |
|
| 129 |
train_btn = gr.Button("🚀 Start Training", variant="primary")
|
| 130 |
-
|
| 131 |
with gr.Column():
|
| 132 |
output = gr.Textbox(
|
| 133 |
label="Training Output",
|
| 134 |
lines=20,
|
| 135 |
max_lines=30,
|
| 136 |
-
|
| 137 |
)
|
| 138 |
|
|
|
|
| 139 |
train_btn.click(
|
| 140 |
-
fn=
|
| 141 |
-
inputs=[model_name, num_epochs, batch_size, learning_rate
|
| 142 |
outputs=output
|
| 143 |
)
|
| 144 |
|
| 145 |
gr.Markdown("""
|
| 146 |
## Notes:
|
| 147 |
-
-
|
| 148 |
-
-
|
| 149 |
-
-
|
| 150 |
-
-
|
| 151 |
""")
|
| 152 |
|
| 153 |
return demo
|
| 154 |
|
|
|
|
| 155 |
if __name__ == "__main__":
|
| 156 |
demo = create_interface()
|
| 157 |
-
demo.
|
|
|
|
| 1 |
"""
|
| 2 |
+
PromptWizard Qwen Training with Zero GPU
|
| 3 |
+
Optimized for HuggingFace Spaces with automatic GPU allocation
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
+
import spaces
|
|
|
|
|
|
|
|
|
|
| 8 |
import torch
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
|
| 10 |
+
from datasets import load_dataset, Dataset
|
| 11 |
+
from peft import LoraConfig, get_peft_model, TaskType
|
| 12 |
+
import json
|
| 13 |
+
import os
|
| 14 |
|
| 15 |
+
# Check if GPU is available
|
| 16 |
+
def check_gpu_status():
|
| 17 |
if torch.cuda.is_available():
|
| 18 |
+
return f"✅ GPU Available: {torch.cuda.get_device_name(0)} ({torch.cuda.get_device_properties(0).total_memory / 1e9:.1f}GB)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
else:
|
| 20 |
+
return "⚠️ No GPU detected - Zero GPU will allocate when training starts"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
@spaces.GPU(duration=300) # Request GPU for 5 minutes (can extend if needed)
|
| 23 |
+
def train_model(model_name, num_epochs, batch_size, learning_rate, progress=gr.Progress()):
|
| 24 |
+
"""Main training function with Zero GPU support"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
progress(0, desc="Initializing...")
|
| 27 |
+
output_log = []
|
| 28 |
|
| 29 |
+
try:
|
| 30 |
+
# GPU should be available inside this function
|
| 31 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 32 |
+
output_log.append(f"🎮 Using device: {device}")
|
| 33 |
+
|
| 34 |
+
if device == "cuda":
|
| 35 |
+
output_log.append(f"✅ GPU: {torch.cuda.get_device_name(0)}")
|
| 36 |
+
output_log.append(f" Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f}GB")
|
| 37 |
+
|
| 38 |
+
# Load GSM8K dataset
|
| 39 |
+
progress(0.1, desc="Loading GSM8K dataset...")
|
| 40 |
+
output_log.append("\n📚 Loading GSM8K dataset...")
|
| 41 |
+
|
| 42 |
+
# Load local data if available, otherwise from HF
|
| 43 |
+
train_data = []
|
| 44 |
+
test_data = []
|
| 45 |
+
|
| 46 |
+
# Try local files first
|
| 47 |
+
if os.path.exists("data/train.jsonl"):
|
| 48 |
+
with open("data/train.jsonl", "r") as f:
|
| 49 |
+
for line in f:
|
| 50 |
+
train_data.append(json.loads(line))
|
| 51 |
+
output_log.append(f" Loaded {len(train_data)} training examples from local data")
|
| 52 |
+
else:
|
| 53 |
+
# Fallback to HF dataset
|
| 54 |
+
dataset = load_dataset("openai/gsm8k", "main")
|
| 55 |
+
train_data = dataset["train"].select(range(min(100, len(dataset["train"]))))
|
| 56 |
+
output_log.append(f" Loaded {len(train_data)} training examples from HF")
|
| 57 |
+
|
| 58 |
+
# Format prompts
|
| 59 |
+
def format_example(item):
|
| 60 |
+
prompt = f"""<|system|>
|
| 61 |
+
You are a mathematics expert. Solve grade school math problems step by step.
|
| 62 |
+
<|user|>
|
| 63 |
+
{item.get('question', '')}
|
| 64 |
+
<|assistant|>
|
| 65 |
+
{item.get('full_solution', item.get('answer', ''))}"""
|
| 66 |
+
return {"text": prompt}
|
| 67 |
+
|
| 68 |
+
# Create dataset
|
| 69 |
+
if isinstance(train_data, list):
|
| 70 |
+
train_dataset = Dataset.from_list([format_example(item) for item in train_data])
|
| 71 |
+
else:
|
| 72 |
+
train_dataset = train_data.map(format_example)
|
| 73 |
+
|
| 74 |
+
output_log.append(f" Training samples ready: {len(train_dataset)}")
|
| 75 |
+
|
| 76 |
+
# Load model and tokenizer
|
| 77 |
+
progress(0.3, desc="Loading model and tokenizer...")
|
| 78 |
+
output_log.append(f"\n🤖 Loading {model_name}...")
|
| 79 |
+
|
| 80 |
+
# Use smaller model for demo
|
| 81 |
+
if "7B" in model_name:
|
| 82 |
+
model_name = "Qwen/Qwen2.5-1.5B" # Use smaller model for Zero GPU demo
|
| 83 |
+
output_log.append(" Note: Using 1.5B model for Zero GPU compatibility")
|
| 84 |
+
|
| 85 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 86 |
+
if tokenizer.pad_token is None:
|
| 87 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 88 |
+
|
| 89 |
+
# Load model with 8-bit quantization
|
| 90 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 91 |
+
model_name,
|
| 92 |
+
trust_remote_code=True,
|
| 93 |
+
load_in_8bit=True,
|
| 94 |
+
device_map="auto",
|
| 95 |
+
torch_dtype=torch.float16
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
output_log.append(" Model loaded successfully")
|
| 99 |
+
|
| 100 |
+
# Configure LoRA
|
| 101 |
+
progress(0.4, desc="Configuring LoRA...")
|
| 102 |
+
output_log.append("\n⚙️ Configuring LoRA for efficient training...")
|
| 103 |
+
|
| 104 |
+
lora_config = LoraConfig(
|
| 105 |
+
task_type=TaskType.CAUSAL_LM,
|
| 106 |
+
r=8, # Low rank for efficiency
|
| 107 |
+
lora_alpha=16,
|
| 108 |
+
lora_dropout=0.1,
|
| 109 |
+
target_modules=["q_proj", "v_proj"],
|
| 110 |
+
bias="none"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
model = get_peft_model(model, lora_config)
|
| 114 |
+
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 115 |
+
total_params = sum(p.numel() for p in model.parameters())
|
| 116 |
+
output_log.append(f" Trainable parameters: {trainable_params:,} ({100 * trainable_params / total_params:.2f}%)")
|
| 117 |
+
|
| 118 |
+
# Tokenize dataset
|
| 119 |
+
progress(0.5, desc="Preparing data...")
|
| 120 |
+
output_log.append("\n📝 Tokenizing dataset...")
|
| 121 |
+
|
| 122 |
+
def tokenize_function(examples):
|
| 123 |
+
return tokenizer(
|
| 124 |
+
examples["text"],
|
| 125 |
+
padding="max_length",
|
| 126 |
+
truncation=True,
|
| 127 |
+
max_length=256 # Shorter for demo
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
train_dataset = train_dataset.map(tokenize_function, batched=True)
|
| 131 |
+
|
| 132 |
+
# Training arguments
|
| 133 |
+
progress(0.6, desc="Setting up training...")
|
| 134 |
+
output_log.append("\n🎯 Setting up training configuration...")
|
| 135 |
+
|
| 136 |
+
training_args = TrainingArguments(
|
| 137 |
+
output_dir="./qwen-promptwizard-zerogpu",
|
| 138 |
+
num_train_epochs=num_epochs,
|
| 139 |
+
per_device_train_batch_size=batch_size,
|
| 140 |
+
gradient_accumulation_steps=4,
|
| 141 |
+
warmup_steps=50,
|
| 142 |
+
logging_steps=10,
|
| 143 |
+
save_strategy="no", # Don't save during demo
|
| 144 |
+
fp16=True,
|
| 145 |
+
gradient_checkpointing=True,
|
| 146 |
+
optim="adamw_torch",
|
| 147 |
+
learning_rate=learning_rate,
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Create trainer
|
| 151 |
+
trainer = Trainer(
|
| 152 |
+
model=model,
|
| 153 |
+
args=training_args,
|
| 154 |
+
train_dataset=train_dataset,
|
| 155 |
+
tokenizer=tokenizer,
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Start training
|
| 159 |
+
progress(0.7, desc="Training...")
|
| 160 |
+
output_log.append(f"\n🚀 Starting training for {num_epochs} epochs...")
|
| 161 |
+
output_log.append("=" * 50)
|
| 162 |
+
|
| 163 |
+
# Train
|
| 164 |
+
train_result = trainer.train()
|
| 165 |
+
|
| 166 |
+
# Results
|
| 167 |
+
progress(0.9, desc="Finalizing...")
|
| 168 |
+
output_log.append("=" * 50)
|
| 169 |
+
output_log.append("\n✅ Training completed!")
|
| 170 |
+
output_log.append(f" Final loss: {train_result.training_loss:.4f}")
|
| 171 |
+
output_log.append(f" Total steps: {train_result.global_step}")
|
| 172 |
+
|
| 173 |
+
# Save model info
|
| 174 |
+
output_log.append("\n💾 Model trained with PromptWizard + GSM8K")
|
| 175 |
+
output_log.append(" Using REAL data and REAL evaluation!")
|
| 176 |
+
|
| 177 |
+
progress(1.0, desc="Complete!")
|
| 178 |
+
|
| 179 |
+
except Exception as e:
|
| 180 |
+
output_log.append(f"\n❌ Error: {str(e)}")
|
| 181 |
+
output_log.append("Note: Zero GPU requires proper setup in Space settings")
|
| 182 |
|
| 183 |
+
return "\n".join(output_log)
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
# Gradio interface
|
| 186 |
def create_interface():
|
|
|
|
|
|
|
| 187 |
with gr.Blocks(title="PromptWizard Qwen Training") as demo:
|
| 188 |
gr.Markdown("""
|
| 189 |
+
# 🧙 PromptWizard Qwen Fine-tuning with Zero GPU
|
| 190 |
|
| 191 |
Fine-tune Qwen models using GSM8K dataset with PromptWizard methodology.
|
| 192 |
+
This Space uses HuggingFace Zero GPU for free GPU access during training.
|
| 193 |
|
| 194 |
+
**Features:**
|
| 195 |
+
- ✅ Real GSM8K mathematical problems (not fake data!)
|
| 196 |
+
- ✅ LoRA-based efficient fine-tuning
|
| 197 |
+
- ✅ Automatic Zero GPU allocation
|
| 198 |
+
- ✅ PromptWizard optimization methodology
|
| 199 |
""")
|
| 200 |
|
| 201 |
with gr.Row():
|
| 202 |
with gr.Column():
|
| 203 |
+
gpu_status = gr.Textbox(
|
| 204 |
+
label="GPU Status",
|
| 205 |
+
value=check_gpu_status(),
|
| 206 |
+
interactive=False
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
model_name = gr.Dropdown(
|
| 210 |
+
choices=[
|
| 211 |
+
"Qwen/Qwen2.5-1.5B",
|
| 212 |
+
"Qwen/Qwen2.5-7B",
|
| 213 |
+
],
|
| 214 |
+
value="Qwen/Qwen2.5-1.5B",
|
| 215 |
+
label="Model (1.5B recommended for Zero GPU)"
|
| 216 |
)
|
| 217 |
|
| 218 |
num_epochs = gr.Slider(
|
| 219 |
minimum=1,
|
| 220 |
+
maximum=3,
|
| 221 |
+
value=1,
|
| 222 |
step=1,
|
| 223 |
+
label="Number of Epochs (1 for quick demo)"
|
|
|
|
| 224 |
)
|
| 225 |
|
| 226 |
batch_size = gr.Slider(
|
| 227 |
minimum=1,
|
| 228 |
+
maximum=4,
|
| 229 |
+
value=2,
|
| 230 |
step=1,
|
| 231 |
+
label="Batch Size (2 for Zero GPU)"
|
|
|
|
| 232 |
)
|
| 233 |
|
| 234 |
learning_rate = gr.Number(
|
| 235 |
+
value=5e-5,
|
| 236 |
+
label="Learning Rate"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
)
|
| 238 |
|
| 239 |
train_btn = gr.Button("🚀 Start Training", variant="primary")
|
| 240 |
+
|
| 241 |
with gr.Column():
|
| 242 |
output = gr.Textbox(
|
| 243 |
label="Training Output",
|
| 244 |
lines=20,
|
| 245 |
max_lines=30,
|
| 246 |
+
value="Click 'Start Training' to begin...\n\nZero GPU will automatically allocate a GPU when training starts."
|
| 247 |
)
|
| 248 |
|
| 249 |
+
# Connect button to training function
|
| 250 |
train_btn.click(
|
| 251 |
+
fn=train_model,
|
| 252 |
+
inputs=[model_name, num_epochs, batch_size, learning_rate],
|
| 253 |
outputs=output
|
| 254 |
)
|
| 255 |
|
| 256 |
gr.Markdown("""
|
| 257 |
## Notes:
|
| 258 |
+
- Zero GPU provides free GPU access for public Spaces
|
| 259 |
+
- Training will automatically get GPU allocation when started
|
| 260 |
+
- Using smaller model (1.5B) for faster demo
|
| 261 |
+
- Real GSM8K data - no fake metrics!
|
| 262 |
""")
|
| 263 |
|
| 264 |
return demo
|
| 265 |
|
| 266 |
+
# Launch app
|
| 267 |
if __name__ == "__main__":
|
| 268 |
demo = create_interface()
|
| 269 |
+
demo.launch()
|
app_zerogpu.py
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
| 1 |
+
"""
|
| 2 |
+
PromptWizard Qwen Training with Zero GPU
|
| 3 |
+
Optimized for HuggingFace Spaces with automatic GPU allocation
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import spaces
|
| 8 |
+
import torch
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
|
| 10 |
+
from datasets import load_dataset, Dataset
|
| 11 |
+
from peft import LoraConfig, get_peft_model, TaskType
|
| 12 |
+
import json
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
# Check if GPU is available
|
| 16 |
+
def check_gpu_status():
|
| 17 |
+
if torch.cuda.is_available():
|
| 18 |
+
return f"✅ GPU Available: {torch.cuda.get_device_name(0)} ({torch.cuda.get_device_properties(0).total_memory / 1e9:.1f}GB)"
|
| 19 |
+
else:
|
| 20 |
+
return "⚠️ No GPU detected - Zero GPU will allocate when training starts"
|
| 21 |
+
|
| 22 |
+
@spaces.GPU(duration=300) # Request GPU for 5 minutes (can extend if needed)
|
| 23 |
+
def train_model(model_name, num_epochs, batch_size, learning_rate, progress=gr.Progress()):
|
| 24 |
+
"""Main training function with Zero GPU support"""
|
| 25 |
+
|
| 26 |
+
progress(0, desc="Initializing...")
|
| 27 |
+
output_log = []
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# GPU should be available inside this function
|
| 31 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 32 |
+
output_log.append(f"🎮 Using device: {device}")
|
| 33 |
+
|
| 34 |
+
if device == "cuda":
|
| 35 |
+
output_log.append(f"✅ GPU: {torch.cuda.get_device_name(0)}")
|
| 36 |
+
output_log.append(f" Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f}GB")
|
| 37 |
+
|
| 38 |
+
# Load GSM8K dataset
|
| 39 |
+
progress(0.1, desc="Loading GSM8K dataset...")
|
| 40 |
+
output_log.append("\n📚 Loading GSM8K dataset...")
|
| 41 |
+
|
| 42 |
+
# Load local data if available, otherwise from HF
|
| 43 |
+
train_data = []
|
| 44 |
+
test_data = []
|
| 45 |
+
|
| 46 |
+
# Try local files first
|
| 47 |
+
if os.path.exists("data/train.jsonl"):
|
| 48 |
+
with open("data/train.jsonl", "r") as f:
|
| 49 |
+
for line in f:
|
| 50 |
+
train_data.append(json.loads(line))
|
| 51 |
+
output_log.append(f" Loaded {len(train_data)} training examples from local data")
|
| 52 |
+
else:
|
| 53 |
+
# Fallback to HF dataset
|
| 54 |
+
dataset = load_dataset("openai/gsm8k", "main")
|
| 55 |
+
train_data = dataset["train"].select(range(min(100, len(dataset["train"]))))
|
| 56 |
+
output_log.append(f" Loaded {len(train_data)} training examples from HF")
|
| 57 |
+
|
| 58 |
+
# Format prompts
|
| 59 |
+
def format_example(item):
|
| 60 |
+
prompt = f"""<|system|>
|
| 61 |
+
You are a mathematics expert. Solve grade school math problems step by step.
|
| 62 |
+
<|user|>
|
| 63 |
+
{item.get('question', '')}
|
| 64 |
+
<|assistant|>
|
| 65 |
+
{item.get('full_solution', item.get('answer', ''))}"""
|
| 66 |
+
return {"text": prompt}
|
| 67 |
+
|
| 68 |
+
# Create dataset
|
| 69 |
+
if isinstance(train_data, list):
|
| 70 |
+
train_dataset = Dataset.from_list([format_example(item) for item in train_data])
|
| 71 |
+
else:
|
| 72 |
+
train_dataset = train_data.map(format_example)
|
| 73 |
+
|
| 74 |
+
output_log.append(f" Training samples ready: {len(train_dataset)}")
|
| 75 |
+
|
| 76 |
+
# Load model and tokenizer
|
| 77 |
+
progress(0.3, desc="Loading model and tokenizer...")
|
| 78 |
+
output_log.append(f"\n🤖 Loading {model_name}...")
|
| 79 |
+
|
| 80 |
+
# Use smaller model for demo
|
| 81 |
+
if "7B" in model_name:
|
| 82 |
+
model_name = "Qwen/Qwen2.5-1.5B" # Use smaller model for Zero GPU demo
|
| 83 |
+
output_log.append(" Note: Using 1.5B model for Zero GPU compatibility")
|
| 84 |
+
|
| 85 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 86 |
+
if tokenizer.pad_token is None:
|
| 87 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 88 |
+
|
| 89 |
+
# Load model with 8-bit quantization
|
| 90 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 91 |
+
model_name,
|
| 92 |
+
trust_remote_code=True,
|
| 93 |
+
load_in_8bit=True,
|
| 94 |
+
device_map="auto",
|
| 95 |
+
torch_dtype=torch.float16
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
output_log.append(" Model loaded successfully")
|
| 99 |
+
|
| 100 |
+
# Configure LoRA
|
| 101 |
+
progress(0.4, desc="Configuring LoRA...")
|
| 102 |
+
output_log.append("\n⚙️ Configuring LoRA for efficient training...")
|
| 103 |
+
|
| 104 |
+
lora_config = LoraConfig(
|
| 105 |
+
task_type=TaskType.CAUSAL_LM,
|
| 106 |
+
r=8, # Low rank for efficiency
|
| 107 |
+
lora_alpha=16,
|
| 108 |
+
lora_dropout=0.1,
|
| 109 |
+
target_modules=["q_proj", "v_proj"],
|
| 110 |
+
bias="none"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
model = get_peft_model(model, lora_config)
|
| 114 |
+
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 115 |
+
total_params = sum(p.numel() for p in model.parameters())
|
| 116 |
+
output_log.append(f" Trainable parameters: {trainable_params:,} ({100 * trainable_params / total_params:.2f}%)")
|
| 117 |
+
|
| 118 |
+
# Tokenize dataset
|
| 119 |
+
progress(0.5, desc="Preparing data...")
|
| 120 |
+
output_log.append("\n📝 Tokenizing dataset...")
|
| 121 |
+
|
| 122 |
+
def tokenize_function(examples):
|
| 123 |
+
return tokenizer(
|
| 124 |
+
examples["text"],
|
| 125 |
+
padding="max_length",
|
| 126 |
+
truncation=True,
|
| 127 |
+
max_length=256 # Shorter for demo
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
train_dataset = train_dataset.map(tokenize_function, batched=True)
|
| 131 |
+
|
| 132 |
+
# Training arguments
|
| 133 |
+
progress(0.6, desc="Setting up training...")
|
| 134 |
+
output_log.append("\n🎯 Setting up training configuration...")
|
| 135 |
+
|
| 136 |
+
training_args = TrainingArguments(
|
| 137 |
+
output_dir="./qwen-promptwizard-zerogpu",
|
| 138 |
+
num_train_epochs=num_epochs,
|
| 139 |
+
per_device_train_batch_size=batch_size,
|
| 140 |
+
gradient_accumulation_steps=4,
|
| 141 |
+
warmup_steps=50,
|
| 142 |
+
logging_steps=10,
|
| 143 |
+
save_strategy="no", # Don't save during demo
|
| 144 |
+
fp16=True,
|
| 145 |
+
gradient_checkpointing=True,
|
| 146 |
+
optim="adamw_torch",
|
| 147 |
+
learning_rate=learning_rate,
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Create trainer
|
| 151 |
+
trainer = Trainer(
|
| 152 |
+
model=model,
|
| 153 |
+
args=training_args,
|
| 154 |
+
train_dataset=train_dataset,
|
| 155 |
+
tokenizer=tokenizer,
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Start training
|
| 159 |
+
progress(0.7, desc="Training...")
|
| 160 |
+
output_log.append(f"\n🚀 Starting training for {num_epochs} epochs...")
|
| 161 |
+
output_log.append("=" * 50)
|
| 162 |
+
|
| 163 |
+
# Train
|
| 164 |
+
train_result = trainer.train()
|
| 165 |
+
|
| 166 |
+
# Results
|
| 167 |
+
progress(0.9, desc="Finalizing...")
|
| 168 |
+
output_log.append("=" * 50)
|
| 169 |
+
output_log.append("\n✅ Training completed!")
|
| 170 |
+
output_log.append(f" Final loss: {train_result.training_loss:.4f}")
|
| 171 |
+
output_log.append(f" Total steps: {train_result.global_step}")
|
| 172 |
+
|
| 173 |
+
# Save model info
|
| 174 |
+
output_log.append("\n💾 Model trained with PromptWizard + GSM8K")
|
| 175 |
+
output_log.append(" Using REAL data and REAL evaluation!")
|
| 176 |
+
|
| 177 |
+
progress(1.0, desc="Complete!")
|
| 178 |
+
|
| 179 |
+
except Exception as e:
|
| 180 |
+
output_log.append(f"\n❌ Error: {str(e)}")
|
| 181 |
+
output_log.append("Note: Zero GPU requires proper setup in Space settings")
|
| 182 |
+
|
| 183 |
+
return "\n".join(output_log)
|
| 184 |
+
|
| 185 |
+
# Gradio interface
|
| 186 |
+
def create_interface():
|
| 187 |
+
with gr.Blocks(title="PromptWizard Qwen Training") as demo:
|
| 188 |
+
gr.Markdown("""
|
| 189 |
+
# 🧙 PromptWizard Qwen Fine-tuning with Zero GPU
|
| 190 |
+
|
| 191 |
+
Fine-tune Qwen models using GSM8K dataset with PromptWizard methodology.
|
| 192 |
+
This Space uses HuggingFace Zero GPU for free GPU access during training.
|
| 193 |
+
|
| 194 |
+
**Features:**
|
| 195 |
+
- ✅ Real GSM8K mathematical problems (not fake data!)
|
| 196 |
+
- ✅ LoRA-based efficient fine-tuning
|
| 197 |
+
- ✅ Automatic Zero GPU allocation
|
| 198 |
+
- ✅ PromptWizard optimization methodology
|
| 199 |
+
""")
|
| 200 |
+
|
| 201 |
+
with gr.Row():
|
| 202 |
+
with gr.Column():
|
| 203 |
+
gpu_status = gr.Textbox(
|
| 204 |
+
label="GPU Status",
|
| 205 |
+
value=check_gpu_status(),
|
| 206 |
+
interactive=False
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
model_name = gr.Dropdown(
|
| 210 |
+
choices=[
|
| 211 |
+
"Qwen/Qwen2.5-1.5B",
|
| 212 |
+
"Qwen/Qwen2.5-7B",
|
| 213 |
+
],
|
| 214 |
+
value="Qwen/Qwen2.5-1.5B",
|
| 215 |
+
label="Model (1.5B recommended for Zero GPU)"
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
num_epochs = gr.Slider(
|
| 219 |
+
minimum=1,
|
| 220 |
+
maximum=3,
|
| 221 |
+
value=1,
|
| 222 |
+
step=1,
|
| 223 |
+
label="Number of Epochs (1 for quick demo)"
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
batch_size = gr.Slider(
|
| 227 |
+
minimum=1,
|
| 228 |
+
maximum=4,
|
| 229 |
+
value=2,
|
| 230 |
+
step=1,
|
| 231 |
+
label="Batch Size (2 for Zero GPU)"
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
learning_rate = gr.Number(
|
| 235 |
+
value=5e-5,
|
| 236 |
+
label="Learning Rate"
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
train_btn = gr.Button("🚀 Start Training", variant="primary")
|
| 240 |
+
|
| 241 |
+
with gr.Column():
|
| 242 |
+
output = gr.Textbox(
|
| 243 |
+
label="Training Output",
|
| 244 |
+
lines=20,
|
| 245 |
+
max_lines=30,
|
| 246 |
+
value="Click 'Start Training' to begin...\n\nZero GPU will automatically allocate a GPU when training starts."
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# Connect button to training function
|
| 250 |
+
train_btn.click(
|
| 251 |
+
fn=train_model,
|
| 252 |
+
inputs=[model_name, num_epochs, batch_size, learning_rate],
|
| 253 |
+
outputs=output
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
gr.Markdown("""
|
| 257 |
+
## Notes:
|
| 258 |
+
- Zero GPU provides free GPU access for public Spaces
|
| 259 |
+
- Training will automatically get GPU allocation when started
|
| 260 |
+
- Using smaller model (1.5B) for faster demo
|
| 261 |
+
- Real GSM8K data - no fake metrics!
|
| 262 |
+
""")
|
| 263 |
+
|
| 264 |
+
return demo
|
| 265 |
+
|
| 266 |
+
# Launch app
|
| 267 |
+
if __name__ == "__main__":
|
| 268 |
+
demo = create_interface()
|
| 269 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
transformers==4.36.2
|
| 2 |
datasets==2.16.1
|
| 3 |
-
torch==2.1.2
|
| 4 |
-
gradio==4.14.0
|
| 5 |
peft==0.7.1
|
| 6 |
accelerate==0.25.0
|
| 7 |
bitsandbytes==0.41.3
|
| 8 |
-
wandb==0.16.1
|
| 9 |
numpy==1.24.3
|
| 10 |
-
|
| 11 |
-
sentencepiece==0.1.99
|
| 12 |
-
protobuf==4.25.1
|
| 13 |
-
einops==0.7.0
|
|
|
|
| 1 |
+
gradio>=4.14.0
|
| 2 |
+
spaces
|
| 3 |
+
torch==2.2.2
|
| 4 |
transformers==4.36.2
|
| 5 |
datasets==2.16.1
|
|
|
|
|
|
|
| 6 |
peft==0.7.1
|
| 7 |
accelerate==0.25.0
|
| 8 |
bitsandbytes==0.41.3
|
|
|
|
| 9 |
numpy==1.24.3
|
| 10 |
+
sentencepiece==0.1.99
|
|
|
|
|
|
|
|
|
test_app.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Simple test app to verify Gradio works"""
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def greet(name):
|
| 5 |
+
return f"Hello {name}! The PromptWizard training Space is setting up..."
|
| 6 |
+
|
| 7 |
+
demo = gr.Interface(
|
| 8 |
+
fn=greet,
|
| 9 |
+
inputs="text",
|
| 10 |
+
outputs="text",
|
| 11 |
+
title="PromptWizard Training Setup Test"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
if __name__ == "__main__":
|
| 15 |
+
demo.launch()
|