-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict.py
229 lines (200 loc) · 7.35 KB
/
predict.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Prediction interface for Cog ⚙️
# https://github.com/replicate/cog/blob/main/docs/python.md
import os, sys, json
import shutil
sys.path.extend(["/stable-diffusion-webui"])
from cog import BasePredictor, BaseModel, Input, Path
class Predictor(BasePredictor):
def _move_model_to_sdwebui_dir(self):
source_dir = "model"
target_dir = "/stable-diffusion-webui/models/Stable-diffusion"
# Get a list of all files in the source directory
files = os.listdir(source_dir)
# Move each file from the source directory to the target directory
for file in files:
source_file = os.path.join(source_dir, file)
target_file = os.path.join(target_dir, file)
shutil.move(source_file, target_file)
def setup(self) -> None:
"""Load the model into memory to make running multiple predictions efficient"""
self._move_model_to_sdwebui_dir()
# workaround for replicate since its entrypoint may contain invalid args
os.environ["IGNORE_CMD_ARGS_ERRORS"] = "1"
from modules import timer
# moved env preparation to build time to reduce the warm-up time
# from modules import launch_utils
# with launch_utils.startup_timer.subcategory("prepare environment"):
# launch_utils.prepare_environment()
from modules import initialize_util
from modules import initialize
startup_timer = timer.startup_timer
startup_timer.record("launcher")
initialize.imports()
initialize.check_versions()
initialize.initialize()
from fastapi import FastAPI
app = FastAPI()
initialize_util.setup_middleware(app)
from modules.api.api import Api
from modules.call_queue import queue_lock
self.api = Api(app, queue_lock)
def predict(
self,
prompt: str = Input(description="Prompt"),
negative_prompt: str = Input(
description="Negative Prompt",
default="EasyNegative",
),
width: int = Input(
description="Width of output image", ge=1, le=1024, default=512
),
height: int = Input(
description="Height of output image", ge=1, le=1024, default=768
),
num_outputs: int = Input(
description="Number of images to output", ge=1, le=4, default=1
),
scheduler: str = Input(
description="scheduler",
choices=[
"DPM++ 2M Karras",
"DPM++ SDE Karras",
"DPM++ 2M SDE Exponential",
"DPM++ 2M SDE Karras",
"Euler a",
"Euler",
"LMS",
"Heun",
"DPM2",
"DPM2 a",
"DPM++ 2S a",
"DPM++ 2M",
"DPM++ SDE",
"DPM++ 2M SDE",
"DPM++ 2M SDE Heun",
"DPM++ 2M SDE Heun Karras",
"DPM++ 2M SDE Heun Exponential",
"DPM++ 3M SDE",
"DPM++ 3M SDE Karras",
"DPM++ 3M SDE Exponential",
"DPM fast",
"DPM adaptive",
"LMS Karras",
"DPM2 Karras",
"DPM2 a Karras",
"DPM++ 2S a Karras",
"Restart",
"DDIM",
"PLMS",
"UniPC",
],
default="DPM++ SDE Karras",
),
num_inference_steps: int = Input(
description="Number of denoising steps", ge=1, le=100, default=20
),
guidance_scale: float = Input(
description="Scale for classifier-free guidance", ge=1, le=50, default=7.5
),
seed: int = Input(
description="Random seed. Leave blank to randomize the seed", default=-1
),
# image: Path = Input(description="Grayscale input image"),
enable_hr: bool = Input(
description="Hires. fix",
default=False,
),
hr_upscaler: str = Input(
description="Upscaler for Hires. fix",
choices=[
"Latent",
"Latent (antialiased)",
"Latent (bicubic)",
"Latent (bicubic antialiased)",
"Latent (nearest)",
"Latent (nearest-exact)",
"None",
"Lanczos",
"Nearest",
"ESRGAN_4x",
"LDSR",
"R-ESRGAN 4x+",
"R-ESRGAN 4x+ Anime6B",
"ScuNET GAN",
"ScuNET PSNR",
"SwinIR 4x",
],
default="Latent",
),
hr_steps: int = Input(
description="Inference steps for Hires. fix", ge=0, le=100, default=20
),
hr_scale: float = Input(
description="Factor to scale image by", ge=1, le=4, default=2
),
denoising_strength: float = Input(
description="Denoising strength. 1.0 corresponds to full destruction of information in init image",
ge=0,
le=1,
default=0.5,
),
enable_adetailer: bool = Input(
description="ADetailer, suggests enabling for expected small faces, e.g. full body, long-range view",
default=False,
),
) -> list[Path]:
"""Run a single prediction on the model"""
# processed_input = preprocess(image)
# output = self.model(processed_image, scale)
# return postprocess(output)
payload = {
# "init_images": [encoded_image],
"prompt": prompt,
"negative_prompt": negative_prompt,
"width": width,
"height": height,
"batch_size": num_outputs,
"steps": num_inference_steps,
"cfg_scale": guidance_scale,
"seed": seed,
"do_not_save_samples": True,
"sampler_name": scheduler,
"enable_hr": enable_hr,
"hr_upscaler": hr_upscaler,
"hr_second_pass_steps": hr_steps,
"denoising_strength": denoising_strength if enable_hr else None,
"hr_scale": hr_scale,
}
alwayson_scripts = {}
if enable_adetailer:
alwayson_scripts["ADetailer"] = {
"args": [
{
"ad_model": "face_yolov8n.pt",
}
],
}
if alwayson_scripts:
payload["alwayson_scripts"] = alwayson_scripts
from modules.api.models import (
StableDiffusionTxt2ImgProcessingAPI,
StableDiffusionImg2ImgProcessingAPI,
)
req = StableDiffusionTxt2ImgProcessingAPI(**payload)
# generate
resp = self.api.text2imgapi(req)
info = json.loads(resp.info)
from PIL import Image
import uuid
import base64
from io import BytesIO
outputs = []
for i, image in enumerate(resp.images):
seed = info["all_seeds"][i]
gen_bytes = BytesIO(base64.b64decode(image))
gen_data = Image.open(gen_bytes)
filename = "{}-{}.png".format(seed, uuid.uuid1())
gen_data.save(fp=filename, format="PNG")
output = Path(filename)
outputs.append(output)
return outputs