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
from PIL import Image
|
| 3 |
+
from typing import Any
|
| 4 |
+
|
| 5 |
+
from constants import DEVICE
|
| 6 |
+
from paths import FastStableDiffusionPaths
|
| 7 |
+
from backend.upscale.upscaler import upscale_image
|
| 8 |
+
from backend.upscale.tiled_upscale import generate_upscaled_image
|
| 9 |
+
from frontend.webui.image_variations_ui import generate_image_variations
|
| 10 |
+
from backend.lora import (
|
| 11 |
+
get_active_lora_weights,
|
| 12 |
+
update_lora_weights,
|
| 13 |
+
load_lora_weight,
|
| 14 |
+
)
|
| 15 |
+
from backend.models.lcmdiffusion_setting import (
|
| 16 |
+
DiffusionTask,
|
| 17 |
+
ControlNetSetting,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
_batch_count = 1
|
| 22 |
+
_edit_lora_settings = False
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def user_value(
|
| 26 |
+
value_type: type,
|
| 27 |
+
message: str,
|
| 28 |
+
default_value: Any,
|
| 29 |
+
) -> Any:
|
| 30 |
+
try:
|
| 31 |
+
value = value_type(input(message))
|
| 32 |
+
except:
|
| 33 |
+
value = default_value
|
| 34 |
+
return value
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def interactive_mode(
|
| 38 |
+
config,
|
| 39 |
+
context,
|
| 40 |
+
):
|
| 41 |
+
print("=============================================")
|
| 42 |
+
print("Welcome to FastSD CPU Interactive CLI")
|
| 43 |
+
print("=============================================")
|
| 44 |
+
while True:
|
| 45 |
+
print("> 1. Text to Image")
|
| 46 |
+
print("> 2. Image to Image")
|
| 47 |
+
print("> 3. Image Variations")
|
| 48 |
+
print("> 4. EDSR Upscale")
|
| 49 |
+
print("> 5. SD Upscale")
|
| 50 |
+
print("> 6. Edit default generation settings")
|
| 51 |
+
print("> 7. Edit LoRA settings")
|
| 52 |
+
print("> 8. Edit ControlNet settings")
|
| 53 |
+
print("> 9. Edit negative prompt")
|
| 54 |
+
print("> 10. Quit")
|
| 55 |
+
option = user_value(
|
| 56 |
+
int,
|
| 57 |
+
"Enter a Diffusion Task number (1): ",
|
| 58 |
+
1,
|
| 59 |
+
)
|
| 60 |
+
if option not in range(1, 11):
|
| 61 |
+
print("Wrong Diffusion Task number!")
|
| 62 |
+
exit()
|
| 63 |
+
|
| 64 |
+
if option == 1:
|
| 65 |
+
interactive_txt2img(
|
| 66 |
+
config,
|
| 67 |
+
context,
|
| 68 |
+
)
|
| 69 |
+
elif option == 2:
|
| 70 |
+
interactive_img2img(
|
| 71 |
+
config,
|
| 72 |
+
context,
|
| 73 |
+
)
|
| 74 |
+
elif option == 3:
|
| 75 |
+
interactive_variations(
|
| 76 |
+
config,
|
| 77 |
+
context,
|
| 78 |
+
)
|
| 79 |
+
elif option == 4:
|
| 80 |
+
interactive_edsr(
|
| 81 |
+
config,
|
| 82 |
+
context,
|
| 83 |
+
)
|
| 84 |
+
elif option == 5:
|
| 85 |
+
interactive_sdupscale(
|
| 86 |
+
config,
|
| 87 |
+
context,
|
| 88 |
+
)
|
| 89 |
+
elif option == 6:
|
| 90 |
+
interactive_settings(
|
| 91 |
+
config,
|
| 92 |
+
context,
|
| 93 |
+
)
|
| 94 |
+
elif option == 7:
|
| 95 |
+
interactive_lora(
|
| 96 |
+
config,
|
| 97 |
+
context,
|
| 98 |
+
True,
|
| 99 |
+
)
|
| 100 |
+
elif option == 8:
|
| 101 |
+
interactive_controlnet(
|
| 102 |
+
config,
|
| 103 |
+
context,
|
| 104 |
+
True,
|
| 105 |
+
)
|
| 106 |
+
elif option == 9:
|
| 107 |
+
interactive_negative(
|
| 108 |
+
config,
|
| 109 |
+
context,
|
| 110 |
+
)
|
| 111 |
+
elif option == 10:
|
| 112 |
+
exit()
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def interactive_negative(
|
| 116 |
+
config,
|
| 117 |
+
context,
|
| 118 |
+
):
|
| 119 |
+
settings = config.lcm_diffusion_setting
|
| 120 |
+
print(f"Current negative prompt: '{settings.negative_prompt}'")
|
| 121 |
+
user_input = input("Write a negative prompt (set guidance > 1.0): ")
|
| 122 |
+
if user_input == "":
|
| 123 |
+
return
|
| 124 |
+
else:
|
| 125 |
+
settings.negative_prompt = user_input
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def interactive_controlnet(
|
| 129 |
+
config,
|
| 130 |
+
context,
|
| 131 |
+
menu_flag=False,
|
| 132 |
+
):
|
| 133 |
+
"""
|
| 134 |
+
@param menu_flag: Indicates whether this function was called from the main
|
| 135 |
+
interactive CLI menu; _True_ if called from the main menu, _False_ otherwise
|
| 136 |
+
"""
|
| 137 |
+
settings = config.lcm_diffusion_setting
|
| 138 |
+
if not settings.controlnet:
|
| 139 |
+
settings.controlnet = ControlNetSetting()
|
| 140 |
+
|
| 141 |
+
current_enabled = settings.controlnet.enabled
|
| 142 |
+
current_adapter_path = settings.controlnet.adapter_path
|
| 143 |
+
current_conditioning_scale = settings.controlnet.conditioning_scale
|
| 144 |
+
current_control_image = settings.controlnet._control_image
|
| 145 |
+
|
| 146 |
+
option = input("Enable ControlNet? (y/N): ")
|
| 147 |
+
settings.controlnet.enabled = True if option.upper() == "Y" else False
|
| 148 |
+
if settings.controlnet.enabled:
|
| 149 |
+
option = input(
|
| 150 |
+
f"Enter ControlNet adapter path ({settings.controlnet.adapter_path}): "
|
| 151 |
+
)
|
| 152 |
+
if option != "":
|
| 153 |
+
settings.controlnet.adapter_path = option
|
| 154 |
+
settings.controlnet.conditioning_scale = user_value(
|
| 155 |
+
float,
|
| 156 |
+
f"Enter ControlNet conditioning scale ({settings.controlnet.conditioning_scale}): ",
|
| 157 |
+
settings.controlnet.conditioning_scale,
|
| 158 |
+
)
|
| 159 |
+
option = input(
|
| 160 |
+
f"Enter ControlNet control image path (Leave empty to reuse current): "
|
| 161 |
+
)
|
| 162 |
+
if option != "":
|
| 163 |
+
try:
|
| 164 |
+
new_image = Image.open(option)
|
| 165 |
+
settings.controlnet._control_image = new_image
|
| 166 |
+
except (AttributeError, FileNotFoundError) as e:
|
| 167 |
+
settings.controlnet._control_image = None
|
| 168 |
+
if (
|
| 169 |
+
not settings.controlnet.adapter_path
|
| 170 |
+
or not path.exists(settings.controlnet.adapter_path)
|
| 171 |
+
or not settings.controlnet._control_image
|
| 172 |
+
):
|
| 173 |
+
print("Invalid ControlNet settings! Disabling ControlNet")
|
| 174 |
+
settings.controlnet.enabled = False
|
| 175 |
+
|
| 176 |
+
if (
|
| 177 |
+
settings.controlnet.enabled != current_enabled
|
| 178 |
+
or settings.controlnet.adapter_path != current_adapter_path
|
| 179 |
+
):
|
| 180 |
+
settings.rebuild_pipeline = True
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def interactive_lora(
|
| 184 |
+
config,
|
| 185 |
+
context,
|
| 186 |
+
menu_flag=False,
|
| 187 |
+
):
|
| 188 |
+
"""
|
| 189 |
+
@param menu_flag: Indicates whether this function was called from the main
|
| 190 |
+
interactive CLI menu; _True_ if called from the main menu, _False_ otherwise
|
| 191 |
+
"""
|
| 192 |
+
if context == None or context.lcm_text_to_image.pipeline == None:
|
| 193 |
+
print("Diffusion pipeline not initialized, please run a generation task first!")
|
| 194 |
+
return
|
| 195 |
+
|
| 196 |
+
print("> 1. Change LoRA weights")
|
| 197 |
+
print("> 2. Load new LoRA model")
|
| 198 |
+
option = user_value(
|
| 199 |
+
int,
|
| 200 |
+
"Enter a LoRA option (1): ",
|
| 201 |
+
1,
|
| 202 |
+
)
|
| 203 |
+
if option not in range(1, 3):
|
| 204 |
+
print("Wrong LoRA option!")
|
| 205 |
+
return
|
| 206 |
+
|
| 207 |
+
if option == 1:
|
| 208 |
+
update_weights = []
|
| 209 |
+
active_weights = get_active_lora_weights()
|
| 210 |
+
for lora in active_weights:
|
| 211 |
+
weight = user_value(
|
| 212 |
+
float,
|
| 213 |
+
f"Enter a new LoRA weight for {lora[0]} ({lora[1]}): ",
|
| 214 |
+
lora[1],
|
| 215 |
+
)
|
| 216 |
+
update_weights.append(
|
| 217 |
+
(
|
| 218 |
+
lora[0],
|
| 219 |
+
weight,
|
| 220 |
+
)
|
| 221 |
+
)
|
| 222 |
+
if len(update_weights) > 0:
|
| 223 |
+
update_lora_weights(
|
| 224 |
+
context.lcm_text_to_image.pipeline,
|
| 225 |
+
config.lcm_diffusion_setting,
|
| 226 |
+
update_weights,
|
| 227 |
+
)
|
| 228 |
+
elif option == 2:
|
| 229 |
+
# Load a new LoRA
|
| 230 |
+
settings = config.lcm_diffusion_setting
|
| 231 |
+
settings.lora.fuse = False
|
| 232 |
+
settings.lora.enabled = False
|
| 233 |
+
settings.lora.path = input("Enter LoRA model path: ")
|
| 234 |
+
settings.lora.weight = user_value(
|
| 235 |
+
float,
|
| 236 |
+
"Enter a LoRA weight (0.5): ",
|
| 237 |
+
0.5,
|
| 238 |
+
)
|
| 239 |
+
if not path.exists(settings.lora.path):
|
| 240 |
+
print("Invalid LoRA model path!")
|
| 241 |
+
return
|
| 242 |
+
settings.lora.enabled = True
|
| 243 |
+
load_lora_weight(context.lcm_text_to_image.pipeline, settings)
|
| 244 |
+
|
| 245 |
+
if menu_flag:
|
| 246 |
+
global _edit_lora_settings
|
| 247 |
+
_edit_lora_settings = False
|
| 248 |
+
option = input("Edit LoRA settings after every generation? (y/N): ")
|
| 249 |
+
if option.upper() == "Y":
|
| 250 |
+
_edit_lora_settings = True
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def interactive_settings(
|
| 254 |
+
config,
|
| 255 |
+
context,
|
| 256 |
+
):
|
| 257 |
+
global _batch_count
|
| 258 |
+
settings = config.lcm_diffusion_setting
|
| 259 |
+
print("Enter generation settings (leave empty to use current value)")
|
| 260 |
+
print("> 1. Use LCM")
|
| 261 |
+
print("> 2. Use LCM-Lora")
|
| 262 |
+
print("> 3. Use OpenVINO")
|
| 263 |
+
option = user_value(
|
| 264 |
+
int,
|
| 265 |
+
"Select inference model option (1): ",
|
| 266 |
+
1,
|
| 267 |
+
)
|
| 268 |
+
if option not in range(1, 4):
|
| 269 |
+
print("Wrong inference model option! Falling back to defaults")
|
| 270 |
+
return
|
| 271 |
+
|
| 272 |
+
settings.use_lcm_lora = False
|
| 273 |
+
settings.use_openvino = False
|
| 274 |
+
if option == 1:
|
| 275 |
+
lcm_model_id = input(f"Enter LCM model ID ({settings.lcm_model_id}): ")
|
| 276 |
+
if lcm_model_id != "":
|
| 277 |
+
settings.lcm_model_id = lcm_model_id
|
| 278 |
+
elif option == 2:
|
| 279 |
+
settings.use_lcm_lora = True
|
| 280 |
+
lcm_lora_id = input(
|
| 281 |
+
f"Enter LCM-Lora model ID ({settings.lcm_lora.lcm_lora_id}): "
|
| 282 |
+
)
|
| 283 |
+
if lcm_lora_id != "":
|
| 284 |
+
settings.lcm_lora.lcm_lora_id = lcm_lora_id
|
| 285 |
+
base_model_id = input(
|
| 286 |
+
f"Enter Base model ID ({settings.lcm_lora.base_model_id}): "
|
| 287 |
+
)
|
| 288 |
+
if base_model_id != "":
|
| 289 |
+
settings.lcm_lora.base_model_id = base_model_id
|
| 290 |
+
elif option == 3:
|
| 291 |
+
settings.use_openvino = True
|
| 292 |
+
openvino_lcm_model_id = input(
|
| 293 |
+
f"Enter OpenVINO model ID ({settings.openvino_lcm_model_id}): "
|
| 294 |
+
)
|
| 295 |
+
if openvino_lcm_model_id != "":
|
| 296 |
+
settings.openvino_lcm_model_id = openvino_lcm_model_id
|
| 297 |
+
|
| 298 |
+
settings.use_offline_model = True
|
| 299 |
+
settings.use_tiny_auto_encoder = True
|
| 300 |
+
option = input("Work offline? (Y/n): ")
|
| 301 |
+
if option.upper() == "N":
|
| 302 |
+
settings.use_offline_model = False
|
| 303 |
+
option = input("Use Tiny Auto Encoder? (Y/n): ")
|
| 304 |
+
if option.upper() == "N":
|
| 305 |
+
settings.use_tiny_auto_encoder = False
|
| 306 |
+
|
| 307 |
+
settings.image_width = user_value(
|
| 308 |
+
int,
|
| 309 |
+
f"Image width ({settings.image_width}): ",
|
| 310 |
+
settings.image_width,
|
| 311 |
+
)
|
| 312 |
+
settings.image_height = user_value(
|
| 313 |
+
int,
|
| 314 |
+
f"Image height ({settings.image_height}): ",
|
| 315 |
+
settings.image_height,
|
| 316 |
+
)
|
| 317 |
+
settings.inference_steps = user_value(
|
| 318 |
+
int,
|
| 319 |
+
f"Inference steps ({settings.inference_steps}): ",
|
| 320 |
+
settings.inference_steps,
|
| 321 |
+
)
|
| 322 |
+
settings.guidance_scale = user_value(
|
| 323 |
+
float,
|
| 324 |
+
f"Guidance scale ({settings.guidance_scale}): ",
|
| 325 |
+
settings.guidance_scale,
|
| 326 |
+
)
|
| 327 |
+
settings.number_of_images = user_value(
|
| 328 |
+
int,
|
| 329 |
+
f"Number of images per batch ({settings.number_of_images}): ",
|
| 330 |
+
settings.number_of_images,
|
| 331 |
+
)
|
| 332 |
+
_batch_count = user_value(
|
| 333 |
+
int,
|
| 334 |
+
f"Batch count ({_batch_count}): ",
|
| 335 |
+
_batch_count,
|
| 336 |
+
)
|
| 337 |
+
# output_format = user_value(int, f"Output format (PNG)", 1)
|
| 338 |
+
print(config.lcm_diffusion_setting)
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
def interactive_txt2img(
|
| 342 |
+
config,
|
| 343 |
+
context,
|
| 344 |
+
):
|
| 345 |
+
global _batch_count
|
| 346 |
+
config.lcm_diffusion_setting.diffusion_task = DiffusionTask.text_to_image.value
|
| 347 |
+
user_input = input("Write a prompt (write 'exit' to quit): ")
|
| 348 |
+
while True:
|
| 349 |
+
if user_input == "exit":
|
| 350 |
+
return
|
| 351 |
+
elif user_input == "":
|
| 352 |
+
user_input = config.lcm_diffusion_setting.prompt
|
| 353 |
+
config.lcm_diffusion_setting.prompt = user_input
|
| 354 |
+
for _ in range(0, _batch_count):
|
| 355 |
+
images = context.generate_text_to_image(
|
| 356 |
+
settings=config,
|
| 357 |
+
device=DEVICE,
|
| 358 |
+
)
|
| 359 |
+
context.save_images(
|
| 360 |
+
images,
|
| 361 |
+
config,
|
| 362 |
+
)
|
| 363 |
+
if _edit_lora_settings:
|
| 364 |
+
interactive_lora(
|
| 365 |
+
config,
|
| 366 |
+
context,
|
| 367 |
+
)
|
| 368 |
+
user_input = input("Write a prompt: ")
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
def interactive_img2img(
|
| 372 |
+
config,
|
| 373 |
+
context,
|
| 374 |
+
):
|
| 375 |
+
global _batch_count
|
| 376 |
+
settings = config.lcm_diffusion_setting
|
| 377 |
+
settings.diffusion_task = DiffusionTask.image_to_image.value
|
| 378 |
+
steps = settings.inference_steps
|
| 379 |
+
source_path = input("Image path: ")
|
| 380 |
+
if source_path == "":
|
| 381 |
+
print("Error : You need to provide a file in img2img mode")
|
| 382 |
+
return
|
| 383 |
+
settings.strength = user_value(
|
| 384 |
+
float,
|
| 385 |
+
f"img2img strength ({settings.strength}): ",
|
| 386 |
+
settings.strength,
|
| 387 |
+
)
|
| 388 |
+
settings.inference_steps = int(steps / settings.strength + 1)
|
| 389 |
+
user_input = input("Write a prompt (write 'exit' to quit): ")
|
| 390 |
+
while True:
|
| 391 |
+
if user_input == "exit":
|
| 392 |
+
settings.inference_steps = steps
|
| 393 |
+
return
|
| 394 |
+
settings.init_image = Image.open(source_path)
|
| 395 |
+
settings.prompt = user_input
|
| 396 |
+
for _ in range(0, _batch_count):
|
| 397 |
+
images = context.generate_text_to_image(
|
| 398 |
+
settings=config,
|
| 399 |
+
device=DEVICE,
|
| 400 |
+
)
|
| 401 |
+
context.save_images(
|
| 402 |
+
images,
|
| 403 |
+
config,
|
| 404 |
+
)
|
| 405 |
+
new_path = input(f"Image path ({source_path}): ")
|
| 406 |
+
if new_path != "":
|
| 407 |
+
source_path = new_path
|
| 408 |
+
settings.strength = user_value(
|
| 409 |
+
float,
|
| 410 |
+
f"img2img strength ({settings.strength}): ",
|
| 411 |
+
settings.strength,
|
| 412 |
+
)
|
| 413 |
+
if _edit_lora_settings:
|
| 414 |
+
interactive_lora(
|
| 415 |
+
config,
|
| 416 |
+
context,
|
| 417 |
+
)
|
| 418 |
+
settings.inference_steps = int(steps / settings.strength + 1)
|
| 419 |
+
user_input = input("Write a prompt: ")
|
| 420 |
+
|
| 421 |
+
|
| 422 |
+
def interactive_variations(
|
| 423 |
+
config,
|
| 424 |
+
context,
|
| 425 |
+
):
|
| 426 |
+
global _batch_count
|
| 427 |
+
settings = config.lcm_diffusion_setting
|
| 428 |
+
settings.diffusion_task = DiffusionTask.image_to_image.value
|
| 429 |
+
steps = settings.inference_steps
|
| 430 |
+
source_path = input("Image path: ")
|
| 431 |
+
if source_path == "":
|
| 432 |
+
print("Error : You need to provide a file in Image variations mode")
|
| 433 |
+
return
|
| 434 |
+
settings.strength = user_value(
|
| 435 |
+
float,
|
| 436 |
+
f"Image variations strength ({settings.strength}): ",
|
| 437 |
+
settings.strength,
|
| 438 |
+
)
|
| 439 |
+
settings.inference_steps = int(steps / settings.strength + 1)
|
| 440 |
+
while True:
|
| 441 |
+
settings.init_image = Image.open(source_path)
|
| 442 |
+
settings.prompt = ""
|
| 443 |
+
for i in range(0, _batch_count):
|
| 444 |
+
generate_image_variations(
|
| 445 |
+
settings.init_image,
|
| 446 |
+
settings.strength,
|
| 447 |
+
)
|
| 448 |
+
if _edit_lora_settings:
|
| 449 |
+
interactive_lora(
|
| 450 |
+
config,
|
| 451 |
+
context,
|
| 452 |
+
)
|
| 453 |
+
user_input = input("Continue in Image variations mode? (Y/n): ")
|
| 454 |
+
if user_input.upper() == "N":
|
| 455 |
+
settings.inference_steps = steps
|
| 456 |
+
return
|
| 457 |
+
new_path = input(f"Image path ({source_path}): ")
|
| 458 |
+
if new_path != "":
|
| 459 |
+
source_path = new_path
|
| 460 |
+
settings.strength = user_value(
|
| 461 |
+
float,
|
| 462 |
+
f"Image variations strength ({settings.strength}): ",
|
| 463 |
+
settings.strength,
|
| 464 |
+
)
|
| 465 |
+
settings.inference_steps = int(steps / settings.strength + 1)
|
| 466 |
+
|
| 467 |
+
|
| 468 |
+
def interactive_edsr(
|
| 469 |
+
config,
|
| 470 |
+
context,
|
| 471 |
+
):
|
| 472 |
+
source_path = input("Image path: ")
|
| 473 |
+
if source_path == "":
|
| 474 |
+
print("Error : You need to provide a file in EDSR mode")
|
| 475 |
+
return
|
| 476 |
+
while True:
|
| 477 |
+
output_path = FastStableDiffusionPaths.get_upscale_filepath(
|
| 478 |
+
source_path,
|
| 479 |
+
2,
|
| 480 |
+
config.generated_images.format,
|
| 481 |
+
)
|
| 482 |
+
result = upscale_image(
|
| 483 |
+
context,
|
| 484 |
+
source_path,
|
| 485 |
+
output_path,
|
| 486 |
+
2,
|
| 487 |
+
)
|
| 488 |
+
user_input = input("Continue in EDSR upscale mode? (Y/n): ")
|
| 489 |
+
if user_input.upper() == "N":
|
| 490 |
+
return
|
| 491 |
+
new_path = input(f"Image path ({source_path}): ")
|
| 492 |
+
if new_path != "":
|
| 493 |
+
source_path = new_path
|
| 494 |
+
|
| 495 |
+
|
| 496 |
+
def interactive_sdupscale_settings(config):
|
| 497 |
+
steps = config.lcm_diffusion_setting.inference_steps
|
| 498 |
+
custom_settings = {}
|
| 499 |
+
print("> 1. Upscale whole image")
|
| 500 |
+
print("> 2. Define custom tiles (advanced)")
|
| 501 |
+
option = user_value(
|
| 502 |
+
int,
|
| 503 |
+
"Select an SD Upscale option (1): ",
|
| 504 |
+
1,
|
| 505 |
+
)
|
| 506 |
+
if option not in range(1, 3):
|
| 507 |
+
print("Wrong SD Upscale option!")
|
| 508 |
+
return
|
| 509 |
+
|
| 510 |
+
# custom_settings["source_file"] = args.file
|
| 511 |
+
custom_settings["source_file"] = ""
|
| 512 |
+
new_path = input(f"Input image path ({custom_settings['source_file']}): ")
|
| 513 |
+
if new_path != "":
|
| 514 |
+
custom_settings["source_file"] = new_path
|
| 515 |
+
if custom_settings["source_file"] == "":
|
| 516 |
+
print("Error : You need to provide a file in SD Upscale mode")
|
| 517 |
+
return
|
| 518 |
+
custom_settings["target_file"] = None
|
| 519 |
+
if option == 2:
|
| 520 |
+
custom_settings["target_file"] = input("Image to patch: ")
|
| 521 |
+
if custom_settings["target_file"] == "":
|
| 522 |
+
print("No target file provided, upscaling whole input image instead!")
|
| 523 |
+
custom_settings["target_file"] = None
|
| 524 |
+
option = 1
|
| 525 |
+
custom_settings["output_format"] = config.generated_images.format
|
| 526 |
+
custom_settings["strength"] = user_value(
|
| 527 |
+
float,
|
| 528 |
+
f"SD Upscale strength ({config.lcm_diffusion_setting.strength}): ",
|
| 529 |
+
config.lcm_diffusion_setting.strength,
|
| 530 |
+
)
|
| 531 |
+
config.lcm_diffusion_setting.inference_steps = int(
|
| 532 |
+
steps / custom_settings["strength"] + 1
|
| 533 |
+
)
|
| 534 |
+
if option == 1:
|
| 535 |
+
custom_settings["scale_factor"] = user_value(
|
| 536 |
+
float,
|
| 537 |
+
f"Scale factor (2.0): ",
|
| 538 |
+
2.0,
|
| 539 |
+
)
|
| 540 |
+
custom_settings["tile_size"] = user_value(
|
| 541 |
+
int,
|
| 542 |
+
f"Split input image into tiles of the following size, in pixels (256): ",
|
| 543 |
+
256,
|
| 544 |
+
)
|
| 545 |
+
custom_settings["tile_overlap"] = user_value(
|
| 546 |
+
int,
|
| 547 |
+
f"Tile overlap, in pixels (16): ",
|
| 548 |
+
16,
|
| 549 |
+
)
|
| 550 |
+
elif option == 2:
|
| 551 |
+
custom_settings["scale_factor"] = user_value(
|
| 552 |
+
float,
|
| 553 |
+
"Input image to Image-to-patch scale_factor (2.0): ",
|
| 554 |
+
2.0,
|
| 555 |
+
)
|
| 556 |
+
custom_settings["tile_size"] = 256
|
| 557 |
+
custom_settings["tile_overlap"] = 16
|
| 558 |
+
custom_settings["prompt"] = input(
|
| 559 |
+
"Write a prompt describing the input image (optional): "
|
| 560 |
+
)
|
| 561 |
+
custom_settings["tiles"] = []
|
| 562 |
+
if option == 2:
|
| 563 |
+
add_tile = True
|
| 564 |
+
while add_tile:
|
| 565 |
+
print("=== Define custom SD Upscale tile ===")
|
| 566 |
+
tile_x = user_value(
|
| 567 |
+
int,
|
| 568 |
+
"Enter tile's X position: ",
|
| 569 |
+
0,
|
| 570 |
+
)
|
| 571 |
+
tile_y = user_value(
|
| 572 |
+
int,
|
| 573 |
+
"Enter tile's Y position: ",
|
| 574 |
+
0,
|
| 575 |
+
)
|
| 576 |
+
tile_w = user_value(
|
| 577 |
+
int,
|
| 578 |
+
"Enter tile's width (256): ",
|
| 579 |
+
256,
|
| 580 |
+
)
|
| 581 |
+
tile_h = user_value(
|
| 582 |
+
int,
|
| 583 |
+
"Enter tile's height (256): ",
|
| 584 |
+
256,
|
| 585 |
+
)
|
| 586 |
+
tile_scale = user_value(
|
| 587 |
+
float,
|
| 588 |
+
"Enter tile's scale factor (2.0): ",
|
| 589 |
+
2.0,
|
| 590 |
+
)
|
| 591 |
+
tile_prompt = input("Enter tile's prompt (optional): ")
|
| 592 |
+
custom_settings["tiles"].append(
|
| 593 |
+
{
|
| 594 |
+
"x": tile_x,
|
| 595 |
+
"y": tile_y,
|
| 596 |
+
"w": tile_w,
|
| 597 |
+
"h": tile_h,
|
| 598 |
+
"mask_box": None,
|
| 599 |
+
"prompt": tile_prompt,
|
| 600 |
+
"scale_factor": tile_scale,
|
| 601 |
+
}
|
| 602 |
+
)
|
| 603 |
+
tile_option = input("Do you want to define another tile? (y/N): ")
|
| 604 |
+
if tile_option == "" or tile_option.upper() == "N":
|
| 605 |
+
add_tile = False
|
| 606 |
+
|
| 607 |
+
return custom_settings
|
| 608 |
+
|
| 609 |
+
|
| 610 |
+
def interactive_sdupscale(
|
| 611 |
+
config,
|
| 612 |
+
context,
|
| 613 |
+
):
|
| 614 |
+
settings = config.lcm_diffusion_setting
|
| 615 |
+
settings.diffusion_task = DiffusionTask.image_to_image.value
|
| 616 |
+
settings.init_image = ""
|
| 617 |
+
source_path = ""
|
| 618 |
+
steps = settings.inference_steps
|
| 619 |
+
|
| 620 |
+
while True:
|
| 621 |
+
custom_upscale_settings = None
|
| 622 |
+
option = input("Edit custom SD Upscale settings? (y/N): ")
|
| 623 |
+
if option.upper() == "Y":
|
| 624 |
+
config.lcm_diffusion_setting.inference_steps = steps
|
| 625 |
+
custom_upscale_settings = interactive_sdupscale_settings(config)
|
| 626 |
+
if not custom_upscale_settings:
|
| 627 |
+
return
|
| 628 |
+
source_path = custom_upscale_settings["source_file"]
|
| 629 |
+
else:
|
| 630 |
+
new_path = input(f"Image path ({source_path}): ")
|
| 631 |
+
if new_path != "":
|
| 632 |
+
source_path = new_path
|
| 633 |
+
if source_path == "":
|
| 634 |
+
print("Error : You need to provide a file in SD Upscale mode")
|
| 635 |
+
return
|
| 636 |
+
settings.strength = user_value(
|
| 637 |
+
float,
|
| 638 |
+
f"SD Upscale strength ({settings.strength}): ",
|
| 639 |
+
settings.strength,
|
| 640 |
+
)
|
| 641 |
+
settings.inference_steps = int(steps / settings.strength + 1)
|
| 642 |
+
|
| 643 |
+
output_path = FastStableDiffusionPaths.get_upscale_filepath(
|
| 644 |
+
source_path,
|
| 645 |
+
2,
|
| 646 |
+
config.generated_images.format,
|
| 647 |
+
)
|
| 648 |
+
generate_upscaled_image(
|
| 649 |
+
config,
|
| 650 |
+
source_path,
|
| 651 |
+
settings.strength,
|
| 652 |
+
upscale_settings=custom_upscale_settings,
|
| 653 |
+
context=context,
|
| 654 |
+
tile_overlap=32 if settings.use_openvino else 16,
|
| 655 |
+
output_path=output_path,
|
| 656 |
+
image_format=config.generated_images.format,
|
| 657 |
+
)
|
| 658 |
+
user_input = input("Continue in SD Upscale mode? (Y/n): ")
|
| 659 |
+
if user_input.upper() == "N":
|
| 660 |
+
settings.inference_steps = steps
|
| 661 |
+
return
|
frontend/utils.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import platform
|
| 2 |
+
from os import path
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
from backend.device import is_openvino_device
|
| 6 |
+
from paths import get_file_name
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def is_reshape_required(
|
| 10 |
+
prev_width: int,
|
| 11 |
+
cur_width: int,
|
| 12 |
+
prev_height: int,
|
| 13 |
+
cur_height: int,
|
| 14 |
+
prev_model: int,
|
| 15 |
+
cur_model: int,
|
| 16 |
+
prev_num_of_images: int,
|
| 17 |
+
cur_num_of_images: int,
|
| 18 |
+
) -> bool:
|
| 19 |
+
reshape_required = False
|
| 20 |
+
if (
|
| 21 |
+
prev_width != cur_width
|
| 22 |
+
or prev_height != cur_height
|
| 23 |
+
or prev_model != cur_model
|
| 24 |
+
or prev_num_of_images != cur_num_of_images
|
| 25 |
+
):
|
| 26 |
+
print("Reshape and compile")
|
| 27 |
+
reshape_required = True
|
| 28 |
+
|
| 29 |
+
return reshape_required
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def enable_openvino_controls() -> bool:
|
| 33 |
+
return (
|
| 34 |
+
is_openvino_device()
|
| 35 |
+
and platform.system().lower() != "darwin"
|
| 36 |
+
and platform.processor().lower() != "arm"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def get_valid_model_id(
|
| 41 |
+
models: List,
|
| 42 |
+
model_id: str,
|
| 43 |
+
default_model: str = "",
|
| 44 |
+
) -> str:
|
| 45 |
+
if len(models) == 0:
|
| 46 |
+
print(
|
| 47 |
+
"Warning: model configuration file/directory is empty,please add some models."
|
| 48 |
+
)
|
| 49 |
+
return ""
|
| 50 |
+
if model_id == "":
|
| 51 |
+
if default_model:
|
| 52 |
+
return default_model
|
| 53 |
+
else:
|
| 54 |
+
return models[0]
|
| 55 |
+
|
| 56 |
+
if model_id in models:
|
| 57 |
+
return model_id
|
| 58 |
+
else:
|
| 59 |
+
if model_id:
|
| 60 |
+
print(
|
| 61 |
+
f"Error:{model_id} Model not found in configuration file,so using first model : {models[0]}"
|
| 62 |
+
)
|
| 63 |
+
return models[0]
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def get_valid_lora_model(
|
| 67 |
+
models: List,
|
| 68 |
+
cur_model: str,
|
| 69 |
+
lora_models_dir: str,
|
| 70 |
+
) -> str:
|
| 71 |
+
if cur_model == "" or cur_model is None:
|
| 72 |
+
print(
|
| 73 |
+
f"No lora models found, please add lora models to {lora_models_dir} directory"
|
| 74 |
+
)
|
| 75 |
+
return ""
|
| 76 |
+
else:
|
| 77 |
+
if path.exists(cur_model):
|
| 78 |
+
return get_file_name(cur_model)
|
| 79 |
+
else:
|
| 80 |
+
print(f"Lora model {cur_model} not found")
|
| 81 |
+
if len(models) > 0:
|
| 82 |
+
print(f"Fallback model - {models[0]}")
|
| 83 |
+
return get_file_name(models[0])
|
| 84 |
+
else:
|
| 85 |
+
print(
|
| 86 |
+
f"No lora models found, please add lora models to {lora_models_dir} directory"
|
| 87 |
+
)
|
| 88 |
+
return ""
|