📘 Introduction

Generating images with AI has never been easier. Hugging Face’s Diffusers library provides a user-friendly way to create stunning visuals using pre-trained diffusion models like Stable Diffusion. In this guide, we’ll walk through the entire process step by step.

🧠 What are Hugging Face Diffusers?

Diffusers are models that iteratively refine random noise into a coherent image based on a given text prompt. Hugging Face’s diffusers library makes it easy to access these models and generate high-quality images with just a few lines of code.

✅ Prerequisites

Before you begin, make sure you have:

🐍☑️ Installed Python
🌐☑️ Created and activated a virtual environment (venv)

📦1️⃣ Install Libraries

Install the following Python packages using pip:

pip install diffusers
pip install transformers
pip install torch torchvision torchaudio
pip install accelerate

📥2️⃣ Import Libraries

Start by importing the required Python modules:

import torch
import transformers
from diffusers import DiffusionPipeline

💻3️⃣ Set Device

Choose whether to run the model on GPU or CPU. On Macs with M1/M2 chips, PyTorch can use Metal Performance Shaders (MPS) for GPU acceleration:

device = "mps" if torch.backends.mps.is_available() else "cpu"

If MPS is available, the model runs on the Mac GPU for faster image generation; otherwise, it defaults to CPU. This ensures optimal speed and efficiency across different hardware.

⚙️4️⃣ Load the Pre-trained Model

Load a ready-to-use Stable Diffusion model from Hugging Face. Using a pre-trained model means we don’t need to train one from scratch—it already knows how to generate high-quality images from text prompts.

pipeline = DiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16
).to(device)

You should get the following output:

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In