• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 2 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🤗 Run Stable Diffusion locally via a REST API on an M1/M2 MacBook

Stable Diffusion REST API npm Version stability experimental build

Run 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

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 the text-to-image and inpaint model checkpoints:

mkdir -p models
curl --output models/text-to-image.ckpt https://www.googleapis.com/storage/v1/b/aai-blog-files/o/sd-v1-4.ckpt?alt=media
curl --output models/inpaint-image.ckpt https://ommer-lab.com/files/latent-diffusion/inpainting_big.zip

Generate a HTTPS certificate and key:

mkcert -install
mkcert -cert-file cert.pem -key-file key.pem 0.0.0.0

Usage


  Usage
    $ stable-diffusion-rest-api [options]

  Options
    --cert                   Path to the SSL certicate  (default ./cert.pem)
    --concurrency            Number of concurrent image generation tasks  (default 1)
    --cors                   Whether to enable CORS  (default true)
    --delete-incomplete      Delete all incomplete image generation tasks before starting the server  (default false)
    --inpaint-image-model    Path to the inpaint image model checkpoint  (default ./models/inpaint-image.ckpt)
    --key                    Path to the SSL certicate key  (default ./key.pem)
    --output                 Directory to output generated images  (default ./output)
    --port                   Port to serve the REST API  (default 8888)
    --repository             Path to the Stable Diffusion repository  (default ./stable-diffusion)
    --text-to-image-model    Path to the text-to-image model checkpoint  (default ./models/text-to-image.ckpt)
    -v, --version            Displays current version
    -h, --help               Displays this message

Start the API server:

npx --yes -- stable-diffusion-rest-api \
  --text-to-image-model ./models/text-to-image.ckpt \
  --inpaint-image-model ./models/inpaint-image.ckpt \
  --concurrency 1 \
  --output ./output \
  --cert ./cert.pem \
  --key ./key.pem \
  --port 8888

API response

All REST API endpoints return JSON with one of the following shapes, depending on the status of the image generation task:

{
  status: 'QUEUED'
  resultUrl: string
}
{
  status: 'IN_PROGRESS'
  resultUrl: string
  progress: {
    currentImageIndex: number
    currentImageProgress: number
    totalImages: number
  }
  imageUrls: Array<string>
}
{
  status: 'COMPLETE'
  resultUrl: string
  imageUrls: Array<string>
}
  • status is one of QUEUED, IN_PROGRESS or COMPLETE
  • resultUrl is the URL to access the results of the image generation task
  • progress contains details about the progress of the image generation task:
    • currentImageIndex is the index of the image currently being generated
    • currentImageProgress is a value between 0 and 1 representing the progress of generating the current image
    • totalImages is the total number of images to be generated
  • imageUrls is the list of URLs of the generated images

Text to Image

POST /text-to-image

curl https://0.0.0.0:8888/text-to-image \
  --form prompt="A digital illustration of a beautiful mountain landscape, detailed, thom tenerys, epic composition, 4k, trending on artstation, fantasy vivid colors" \
  --form iterations="3" \
  --form steps="8" \
  --form seed="42" \
  --header "Content-Type: multipart/form-data" \
  --location

Sample response

{
  "status": "QUEUED",
  "resultUrl": "/text-to-image/61f957e4462ea8eff36d9e7a7b650994"
}

GET /text-to-image/<ID>

curl https://0.0.0.0:8888/text-to-image/61f957e4462ea8eff36d9e7a7b650994

Sample response

{
  "status": "IN_PROGRESS",
  "resultUrl": "/text-to-image/61f957e4462ea8eff36d9e7a7b650994",
  "progress": {
    "totalImages": 3,
    "currentImageIndex": 3,
    "currentImageProgress": 0.5
  },
  "imageUrls": [
    "/text-to-image/61f957e4462ea8eff36d9e7a7b650994/1.png",
    "/text-to-image/61f957e4462ea8eff36d9e7a7b650994/2.png"
  ]
}
{
  "status": "COMPLETE",
  "resultUrl": "/text-to-image/61f957e4462ea8eff36d9e7a7b650994",
  "imageUrls": [
    "/text-to-image/61f957e4462ea8eff36d9e7a7b650994/1.png",
    "/text-to-image/61f957e4462ea8eff36d9e7a7b650994/2.png",
    "/text-to-image/61f957e4462ea8eff36d9e7a7b650994/3.png"
  ]
}

Image to Image

POST /image-to-image

curl https://0.0.0.0:8888/image-to-image \
  --form prompt="A digital illustration of a beautiful mountain landscape, detailed, thom tenery, epic composition, 4k, trending on artstation, fantasy vivid colors" \
  --form image=@./image.png \
  --form iterations="3" \
  --form steps="24" \
  --form seed="42" \
  --header "Content-Type: multipart/form-data" \
  --location

Sample response

{
  "status": "QUEUED",
  "resultUrl": "/image-to-image/ab1104f3b55fbab7779cdbdc73ed276e"
}

GET /image-to-image/<ID>

curl https://0.0.0.0:8888/image-to-image/ab1104f3b55fbab7779cdbdc73ed276e

