Last mod: 2025.04.26

Juggernaut XL on CPU (text-to-image)

Juggernaut XL is a much more advanced model for generating images than Stable Diffusion. The example includes information on how to quickly install the relevant libraries with a few commands and write a Python script that generates images.

Hardware and basic software

The example is run on:

  • CPU i7-4770K
  • 32GB RAM
  • Linux Mint 22.1
  • Python 3.12 (After a default installation)

Environment and required libraries

Install virtual environments venv:

sudo apt update
sudo apt install python3-venv -y

Create and run virtual environment:

python3 -m venv ~/juggernaut-venv
source ~/juggernaut-venv/bin/activate

Installation of required libraries

pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118
pip install diffusers transformers

Script

Writing a script to run juggernaut-xl.py with code:

from diffusers import StableDiffusionXLPipeline
import torch
import random

# Load the model
model_path = "RunDiffusion/Juggernaut-XL-v9"
pipe = StableDiffusionXLPipeline.from_single_file(
    "https://huggingface.co/RunDiffusion/Juggernaut-XL-v9/blob/main/Juggernaut-XL_v9_RunDiffusionPhoto_v2.safetensors",
    torch_dtype=torch.float32,  # Use float32 for CPU compatibility
    use_safetensors=True
).to("cpu")  # Force CPU usage

# The image description 
prompt = "A skyscraper on a starry night with the moon in the background"

# Generate an image
seed = random.randint(0, 2**32 - 1)
generator = torch.Generator(device="cpu").manual_seed(random.randint(0, 2**32 - 1))
image = pipe(prompt=prompt, generator=generator, num_inference_steps=30, width=512, height=512).images[0]

# Save image
path = "image_{}.png".format(seed)
image.save(path)

Run:

python3 juggernaut-xl.py

Let's wait a few minutes and see the end result:

stable-diffusion generated image