Aulafy
Courses/Generative AI: image, voice and video/Reproducible Diffusers with Python

Reproducible Diffusers with Python

When an image is part of a course, campaign, or product, you need to reproduce it, compare it, and document it. That's where Diffusers wins: it turns generation into versionable code.

  • Create a minimal image generation script.
  • Control seed, size, steps, and output filename.
  • Save a manifest to audit results.

Environment

TerminalCode
mkdir imagenes-diffusers
cd imagenes-diffusers
python -m venv .venv
source .venv/bin/activate
pip install -U diffusers transformers accelerate sentencepiece safetensors

Base script

TerminalCode
import json
import torch
from diffusers import FluxPipeline

model_id = "black-forest-labs/FLUX.1-schnell"
prompt = "clear educational diagram about local AI, calm workspace, Spanish labels, no logos"
seed = 2048

pipe = FluxPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
pipe.enable_model_cpu_offload()

image = pipe(
    prompt,
    num_inference_steps=4,
    guidance_scale=0.0,
    generator=torch.Generator("cpu").manual_seed(seed),
).images[0]

image.save("salida-local-ai-2048.png")

manifest = {
    "model_id": model_id,
    "prompt": prompt,
    "seed": seed,
    "steps": 4,
    "guidance_scale": 0.0,
    "output": "salida-local-ai-2048.png",
}

with open("salida-local-ai-2048.manifest.json", "w", encoding="utf-8") as f:
    json.dump(manifest, f, ensure_ascii=False, indent=2)