Stable Diffusion REST API
A CLI for running Stable Diffusion locally via a REST API on an M1/M2 MacBook
Pre-requisites
Initial setup
Adapted from Run Stable Diffusion on your M1 Mac’s GPU by Ben Firshman
Update Homebrew and upgrade all existing Homebrew packages:
brew update
brew upgrade
Install Homebrew dependencies:
brew install cmake mkcert protobuf rust
Clone the Stable Diffusion fork:
git clone -b apple-silicon-mps-support https://github.com/bfirsh/stable-diffusion.git
Add the following line to the stable-diffusion/scripts/img2img.py
file, right before the from ldm.util import instantiate_from_config
line:
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
Set up a virtualenv
and install dependencies:
cd stable-diffusion
python3 -m pip install virtualenv
python3 -m virtualenv venv
pip install -r requirements.txt
cd ..
Download sd-v1-4.ckpt
, and move it to the current working directory:
mv ~/Downloads/sd-v1-4.ckpt ./
Generate a certificate and key to enable HTTPS:
mkcert -install
mkcert -cert-file cert.pem -key-file key.pem 0.0.0.0
Usage
Start the API server:
npx --yes -- stable-diffusion-rest-api \
--cert ./cert.pem \
--key ./key.pem \
--output ./output \
--model ./sd-v1-4.ckpt \
--port 8888
Text → Image
(txt2img
)
POST
/text
curl https://0.0.0.0:8888/text \
--form ddimSteps="8" \
--form iterations="1" \
--form prompt="A digital illustration of a dystopian landscape, thom tenerys, cyberpunk, epic composition, 4k, detailed, trending on artstation, fantasy vivid colors" \
--form seed="42" \
--header "Content-Type: multipart/form-data" \
--output output.json \
--request POST
Text + Image → Image
(img2img
)
POST
/text-with-image
curl https://0.0.0.0:8888/text-with-image \
--form ddimSteps="8" \
--form iterations="1" \
--form image=@./input.png \
--form prompt="A digital illustration of a dystopian landscape, thom tenerys, cyberpunk, epic composition, 4k, detailed, trending on artstation, fantasy vivid colors" \
--form seed="42" \
--header "Content-Type: multipart/form-data" \
--output output.json \
--request POST