Sample response

{
  "status": "IN_PROGRESS",
  "resultUrl": "/image-to-image/ab1104f3b55fbab7779cdbdc73ed276e",
  "progress": {
    "totalImages": 3,
    "currentImageIndex": 3,
    "currentImageProgress": 0.5
  },
  "imageUrls": [
    "/image-to-image/ab1104f3b55fbab7779cdbdc73ed276e/1.png",
    "/image-to-image/ab1104f3b55fbab7779cdbdc73ed276e/2.png"
  ]
}
{
  "status": "COMPLETE",
  "resultUrl": "/image-to-image/ab1104f3b55fbab7779cdbdc73ed276e",
  "imageUrls": [
    "/image-to-image/ab1104f3b55fbab7779cdbdc73ed276e/1.png",
    "/image-to-image/ab1104f3b55fbab7779cdbdc73ed276e/2.png"
    "/image-to-image/ab1104f3b55fbab7779cdbdc73ed276e/3.png"
  ]
}

Inpaint Image

POST /inpaint-image

curl https://0.0.0.0:8888/inpaint-image \
  --form image=@./image.png \
  --form mask=@./image-mask.png \
  --form steps="32" \
  --form seed="42" \
  --header "Content-Type: multipart/form-data" \
  --location

Sample response

{
  "status": "QUEUED",
  "resultUrl": "/inpaint-image/59a89dfc9f075942ce9afc08312b8296"
}

GET /inpaint-image/<ID>

curl https://0.0.0.0:8888/inpaint-image/59a89dfc9f075942ce9afc08312b8296

Sample response

{
  "status": "IN_PROGRESS",
  "resultUrl": "/inpaint-image/59a89dfc9f075942ce9afc08312b8296",
  "progress": {
    "totalImages": 1,
    "currentImageIndex": 1,
    "currentImageProgress": 0.5
  },
  "imageUrls": []
}
{
  "status": "COMPLETE",
  "resultUrl": "/inpaint-image/59a89dfc9f075942ce9afc08312b8296",
  "imageUrls": [
    "/inpaint-image/59a89dfc9f075942ce9afc08312b8296/1.png",
  ]
}

More Repositories

1

vdx

🎞️ An intuitive CLI for processing video, powered by FFmpeg
TypeScript
1,665
star
2

charming

🎩 Lettering.js in vanilla JavaScript
JavaScript
1,005
star
3

create-figma-plugin

🔋 The comprehensive toolkit for developing plugins and widgets for Figma and FigJam
TypeScript
943
star
4

malarkey

⌨️ Simulate a typewriter effect in vanilla JavaScript.
JavaScript
239
star
5

figma-plugins

🔥 Monorepo for my Figma plugins
TypeScript
238
star
6

ffmpeg-cheatsheet

📼 A cheatsheet for common video processing operations in FFmpeg
185
star
7

load-google-maps-api

🌏 A lightweight Promise-returning helper for loading the Google Maps JavaScript API
JavaScript
170
star
8

vim-basics

✌️ Just the basics to get you up and running with Vim
Vim Script
136
star
9

line-clamp

🗜️ Line clamp a DOM element in vanilla JavaScript
JavaScript
75
star
10

autosize-input

🎈 Effortless, dynamic-width text boxes in vanilla JavaScript
JavaScript
68
star
11

figma-plugins-stats

📈 A CLI to get live and historical stats for your Figma plugins
TypeScript
61
star
12

cuba

🇨🇺 Google Sheets + SQL = JSON
JavaScript
55
star
13

single-page-markdown-website

🌶️ Create a nice single-page documentation website from one or more Markdown files
TypeScript
47
star
14

menu-aim

🎯 Instant mega menus in vanilla JavaScript.
JavaScript
40
star
15

fastmatter

👀 A fast frontmatter parser. Supports both string and stream inputs.
JavaScript
28
star
16

awesome-create-figma-plugin

26
star
17

regular-expressions-cheatsheet

🅰️ A concise cheatsheet for using Regular Expressions in JavaScript
JavaScript
16
star
18

generate-css

💥 Dynamically generate functional CSS classes from HTML and JavaScript source files
TypeScript
16
star
19

stoke

🌲 Generate the Abstract Syntax Tree (AST) of a Bash command
JavaScript
15
star
20

gulp-tape

👻 Run Tape tests in Gulp.
JavaScript
14
star
21

generate-ts-docs

📘 Utilities to parse type information and JSDoc annotations from TypeScript source files, and render Markdown documentation
TypeScript
12
star
22

shell-basics

🐚 A quick overview of the Unix shell
12
star
23

macos-keyboard-shortcuts

⌨️ A CLI to set application-specific keyboard shortcuts for macOS
TypeScript
12
star
24

rdd

📝 Preview your Markdown locally as it would appear on GitHub, with live updating
JavaScript
11
star
25

code-problems

🐫 Working through code problems in OCaml
OCaml
7
star
26

git-cheatsheet

🌳 A handy list of commonly used Git commands
6
star
27

interpolate

