How to run Hugging Face stable-diffusion on Mac


This post goes over how to run Hugging Face stable-diffusion AI model on macOS.

Prerequisites

Install Python:

brew install python

Create the virtualenv:

python3 -m venv .venv

Activate the virtualenv:

source .venv/bin/activate

Install the dependencies:

pip3 install accelerate diffusers torch transformers

Text-to-Image

Create a script to generate text-to-image:

touch text_to_image.py

Import the diffusers module:

from diffusers import AutoPipelineForText2Image

Create a pipeline using the dreamlike-art/dreamlike-photoreal-2.0 checkpoint:

model = "dreamlike-art/dreamlike-photoreal-2.0"
pipeline = AutoPipelineForText2Image.from_pretrained(model)

Pass the prompt to the pipeline and generate the image:

prompt = "cinematic photo of Godzilla eating sushi with a cat in a izakaya, 35mm photograph, film, professional, 4k, highly detailed"
image = pipeline(prompt).images[0]

Save the image:

image.save("my_image.png")

Run the script:

python3 text_to_image.py

Code

Here’s the full script:

from diffusers import AutoPipelineForText2Image

model = "dreamlike-art/dreamlike-photoreal-2.0"
pipeline = AutoPipelineForText2Image.from_pretrained(model)

prompt = "cinematic photo of Godzilla eating sushi with a cat in a izakaya, 35mm photograph, film, professional, 4k, highly detailed"
image = pipeline(prompt).images[0]

image.save("my_image.png")

See the GitHub demo.



Please support this site and join our Discord!