forked from Sygil-Dev/stable-diffusion
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathkrita_server.py
365 lines (287 loc) · 10.3 KB
/
krita_server.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import contextlib
import threading
import math
import time
import yaml
import os
from typing import Optional
import numpy as np
from pydantic import BaseModel
from fastapi import FastAPI
import uvicorn
from webui import *
from PIL import Image
app = FastAPI()
def load_config():
with open("krita_config.yaml") as file:
return yaml.safe_load(file)
def save_img(image, sample_path, filename):
path = os.path.join(sample_path, filename)
image.save(path)
return os.path.abspath(path)
def fix_aspect_ratio(base_size, max_size, orig_width, orig_height):
def rnd(r, x):
z = 64
return z * round(r * x / z)
ratio = orig_width / orig_height
if orig_width > orig_height:
width, height = rnd(ratio, base_size), base_size
if width > max_size:
width, height = max_size, rnd(1 / ratio, max_size)
else:
width, height = base_size, rnd(1 / ratio, base_size)
if height > max_size:
width, height = rnd(ratio, max_size), max_size
new_ratio = width / height
print(f"img size: {orig_width}x{orig_height} -> {width}x{height}, "
f"aspect ratio: {ratio:.2f} -> {new_ratio:.2f}, {100 * (new_ratio - ratio) / ratio :.2f}% change")
return width, height
def collect_prompt(opts, key):
prompts = opts[key]
if isinstance(prompts, str):
return prompts
if isinstance(prompts, list):
return ", ".join(prompts)
if isinstance(prompts, dict):
prompt = ""
for item, weight in prompts.items():
if not prompt == "":
prompt += " "
if weight is None:
prompt += f"{item}"
else:
prompt += f"{item}:{weight}"
return prompt
raise Exception("wtf man, fix your prompts")
class Txt2ImgRequest(BaseModel):
orig_width: int
orig_height: int
prompt: Optional[str]
negative_prompt: Optional[str]
sampler_name: Optional[str]
steps: Optional[int]
cfg_scale: Optional[float]
batch_count: Optional[int]
batch_size: Optional[int]
base_size: Optional[int]
max_size: Optional[int]
seed: Optional[str]
tiling: Optional[bool]
use_gfpgan: Optional[bool]
face_restorer: Optional[str]
codeformer_weight: Optional[float]
class Img2ImgRequest(BaseModel):
mode: Optional[int]
src_path: str
mask_path: Optional[str]
prompt: Optional[str]
negative_prompt: Optional[str]
sampler_name: Optional[str]
steps: Optional[int]
cfg_scale: Optional[float]
denoising_strength: Optional[float]
batch_count: Optional[int]
batch_size: Optional[int]
base_size: Optional[int]
max_size: Optional[int]
seed: Optional[str]
tiling: Optional[bool]
use_gfpgan: Optional[bool]
face_restorer: Optional[str]
codeformer_weight: Optional[float]
upscale_overlap: Optional[int]
upscaler_name: Optional[str]
inpainting_fill: Optional[int]
inpaint_full_res: Optional[bool]
mask_blur: Optional[int]
invert_mask: Optional[bool]
class UpscaleRequest(BaseModel):
src_path: str
upscaler_name: Optional[str]
downscale_first: Optional[bool]
def get_sampler_index(sampler_name: str):
for index, sampler in enumerate(modules.sd_samplers.samplers):
name, constructor, aliases = sampler
if sampler_name == name or sampler_name in aliases:
return index
return 0
def get_upscaler_index(upscaler_name: str):
for index, upscaler in enumerate(shared.sd_upscalers):
if upscaler.name == upscaler_name:
return index
return 0
def set_face_restorer(face_restorer: str, codeformer_weight: float):
shared.opts.face_restoration_model = face_restorer
shared.opts.code_former_weight = codeformer_weight
@app.get("/config")
async def read_item():
opt = load_config()['plugin']
sample_path = opt['sample_path']
os.makedirs(sample_path, exist_ok=True)
filename = f"{int(time.time())}"
path = os.path.join(sample_path, filename)
src_path = os.path.abspath(path)
return {"new_img": src_path + ".png",
"new_img_mask": src_path + "_mask.png",
"upscalers": [upscaler.name for upscaler in shared.sd_upscalers],
**opt}
@app.post("/txt2img")
async def f_txt2img(req: Txt2ImgRequest):
print(f"txt2img: {req}")
opt = load_config()['txt2img']
set_face_restorer(req.face_restorer or opt['face_restorer'],
req.codeformer_weight or opt['codeformer_weight'])
sampler_index = get_sampler_index(req.sampler_name or opt['sampler_name'])
seed = opt['seed']
if req.seed is not None and not req.seed == '':
seed = int(req.seed)
width, height = fix_aspect_ratio(req.base_size or opt['base_size'], req.max_size or opt['max_size'],
req.orig_width, req.orig_height)
output_images, info, html = modules.txt2img.txt2img(
req.prompt or collect_prompt(opt, "prompts"),
req.negative_prompt or collect_prompt(opt, "negative_prompt"),
"None",
"None",
req.steps or opt['steps'],
sampler_index,
req.use_gfpgan or opt['use_gfpgan'],
req.tiling or opt['tiling'],
req.batch_count or opt['n_iter'],
req.batch_size or opt['batch_size'],
req.cfg_scale or opt['cfg_scale'],
seed,
None,
0,
0,
0,
height,
width,
False,
None,
None,
0
)
sample_path = opt['sample_path']
os.makedirs(sample_path, exist_ok=True)
resized_images = [modules.images.resize_image(0, image, req.orig_width, req.orig_height) for image in output_images]
outputs = [save_img(image, sample_path, filename=f"{int(time.time())}_{i}.png")
for i, image in enumerate(resized_images)]
print(f"finished: {outputs}\n{info}")
return {"outputs": outputs, "info": info}
@app.post("/img2img")
async def f_img2img(req: Img2ImgRequest):
print(f"img2img: {req}")
opt = load_config()['img2img']
set_face_restorer(req.face_restorer or opt['face_restorer'],
req.codeformer_weight or opt['codeformer_weight'])
sampler_index = get_sampler_index(req.sampler_name or opt['sampler_name'])
seed = opt['seed']
if req.seed is not None and not req.seed == '':
seed = int(req.seed)
mode = req.mode or opt['mode']
image = Image.open(req.src_path)
orig_width, orig_height = image.size
if mode == 1:
mask = Image.open(req.mask_path).convert('L')
else:
mask = None
# because API in webui changed
if mode == 3:
mode = 2
upscaler_index = get_upscaler_index(req.upscaler_name or opt['upscaler_name'])
base_size = req.base_size or opt['base_size']
if mode == 2:
width, height = base_size, base_size
if upscaler_index > 0:
image = image.convert("RGB")
else:
width, height = fix_aspect_ratio(base_size, req.max_size or opt['max_size'],
orig_width, orig_height)
output_images, info, html = modules.img2img.img2img(
req.prompt or collect_prompt(opt, 'prompts'),
req.negative_prompt or collect_prompt(opt, 'negative_prompt'),
"None",
"None",
image,
{"image": image, "mask": mask},
mask,
1,
req.steps or opt['steps'],
sampler_index,
req.mask_blur or opt['mask_blur'],
req.inpainting_fill or opt['inpainting_fill'],
req.use_gfpgan or opt['use_gfpgan'],
req.tiling or opt['tiling'],
mode,
req.batch_count or opt['n_iter'],
req.batch_size or opt['batch_size'],
req.cfg_scale or opt['cfg_scale'],
req.denoising_strength or opt['denoising_strength'],
seed,
None,
0,
0,
0,
height,
width,
opt['resize_mode'],
upscaler_index,
req.upscale_overlap or opt['upscale_overlap'],
req.inpaint_full_res or opt['inpaint_full_res'],
False, # req.invert_mask or opt['invert_mask'],
0
)
resized_images = [modules.images.resize_image(0, image, orig_width, orig_height) for image in output_images]
if mode == 1:
def remove_not_masked(img):
masked_img = Image.new("RGBA", img.size, (0, 0, 0, 0))
masked_img.paste(img, (0, 0), mask=mask)
return masked_img
resized_images = [remove_not_masked(x) for x in resized_images]
sample_path = opt['sample_path']
os.makedirs(sample_path, exist_ok=True)
outputs = [save_img(image, sample_path, filename=f"{int(time.time())}_{i}.png")
for i, image in enumerate(resized_images)]
print(f"finished: {outputs}\n{info}")
return {"outputs": outputs, "info": info}
@app.post("/upscale")
async def f_upscale(req: UpscaleRequest):
print(f"upscale: {req}")
opt = load_config()['upscale']
image = Image.open(req.src_path).convert('RGB')
orig_width, orig_height = image.size
upscaler_index = get_upscaler_index(req.upscaler_name or opt['upscaler_name'])
upscaler = shared.sd_upscalers[upscaler_index]
if upscaler.name == 'None':
print(f"No upscaler selected, will do nothing")
return
if req.downscale_first or opt['downscale_first']:
image = modules.images.resize_image(0, image, orig_width // 2, orig_height // 2)
upscaled_image = upscaler.upscale(image, 2 * orig_width, 2 * orig_height)
resized_image = modules.images.resize_image(0, upscaled_image, orig_width, orig_height)
sample_path = opt['sample_path']
os.makedirs(sample_path, exist_ok=True)
output = save_img(resized_image, sample_path, filename=f"{int(time.time())}.png")
print(f"finished: {output}")
return {"output": output}
class Server(uvicorn.Server):
def install_signal_handlers(self):
pass
@contextlib.contextmanager
def run_in_thread(self):
thread = threading.Thread(target=self.run)
thread.start()
try:
while not self.started:
time.sleep(1e-3)
yield
finally:
self.should_exit = True
thread.join()
def start():
config = uvicorn.Config("krita_server:app", host="127.0.0.1", port=8000, log_level="info")
server = Server(config=config)
with server.run_in_thread():
webui()
if __name__ == "__main__":
start()