Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to readme and fixes to allow seamless switching between models. #7

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ This could likely be done with a simple python script.

See the [Stable Diffusion directory structure section](#stable-diffusion-directory-structure) for more information.

---

The server users diffusers. On first run it will connect to huggingface.co and
download all the models required to run a given diffuser and store them in the
huggingface cache directory. This can take a while depending on your internet
connection.

---

## Client

For an example client, take a look at [krita_stable_diffusion connect.py file](https://github.com/w4ffl35/krita_stable_diffusion/blob/master/krita_stable_diffusion/connect.py) which uses this server.

---

## Features

- Offline friendly - works completely locally with no internet connection
Expand Down Expand Up @@ -85,9 +98,12 @@ Default directory structure for runai Stable Diffusion
│         │   ├── <file(diffusers direcotry)>
│         │   ├── <file>.ckpt
│         │   ├── <file>.safetensor
│         ├── CLIP
│         ├── CompVis
│         ├── openai
│      ├── clip-vit-large-patch14
│      ├── CLIP
│      ├── CompVis
│      ├── openai
│      ├── runwayml
│      ├── stabilityai
```

If you are using **Automatic1111** you can place your checkpoints in the
Expand Down Expand Up @@ -123,6 +139,12 @@ You would then set BASE_DIR to `/home/USER/stable-diffusion-webui/models/Stable-

## Development Installation

### Requirements

- git
- conda
- a cuda capable GPU

1. [Install CUDA Toolkit 11.7](https://developer.nvidia.com/cuda-11-7-0-download-archive?target_os=Linux&target_arch=x86_64)
2. [Install miniconda](https://docs.conda.io/en/latest/miniconda.html)
3. Activate environment ``
Expand Down
4 changes: 3 additions & 1 deletion bin/buildlinux.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/bash

DISABLE_TELEMETRY=true
export HF_ENDPOINT=""
export HF_HUB_OFFLINE=true
PYTHONOPTIMIZE=0 pyinstaller --log-level=DEBUG --noconfirm ./build.spec --clean

# get version from setup.py
Expand Down
159 changes: 159 additions & 0 deletions bin/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/usr/bin/bash

STABLE_DIFFUSION_DIR=~/stablediffusion
OPENAI_DIR=$STABLE_DIFFUSION_DIR/openai
CLIP_VIT_LARGE=$OPENAI_DIR/clip-vit-large-patch14
RUNWAYML_DIR=$STABLE_DIFFUSION_DIR/runwayml
STABILITY_AI_DIR=$STABLE_DIFFUSION_DIR/stabilityai

SD_MODEL_V1=$RUNWAYML_DIR/stable-diffusion-v1-5
INPAINT_MODEL_V1=$RUNWAYML_DIR/stable-diffusion-inpainting

SD_MODEL_V2=$STABILITY_AI_DIR/stable-diffusion-2-1-base
INPAINT_MODEL_V2=$STABILITY_AI_DIR/stable-diffusion-2-inpainting

V1_DIR=$STABLE_DIFFUSION_DIR/v1
V2_DIR=$STABLE_DIFFUSION_DIR/v2

CLIP_DIR=$STABLE_DIFFUSION_DIR/CLIP/clip/
VOCAB_MODEL=$CLIP_DIR/bpe_simple_vocab_16e6.txt.gz

COMPVIS_DIR=$STABLE_DIFFUSION_DIR/CompVis
SAFETY_CHECKER_DIR=$COMPVIS_DIR/stable-diffusion-safety-checker
SAFETY_CHECKER_MODEL=$SAFETY_CHECKER_DIR/pytorch_model.bin


# install the server with all models in the expected location

# check if STABLE_DIFFUSION_DIR exists
if [ ! -d "$STABLE_DIFFUSION_DIR" ]; then
echo "Creating $STABLE_DIFFUSION_DIR"
mkdir $STABLE_DIFFUSION_DIR
else
echo "Directory $STABLE_DIFFUSION_DIR already exists."
fi

cd $STABLE_DIFFUSION_DIR

# check if $CLIP_VIT_LARGE exists
if [ ! -d "OPENAI_DIR" ]; then
echo "Creating $OPENAI_DIR"
mkdir -p OPENAI_DIR
else
echo "Directory $OPENAI_DIR already exists."
fi

cd $OPENAI_DIR

# check if clip-vit-large-patch14 exists
if [ ! -d "$CLIP_VIT_LARGE" ]; then
git clone https://huggingface.co/openai/clip-vit-large-patch14
fi

# check if $RUNWAYML_DIR exists
if [ ! -d "$RUNWAYML_DIR" ]; then
echo "Creating $RUNWAYML_DIR"
mkdir -p $RUNWAYML_DIR
else
echo "Directory $RUNWAYML_DIR already exists."
fi

cd $RUNWAYML_DIR

# check if stable-diffusion-v1-5 exists
if [ ! -d "$SD_MODEL_V1" ]; then
echo "Downloading stable-diffusion-v1-5"
git clone https://huggingface.co/runway/stable-diffusion-v1-5 --branch fp16
else
echo "Directory $SD_MODEL_V1 already exists."
fi

# check if stable-diffusion-inpainting exists
if [ ! -d "$INPAINT_MODEL_V1" ]; then
echo "Downloading stable-diffusion-inpainting"
git clone https://huggingface.co/runway/stable-diffusion-inpainting --branch fp16
else
echo "Directory $INPAINT_MODEL_V1 already exists."
fi

# check if $STABILITY_AI_DIR exists
if [ ! -d "$STABILITY_AI_DIR" ]; then
echo "Creating $STABILITY_AI_DIR"
mkdir -p $STABILITY_AI_DIR
else
echo "Directory $STABILITY_AI_DIR already exists."
fi

cd $STABILITY_AI_DIR

# check if stable-diffusion-2-1-base exists
if [ ! -d "$SD_MODEL_V2" ]; then
echo "Downloading stable-diffusion-2-1-base"
git clone https://huggingface.co/stabilityai/stable-diffusion-2-1-base --branch fp16
else
echo "Directory $SD_MODEL_V2 already exists."
fi

# check if stable-diffusion-2-inpainting exists
if [ ! -d "$INPAINT_MODEL_V2" ]; then
echo "Downloading stable-diffusion-2-inpainting"
git clone https://huggingface.co/stabilityai/stable-diffusion-2-inpainting --branch fp16
else
echo "Directory $INPAINT_MODEL_V2 already exists."
fi

# check if $V1_DIR exists
if [ ! -d "$V1_DIR" ]; then
echo "Creating $V1_DIR"
mkdir -p $V1_DIR
else
echo "Directory $V1_DIR already exists."
fi

cd $V1_DIR

# check if $V2_DIR exists
if [ ! -d "$V2_DIR" ]; then
echo "Creating $V2_DIR"
mkdir -p $V2_DIR
else
echo "Directory $V2_DIR already exists."
fi

cd $V2_DIR

# check if $CLIP_DIR exists
if [ ! -d "$CLIP_DIR" ]; then
echo "Creating $CLIP_DIR"
mkdir -p $CLIP_DIR
else
echo "Directory $CLIP_DIR already exists."
fi

cd $CLIP_DIR

# check if bpe_simple_vocab_16e6.txt.gz exists
if [ ! -f "$VOCAB_MODEL" ]; then
echo "Downloading bpe_simple_vocab_16e6.txt.gz"
git clone https://github.com/openai/CLIP.git
else
echo "File $VOCAB_MODEL already exists."
fi

# check if $COMPVIS_DIR exists
if [ ! -d "$COMPVIS_DIR" ]; then
echo "Creating $COMPVIS_DIR"
mkdir -p $COMPVIS_DIR
else
echo "Directory $COMPVIS_DIR already exists."
fi

cd $COMPVIS_DIR

# check if stable-diffusion-safety-checker exists
if [ ! -d "$SAFETY_CHECKER_DIR" ]; then
echo "Downloading stable-diffusion-safety-checker"
git clone https://huggingface.co/CompVis/stable-diffusion-safety-checker
else
echo "Directory $SAFETY_CHECKER_DIR already exists."
fi
8 changes: 6 additions & 2 deletions runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import numpy as np
import torch
import base64
Expand Down Expand Up @@ -84,9 +86,11 @@ def load_model(self):
# delete old models from memory and clear cache
if hasattr(self, "txt2img"):
del self.txt2img
if hasattr(self, "img2img"):
del self.img2img
if hasattr(self, "inpaint"):
del self.inpaint
torch.cuda.empty_cache()
torch.cuda.empty_cache()

# here we must load checkpoint using stablediffusion
logger.info(f"Loading checkpoint model from {self.model_base_path}")
Expand All @@ -112,7 +116,7 @@ def load_model(self):
)
else:
self.txt2img = StableDiffusionPipeline.from_pretrained(
model_path,
self.model_path,
torch_dtype=torch.half,
scheduler=self.scheduler,
low_cpu_mem_usage=True,
Expand Down