Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,597 +5,602 @@ import findfile
|
|
| 5 |
import PIL.Image
|
| 6 |
import autocuda
|
| 7 |
from pyabsa.utils.pyabsa_utils import fprint
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
import torch
|
| 28 |
-
from PIL import Image
|
| 29 |
-
import utils
|
| 30 |
-
import datetime
|
| 31 |
-
import time
|
| 32 |
-
import psutil
|
| 33 |
-
from Waifu2x.magnify import ImageMagnifier
|
| 34 |
-
from RealESRGANv030.interface import realEsrgan
|
| 35 |
-
|
| 36 |
-
magnifier = ImageMagnifier()
|
| 37 |
-
|
| 38 |
-
start_time = time.time()
|
| 39 |
-
is_colab = utils.is_google_colab()
|
| 40 |
-
|
| 41 |
-
CUDA_VISIBLE_DEVICES = ""
|
| 42 |
-
device = autocuda.auto_cuda()
|
| 43 |
-
|
| 44 |
-
dtype = torch.float16 if device != "cpu" else torch.float32
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
class Model:
|
| 49 |
-
def __init__(self, name, path="", prefix=""):
|
| 50 |
-
self.name = name
|
| 51 |
-
self.path = path
|
| 52 |
-
self.prefix = prefix
|
| 53 |
-
self.pipe_t2i = None
|
| 54 |
-
self.pipe_i2i = None
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
models = [
|
| 58 |
-
# Model("anything v3", "Linaqruf/anything-v3.0", "anything v3 style"),
|
| 59 |
-
Model("anything v5", "stablediffusionapi/anything-v5", "anything v5 style"),
|
| 60 |
-
]
|
| 61 |
-
# Model("Spider-Verse", "nitrosocke/spider-verse-diffusion", "spiderverse style "),
|
| 62 |
-
# Model("Balloon Art", "Fictiverse/Stable_Diffusion_BalloonArt_Model", "BalloonArt "),
|
| 63 |
-
# Model("Elden Ring", "nitrosocke/elden-ring-diffusion", "elden ring style "),
|
| 64 |
-
# Model("Tron Legacy", "dallinmackay/Tron-Legacy-diffusion", "trnlgcy ")
|
| 65 |
-
# Model("Pokémon", "lambdalabs/sd-pokemon-diffusers", ""),
|
| 66 |
-
# Model("Pony Diffusion", "AstraliteHeart/pony-diffusion", ""),
|
| 67 |
-
# Model("Robo Diffusion", "nousr/robo-diffusion", ""),
|
| 68 |
-
|
| 69 |
-
scheduler = DPMSolverMultistepScheduler(
|
| 70 |
-
beta_start=0.00085,
|
| 71 |
-
beta_end=0.012,
|
| 72 |
-
beta_schedule="scaled_linear",
|
| 73 |
-
num_train_timesteps=1000,
|
| 74 |
-
trained_betas=None,
|
| 75 |
-
predict_epsilon=True,
|
| 76 |
-
thresholding=False,
|
| 77 |
-
algorithm_type="dpmsolver++",
|
| 78 |
-
solver_type="midpoint",
|
| 79 |
-
solver_order=2,
|
| 80 |
-
# lower_order_final=True,
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
custom_model = None
|
| 84 |
-
if is_colab:
|
| 85 |
-
models.insert(0, Model("Custom model"))
|
| 86 |
-
custom_model = models[0]
|
| 87 |
-
|
| 88 |
-
last_mode = "txt2img"
|
| 89 |
-
current_model = models[1] if is_colab else models[0]
|
| 90 |
-
current_model_path = current_model.path
|
| 91 |
-
|
| 92 |
-
if is_colab:
|
| 93 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
| 94 |
-
current_model.path,
|
| 95 |
-
torch_dtype=dtype,
|
| 96 |
-
scheduler=scheduler,
|
| 97 |
-
safety_checker=lambda images, clip_input: (images, False),
|
| 98 |
)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
)
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
try:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
except Exception as e:
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
# model.pipe_t2i = torch.compile(model.pipe_t2i)
|
| 139 |
-
if torch.cuda.is_available():
|
| 140 |
-
pipe = pipe.to(device)
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
def error_str(error, title="Error"):
|
| 147 |
-
return (
|
| 148 |
-
f"""#### {title}
|
| 149 |
-
{error}"""
|
| 150 |
-
if error
|
| 151 |
-
else ""
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
def custom_model_changed(path):
|
| 156 |
-
models[0].path = path
|
| 157 |
-
global current_model
|
| 158 |
-
current_model = models[0]
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
def on_model_change(model_name):
|
| 162 |
-
prefix = (
|
| 163 |
-
'Enter prompt. "'
|
| 164 |
-
+ next((m.prefix for m in models if m.name == model_name), None)
|
| 165 |
-
+ '" is prefixed automatically'
|
| 166 |
-
if model_name != models[0].name
|
| 167 |
-
else "Don't forget to use the custom model prefix in the prompt!"
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
return (
|
| 171 |
-
gr.update(visible=model_name == models[0].name),
|
| 172 |
-
gr.update(placeholder=prefix),
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
def inference(
|
| 177 |
-
model_name,
|
| 178 |
-
prompt,
|
| 179 |
-
guidance,
|
| 180 |
-
steps,
|
| 181 |
-
width=512,
|
| 182 |
-
height=512,
|
| 183 |
-
seed=0,
|
| 184 |
-
img=None,
|
| 185 |
-
strength=0.5,
|
| 186 |
-
neg_prompt="",
|
| 187 |
-
scale="ESRGAN4x",
|
| 188 |
-
scale_factor=2,
|
| 189 |
-
):
|
| 190 |
-
fprint(psutil.virtual_memory()) # print memory usage
|
| 191 |
-
|
| 192 |
-
fprint(f"Prompt: {prompt}")
|
| 193 |
-
global current_model
|
| 194 |
-
for model in models:
|
| 195 |
-
if model.name == model_name:
|
| 196 |
-
current_model = model
|
| 197 |
-
model_path = current_model.path
|
| 198 |
-
|
| 199 |
-
generator = torch.Generator(device).manual_seed(seed) if seed != 0 else None
|
| 200 |
-
|
| 201 |
-
try:
|
| 202 |
-
if img is not None:
|
| 203 |
-
return (
|
| 204 |
-
img_to_img(
|
| 205 |
-
model_path,
|
| 206 |
-
prompt,
|
| 207 |
-
neg_prompt,
|
| 208 |
-
img,
|
| 209 |
-
strength,
|
| 210 |
-
guidance,
|
| 211 |
-
steps,
|
| 212 |
-
width,
|
| 213 |
-
height,
|
| 214 |
-
generator,
|
| 215 |
-
scale,
|
| 216 |
-
scale_factor,
|
| 217 |
-
),
|
| 218 |
-
None,
|
| 219 |
-
)
|
| 220 |
-
else:
|
| 221 |
-
return (
|
| 222 |
-
txt_to_img(
|
| 223 |
-
model_path,
|
| 224 |
-
prompt,
|
| 225 |
-
neg_prompt,
|
| 226 |
-
guidance,
|
| 227 |
-
steps,
|
| 228 |
-
width,
|
| 229 |
-
height,
|
| 230 |
-
generator,
|
| 231 |
-
scale,
|
| 232 |
-
scale_factor,
|
| 233 |
-
),
|
| 234 |
-
None,
|
| 235 |
-
)
|
| 236 |
-
except Exception as e:
|
| 237 |
-
return None, error_str(e)
|
| 238 |
-
# if img is not None:
|
| 239 |
-
# return img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height,
|
| 240 |
-
# generator, scale, scale_factor), None
|
| 241 |
-
# else:
|
| 242 |
-
# return txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator, scale, scale_factor), None
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
def txt_to_img(
|
| 246 |
-
model_path,
|
| 247 |
-
prompt,
|
| 248 |
-
neg_prompt,
|
| 249 |
-
guidance,
|
| 250 |
-
steps,
|
| 251 |
-
width,
|
| 252 |
-
height,
|
| 253 |
-
generator,
|
| 254 |
-
scale,
|
| 255 |
-
scale_factor,
|
| 256 |
-
):
|
| 257 |
-
print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")
|
| 258 |
-
|
| 259 |
-
global last_mode
|
| 260 |
-
global pipe
|
| 261 |
-
global current_model_path
|
| 262 |
-
if model_path != current_model_path or last_mode != "txt2img":
|
| 263 |
-
current_model_path = model_path
|
| 264 |
-
|
| 265 |
-
if is_colab or current_model == custom_model:
|
| 266 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
| 267 |
-
current_model_path,
|
| 268 |
-
torch_dtype=dtype,
|
| 269 |
-
scheduler=scheduler,
|
| 270 |
-
safety_checker=lambda images, clip_input: (images, False),
|
| 271 |
-
)
|
| 272 |
-
else:
|
| 273 |
-
# pipe = pipe.to("cpu")
|
| 274 |
-
pipe = current_model.pipe_t2i
|
| 275 |
-
|
| 276 |
-
if torch.cuda.is_available():
|
| 277 |
-
pipe = pipe.to(device)
|
| 278 |
-
last_mode = "txt2img"
|
| 279 |
-
|
| 280 |
-
prompt = current_model.prefix + prompt
|
| 281 |
-
result = pipe(
|
| 282 |
prompt,
|
| 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 |
-
prompt = current_model.prefix + prompt
|
| 353 |
-
ratio = min(height / img.height, width / img.width)
|
| 354 |
-
img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
|
| 355 |
-
result = pipe(
|
| 356 |
prompt,
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
result.images[0].save(
|
| 383 |
-
"imgs/result-{}.png".format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
|
| 384 |
-
)
|
| 385 |
-
return replace_nsfw_images(result)
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
def replace_nsfw_images(results):
|
| 389 |
-
if is_colab:
|
| 390 |
-
return results.images[0]
|
| 391 |
-
if hasattr(results, "nsfw_content_detected") and results.nsfw_content_detected:
|
| 392 |
-
for i in range(len(results.images)):
|
| 393 |
-
if results.nsfw_content_detected[i]:
|
| 394 |
-
results.images[i] = Image.open("nsfw.png")
|
| 395 |
-
return results.images[0]
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
css = """.finetuned-diffusion-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.finetuned-diffusion-div div h1{font-weight:900;margin-bottom:7px}.finetuned-diffusion-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
|
| 399 |
-
"""
|
| 400 |
-
with gr.Blocks(css=css) as demo:
|
| 401 |
-
if not os.path.exists("imgs"):
|
| 402 |
-
os.mkdir("imgs")
|
| 403 |
-
|
| 404 |
-
gr.Markdown("# Super Resolution Anime Diffusion")
|
| 405 |
-
gr.Markdown(
|
| 406 |
-
"## Author: [yangheng95](https://github.com/yangheng95) Github:[Github](https://github.com/yangheng95/stable-diffusion-webui)"
|
| 407 |
-
)
|
| 408 |
-
gr.Markdown(
|
| 409 |
-
"### This demo is running on a CPU, so it will take at least 20 minutes. "
|
| 410 |
-
"If you have a GPU, you can clone from [Github](https://github.com/yangheng95/SuperResolutionAnimeDiffusion) and run it locally."
|
| 411 |
-
)
|
| 412 |
-
gr.Markdown(
|
| 413 |
-
"### FYI: to generate a 512*512 image and magnify 4x, it only takes 5~8 seconds on a RTX 2080 GPU"
|
| 414 |
-
)
|
| 415 |
-
gr.Markdown(
|
| 416 |
-
"### You can duplicate this demo on HuggingFace Spaces, click [here](https://huggingface.co/spaces/yangheng/Super-Resolution-Anime-Diffusion?duplicate=true)"
|
| 417 |
-
)
|
| 418 |
-
|
| 419 |
-
with gr.Row():
|
| 420 |
-
with gr.Column(scale=55):
|
| 421 |
-
with gr.Group():
|
| 422 |
-
gr.Markdown("Text to image")
|
| 423 |
-
|
| 424 |
-
model_name = gr.Dropdown(
|
| 425 |
-
label="Model",
|
| 426 |
-
choices=[m.name for m in models],
|
| 427 |
-
value=current_model.name,
|
| 428 |
)
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
)
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
label="Negative prompt",
|
| 454 |
-
value="bad result, worst, random, invalid, inaccurate, imperfect, blurry, deformed,"
|
| 455 |
-
" disfigured, mutation, mutated, ugly, out of focus, bad anatomy, text, error,"
|
| 456 |
-
" extra digit, fewer digits, worst quality, low quality, normal quality, noise, "
|
| 457 |
-
"jpeg artifact, compression artifact, signature, watermark, username, logo, "
|
| 458 |
-
"low resolution, worst resolution, bad resolution, normal resolution, bad detail,"
|
| 459 |
-
" bad details, bad lighting, bad shadow, bad shading, bad background,"
|
| 460 |
-
" worst background.",
|
| 461 |
)
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
error_output = gr.Markdown()
|
| 465 |
-
|
| 466 |
-
with gr.Row():
|
| 467 |
-
gr.Markdown(
|
| 468 |
-
"# Random Image Generation Preview (512*768)x4 magnified"
|
| 469 |
-
)
|
| 470 |
-
for f_img in findfile.find_cwd_files(".png", recursive=2):
|
| 471 |
-
with gr.Row():
|
| 472 |
-
image = gr.Image(height=512, value=PIL.Image.open(f_img))
|
| 473 |
-
# gallery = gr.Gallery(
|
| 474 |
-
# label="Generated images", show_label=False, elem_id="gallery"
|
| 475 |
-
# ).style(grid=[1], height="auto")
|
| 476 |
-
|
| 477 |
-
with gr.Column(scale=45):
|
| 478 |
-
with gr.Group():
|
| 479 |
-
gr.Markdown("Image to Image")
|
| 480 |
-
|
| 481 |
-
with gr.Row():
|
| 482 |
-
with gr.Group():
|
| 483 |
-
image = gr.Image(
|
| 484 |
-
label="Image", height=256, type="pil"
|
| 485 |
)
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
|
|
|
| 492 |
)
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
|
|
|
|
|
|
|
|
|
| 504 |
)
|
| 505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
with gr.Row():
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
)
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
|
|
|
| 526 |
)
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
)
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
)
|
| 547 |
-
|
| 548 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
)
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
prompt,
|
| 559 |
-
guidance,
|
| 560 |
-
steps,
|
| 561 |
-
width,
|
| 562 |
-
height,
|
| 563 |
-
seed,
|
| 564 |
-
image,
|
| 565 |
-
strength,
|
| 566 |
-
neg_prompt,
|
| 567 |
-
scale,
|
| 568 |
-
scale_factor,
|
| 569 |
-
]
|
| 570 |
-
outputs = [image_out, error_output]
|
| 571 |
-
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
| 572 |
-
generate.click(inference, inputs=inputs, outputs=outputs, api_name="generate")
|
| 573 |
-
|
| 574 |
-
prompt_keys = [
|
| 575 |
-
"girl",
|
| 576 |
-
"lovely",
|
| 577 |
-
"cute",
|
| 578 |
-
"beautiful eyes",
|
| 579 |
-
"cumulonimbus clouds",
|
| 580 |
-
random.choice(["dress"]),
|
| 581 |
-
random.choice(["white hair"]),
|
| 582 |
-
random.choice(["blue eyes"]),
|
| 583 |
-
random.choice(["flower meadow"]),
|
| 584 |
-
random.choice(["Elif", "Angel"]),
|
| 585 |
-
]
|
| 586 |
-
prompt.value = ",".join(prompt_keys)
|
| 587 |
-
ex = gr.Examples(
|
| 588 |
-
[
|
| 589 |
-
[models[0].name, prompt.value, 7.5, 15],
|
| 590 |
-
],
|
| 591 |
-
inputs=[model_name, prompt, guidance, steps, seed],
|
| 592 |
-
outputs=outputs,
|
| 593 |
-
fn=inference,
|
| 594 |
-
cache_examples=False,
|
| 595 |
-
)
|
| 596 |
-
|
| 597 |
-
print(f"Space built in {time.time() - start_time:.2f} seconds")
|
| 598 |
-
|
| 599 |
-
if not is_colab:
|
| 600 |
-
demo.queue()
|
| 601 |
-
demo.launch(debug=is_colab, share=is_colab)
|
|
|
|
| 5 |
import PIL.Image
|
| 6 |
import autocuda
|
| 7 |
from pyabsa.utils.pyabsa_utils import fprint
|
| 8 |
+
import spaces
|
| 9 |
|
| 10 |
+
@spaces.GPU
|
| 11 |
+
def __main:
|
| 12 |
+
try:
|
| 13 |
+
for z_file in findfile.find_cwd_files(and_key=['.zip'],
|
| 14 |
+
exclude_key=['.ignore', 'git', 'SuperResolutionAnimeDiffusion'],
|
| 15 |
+
recursive=10):
|
| 16 |
+
fprint(f"Extracting {z_file}...")
|
| 17 |
+
with zipfile.ZipFile(z_file, 'r') as zip_ref:
|
| 18 |
+
zip_ref.extractall(os.path.dirname(z_file))
|
| 19 |
+
except Exception as e:
|
| 20 |
+
os.system('unzip random_examples.zip')
|
| 21 |
+
|
| 22 |
+
from diffusers import (
|
| 23 |
+
AutoencoderKL,
|
| 24 |
+
UNet2DConditionModel,
|
| 25 |
+
StableDiffusionPipeline,
|
| 26 |
+
StableDiffusionImg2ImgPipeline,
|
| 27 |
+
DPMSolverMultistepScheduler,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
+
import gradio as gr
|
| 30 |
+
import torch
|
| 31 |
+
from PIL import Image
|
| 32 |
+
import utils
|
| 33 |
+
import datetime
|
| 34 |
+
import time
|
| 35 |
+
import psutil
|
| 36 |
+
from Waifu2x.magnify import ImageMagnifier
|
| 37 |
+
from RealESRGANv030.interface import realEsrgan
|
| 38 |
+
|
| 39 |
+
magnifier = ImageMagnifier()
|
| 40 |
+
|
| 41 |
+
start_time = time.time()
|
| 42 |
+
is_colab = utils.is_google_colab()
|
| 43 |
+
|
| 44 |
+
CUDA_VISIBLE_DEVICES = ""
|
| 45 |
+
device = autocuda.auto_cuda()
|
| 46 |
+
|
| 47 |
+
dtype = torch.float16 if device != "cpu" else torch.float32
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class Model:
|
| 52 |
+
def __init__(self, name, path="", prefix=""):
|
| 53 |
+
self.name = name
|
| 54 |
+
self.path = path
|
| 55 |
+
self.prefix = prefix
|
| 56 |
+
self.pipe_t2i = None
|
| 57 |
+
self.pipe_i2i = None
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
models = [
|
| 61 |
+
# Model("anything v3", "Linaqruf/anything-v3.0", "anything v3 style"),
|
| 62 |
+
Model("anything v5", "stablediffusionapi/anything-v5", "anything v5 style"),
|
| 63 |
+
]
|
| 64 |
+
# Model("Spider-Verse", "nitrosocke/spider-verse-diffusion", "spiderverse style "),
|
| 65 |
+
# Model("Balloon Art", "Fictiverse/Stable_Diffusion_BalloonArt_Model", "BalloonArt "),
|
| 66 |
+
# Model("Elden Ring", "nitrosocke/elden-ring-diffusion", "elden ring style "),
|
| 67 |
+
# Model("Tron Legacy", "dallinmackay/Tron-Legacy-diffusion", "trnlgcy ")
|
| 68 |
+
# Model("Pokémon", "lambdalabs/sd-pokemon-diffusers", ""),
|
| 69 |
+
# Model("Pony Diffusion", "AstraliteHeart/pony-diffusion", ""),
|
| 70 |
+
# Model("Robo Diffusion", "nousr/robo-diffusion", ""),
|
| 71 |
+
|
| 72 |
+
scheduler = DPMSolverMultistepScheduler(
|
| 73 |
+
beta_start=0.00085,
|
| 74 |
+
beta_end=0.012,
|
| 75 |
+
beta_schedule="scaled_linear",
|
| 76 |
+
num_train_timesteps=1000,
|
| 77 |
+
trained_betas=None,
|
| 78 |
+
predict_epsilon=True,
|
| 79 |
+
thresholding=False,
|
| 80 |
+
algorithm_type="dpmsolver++",
|
| 81 |
+
solver_type="midpoint",
|
| 82 |
+
solver_order=2,
|
| 83 |
+
# lower_order_final=True,
|
| 84 |
)
|
| 85 |
+
|
| 86 |
+
custom_model = None
|
| 87 |
+
if is_colab:
|
| 88 |
+
models.insert(0, Model("Custom model"))
|
| 89 |
+
custom_model = models[0]
|
| 90 |
+
|
| 91 |
+
last_mode = "txt2img"
|
| 92 |
+
current_model = models[1] if is_colab else models[0]
|
| 93 |
+
current_model_path = current_model.path
|
| 94 |
+
|
| 95 |
+
if is_colab:
|
| 96 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 97 |
+
current_model.path,
|
| 98 |
+
torch_dtype=dtype,
|
| 99 |
+
scheduler=scheduler,
|
| 100 |
+
safety_checker=lambda images, clip_input: (images, False),
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
else: # download all models
|
| 104 |
+
print(f"{datetime.datetime.now()} Downloading vae...")
|
| 105 |
+
vae = AutoencoderKL.from_pretrained(
|
| 106 |
+
current_model.path, subfolder="vae", torch_dtype=dtype
|
| 107 |
+
)
|
| 108 |
+
for model in models:
|
| 109 |
+
try:
|
| 110 |
+
print(f"{datetime.datetime.now()} Downloading {model.name} model...")
|
| 111 |
+
unet = UNet2DConditionModel.from_pretrained(
|
| 112 |
+
model.path, subfolder="unet", torch_dtype=dtype
|
| 113 |
+
)
|
| 114 |
+
model.pipe_t2i = StableDiffusionPipeline.from_pretrained(
|
| 115 |
+
model.path,
|
| 116 |
+
unet=unet,
|
| 117 |
+
vae=vae,
|
| 118 |
+
torch_dtype=dtype,
|
| 119 |
+
scheduler=scheduler,
|
| 120 |
+
# safety_checker=None,
|
| 121 |
+
)
|
| 122 |
+
model.pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 123 |
+
model.path,
|
| 124 |
+
unet=unet,
|
| 125 |
+
vae=vae,
|
| 126 |
+
torch_dtype=dtype,
|
| 127 |
+
scheduler=scheduler,
|
| 128 |
+
# safety_checker=None,
|
| 129 |
+
)
|
| 130 |
+
except Exception as e:
|
| 131 |
+
print(
|
| 132 |
+
f"{datetime.datetime.now()} Failed to load model "
|
| 133 |
+
+ model.name
|
| 134 |
+
+ ": "
|
| 135 |
+
+ str(e)
|
| 136 |
+
)
|
| 137 |
+
models.remove(model)
|
| 138 |
+
pipe = models[0].pipe_t2i
|
| 139 |
+
|
| 140 |
+
# model.pipe_i2i = torch.compile(model.pipe_i2i)
|
| 141 |
+
# model.pipe_t2i = torch.compile(model.pipe_t2i)
|
| 142 |
+
if torch.cuda.is_available():
|
| 143 |
+
pipe = pipe.to(device)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
# device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def error_str(error, title="Error"):
|
| 150 |
+
return (
|
| 151 |
+
f"""#### {title}
|
| 152 |
+
{error}"""
|
| 153 |
+
if error
|
| 154 |
+
else ""
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def custom_model_changed(path):
|
| 159 |
+
models[0].path = path
|
| 160 |
+
global current_model
|
| 161 |
+
current_model = models[0]
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def on_model_change(model_name):
|
| 165 |
+
prefix = (
|
| 166 |
+
'Enter prompt. "'
|
| 167 |
+
+ next((m.prefix for m in models if m.name == model_name), None)
|
| 168 |
+
+ '" is prefixed automatically'
|
| 169 |
+
if model_name != models[0].name
|
| 170 |
+
else "Don't forget to use the custom model prefix in the prompt!"
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
return (
|
| 174 |
+
gr.update(visible=model_name == models[0].name),
|
| 175 |
+
gr.update(placeholder=prefix),
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def inference(
|
| 180 |
+
model_name,
|
| 181 |
+
prompt,
|
| 182 |
+
guidance,
|
| 183 |
+
steps,
|
| 184 |
+
width=512,
|
| 185 |
+
height=512,
|
| 186 |
+
seed=0,
|
| 187 |
+
img=None,
|
| 188 |
+
strength=0.5,
|
| 189 |
+
neg_prompt="",
|
| 190 |
+
scale="ESRGAN4x",
|
| 191 |
+
scale_factor=2,
|
| 192 |
+
):
|
| 193 |
+
fprint(psutil.virtual_memory()) # print memory usage
|
| 194 |
+
|
| 195 |
+
fprint(f"Prompt: {prompt}")
|
| 196 |
+
global current_model
|
| 197 |
+
for model in models:
|
| 198 |
+
if model.name == model_name:
|
| 199 |
+
current_model = model
|
| 200 |
+
model_path = current_model.path
|
| 201 |
+
|
| 202 |
+
generator = torch.Generator(device).manual_seed(seed) if seed != 0 else None
|
| 203 |
+
|
| 204 |
try:
|
| 205 |
+
if img is not None:
|
| 206 |
+
return (
|
| 207 |
+
img_to_img(
|
| 208 |
+
model_path,
|
| 209 |
+
prompt,
|
| 210 |
+
neg_prompt,
|
| 211 |
+
img,
|
| 212 |
+
strength,
|
| 213 |
+
guidance,
|
| 214 |
+
steps,
|
| 215 |
+
width,
|
| 216 |
+
height,
|
| 217 |
+
generator,
|
| 218 |
+
scale,
|
| 219 |
+
scale_factor,
|
| 220 |
+
),
|
| 221 |
+
None,
|
| 222 |
+
)
|
| 223 |
+
else:
|
| 224 |
+
return (
|
| 225 |
+
txt_to_img(
|
| 226 |
+
model_path,
|
| 227 |
+
prompt,
|
| 228 |
+
neg_prompt,
|
| 229 |
+
guidance,
|
| 230 |
+
steps,
|
| 231 |
+
width,
|
| 232 |
+
height,
|
| 233 |
+
generator,
|
| 234 |
+
scale,
|
| 235 |
+
scale_factor,
|
| 236 |
+
),
|
| 237 |
+
None,
|
| 238 |
+
)
|
| 239 |
except Exception as e:
|
| 240 |
+
return None, error_str(e)
|
| 241 |
+
# if img is not None:
|
| 242 |
+
# return img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height,
|
| 243 |
+
# generator, scale, scale_factor), None
|
| 244 |
+
# else:
|
| 245 |
+
# return txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator, scale, scale_factor), None
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def txt_to_img(
|
| 249 |
+
model_path,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
prompt,
|
| 251 |
+
neg_prompt,
|
| 252 |
+
guidance,
|
| 253 |
+
steps,
|
| 254 |
+
width,
|
| 255 |
+
height,
|
| 256 |
+
generator,
|
| 257 |
+
scale,
|
| 258 |
+
scale_factor,
|
| 259 |
+
):
|
| 260 |
+
print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")
|
| 261 |
+
|
| 262 |
+
global last_mode
|
| 263 |
+
global pipe
|
| 264 |
+
global current_model_path
|
| 265 |
+
if model_path != current_model_path or last_mode != "txt2img":
|
| 266 |
+
current_model_path = model_path
|
| 267 |
+
|
| 268 |
+
if is_colab or current_model == custom_model:
|
| 269 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 270 |
+
current_model_path,
|
| 271 |
+
torch_dtype=dtype,
|
| 272 |
+
scheduler=scheduler,
|
| 273 |
+
safety_checker=lambda images, clip_input: (images, False),
|
| 274 |
+
)
|
| 275 |
+
else:
|
| 276 |
+
# pipe = pipe.to("cpu")
|
| 277 |
+
pipe = current_model.pipe_t2i
|
| 278 |
+
|
| 279 |
+
if torch.cuda.is_available():
|
| 280 |
+
pipe = pipe.to(device)
|
| 281 |
+
last_mode = "txt2img"
|
| 282 |
+
|
| 283 |
+
prompt = current_model.prefix + prompt
|
| 284 |
+
result = pipe(
|
| 285 |
+
prompt,
|
| 286 |
+
negative_prompt=neg_prompt,
|
| 287 |
+
# num_images_per_prompt=n_images,
|
| 288 |
+
num_inference_steps=int(steps),
|
| 289 |
+
guidance_scale=guidance,
|
| 290 |
+
width=width,
|
| 291 |
+
height=height,
|
| 292 |
+
generator=generator,
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
# result.images[0] = magnifier.magnify(result.images[0], scale_factor=scale_factor)
|
| 296 |
+
# enhance resolution
|
| 297 |
+
if scale_factor > 1:
|
| 298 |
+
if scale == "ESRGAN4x":
|
| 299 |
+
fp32 = True if device == "cpu" else False
|
| 300 |
+
result.images[0] = realEsrgan(
|
| 301 |
+
input_dir=result.images[0],
|
| 302 |
+
suffix="",
|
| 303 |
+
output_dir="imgs",
|
| 304 |
+
fp32=fp32,
|
| 305 |
+
outscale=scale_factor,
|
| 306 |
+
)[0]
|
| 307 |
+
else:
|
| 308 |
+
result.images[0] = magnifier.magnify(
|
| 309 |
+
result.images[0], scale_factor=scale_factor
|
| 310 |
+
)
|
| 311 |
+
# save image
|
| 312 |
+
result.images[0].save(
|
| 313 |
+
"imgs/result-{}.png".format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
|
| 314 |
+
)
|
| 315 |
+
return replace_nsfw_images(result)
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
def img_to_img(
|
| 319 |
+
model_path,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
prompt,
|
| 321 |
+
neg_prompt,
|
| 322 |
+
img,
|
| 323 |
+
strength,
|
| 324 |
+
guidance,
|
| 325 |
+
steps,
|
| 326 |
+
width,
|
| 327 |
+
height,
|
| 328 |
+
generator,
|
| 329 |
+
scale,
|
| 330 |
+
scale_factor,
|
| 331 |
+
):
|
| 332 |
+
fprint(f"{datetime.datetime.now()} img_to_img, model: {model_path}")
|
| 333 |
+
|
| 334 |
+
global last_mode
|
| 335 |
+
global pipe
|
| 336 |
+
global current_model_path
|
| 337 |
+
if model_path != current_model_path or last_mode != "img2img":
|
| 338 |
+
current_model_path = model_path
|
| 339 |
+
|
| 340 |
+
if is_colab or current_model == custom_model:
|
| 341 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 342 |
+
current_model_path,
|
| 343 |
+
torch_dtype=dtype,
|
| 344 |
+
scheduler=scheduler,
|
| 345 |
+
safety_checker=lambda images, clip_input: (images, False),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
)
|
| 347 |
+
else:
|
| 348 |
+
# pipe = pipe.to("cpu")
|
| 349 |
+
pipe = current_model.pipe_i2i
|
| 350 |
+
|
| 351 |
+
if torch.cuda.is_available():
|
| 352 |
+
pipe = pipe.to(device)
|
| 353 |
+
last_mode = "img2img"
|
| 354 |
+
|
| 355 |
+
prompt = current_model.prefix + prompt
|
| 356 |
+
ratio = min(height / img.height, width / img.width)
|
| 357 |
+
img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
|
| 358 |
+
result = pipe(
|
| 359 |
+
prompt,
|
| 360 |
+
negative_prompt=neg_prompt,
|
| 361 |
+
# num_images_per_prompt=n_images,
|
| 362 |
+
image=img,
|
| 363 |
+
num_inference_steps=int(steps),
|
| 364 |
+
strength=strength,
|
| 365 |
+
guidance_scale=guidance,
|
| 366 |
+
# width=width,
|
| 367 |
+
# height=height,
|
| 368 |
+
generator=generator,
|
| 369 |
+
)
|
| 370 |
+
if scale_factor > 1:
|
| 371 |
+
if scale == "ESRGAN4x":
|
| 372 |
+
fp32 = True if device == "cpu" else False
|
| 373 |
+
result.images[0] = realEsrgan(
|
| 374 |
+
input_dir=result.images[0],
|
| 375 |
+
suffix="",
|
| 376 |
+
output_dir="imgs",
|
| 377 |
+
fp32=fp32,
|
| 378 |
+
outscale=scale_factor,
|
| 379 |
+
)[0]
|
| 380 |
+
else:
|
| 381 |
+
result.images[0] = magnifier.magnify(
|
| 382 |
+
result.images[0], scale_factor=scale_factor
|
| 383 |
+
)
|
| 384 |
+
# save image
|
| 385 |
+
result.images[0].save(
|
| 386 |
+
"imgs/result-{}.png".format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
|
| 387 |
+
)
|
| 388 |
+
return replace_nsfw_images(result)
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
def replace_nsfw_images(results):
|
| 392 |
+
if is_colab:
|
| 393 |
+
return results.images[0]
|
| 394 |
+
if hasattr(results, "nsfw_content_detected") and results.nsfw_content_detected:
|
| 395 |
+
for i in range(len(results.images)):
|
| 396 |
+
if results.nsfw_content_detected[i]:
|
| 397 |
+
results.images[i] = Image.open("nsfw.png")
|
| 398 |
+
return results.images[0]
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
css = """.finetuned-diffusion-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.finetuned-diffusion-div div h1{font-weight:900;margin-bottom:7px}.finetuned-diffusion-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
|
| 402 |
+
"""
|
| 403 |
+
with gr.Blocks(css=css) as demo:
|
| 404 |
+
if not os.path.exists("imgs"):
|
| 405 |
+
os.mkdir("imgs")
|
| 406 |
+
|
| 407 |
+
gr.Markdown("# Super Resolution Anime Diffusion")
|
| 408 |
+
gr.Markdown(
|
| 409 |
+
"## Author: [yangheng95](https://github.com/yangheng95) Github:[Github](https://github.com/yangheng95/stable-diffusion-webui)"
|
| 410 |
+
)
|
| 411 |
+
gr.Markdown(
|
| 412 |
+
"### This demo is running on a CPU, so it will take at least 20 minutes. "
|
| 413 |
+
"If you have a GPU, you can clone from [Github](https://github.com/yangheng95/SuperResolutionAnimeDiffusion) and run it locally."
|
| 414 |
+
)
|
| 415 |
+
gr.Markdown(
|
| 416 |
+
"### FYI: to generate a 512*512 image and magnify 4x, it only takes 5~8 seconds on a RTX 2080 GPU"
|
| 417 |
+
)
|
| 418 |
+
gr.Markdown(
|
| 419 |
+
"### You can duplicate this demo on HuggingFace Spaces, click [here](https://huggingface.co/spaces/yangheng/Super-Resolution-Anime-Diffusion?duplicate=true)"
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
with gr.Row():
|
| 423 |
+
with gr.Column(scale=55):
|
| 424 |
+
with gr.Group():
|
| 425 |
+
gr.Markdown("Text to image")
|
| 426 |
+
|
| 427 |
+
model_name = gr.Dropdown(
|
| 428 |
+
label="Model",
|
| 429 |
+
choices=[m.name for m in models],
|
| 430 |
+
value=current_model.name,
|
| 431 |
)
|
| 432 |
+
|
| 433 |
+
with gr.Row(visible=False) as custom_model_group:
|
| 434 |
+
custom_model_path = gr.Textbox(
|
| 435 |
+
label="Custom model path",
|
| 436 |
+
placeholder="Path to model, e.g. nitrosocke/Arcane-Diffusion",
|
| 437 |
+
interactive=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
)
|
| 439 |
+
gr.HTML(
|
| 440 |
+
"<div><font size='2'>Custom models have to be downloaded first, so give it some time.</font></div>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
)
|
| 442 |
+
|
| 443 |
+
with gr.Row():
|
| 444 |
+
prompt = gr.Textbox(
|
| 445 |
+
label="Prompt",
|
| 446 |
+
show_label=False,
|
| 447 |
+
max_lines=2,
|
| 448 |
+
placeholder="Enter prompt. Style applied automatically",
|
| 449 |
)
|
| 450 |
+
with gr.Row():
|
| 451 |
+
generate = gr.Button(value="Generate")
|
| 452 |
+
|
| 453 |
+
with gr.Row():
|
| 454 |
+
with gr.Group():
|
| 455 |
+
neg_prompt = gr.Textbox(
|
| 456 |
+
label="Negative prompt",
|
| 457 |
+
value="bad result, worst, random, invalid, inaccurate, imperfect, blurry, deformed,"
|
| 458 |
+
" disfigured, mutation, mutated, ugly, out of focus, bad anatomy, text, error,"
|
| 459 |
+
" extra digit, fewer digits, worst quality, low quality, normal quality, noise, "
|
| 460 |
+
"jpeg artifact, compression artifact, signature, watermark, username, logo, "
|
| 461 |
+
"low resolution, worst resolution, bad resolution, normal resolution, bad detail,"
|
| 462 |
+
" bad details, bad lighting, bad shadow, bad shading, bad background,"
|
| 463 |
+
" worst background.",
|
| 464 |
)
|
| 465 |
+
|
| 466 |
+
image_out = gr.Image(height="auto", width="auto")
|
| 467 |
+
error_output = gr.Markdown()
|
| 468 |
+
|
| 469 |
+
with gr.Row():
|
| 470 |
+
gr.Markdown(
|
| 471 |
+
"# Random Image Generation Preview (512*768)x4 magnified"
|
| 472 |
+
)
|
| 473 |
+
for f_img in findfile.find_cwd_files(".png", recursive=2):
|
| 474 |
with gr.Row():
|
| 475 |
+
image = gr.Image(height=512, value=PIL.Image.open(f_img))
|
| 476 |
+
# gallery = gr.Gallery(
|
| 477 |
+
# label="Generated images", show_label=False, elem_id="gallery"
|
| 478 |
+
# ).style(grid=[1], height="auto")
|
| 479 |
+
|
| 480 |
+
with gr.Column(scale=45):
|
| 481 |
+
with gr.Group():
|
| 482 |
+
gr.Markdown("Image to Image")
|
| 483 |
+
|
| 484 |
+
with gr.Row():
|
| 485 |
+
with gr.Group():
|
| 486 |
+
image = gr.Image(
|
| 487 |
+
label="Image", height=256, type="pil"
|
| 488 |
)
|
| 489 |
+
strength = gr.Slider(
|
| 490 |
+
label="Transformation strength",
|
| 491 |
+
minimum=0,
|
| 492 |
+
maximum=1,
|
| 493 |
+
step=0.01,
|
| 494 |
+
value=0.5,
|
| 495 |
)
|
| 496 |
+
|
| 497 |
+
with gr.Row():
|
| 498 |
+
with gr.Group():
|
| 499 |
+
# n_images = gr.Slider(label="Images", value=1, minimum=1, maximum=4, step=1)
|
| 500 |
+
|
| 501 |
+
with gr.Row():
|
| 502 |
+
guidance = gr.Slider(
|
| 503 |
+
label="Guidance scale", value=7.5, maximum=15
|
| 504 |
+
)
|
| 505 |
+
steps = gr.Slider(
|
| 506 |
+
label="Steps", value=15, minimum=2, maximum=75, step=1
|
| 507 |
+
)
|
| 508 |
+
|
| 509 |
+
with gr.Row():
|
| 510 |
+
width = gr.Slider(
|
| 511 |
+
label="Width",
|
| 512 |
+
value=512,
|
| 513 |
+
minimum=64,
|
| 514 |
+
maximum=1024,
|
| 515 |
+
step=8,
|
| 516 |
+
)
|
| 517 |
+
height = gr.Slider(
|
| 518 |
+
label="Height",
|
| 519 |
+
value=768,
|
| 520 |
+
minimum=64,
|
| 521 |
+
maximum=1024,
|
| 522 |
+
step=8,
|
| 523 |
+
)
|
| 524 |
+
with gr.Row():
|
| 525 |
+
scale = gr.Radio(
|
| 526 |
+
label="Scale",
|
| 527 |
+
choices=["Waifu2x", "ESRGAN4x"],
|
| 528 |
+
value="Waifu2x",
|
| 529 |
+
)
|
| 530 |
+
with gr.Row():
|
| 531 |
+
scale_factor = gr.Slider(
|
| 532 |
+
1,
|
| 533 |
+
8,
|
| 534 |
+
label="Scale factor (to magnify image) (1, 2, 4, 8)",
|
| 535 |
+
value=1,
|
| 536 |
+
step=1,
|
| 537 |
+
)
|
| 538 |
+
|
| 539 |
+
seed = gr.Slider(
|
| 540 |
+
0, 2147483647, label="Seed (0 = random)", value=0, step=1
|
| 541 |
)
|
| 542 |
+
|
| 543 |
+
if is_colab:
|
| 544 |
+
model_name.change(
|
| 545 |
+
on_model_change,
|
| 546 |
+
inputs=model_name,
|
| 547 |
+
outputs=[custom_model_group, prompt],
|
| 548 |
+
queue=False,
|
| 549 |
+
)
|
| 550 |
+
custom_model_path.change(
|
| 551 |
+
custom_model_changed, inputs=custom_model_path, outputs=None
|
| 552 |
+
)
|
| 553 |
+
# n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)
|
| 554 |
+
|
| 555 |
+
gr.Markdown(
|
| 556 |
+
"### based on [Anything V5]"
|
| 557 |
)
|
| 558 |
+
|
| 559 |
+
inputs = [
|
| 560 |
+
model_name,
|
| 561 |
+
prompt,
|
| 562 |
+
guidance,
|
| 563 |
+
steps,
|
| 564 |
+
width,
|
| 565 |
+
height,
|
| 566 |
+
seed,
|
| 567 |
+
image,
|
| 568 |
+
strength,
|
| 569 |
+
neg_prompt,
|
| 570 |
+
scale,
|
| 571 |
+
scale_factor,
|
| 572 |
+
]
|
| 573 |
+
outputs = [image_out, error_output]
|
| 574 |
+
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
| 575 |
+
generate.click(inference, inputs=inputs, outputs=outputs, api_name="generate")
|
| 576 |
+
|
| 577 |
+
prompt_keys = [
|
| 578 |
+
"girl",
|
| 579 |
+
"lovely",
|
| 580 |
+
"cute",
|
| 581 |
+
"beautiful eyes",
|
| 582 |
+
"cumulonimbus clouds",
|
| 583 |
+
random.choice(["dress"]),
|
| 584 |
+
random.choice(["white hair"]),
|
| 585 |
+
random.choice(["blue eyes"]),
|
| 586 |
+
random.choice(["flower meadow"]),
|
| 587 |
+
random.choice(["Elif", "Angel"]),
|
| 588 |
+
]
|
| 589 |
+
prompt.value = ",".join(prompt_keys)
|
| 590 |
+
ex = gr.Examples(
|
| 591 |
+
[
|
| 592 |
+
[models[0].name, prompt.value, 7.5, 15],
|
| 593 |
+
],
|
| 594 |
+
inputs=[model_name, prompt, guidance, steps, seed],
|
| 595 |
+
outputs=outputs,
|
| 596 |
+
fn=inference,
|
| 597 |
+
cache_examples=False,
|
| 598 |
)
|
| 599 |
+
|
| 600 |
+
print(f"Space built in {time.time() - start_time:.2f} seconds")
|
| 601 |
+
|
| 602 |
+
if not is_colab:
|
| 603 |
+
demo.queue()
|
| 604 |
+
demo.launch(debug=is_colab, share=is_colab)
|
| 605 |
+
|
| 606 |
+
__main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|