Simple string interpolation in PHP.
PHP
5
star
28

sketch-plugins

Monorepo for my Sketch plugins
JavaScript
5
star
29

quine

✍️ Programs that write themselves.
Shell
5
star
30

sketch-plugin-helper

Utilities to help you make Sketch plugins
JavaScript
5
star
31

grouper

Assign objects to groups by one or more properties, by object value, or by using a comparator function.
JavaScript
4
star
32

modal

Super-simple modals in vanilla JavaScript.
JavaScript
4
star
33

react-redux-modal

🔮 A React modal bound to the Redux store.
JavaScript
4
star
34

widget

TypeScript
3
star
35

yuanqing.sg

⚪ My personal website
HTML
3
star
36

figmake

🍦 A tiny CLI for making Figma plugins in HTML, CSS, and vanilla JavaScript
TypeScript
3
star
37

sketch-handbook

📙 A concise handbook on working precisely and effectively in Sketch
Shell
3
star
38

figma-plugins-monetization-stats

JavaScript
3
star
39

figma-add-auto-layout-item

TypeScript
2
star
40

versus

Compare two variables with the comparison operator specified as a string.
JavaScript
2
star
41

google-sheets-rest-api

JavaScript
2
star
42

q

🍦 An opinionated CLI for creating lightweight, vanilla HTML/CSS websites
JavaScript
2
star
43

jockey

Modelling a playlist that supports shuffle, repeat, and reordering.
JavaScript
2
star
44

xoxo

🎧 A YouTube mixtape maker thing.
JavaScript
2
star
45

asset-versioning

🗃️ Version your assets by appending a hash to the filename.
JavaScript
2
star
46

shared-event-listeners

A utility for minimising the number of event listeners bound on a given DOM element.
JavaScript
2
star
47

Q.js

A micro JavaScript library.
JavaScript
2
star
48

create-figma-plugin-react-example

JavaScript
2
star
49

sketch-add-artboard-borders-and-titles

JavaScript
2
star
50

extract

Sugar for getting data out of strings in PHP.
PHP
2
star
51

fi

Query a collection of text files like a document database in PHP.
PHP
2
star
52

string-extractor

Regular expression sugar for getting data out of strings.
JavaScript
1
star
53

cli

TypeScript
1
star
54

cors

JavaScript
1
star
55

savoy

Higher-order functions (synchronous and asynchronous each/eachSeries/map/filter/fold) and functions for flow control (parallel/series/waterfall) in under 1 KB.
JavaScript
1
star
56

kontact

An extensible contact form in PHP and vanilla JavaScript/AJAX.
JavaScript
1
star
57

abstract-backup

💾 Download a local backup of the Sketch files from all your Abstract projects
JavaScript
1
star
58

sketch-sort-layer-list

JavaScript
1
star
59

npm-boilerplate

Barebones boilerplate using `npm run-script`.
JavaScript
1
star
60

clinical

💊 A minimum-viable arguments parser in ~90 LOC with zero dependencies
TypeScript
1
star
61

google-sheets

🍃 An easier interface to read from and write to Google Sheets
JavaScript
1
star
62

sketch-clean-document

JavaScript
1
star
63

gulp-boilerplate

A Gulp-powered boilerplate for Node and JavaScript projects.
JavaScript
1
star
64

segue

Enqueue functions, and call them in series.
JavaScript
1
star
65

unfurl

Extract the query string and hash of a URL.
JavaScript
1
star
66

sketch-extract-text

JavaScript
1
star
67

bbqr

JavaScript
1
star
68

sketch-move-layers

JavaScript
1
star
69

pppath

Join strings to form a valid file path.
JavaScript
1
star
70

simple-code-editor

TypeScript
1
star
71

resizable

TypeScript
1
star
72

svg-inserter

TypeScript
1
star
73

build-website

TypeScript
1
star
74

.github

1
star
75

eslint-config-yuanqing

⚙️ An opinionated ESLint configuration for TypeScript and Preact projects
JavaScript
1
star
76

append-js-extension-to-local-imports

JavaScript
1
star
77

screen-recorder

JavaScript
1
star
78

sketch-select-layers

JavaScript
1
star
79

sketch-draw-slice-over-selection

JavaScript
1
star
80

sketch-align-to-grid

JavaScript
1
star
81

frontend-boilerplate

🌱 Super basic web boilerplate with Sass and vanilla JavaScript.
HTML
1
star
82

isomorphic

JavaScript
1
star
83

tsu

Utilities and functional wrappers over `through2`.
JavaScript
1
star
84

sketch-rename-layers

🖋️ [WIP] A Sketch plugin to automate layer renaming
JavaScript
1
star
85

johnson-trotter

An implementation of the Johnson-Trotter permutation algorithm.
JavaScript
1
star
86

rectify

Unsmarten your quotes, dashes, and ellipses.
JavaScript
1
star
87

stylelint-config-yuanqing

🕴️ An opinionated stylelint configuration
JavaScript
1
star
88

react-autocomplete

[UNMAINTAINED] React autocomplete component with an opinionated UX.
JavaScript
1
star