-
Notifications
You must be signed in to change notification settings - Fork 0
/
stable-diffusion-webui.py
143 lines (121 loc) · 4.54 KB
/
stable-diffusion-webui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from colorama import Fore
from pathlib import Path
import modal
import shutil
import subprocess
import sys
import shlex
import os
# modal系の変数の定義
app = modal.App("stable-diffusion-webui-2")
volume_main = modal.NetworkFileSystem.from_name("stable-diffusion-webui-main-2", create_if_missing=True)
# 色んなパスの定義
webui_dir = "/content/stable-diffusion-webui"
webui_model_dir = webui_dir + "/models/Stable-diffusion/"
# モデルのID
model_ids = [
{
"repo_id": "runwayml/stable-diffusion-v1-5",
"model_path": "v1-5-pruned.ckpt",
"config_file_path": "v1-inference.yaml",
},
]
@app.function(
image=modal.Image.from_registry("python:3.8-slim")
.apt_install(
"git", "libgl1-mesa-dev", "libglib2.0-0", "libsm6", "libxrender1", "libxext6"
)
.run_commands(
"pip install -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers"
)
.pip_install(
"blendmodes==2022",
"transformers==4.25.1",
"accelerate==0.12.0",
"basicsr==1.4.2",
"gfpgan==1.3.8",
"gradio==3.16.2",
"numpy==1.23.3",
"Pillow==9.4.0",
"realesrgan==0.3.0",
"torch",
"omegaconf==2.2.3",
"pytorch_lightning==1.7.6",
"scikit-image==0.19.2",
"fonts",
"font-roboto",
"timm==0.6.7",
"piexif==1.1.3",
"einops==0.4.1",
"jsonmerge==1.8.0",
"clean-fid==0.1.29",
"resize-right==0.0.2",
"torchdiffeq==0.2.3",
"kornia==0.6.7",
"lark==1.1.2",
"inflection==0.5.1",
"GitPython==3.1.27",
"torchsde==0.2.5",
"safetensors==0.2.7",
"httpcore<=0.15",
"tensorboard==2.9.1",
"taming-transformers==0.0.1",
"clip",
"xformers",
"test-tube",
"diffusers",
"invisible-watermark",
"pyngrok",
"xformers==0.0.16rc425",
"gdown",
"huggingface_hub",
"colorama",
"torchmetrics==0.11.4",
)
.pip_install("git+https://github.com/mlfoundations/open_clip.git@bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b"),
secrets=[modal.Secret.from_name("my-huggingface-secret")],
network_file_systems={webui_dir: volume_main},
gpu="a10g",
timeout=6000,
)
async def run_stable_diffusion_webui():
print(Fore.CYAN + "\n---------- セットアップ開始 ----------\n")
webui_dir_path = Path(webui_model_dir)
if not webui_dir_path.exists():
subprocess.run(f"git clone -b v2.0 https://github.com/camenduru/stable-diffusion-webui {webui_dir}", shell=True)
# Hugging faceからファイルをダウンロードしてくる関数
def download_hf_file(repo_id, filename):
from huggingface_hub import hf_hub_download
download_dir = hf_hub_download(repo_id=repo_id, filename=filename)
return download_dir
for model_id in model_ids:
print(Fore.GREEN + model_id["repo_id"] + "のセットアップを開始します...")
if not Path(webui_model_dir + model_id["model_path"]).exists():
# モデルのダウンロード&コピー
model_downloaded_dir = download_hf_file(
model_id["repo_id"],
model_id["model_path"],
)
shutil.copy(model_downloaded_dir, webui_model_dir + os.path.basename(model_id["model_path"]))
if "config_file_path" not in model_id:
continue
if not Path(webui_model_dir + model_id["config_file_path"]).exists():
# コンフィグのダウンロード&コピー
config_downloaded_dir = download_hf_file(
model_id["repo_id"], model_id["config_file_path"]
)
shutil.copy(config_downloaded_dir, webui_model_dir + os.path.basename(model_id["config_file_path"]))
print(Fore.GREEN + model_id["repo_id"] + "のセットアップが完了しました!")
print(Fore.CYAN + "\n---------- セットアップ完了 ----------\n")
# WebUIを起動
sys.path.append(webui_dir)
sys.argv += shlex.split("--skip-install --xformers")
os.chdir(webui_dir)
from launch import start, prepare_environment
prepare_environment()
# 最初のargumentは無視されるので注意
sys.argv = shlex.split("--a --gradio-debug --share --xformers")
start()
@app.local_entrypoint()
def main():
run_stable_diffusion_webui.remote()