Learn how to train custom Flux.Dev LoRA (Low-Rank Adaptation) models using the Weights API. This guide walks you through creating personalized AI models that can generate images in your specific style or concept.
LoRA models allow you to fine-tune AI image generation to your specific needs. You can train models on your own images to create consistent styles, characters, or concepts that can be used in image generation.
// Example: Prepare training imagesconst trainingImages = [ { url: "https://example.com/image1.jpg", description: "Portrait of a woman with red hair", }, { url: "https://example.com/image2.jpg", description: "Close-up of the same woman smiling", }, { url: "https://example.com/image3.jpg", description: "Woman in different lighting", }, // ... more images];console.log(`Prepared ${trainingImages.length} training images`);
LoRA training is asynchronous and can take several hours. Poll the status to track progress:
Copy
Ask AI
async function checkTrainingStatus(modelId:string) { const status = await client.imageLoraModels.retrieveStatus(modelId); if (!status) { console.log("Model not found or access denied"); return null; } console.log(`Status: ${status.status}`); console.log(`Queue Position: ${status.queuePosition || "N/A"}`); console.log(`Status Text: ${status.shortStatusText || "N/A"}`); switch (status.status) { case "QUEUED": console.log("Your model is waiting in the training queue..."); break; case "PROCESSING": console.log("Your model is being trained..."); break; case "SUCCEEDED": console.log("Training completed successfully!"); break; case "ERRORED": console.log("Training failed. Please check your images and try again."); break; } return status;}// Check status every 5 minutesconst status = await checkTrainingStatus(loraModel.id);
Once training is complete, you can use your LoRA model in image generation:
Copy
Ask AI
// Generate an image using your custom LoRA modelconst imageWithLora = await client.images.create({ prompt: "A portrait of a woman in my_style", loraIds: [loraModel.id], dimensions: "PORTRAIT", numImages: 1,});console.log(`Image generated with custom LoRA: ${imageWithLora.id}`);
For advanced users, you can download your trained LoRA model files. This is useful for:
Using models in other applications
Backing up your trained models
Sharing models with other users
Local development and testing
Copy
Ask AI
// Get a download URL for your trained LoRA modelconst downloadInfo = await client.imageLoraModels.retrieveDownloadURL( loraModel.id);if (downloadInfo.downloadUrl) { console.log("Download URL:", downloadInfo.downloadUrl); console.log("⚠️ URL expires in 5 minutes - download quickly!"); // You can now download the model file // The URL provides direct access to the model file} else { console.log("Model not ready for download or access denied"); console.log("Make sure the model training has completed successfully");}
Here’s a more robust example with proper error handling:
Copy
Ask AI
async function downloadLoraModel(modelId:string) { try { // First, check if the model is ready const model = await client.imageLoraModels.retrieve(modelId); if (!model) { console.log("Model not found or access denied"); return null; } if (model.trainingJob?.status !== 'SUCCEEDED') { console.log("Model training not completed yet"); console.log(`Current status: ${model.trainingJob?.status}`); return null; } // Get the download URL const downloadInfo = await client.imageLoraModels.retrieveDownloadURL(modelId); if (downloadInfo.downloadUrl) { console.log("✅ Download URL obtained successfully!"); console.log("📁 Model file ready for download"); console.log("⏰ URL expires in 5 minutes"); console.log("🔗 URL:", downloadInfo.downloadUrl); return downloadInfo.downloadUrl; } else { console.log("❌ Failed to get download URL"); return null; } } catch (error) { if (error.code === 'UNAUTHORIZED') { console.log("❌ Authentication failed - check your API key"); } else if (error.code === 'FORBIDDEN') { console.log("❌ Access denied - model may not belong to you"); } else if (error.code === 'NOT_FOUND') { console.log("❌ Model not found"); } else { console.log("❌ Error downloading model:", error.message); } return null; }}// Usage exampleconst downloadUrl = await downloadLoraModel(loraModel.id);if (downloadUrl) { // Use the download URL to fetch the model file // You can use fetch(), axios, or any HTTP client console.log("Ready to download model file from:", downloadUrl);}
// Example: Training a character modelconst characterModel = await client.imageLoraModels.create({ name: "My OC Character - Luna", images: characterImages, triggerWord: "luna_character"});// Example: Training a style modelconst styleModel = await client.imageLoraModels.create({ name: "Cyberpunk Art Style", images: cyberpunkStyleImages, triggerWord: "cyberpunk_style"});