This Git Repo contains python notebook that is designed for running the Stable Diffusion XL Model on Google Colab with a T4 GPU. This open-source model is entirely free for you to use as much as you'd like, enabling you to generate an unlimited number of high quality AI images surpassing those from the mid-journey. Don't forget to share this resource with your friends, and happy generating!
%pip install --quiet --upgrade diffusers transformers accelerate invisible_watermark mediapy
use_refiner = False
import mediapy as media
import random
import sys
import torch
from diffusers import DiffusionPipeline
pipe = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
use_safetensors=True,
variant="fp16",
)
if use_refiner:
refiner = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0",
text_encoder_2=pipe.text_encoder_2,
vae=pipe.vae,
torch_dtype=torch.float16,
use_safetensors=True,
variant="fp16",
)
refiner = refiner.to("cuda")
pipe.enable_model_cpu_offload()
else:
pipe = pipe.to("cuda")
prompt = "a photo protrait of girl, wearing sunglasses, red blue hair, white colorful background, realistic, high resolution, HD quality"
seed = random.randint(0, sys.maxsize)
images = pipe(
prompt = prompt,
output_type = "latent" if use_refiner else "pil",
generator = torch.Generator("cuda").manual_seed(seed),
).images
if use_refiner:
images = refiner(
prompt = prompt,
image = images,
).images
print(f"Prompt:\t{prompt}\nSeed:\t{seed}")
media.show_images(images)
images[0].save("output.jpg")