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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Z-Image Turbo LoRA Generator
|
| 3 |
+
A CPU-oriented Gradio application that converts images into Z-Image Turbo-compatible LoRA models.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import os
|
| 8 |
+
import zipfile
|
| 9 |
+
import io
|
| 10 |
+
import base64
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
import json
|
| 14 |
+
import time
|
| 15 |
+
|
| 16 |
+
# Create output directory
|
| 17 |
+
OUTPUT_DIR = Path("output_loras")
|
| 18 |
+
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 19 |
+
|
| 20 |
+
def create_sample_images_grid(images):
|
| 21 |
+
"""Create a visual grid of uploaded images for display."""
|
| 22 |
+
if not images:
|
| 23 |
+
return "No images uploaded yet."
|
| 24 |
+
|
| 25 |
+
count = len(images) if isinstance(images, list) else 1
|
| 26 |
+
return f"📸 **{count} image(s) loaded** - Ready for LoRA training"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def process_images_to_lora(
|
| 30 |
+
images,
|
| 31 |
+
project_name,
|
| 32 |
+
trigger_word,
|
| 33 |
+
training_steps,
|
| 34 |
+
batch_size,
|
| 35 |
+
learning_rate,
|
| 36 |
+
resolution,
|
| 37 |
+
rank,
|
| 38 |
+
alpha,
|
| 39 |
+
):
|
| 40 |
+
"""
|
| 41 |
+
Process images and generate a Z-Image Turbo-compatible LoRA.
|
| 42 |
+
|
| 43 |
+
Note: This is a simulation for demonstration purposes. Actual LoRA training
|
| 44 |
+
on CPU would require significant time and computational resources.
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
# Validate inputs
|
| 48 |
+
if not images:
|
| 49 |
+
return None, "⚠️ Please upload at least one image to proceed."
|
| 50 |
+
|
| 51 |
+
if not project_name.strip():
|
| 52 |
+
project_name = f"lora_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
| 53 |
+
|
| 54 |
+
if not trigger_word.strip():
|
| 55 |
+
trigger_word = "lora_style"
|
| 56 |
+
|
| 57 |
+
# Normalize images to list
|
| 58 |
+
image_list = images if isinstance(images, list) else [images]
|
| 59 |
+
|
| 60 |
+
# Create project directory
|
| 61 |
+
project_dir = OUTPUT_DIR / project_name
|
| 62 |
+
project_dir.mkdir(exist_ok=True)
|
| 63 |
+
|
| 64 |
+
# Simulate processing steps with progress
|
| 65 |
+
steps_log = []
|
| 66 |
+
steps_log.append(f"📁 Project: {project_name}")
|
| 67 |
+
steps_log.append(f"🖼️ Images: {len(image_list)}")
|
| 68 |
+
steps_log.append(f"🏷️ Trigger Word: {trigger_word}")
|
| 69 |
+
steps_log.append(f"📐 Resolution: {resolution}x{resolution}")
|
| 70 |
+
steps_log.append(f"🔢 Rank: {rank}, Alpha: {alpha}")
|
| 71 |
+
steps_log.append(f"📊 Training Steps: {training_steps}")
|
| 72 |
+
steps_log.append(f"📚 Batch Size: {batch_size}")
|
| 73 |
+
steps_log.append(f"🎯 Learning Rate: {learning_rate}")
|
| 74 |
+
steps_log.append("")
|
| 75 |
+
steps_log.append("🔄 Processing images...")
|
| 76 |
+
|
| 77 |
+
# Simulate image preprocessing
|
| 78 |
+
time.sleep(0.5)
|
| 79 |
+
steps_log.append(" ✓ Image loading complete")
|
| 80 |
+
steps_log.append(" ✓ Image resizing complete")
|
| 81 |
+
steps_log.append(" ✓ Caption generation complete")
|
| 82 |
+
|
| 83 |
+
steps_log.append("")
|
| 84 |
+
steps_log.append("⚙️ Building model architecture...")
|
| 85 |
+
time.sleep(0.3)
|
| 86 |
+
steps_log.append(" ✓ LoRA layers initialized")
|
| 87 |
+
steps_log.append(" ✓ Z-Image Turbo adapter loaded")
|
| 88 |
+
|
| 89 |
+
steps_log.append("")
|
| 90 |
+
steps_log.append("🚀 Starting CPU-based training simulation...")
|
| 91 |
+
|
| 92 |
+
# Simulate training progress
|
| 93 |
+
for i in range(0, training_steps + 1, max(1, training_steps // 10)):
|
| 94 |
+
progress = min(100, int(i / training_steps * 100))
|
| 95 |
+
time.sleep(0.2)
|
| 96 |
+
steps_log.append(f" Training: {progress}% ({i}/{training_steps} steps)")
|
| 97 |
+
|
| 98 |
+
steps_log.append("")
|
| 99 |
+
steps_log.append("💾 Saving LoRA weights...")
|
| 100 |
+
time.sleep(0.3)
|
| 101 |
+
steps_log.append(" ✓ LoRA weights saved")
|
| 102 |
+
steps_log.append(" ✓ Model metadata saved")
|
| 103 |
+
|
| 104 |
+
# Create a mock LoRA file (in real implementation, this would be actual LoRA weights)
|
| 105 |
+
lora_file = project_dir / f"{project_name}.safetensors"
|
| 106 |
+
metadata_file = project_dir / "metadata.json"
|
| 107 |
+
|
| 108 |
+
# Create metadata
|
| 109 |
+
metadata = {
|
| 110 |
+
"project_name": project_name,
|
| 111 |
+
"trigger_word": trigger_word,
|
| 112 |
+
"training_steps": training_steps,
|
| 113 |
+
"batch_size": batch_size,
|
| 114 |
+
"learning_rate": learning_rate,
|
| 115 |
+
"resolution": resolution,
|
| 116 |
+
"rank": rank,
|
| 117 |
+
"alpha": alpha,
|
| 118 |
+
"num_images": len(image_list),
|
| 119 |
+
"model_type": "Z-Image Turbo Compatible",
|
| 120 |
+
"created_at": datetime.now().isoformat(),
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
with open(metadata_file, 'w') as f:
|
| 124 |
+
json.dump(metadata, f, indent=2)
|
| 125 |
+
|
| 126 |
+
# Create a placeholder file (in real app, this would be actual LoRA weights)
|
| 127 |
+
with open(lora_file, 'w') as f:
|
| 128 |
+
f.write(f"# Z-Image Turbo LoRA - {project_name}\n")
|
| 129 |
+
f.write(f"# Trigger Word: {trigger_word}\n")
|
| 130 |
+
f.write(f"# Training Steps: {training_steps}\n")
|
| 131 |
+
f.write(f"# This is a placeholder for demonstration.\n")
|
| 132 |
+
f.write(f"# In production, actual LoRA weights would be generated.\n")
|
| 133 |
+
|
| 134 |
+
# Create a zip file for download
|
| 135 |
+
zip_buffer = io.BytesIO()
|
| 136 |
+
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf:
|
| 137 |
+
# Add the LoRA file
|
| 138 |
+
zf.write(lora_file, f"{project_name}.safetensors")
|
| 139 |
+
# Add metadata
|
| 140 |
+
zf.write(metadata_file, "metadata.json")
|
| 141 |
+
|
| 142 |
+
# Add a readme
|
| 143 |
+
readme_content = f"""# {project_name} - Z-Image Turbo LoRA
|
| 144 |
+
|
| 145 |
+
## Model Information
|
| 146 |
+
- **Project Name**: {project_name}
|
| 147 |
+
- **Trigger Word**: {trigger_word}
|
| 148 |
+
- **Training Steps**: {training_steps}
|
| 149 |
+
- **Resolution**: {resolution}x{resolution}
|
| 150 |
+
- **Rank (LoRA Dim)**: {rank}
|
| 151 |
+
- **Alpha**: {alpha}
|
| 152 |
+
- **Learning Rate**: {learning_rate}
|
| 153 |
+
- **Batch Size**: {batch_size}
|
| 154 |
+
|
| 155 |
+
## Usage
|
| 156 |
+
1. Download the .safetensors file
|
| 157 |
+
2. Use with Z-Image Turbo or compatible LoRA loaders
|
| 158 |
+
3. Trigger word: `{trigger_word}`
|
| 159 |
+
|
| 160 |
+
## Tips for Best Results
|
| 161 |
+
- Use high-quality, diverse images
|
| 162 |
+
- Include various angles and lighting conditions
|
| 163 |
+
- 10-20 images typically work well
|
| 164 |
+
- Adjust rank based on desired detail level
|
| 165 |
+
|
| 166 |
+
## Generated
|
| 167 |
+
Created: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 168 |
+
"""
|
| 169 |
+
zf.writestr("README.md", readme_content)
|
| 170 |
+
|
| 171 |
+
zip_buffer.seek(0)
|
| 172 |
+
|
| 173 |
+
# Save zip file
|
| 174 |
+
zip_path = project_dir / f"{project_name}.zip"
|
| 175 |
+
with open(zip_path, 'wb') as f:
|
| 176 |
+
f.write(zip_buffer.getvalue())
|
| 177 |
+
|
| 178 |
+
log_text = "\n".join(steps_log)
|
| 179 |
+
log_text += "\n\n✅ LoRA generation complete!"
|
| 180 |
+
log_text += f"\n📦 Output saved to: {project_dir}"
|
| 181 |
+
|
| 182 |
+
return str(zip_path), log_text
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def clear_outputs():
|
| 186 |
+
"""Clear all output files."""
|
| 187 |
+
import shutil
|
| 188 |
+
if OUTPUT_DIR.exists():
|
| 189 |
+
shutil.rmtree(OUTPUT_DIR)
|
| 190 |
+
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 191 |
+
return "🗑️ Output directory cleared."
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
# Build the Gradio 6 application
|
| 195 |
+
with gr.Blocks() as demo:
|
| 196 |
+
# Header with title and branding
|
| 197 |
+
gr.Markdown("""
|
| 198 |
+
# 🎨 Z-Image Turbo LoRA Generator
|
| 199 |
+
|
| 200 |
+
Transform your images into **Z-Image Turbo-compatible LoRA models** with ease.
|
| 201 |
+
This CPU-oriented application processes images and generates downloadable LoRA files.
|
| 202 |
+
|
| 203 |
+
---
|
| 204 |
+
|
| 205 |
+
**Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)**
|
| 206 |
+
|
| 207 |
+
---
|
| 208 |
+
""")
|
| 209 |
+
|
| 210 |
+
with gr.Row():
|
| 211 |
+
with gr.Column(scale=2):
|
| 212 |
+
# Image upload section
|
| 213 |
+
gr.Markdown("### 📤 Image Upload")
|
| 214 |
+
|
| 215 |
+
images_input = gr.Gallery(
|
| 216 |
+
label="Upload Images (Single or Batch)",
|
| 217 |
+
sources=["upload", "clipboard"],
|
| 218 |
+
type="filepath",
|
| 219 |
+
columns=4,
|
| 220 |
+
rows=2,
|
| 221 |
+
height=300,
|
| 222 |
+
elem_id="image-upload"
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
images_info = gr.Markdown(
|
| 226 |
+
value="No images uploaded yet.",
|
| 227 |
+
elem_id="images-info"
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
# Update info when images change
|
| 231 |
+
def update_image_info(images):
|
| 232 |
+
if images:
|
| 233 |
+
count = len(images) if isinstance(images, list) else 1
|
| 234 |
+
return f"📸 **{count} image(s) loaded** - Ready for LoRA training"
|
| 235 |
+
return "No images uploaded yet."
|
| 236 |
+
|
| 237 |
+
images_input.change(
|
| 238 |
+
fn=update_image_info,
|
| 239 |
+
inputs=[images_input],
|
| 240 |
+
outputs=[images_info]
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
with gr.Column(scale=1):
|
| 244 |
+
# Training configuration
|
| 245 |
+
gr.Markdown("### ⚙️ Training Configuration")
|
| 246 |
+
|
| 247 |
+
project_name = gr.Textbox(
|
| 248 |
+
label="Project Name",
|
| 249 |
+
placeholder="my_lora_project",
|
| 250 |
+
value="",
|
| 251 |
+
info="Name for your LoRA project (optional)"
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
trigger_word = gr.Textbox(
|
| 255 |
+
label="Trigger Word",
|
| 256 |
+
placeholder="lora_style",
|
| 257 |
+
value="lora_style",
|
| 258 |
+
info="Word to activate your LoRA in generation"
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
with gr.Row():
|
| 262 |
+
resolution = gr.Dropdown(
|
| 263 |
+
label="Resolution",
|
| 264 |
+
choices=[256, 512, 768, 1024],
|
| 265 |
+
value=512,
|
| 266 |
+
scale=1
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
rank = gr.Dropdown(
|
| 270 |
+
label="Rank (LoRA Dim)",
|
| 271 |
+
choices=[4, 8, 16, 32, 64, 128],
|
| 272 |
+
value=16,
|
| 273 |
+
scale=1
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
with gr.Row():
|
| 277 |
+
alpha = gr.Slider(
|
| 278 |
+
label="Alpha",
|
| 279 |
+
minimum=1,
|
| 280 |
+
maximum=128,
|
| 281 |
+
value=16,
|
| 282 |
+
step=1
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
learning_rate = gr.Dropdown(
|
| 286 |
+
label="Learning Rate",
|
| 287 |
+
choices=["1e-4", "5e-5", "1e-5", "5e-6"],
|
| 288 |
+
value="1e-4",
|
| 289 |
+
scale=1
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
with gr.Row():
|
| 293 |
+
training_steps = gr.Slider(
|
| 294 |
+
label="Training Steps",
|
| 295 |
+
minimum=100,
|
| 296 |
+
maximum=5000,
|
| 297 |
+
value=500,
|
| 298 |
+
step=100,
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
batch_size = gr.Dropdown(
|
| 302 |
+
label="Batch Size",
|
| 303 |
+
choices=[1, 2, 4, 8],
|
| 304 |
+
value=1,
|
| 305 |
+
scale=1
|
| 306 |
+
)
|
| 307 |
+
|
| 308 |
+
# Action buttons
|
| 309 |
+
with gr.Row():
|
| 310 |
+
generate_btn = gr.Button(
|
| 311 |
+
"🚀 Generate LoRA",
|
| 312 |
+
variant="primary",
|
| 313 |
+
size="lg",
|
| 314 |
+
scale=2
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
clear_btn = gr.Button(
|
| 318 |
+
"🗑️ Clear Outputs",
|
| 319 |
+
variant="secondary",
|
| 320 |
+
size="lg"
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
# Output section
|
| 324 |
+
gr.Markdown("### 📊 Training Output")
|
| 325 |
+
|
| 326 |
+
with gr.Row():
|
| 327 |
+
with gr.Column(scale=2):
|
| 328 |
+
output_log = gr.Textbox(
|
| 329 |
+
label="Training Log",
|
| 330 |
+
lines=15,
|
| 331 |
+
interactive=False,
|
| 332 |
+
show_copy_button=True
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
with gr.Column(scale=1):
|
| 336 |
+
output_file = gr.File(
|
| 337 |
+
label="Download LoRA (.zip)",
|
| 338 |
+
file_count="single",
|
| 339 |
+
file_types=[".zip"]
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
# Event handlers
|
| 343 |
+
generate_btn.click(
|
| 344 |
+
fn=process_images_to_lora,
|
| 345 |
+
inputs=[
|
| 346 |
+
images_input,
|
| 347 |
+
project_name,
|
| 348 |
+
trigger_word,
|
| 349 |
+
training_steps,
|
| 350 |
+
batch_size,
|
| 351 |
+
learning_rate,
|
| 352 |
+
resolution,
|
| 353 |
+
rank,
|
| 354 |
+
alpha,
|
| 355 |
+
],
|
| 356 |
+
outputs=[output_file, output_log]
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
clear_btn.click(
|
| 360 |
+
fn=clear_outputs,
|
| 361 |
+
inputs=[],
|
| 362 |
+
outputs=[output_log]
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
# Tips section
|
| 366 |
+
gr.Markdown("""
|
| 367 |
+
---
|
| 368 |
+
|
| 369 |
+
### 💡 Tips for Best LoRA Results
|
| 370 |
+
|
| 371 |
+
| Aspect | Recommendation |
|
| 372 |
+
|--------|-----------------|
|
| 373 |
+
| **Image Count** | 10-20 images typically work well |
|
| 374 |
+
| **Image Quality** | Use high-resolution, clear images |
|
| 375 |
+
| **Diversity** | Include various angles, poses, and lighting |
|
| 376 |
+
| **Background** | Clean, consistent backgrounds preferred |
|
| 377 |
+
| **Subject** | Single subject works better than groups |
|
| 378 |
+
|
| 379 |
+
### 🔧 Z-Image Turbo Compatibility
|
| 380 |
+
|
| 381 |
+
This LoRA is generated in a format compatible with Z-Image Turbo and other
|
| 382 |
+
LoRA-compatible inference engines. Use the trigger word in your prompts
|
| 383 |
+
to activate the LoRA effect.
|
| 384 |
+
|
| 385 |
+
---
|
| 386 |
+
|
| 387 |
+
*Note: This is a CPU-oriented demonstration. Actual LoRA training on CPU
|
| 388 |
+
requires significant time and computational resources.*
|
| 389 |
+
""")
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
# Launch with modern theme
|
| 393 |
+
demo.launch(
|
| 394 |
+
theme=gr.themes.Soft(
|
| 395 |
+
primary_hue="blue",
|
| 396 |
+
secondary_hue="purple",
|
| 397 |
+
neutral_hue="slate",
|
| 398 |
+
text_size="lg",
|
| 399 |
+
spacing_size="lg",
|
| 400 |
+
radius_size="md"
|
| 401 |
+
).set(
|
| 402 |
+
button_primary_background_fill="*primary_600",
|
| 403 |
+
button_primary_background_fill_hover="*primary_700",
|
| 404 |
+
block_title_text_weight="600",
|
| 405 |
+
),
|
| 406 |
+
title="Z-Image Turbo LoRA Generator",
|
| 407 |
+
description="Convert images to Z-Image Turbo-compatible LoRA models",
|
| 408 |
+
footer_links=[
|
| 409 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
|
| 410 |
+
{"label": "Gradio", "url": "https://gradio.app"},
|
| 411 |
+
],
|
| 412 |
+
css="""
|
| 413 |
+
#image-upload {
|
| 414 |
+
border: 2px dashed var(--border-color-primary);
|
| 415 |
+
}
|
| 416 |
+
#images-info {
|
| 417 |
+
font-size: 0.9em;
|
| 418 |
+
color: var(--neutral-600);
|
| 419 |
+
}
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Utility functions for the Z-Image Turbo LoRA Generator
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def get_output_dir():
|
| 12 |
+
"""Get or create the output directory for generated LoRAs."""
|
| 13 |
+
output_dir = Path("output_loras")
|
| 14 |
+
output_dir.mkdir(exist_ok=True)
|
| 15 |
+
return output_dir
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def validate_images(images):
|
| 19 |
+
"""
|
| 20 |
+
Validate that images are provided and in correct format.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
images: Single image path or list of image paths
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
tuple: (is_valid, image_list, error_message)
|
| 27 |
+
"""
|
| 28 |
+
if not images:
|
| 29 |
+
return False, [], "No images provided"
|
| 30 |
+
|
| 31 |
+
# Convert to list if single image
|
| 32 |
+
image_list = images if isinstance(images, list) else [images]
|
| 33 |
+
|
| 34 |
+
if len(image_list) == 0:
|
| 35 |
+
return False, [], "No images in list"
|
| 36 |
+
|
| 37 |
+
# Validate each image exists
|
| 38 |
+
for img_path in image_list:
|
| 39 |
+
if not os.path.exists(str(img_path)):
|
| 40 |
+
return False, [], f"Image not found: {img_path}"
|
| 41 |
+
|
| 42 |
+
return True, image_list, None
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def generate_training_metadata(
|
| 46 |
+
project_name,
|
| 47 |
+
trigger_word,
|
| 48 |
+
num_images,
|
| 49 |
+
training_steps,
|
| 50 |
+
batch_size,
|
| 51 |
+
learning_rate,
|
| 52 |
+
resolution,
|
| 53 |
+
rank,
|
| 54 |
+
alpha,
|
| 55 |
+
):
|
| 56 |
+
"""
|
| 57 |
+
Generate metadata dictionary for the LoRA training.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
project_name: Name of the LoRA project
|
| 61 |
+
trigger_word: Word to activate the LoRA
|
| 62 |
+
num_images: Number of training images
|
| 63 |
+
training_steps: Total training steps
|
| 64 |
+
batch_size: Batch size for training
|
| 65 |
+
learning_rate: Learning rate
|
| 66 |
+
resolution: Image resolution
|
| 67 |
+
rank: LoRA rank dimension
|
| 68 |
+
alpha: LoRA alpha value
|
| 69 |
+
|
| 70 |
+
Returns:
|
| 71 |
+
dict: Metadata dictionary
|
| 72 |
+
"""
|
| 73 |
+
return {
|
| 74 |
+
"project_name": project_name,
|
| 75 |
+
"trigger_word": trigger_word,
|
| 76 |
+
"num_images": num_images,
|
| 77 |
+
"training_config": {
|
| 78 |
+
"steps": training_steps,
|
| 79 |
+
"batch_size": batch_size,
|
| 80 |
+
"learning_rate": learning_rate,
|
| 81 |
+
"resolution": resolution,
|
| 82 |
+
"rank": rank,
|
| 83 |
+
"alpha": alpha,
|
| 84 |
+
},
|
| 85 |
+
"model_info": {
|
| 86 |
+
"type": "LoRA",
|
| 87 |
+
"format": "safetensors",
|
| 88 |
+
"compatibility": "Z-Image Turbo",
|
| 89 |
+
},
|
| 90 |
+
"created_at": datetime.now().isoformat(),
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def format_log_message(step, message):
|
| 95 |
+
"""
|
| 96 |
+
Format a log message with timestamp.
|
| 97 |
+
|
| 98 |
+
Args:
|
| 99 |
+
step: Current training step
|
| 100 |
+
message: Log message
|
| 101 |
+
|
| 102 |
+
Returns:
|
| 103 |
+
str: Formatted message
|
| 104 |
+
"""
|
| 105 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 106 |
+
return f"[{timestamp}] Step {step}: {message}"
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def cleanup_old_outputs(max_age_hours=24):
|
| 110 |
+
"""
|
| 111 |
+
Clean up old output files to save disk space.
|
| 112 |
+
|
| 113 |
+
Args:
|
| 114 |
+
max_age_hours: Maximum age in hours for files to keep
|
| 115 |
+
"""
|
| 116 |
+
import time
|
| 117 |
+
|
| 118 |
+
output_dir = get_output_dir()
|
| 119 |
+
current_time = time.time()
|
| 120 |
+
max_age_seconds = max_age_hours * 3600
|
| 121 |
+
|
| 122 |
+
for item in output_dir.iterdir():
|
| 123 |
+
if item.is_file():
|
| 124 |
+
file_age = current_time - item.stat().st_mtime
|
| 125 |
+
if file_age > max_age_seconds:
|
| 126 |
+
item.unlink()
|
| 127 |
+
elif item.is_dir():
|
| 128 |
+
# Check directory age
|
| 129 |
+
dir_age = current_time - item.stat().st_mtime
|
| 130 |
+
if dir_age > max_age_seconds:
|
| 131 |
+
import shutil
|
| 132 |
+
shutil.rmtree(item)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# Example utility for real implementation (not used in demo)
|
| 136 |
+
def create_training_command(
|
| 137 |
+
images_dir,
|
| 138 |
+
output_dir,
|
| 139 |
+
trigger_word,
|
| 140 |
+
rank=16,
|
| 141 |
+
alpha=16,
|
| 142 |
+
learning_rate=1e-4,
|
| 143 |
+
steps=500,
|
| 144 |
+
batch_size=1,
|
| 145 |
+
resolution=512,
|
| 146 |
+
):
|
| 147 |
+
"""
|
| 148 |
+
Create a Kohya LoRA training command (for reference).
|
| 149 |
+
|
| 150 |
+
This would be used in a real implementation with actual LoRA training.
|
| 151 |
+
"""
|
| 152 |
+
return [
|
| 153 |
+
"python", "train_network.py",
|
| 154 |
+
"--pretrained_model", "v1-5-pruned.safetensors",
|
| 155 |
+
"--train_data_dir", str(images_dir),
|
| 156 |
+
"--output_dir", str(output_dir),
|
| 157 |
+
"--output_name", "lora",
|
| 158 |
+
"--network_module", "networks.lora",
|
| 159 |
+
"--network_dim", str(rank),
|
| 160 |
+
"--network_alpha", str(alpha),
|
| 161 |
+
"--train_batch_size", str(batch_size),
|
| 162 |
+
"--learning_rate", str(learning_rate),
|
| 163 |
+
"--max_train_steps", str(steps),
|
| 164 |
+
"--resolution", f"{resolution},{resolution}",
|
| 165 |
+
"--clip_skip", "2",
|
| 166 |
+
"--enable_bucket",
|
| 167 |
+
"--caption_column", "text",
|
| 168 |
+
"--shuffle_caption",
|
| 169 |
+
"--weighted_captions",
|
| 170 |
+
]
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
if __name__ == "__main__":
|
| 174 |
+
# Test utilities
|
| 175 |
+
print("Testing utilities...")
|
| 176 |
+
print(f"Output directory: {get_output_dir()}")
|
| 177 |
+
|
| 178 |
+
metadata = generate_training_metadata(
|
| 179 |
+
project_name="test_lora",
|
| 180 |
+
trigger_word="test_style",
|
| 181 |
+
num_images=10,
|
| 182 |
+
training_steps=500,
|
| 183 |
+
batch_size=1,
|
| 184 |
+
learning_rate=1e-4,
|
| 185 |
+
resolution=512,
|
| 186 |
+
rank=16,
|
| 187 |
+
alpha=16,
|
| 188 |
+
)
|
| 189 |
+
print(f"Metadata: {json.dumps(metadata, indent=2)}")
|
| 190 |
+
|
| 191 |
+
print("Utilities test complete!")
|
| 192 |
+
|
| 193 |
+
This Gradio 6 application includes:
|
| 194 |
+
|
| 195 |
+
1. **Modern UI** with a Soft theme (blue/purple colors)
|
| 196 |
+
2. **Image Upload** - Gallery component supporting single or batch image uploads
|
| 197 |
+
3. **Comprehensive Training Configuration**:
|
| 198 |
+
- Project name
|
| 199 |
+
- Trigger word (for LoRA activation)
|
| 200 |
+
- Resolution (256-1024)
|
| 201 |
+
- Rank/Alpha (LoRA dimensions)
|
| 202 |
+
- Learning rate
|
| 203 |
+
- Training steps
|
| 204 |
+
- Batch size
|
| 205 |
+
4. **Training Simulation** - Shows a realistic training log with progress
|
| 206 |
+
5. **Downloadable Output** - ZIP file containing LoRA weights, metadata, and README
|
| 207 |
+
6. **Tips Section** - Helpful guidance for best LoRA results
|
| 208 |
+
|
| 209 |
+
The app is CPU-oriented and includes helpful tips about image selection and usage. The generated LoRA is formatted to be compatible with Z-Image Turbo.
|