webui.py 41.3 KB
Newer Older
A
first  
AUTOMATIC 已提交
1
import argparse, os, sys, glob
A
AUTOMATIC 已提交
2 3
from collections import namedtuple

A
first  
AUTOMATIC 已提交
4 5 6 7 8
import torch
import torch.nn as nn
import numpy as np
import gradio as gr
from omegaconf import OmegaConf
9
from PIL import Image, ImageFont, ImageDraw, PngImagePlugin
A
first  
AUTOMATIC 已提交
10 11 12 13 14
from itertools import islice
from einops import rearrange, repeat
from torch import autocast
import mimetypes
import random
15
import math
A
AUTOMATIC 已提交
16 17
import html
import time
A
AUTOMATIC 已提交
18 19
import json
import traceback
A
first  
AUTOMATIC 已提交
20

A
AUTOMATIC 已提交
21
import k_diffusion.sampling
A
first  
AUTOMATIC 已提交
22 23 24
from ldm.util import instantiate_from_config
from ldm.models.diffusion.ddim import DDIMSampler
from ldm.models.diffusion.plms import PLMSSampler
A
AUTOMATIC 已提交
25
import ldm.modules.encoders.modules
A
first  
AUTOMATIC 已提交
26

27 28 29 30 31 32 33 34
try:
    # this silences the annoying "Some weights of the model checkpoint were not used when initializing..." message at start.

    from transformers import logging
    logging.set_verbosity_error()
except:
    pass

A
first  
AUTOMATIC 已提交
35 36 37 38 39 40 41 42
# this is a fix for Windows users. Without it, javascript files will be served with text/html content-type and the bowser will not show any UI
mimetypes.init()
mimetypes.add_type('application/javascript', '.js')

# some of those options should not be changed at all because they would break the model, so I removed them from options.
opt_C = 4
opt_f = 8

43
LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
44
invalid_filename_chars = '<>:"/\|?*\n'
A
AUTOMATIC 已提交
45
config_filename = "config.json"
A
AUTOMATIC 已提交
46

A
first  
AUTOMATIC 已提交
47 48 49
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, default="configs/stable-diffusion/v1-inference.yaml", help="path to config which constructs model",)
parser.add_argument("--ckpt", type=str, default="models/ldm/stable-diffusion-v1/model.ckpt", help="path to checkpoint of model",)
50
parser.add_argument("--gfpgan-dir", type=str, help="GFPGAN directory", default=('./src/gfpgan' if os.path.exists('./src/gfpgan') else './GFPGAN')) # i disagree with where you're putting it but since all guidefags are doing it this way, there you go
51
parser.add_argument("--no-half", action='store_true', help="do not switch the model to 16-bit floats")
52
parser.add_argument("--no-progressbar-hiding", action='store_true', help="do not hide progressbar in gradio UI (we hide it because it slows down ML if you have hardware accleration in browser)")
53
parser.add_argument("--max-batch-count", type=int, default=16, help="maximum batch count value for the UI")
A
AUTOMATIC 已提交
54
parser.add_argument("--embeddings-dir", type=str, default='embeddings', help="embeddings dirtectory for textual inversion (default: embeddings)")
55

A
AUTOMATIC 已提交
56
cmd_opts = parser.parse_args()
A
first  
AUTOMATIC 已提交
57

58 59 60 61 62 63
css_hide_progressbar = """
.wrap .m-12 svg { display:none!important; }
.wrap .m-12::before { content:"Loading..." }
.progress-bar { display:none!important; }
.meta-text { display:none!important; }
"""
A
first  
AUTOMATIC 已提交
64

A
AUTOMATIC 已提交
65 66
SamplerData = namedtuple('SamplerData', ['name', 'constructor'])
samplers = [
A
AUTOMATIC 已提交
67
    *[SamplerData(x[0], lambda m, funcname=x[1]: KDiffusionSampler(m, funcname)) for x in [
A
AUTOMATIC 已提交
68 69 70 71 72 73 74
        ('LMS', 'sample_lms'),
        ('Heun', 'sample_heun'),
        ('Euler', 'sample_euler'),
        ('Euler ancestral', 'sample_euler_ancestral'),
        ('DPM 2', 'sample_dpm_2'),
        ('DPM 2 Ancestral', 'sample_dpm_2_ancestral'),
    ] if hasattr(k_diffusion.sampling, x[1])],
A
AUTOMATIC 已提交
75 76
    SamplerData('DDIM', lambda m: DDIMSampler(model)),
    SamplerData('PLMS', lambda m: PLMSSampler(model)),
A
AUTOMATIC 已提交
77
]
A
AUTOMATIC 已提交
78
samplers_for_img2img = [x for x in samplers if x.name != 'DDIM' and x.name != 'PLMS']
A
AUTOMATIC 已提交
79

A
AUTOMATIC 已提交
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
RealesrganModelInfo = namedtuple("RealesrganModelInfo", ["name", "location", "model", "netscale"])

try:
    from basicsr.archs.rrdbnet_arch import RRDBNet
    from realesrgan import RealESRGANer
    from realesrgan.archs.srvgg_arch import SRVGGNetCompact

    realesrgan_models = [
        RealesrganModelInfo(
            name="Real-ESRGAN 2x plus",
            location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
            netscale=2, model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
        ),
        RealesrganModelInfo(
            name="Real-ESRGAN 4x plus",
            location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
            netscale=4, model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
        ),
        RealesrganModelInfo(
            name="Real-ESRGAN 4x plus anime 6B",
            location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",
            netscale=4, model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
        ),
    ]
    have_realesrgan = True
except:
    print("Error loading Real-ESRGAN:", file=sys.stderr)
    print(traceback.format_exc(), file=sys.stderr)

    realesrgan_models = [RealesrganModelInfo('None', '', 0, None)]
    have_realesrgan = False

A
AUTOMATIC 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125

class Options:
    data = None
    data_labels = {
        "outdir": ("", "Output dictectory; if empty, defaults to 'outputs/*'"),
        "samples_save": (True, "Save indiviual samples"),
        "samples_format": ('png', 'File format for indiviual samples'),
        "grid_save": (True, "Save image grids"),
        "grid_format": ('png', 'File format for grids'),
        "grid_extended_filename": (False, "Add extended info (seed, prompt) to filename when saving grid"),
        "n_rows": (-1, "Grid row count; use -1 for autodetect and 0 for it to be same as batch size", -1, 16),
        "jpeg_quality": (80, "Quality for saved jpeg images", 1, 100),
        "verify_input": (True, "Check input, and produce warning if it's too long"),
        "enable_pnginfo": (True, "Save text information about generation parameters as chunks to png files"),
126
        "prompt_matrix_add_to_start": (True, "In prompt matrix, add the variable combination of text to the start of the prompt, rather than the end"),
A
AUTOMATIC 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    }

    def __init__(self):
        self.data = {k: v[0] for k, v in self.data_labels.items()}

    def __setattr__(self, key, value):
        if self.data is not None:
            if key in self.data:
                self.data[key] = value

        return super(Options, self).__setattr__(key, value)

    def __getattr__(self, item):
        if self.data is not None:
            if item in self.data:
                return self.data[item]

144 145 146
        if item in self.data_labels:
            return self.data_labels[item][0]

A
AUTOMATIC 已提交
147 148 149 150 151 152 153 154 155 156 157
        return super(Options, self).__getattribute__(item)

    def save(self, filename):
        with open(filename, "w", encoding="utf8") as file:
            json.dump(self.data, file)

    def load(self, filename):
        with open(filename, "r", encoding="utf8") as file:
            self.data = json.load(file)


A
first  
AUTOMATIC 已提交
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
def chunk(it, size):
    it = iter(it)
    return iter(lambda: tuple(islice(it, size)), ())


def load_model_from_config(config, ckpt, verbose=False):
    print(f"Loading model from {ckpt}")
    pl_sd = torch.load(ckpt, map_location="cpu")
    if "global_step" in pl_sd:
        print(f"Global Step: {pl_sd['global_step']}")
    sd = pl_sd["state_dict"]
    model = instantiate_from_config(config.model)
    m, u = model.load_state_dict(sd, strict=False)
    if len(m) > 0 and verbose:
        print("missing keys:")
        print(m)
    if len(u) > 0 and verbose:
        print("unexpected keys:")
        print(u)

    model.cuda()
    model.eval()
    return model


class CFGDenoiser(nn.Module):
    def __init__(self, model):
        super().__init__()
        self.inner_model = model

    def forward(self, x, sigma, uncond, cond, cond_scale):
        x_in = torch.cat([x] * 2)
        sigma_in = torch.cat([sigma] * 2)
        cond_in = torch.cat([uncond, cond])
        uncond, cond = self.inner_model(x_in, sigma_in, cond=cond_in).chunk(2)
        return uncond + (cond - uncond) * cond_scale


A
AUTOMATIC 已提交
196
class KDiffusionSampler:
A
AUTOMATIC 已提交
197
    def __init__(self, m, funcname):
A
AUTOMATIC 已提交
198
        self.model = m
A
AUTOMATIC 已提交
199 200
        self.model_wrap = k_diffusion.external.CompVisDenoiser(m)
        self.funcname = funcname
A
AUTOMATIC 已提交
201
        self.func = getattr(k_diffusion.sampling, self.funcname)
A
AUTOMATIC 已提交
202 203 204 205 206

    def sample(self, S, conditioning, batch_size, shape, verbose, unconditional_guidance_scale, unconditional_conditioning, eta, x_T):
        sigmas = self.model_wrap.get_sigmas(S)
        x = x_T * sigmas[0]
        model_wrap_cfg = CFGDenoiser(self.model_wrap)
A
AUTOMATIC 已提交
207

A
AUTOMATIC 已提交
208
        samples_ddim = self.func(model_wrap_cfg, x, sigmas, extra_args={'cond': conditioning, 'uncond': unconditional_conditioning, 'cond_scale': unconditional_guidance_scale}, disable=False)
A
AUTOMATIC 已提交
209 210 211 212

        return samples_ddim, None


A
AUTOMATIC 已提交
213
def create_random_tensors(shape, seeds):
A
AUTOMATIC 已提交
214
    xs = []
A
AUTOMATIC 已提交
215 216 217 218 219 220 221
    for seed in seeds:
        torch.manual_seed(seed)

        # randn results depend on device; gpu and cpu get different results for same seed;
        # the way I see it, it's better to do this on CPU, so that everyone gets same result;
        # but the original script had it like this so i do not dare change it for now because
        # it will break everyone's seeds.
A
AUTOMATIC 已提交
222 223 224 225 226
        xs.append(torch.randn(shape, device=device))
    x = torch.stack(xs)
    return x


H
hlky 已提交
227 228 229
def torch_gc():
    torch.cuda.empty_cache()
    torch.cuda.ipc_collect()
A
AUTOMATIC 已提交
230

231

232
def save_image(image, path, basename, seed, prompt, extension, info=None, short_filename=False):
233 234 235 236 237 238 239
    prompt = sanitize_filename_part(prompt)

    if short_filename:
        filename = f"{basename}.{extension}"
    else:
        filename = f"{basename}-{seed}-{prompt[:128]}.{extension}"

A
AUTOMATIC 已提交
240
    if extension == 'png' and opts.enable_pnginfo and info is not None:
241 242 243 244 245
        pnginfo = PngImagePlugin.PngInfo()
        pnginfo.add_text("parameters", info)
    else:
        pnginfo = None

A
AUTOMATIC 已提交
246
    image.save(os.path.join(path, filename), quality=opts.jpeg_quality, pnginfo=pnginfo)
247 248


A
AUTOMATIC 已提交
249 250 251 252
def sanitize_filename_part(text):
    return text.replace(' ', '_').translate({ord(x): '' for x in invalid_filename_chars})[:128]


A
AUTOMATIC 已提交
253 254 255 256 257
def plaintext_to_html(text):
    text = "".join([f"<p>{html.escape(x)}</p>\n" for x in text.split('\n')])
    return text


A
first  
AUTOMATIC 已提交
258 259
def load_GFPGAN():
    model_name = 'GFPGANv1.3'
A
AUTOMATIC 已提交
260
    model_path = os.path.join(cmd_opts.gfpgan_dir, 'experiments/pretrained_models', model_name + '.pth')
A
first  
AUTOMATIC 已提交
261 262 263
    if not os.path.isfile(model_path):
        raise Exception("GFPGAN model not found at path "+model_path)

A
AUTOMATIC 已提交
264
    sys.path.append(os.path.abspath(cmd_opts.gfpgan_dir))
A
first  
AUTOMATIC 已提交
265 266 267 268 269
    from gfpgan import GFPGANer

    return GFPGANer(model_path=model_path, upscale=1, arch='clean', channel_multiplier=2, bg_upsampler=None)


A
AUTOMATIC 已提交
270 271 272
def image_grid(imgs, batch_size, round_down=False, force_n_rows=None):
    if force_n_rows is not None:
        rows = force_n_rows
A
AUTOMATIC 已提交
273 274 275
    elif opts.n_rows > 0:
        rows = opts.n_rows
    elif opts.n_rows == 0:
276 277
        rows = batch_size
    else:
A
AUTOMATIC 已提交
278 279
        rows = math.sqrt(len(imgs))
        rows = int(rows) if round_down else round(rows)
280 281

    cols = math.ceil(len(imgs) / rows)
A
first  
AUTOMATIC 已提交
282 283

    w, h = imgs[0].size
284
    grid = Image.new('RGB', size=(cols * w, rows * h), color='black')
A
first  
AUTOMATIC 已提交
285 286 287 288 289 290

    for i, img in enumerate(imgs):
        grid.paste(img, box=(i % cols * w, i // cols * h))

    return grid

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
def draw_prompt_matrix(im, width, height, all_prompts):
    def wrap(text, d, font, line_length):
        lines = ['']
        for word in text.split():
            line = f'{lines[-1]} {word}'.strip()
            if d.textlength(line, font=font) <= line_length:
                lines[-1] = line
            else:
                lines.append(word)
        return '\n'.join(lines)

    def draw_texts(pos, x, y, texts, sizes):
        for i, (text, size) in enumerate(zip(texts, sizes)):
            active = pos & (1 << i) != 0

            if not active:
                text = '\u0336'.join(text) + '\u0336'

            d.multiline_text((x, y + size[1] / 2), text, font=fnt, fill=color_active if active else color_inactive, anchor="mm", align="center")

            y += size[1] + line_spacing

    fontsize = (width + height) // 25
    line_spacing = fontsize // 2
    fnt = ImageFont.truetype("arial.ttf", fontsize)
    color_active = (0, 0, 0)
    color_inactive = (153, 153, 153)

    pad_top = height // 4
A
AUTOMATIC 已提交
321
    pad_left = width * 3 // 4 if len(all_prompts) > 2 else 0
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

    cols = im.width // width
    rows = im.height // height

    prompts = all_prompts[1:]

    result = Image.new("RGB", (im.width + pad_left, im.height + pad_top), "white")
    result.paste(im, (pad_left, pad_top))

    d = ImageDraw.Draw(result)

    boundary = math.ceil(len(prompts) / 2)
    prompts_horiz = [wrap(x, d, fnt, width) for x in prompts[:boundary]]
    prompts_vert = [wrap(x, d, fnt, pad_left) for x in prompts[boundary:]]

    sizes_hor = [(x[2] - x[0], x[3] - x[1]) for x in [d.multiline_textbbox((0, 0), x, font=fnt) for x in prompts_horiz]]
    sizes_ver = [(x[2] - x[0], x[3] - x[1]) for x in [d.multiline_textbbox((0, 0), x, font=fnt) for x in prompts_vert]]
    hor_text_height = sum([x[1] + line_spacing for x in sizes_hor]) - line_spacing
    ver_text_height = sum([x[1] + line_spacing for x in sizes_ver]) - line_spacing

    for col in range(cols):
        x = pad_left + width * col + width / 2
        y = pad_top / 2 - hor_text_height / 2

        draw_texts(col, x, y, prompts_horiz, sizes_hor)

    for row in range(rows):
        x = pad_left / 2
        y = pad_top + height * row + height / 2 - ver_text_height / 2

        draw_texts(row, x, y, prompts_vert, sizes_ver)

    return result


A
AUTOMATIC 已提交
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 383 384
def resize_image(resize_mode, im, width, height):
    if resize_mode == 0:
        res = im.resize((width, height), resample=LANCZOS)
    elif resize_mode == 1:
        ratio = width / height
        src_ratio = im.width / im.height

        src_w = width if ratio > src_ratio else im.width * height // im.height
        src_h = height if ratio <= src_ratio else im.height * width // im.width

        resized = im.resize((src_w, src_h), resample=LANCZOS)
        res = Image.new("RGB", (width, height))
        res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
    else:
        ratio = width / height
        src_ratio = im.width / im.height

        src_w = width if ratio < src_ratio else im.width * height // im.height
        src_h = height if ratio >= src_ratio else im.height * width // im.width

        resized = im.resize((src_w, src_h), resample=LANCZOS)
        res = Image.new("RGB", (width, height))
        res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))

        if ratio < src_ratio:
            fill_height = height // 2 - src_h // 2
            res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
            res.paste(resized.resize((width, fill_height), box=(0, resized.height, width, resized.height)), box=(0, fill_height + src_h))
385
        elif ratio > src_ratio:
A
AUTOMATIC 已提交
386 387 388 389 390 391 392
            fill_width = width // 2 - src_w // 2
            res.paste(resized.resize((fill_width, height), box=(0, 0, 0, height)), box=(0, 0))
            res.paste(resized.resize((fill_width, height), box=(resized.width, 0, resized.width, height)), box=(fill_width + src_w, 0))

    return res


393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
def check_prompt_length(prompt, comments):
    """this function tests if prompt is too long, and if so, adds a message to comments"""

    tokenizer = model.cond_stage_model.tokenizer
    max_length = model.cond_stage_model.max_length

    info = model.cond_stage_model.tokenizer([prompt], truncation=True, max_length=max_length, return_overflowing_tokens=True, padding="max_length", return_tensors="pt")
    ovf = info['overflowing_tokens'][0]
    overflowing_count = ovf.shape[0]
    if overflowing_count == 0:
        return

    vocab = {v: k for k, v in tokenizer.get_vocab().items()}
    overflowing_words = [vocab.get(int(x), "") for x in ovf]
    overflowing_text = tokenizer.convert_tokens_to_string(''.join(overflowing_words))

    comments.append(f"Warning: too many input tokens; some ({len(overflowing_words)}) have been truncated:\n{overflowing_text}\n")


A
AUTOMATIC 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425
def wrap_gradio_call(func):
    def f(*p1, **p2):
        t = time.perf_counter()
        res = list(func(*p1, **p2))
        elapsed = time.perf_counter() - t

        # last item is always HTML
        res[-1] = res[-1] + f"<p class='performance'>Time taken: {elapsed:.2f}s</p>"

        return tuple(res)

    return f


A
AUTOMATIC 已提交
426 427 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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
GFPGAN = None
if os.path.exists(cmd_opts.gfpgan_dir):
    try:
        GFPGAN = load_GFPGAN()
        print("Loaded GFPGAN")
    except Exception:
        print("Error loading GFPGAN:", file=sys.stderr)
        print(traceback.format_exc(), file=sys.stderr)


class TextInversionEmbeddings:
    ids_lookup = {}
    word_embeddings = {}
    word_embeddings_checksums = {}
    fixes = []
    used_custom_terms = []
    dir_mtime = None

    def load(self, dir, model):
        mt = os.path.getmtime(dir)
        if self.dir_mtime is not None and mt <= self.dir_mtime:
            return

        self.dir_mtime = mt
        self.ids_lookup.clear()
        self.word_embeddings.clear()

        tokenizer = model.cond_stage_model.tokenizer

        def const_hash(a):
            r = 0
            for v in a:
                r = (r * 281 ^ int(v) * 997) & 0xFFFFFFFF
            return r

        def process_file(path, filename):
            name = os.path.splitext(filename)[0]

            data = torch.load(path)
            param_dict = data['string_to_param']
            assert len(param_dict) == 1, 'embedding file has multiple terms in it'
            emb = next(iter(param_dict.items()))[1].reshape(768)
            self.word_embeddings[name] = emb
            self.word_embeddings_checksums[name] = f'{const_hash(emb)&0xffff:04x}'

            ids = tokenizer([name], add_special_tokens=False)['input_ids'][0]
            first_id = ids[0]
            if first_id not in self.ids_lookup:
                self.ids_lookup[first_id] = []
            self.ids_lookup[first_id].append((ids, name))

        for fn in os.listdir(dir):
            try:
                process_file(os.path.join(dir, fn), fn)
            except:
                print(f"Error loading emedding {fn}:", file=sys.stderr)
                print(traceback.format_exc(), file=sys.stderr)
                continue

        print(f"Loaded a total of {len(self.word_embeddings)} text inversion embeddings.")

    def hijack(self, m):
        model_embeddings = m.cond_stage_model.transformer.text_model.embeddings

        model_embeddings.token_embedding = EmbeddingsWithFixes(model_embeddings.token_embedding, self)
        m.cond_stage_model = FrozenCLIPEmbedderWithCustomWords(m.cond_stage_model, self)

class FrozenCLIPEmbedderWithCustomWords(torch.nn.Module):
    def __init__(self, wrapped, embeddings):
        super().__init__()
        self.wrapped = wrapped
        self.embeddings = embeddings
        self.tokenizer = wrapped.tokenizer
        self.max_length = wrapped.max_length

    def forward(self, text):
        self.embeddings.fixes = []
        self.embeddings.used_custom_terms = []
        remade_batch_tokens = []
        id_start = self.wrapped.tokenizer.bos_token_id
        id_end = self.wrapped.tokenizer.eos_token_id
        maxlen = self.wrapped.max_length - 2

        cache = {}
        batch_tokens = self.wrapped.tokenizer(text, truncation=False, add_special_tokens=False)["input_ids"]
        for tokens in batch_tokens:
            tuple_tokens = tuple(tokens)

            if tuple_tokens in cache:
                remade_tokens, fixes = cache[tuple_tokens]
            else:
                fixes = []
                remade_tokens = []

                i = 0
                while i < len(tokens):
                    token = tokens[i]

                    possible_matches = self.embeddings.ids_lookup.get(token, None)

                    if possible_matches is None:
                        remade_tokens.append(token)
                    else:
                        found = False
                        for ids, word in possible_matches:
                            if tokens[i:i+len(ids)] == ids:
                                fixes.append((len(remade_tokens), word))
                                remade_tokens.append(777)
                                i += len(ids) - 1
                                found = True
                                self.embeddings.used_custom_terms.append((word, self.embeddings.word_embeddings_checksums[word]))
                                break

                        if not found:
                            remade_tokens.append(token)

                    i += 1

                remade_tokens = remade_tokens + [id_end] * (maxlen - 2 - len(remade_tokens))
                remade_tokens = [id_start] + remade_tokens[0:maxlen-2] + [id_end]
                cache[tuple_tokens] = (remade_tokens, fixes)

            remade_batch_tokens.append(remade_tokens)
            self.embeddings.fixes.append(fixes)

        tokens = torch.asarray(remade_batch_tokens).to(self.wrapped.device)
        outputs = self.wrapped.transformer(input_ids=tokens)
        z = outputs.last_hidden_state
        return z


class EmbeddingsWithFixes(nn.Module):
    def __init__(self, wrapped, embeddings):
        super().__init__()
        self.wrapped = wrapped
        self.embeddings = embeddings

    def forward(self, input_ids):
        batch_fixes = self.embeddings.fixes
        self.embeddings.fixes = []

        inputs_embeds = self.wrapped(input_ids)

        for fixes, tensor in zip(batch_fixes, inputs_embeds):
            for offset, word in fixes:
                tensor[offset] = self.embeddings.word_embeddings[word]

        return inputs_embeds


def get_learned_conditioning_with_embeddings(model, prompts):
    if os.path.exists(cmd_opts.embeddings_dir):
        text_inversion_embeddings.load(cmd_opts.embeddings_dir, model)

    return model.get_learned_conditioning(prompts)


583
def process_images(outpath, func_init, func_sample, prompt, seed, sampler_index, batch_size, n_iter, steps, cfg_scale, width, height, prompt_matrix, use_GFPGAN, do_not_save_grid=False, extra_generation_params=None):
A
AUTOMATIC 已提交
584
    """this is the main loop that both txt2img and img2img use; it calls func_init once inside all the scopes and func_sample once per batch"""
A
first  
AUTOMATIC 已提交
585

A
AUTOMATIC 已提交
586
    assert prompt is not None
H
hlky 已提交
587
    torch_gc()
A
first  
AUTOMATIC 已提交
588 589 590 591 592 593 594 595 596 597 598 599

    if seed == -1:
        seed = random.randrange(4294967294)
    seed = int(seed)

    os.makedirs(outpath, exist_ok=True)

    sample_path = os.path.join(outpath, "samples")
    os.makedirs(sample_path, exist_ok=True)
    base_count = len(os.listdir(sample_path))
    grid_count = len(os.listdir(outpath)) - 1

600 601
    comments = []

602
    prompt_matrix_parts = []
A
AUTOMATIC 已提交
603
    if prompt_matrix:
A
AUTOMATIC 已提交
604
        all_prompts = []
605
        prompt_matrix_parts = prompt.split("|")
A
AUTOMATIC 已提交
606
        combination_count = 2 ** (len(prompt_matrix_parts) - 1)
A
AUTOMATIC 已提交
607
        for combination_num in range(combination_count):
608
            selected_prompts = [text.strip().strip(',') for n, text in enumerate(prompt_matrix_parts[1:]) if combination_num & (1<<n)]
A
AUTOMATIC 已提交
609

610 611 612 613
            if opts.prompt_matrix_add_to_start:
                selected_prompts = selected_prompts + [prompt_matrix_parts[0]]
            else:
                selected_prompts = [prompt_matrix_parts[0]] + selected_prompts
A
AUTOMATIC 已提交
614

615
            all_prompts.append( ", ".join(selected_prompts))
A
AUTOMATIC 已提交
616

A
AUTOMATIC 已提交
617 618 619 620 621
        n_iter = math.ceil(len(all_prompts) / batch_size)
        all_seeds = len(all_prompts) * [seed]

        print(f"Prompt matrix will create {len(all_prompts)} images using a total of {n_iter} batches.")
    else:
622

A
AUTOMATIC 已提交
623
        if opts.verify_input:
624 625 626 627 628 629 630
            try:
                check_prompt_length(prompt, comments)
            except:
                import traceback
                print("Error verifying input:", file=sys.stderr)
                print(traceback.format_exc(), file=sys.stderr)

A
AUTOMATIC 已提交
631 632
        all_prompts = batch_size * n_iter * [prompt]
        all_seeds = [seed + x for x in range(len(all_prompts))]
A
AUTOMATIC 已提交
633

634 635 636 637 638 639 640 641 642 643 644 645 646
    generation_params = {
        "Steps": steps,
        "Sampler": samplers[sampler_index].name,
        "CFG scale": cfg_scale,
        "Seed": seed,
        "GFPGAN": ("GFPGAN" if use_GFPGAN and GFPGAN is not None else None)
    }

    if extra_generation_params is not None:
        generation_params.update(extra_generation_params)

    generation_params_text = ", ".join([k if k == v else f'{k}: {v}' for k, v in generation_params.items() if v is not None])

A
AUTOMATIC 已提交
647
    def infotext():
648
        return f"{prompt}\n{generation_params_text}".strip() + "".join(["\n\n" + x for x in comments])
A
AUTOMATIC 已提交
649 650 651

    if os.path.exists(cmd_opts.embeddings_dir):
        text_inversion_embeddings.load(cmd_opts.embeddings_dir, model)
652

A
first  
AUTOMATIC 已提交
653
    output_images = []
A
AUTOMATIC 已提交
654
    with torch.no_grad(), autocast("cuda"), model.ema_scope():
A
AUTOMATIC 已提交
655 656
        init_data = func_init()

A
first  
AUTOMATIC 已提交
657
        for n in range(n_iter):
A
AUTOMATIC 已提交
658 659
            prompts = all_prompts[n * batch_size:(n + 1) * batch_size]
            seeds = all_seeds[n * batch_size:(n + 1) * batch_size]
A
AUTOMATIC 已提交
660

A
AUTOMATIC 已提交
661
            uc = model.get_learned_conditioning(len(prompts) * [""])
A
AUTOMATIC 已提交
662 663
            c = model.get_learned_conditioning(prompts)

A
AUTOMATIC 已提交
664 665 666
            if len(text_inversion_embeddings.used_custom_terms) > 0:
                comments.append("Used custom terms: " + ", ".join([f'{word} [{checksum}]' for word, checksum in text_inversion_embeddings.used_custom_terms]))

A
AUTOMATIC 已提交
667
            # we manually generate all input noises because each one should have a specific seed
A
AUTOMATIC 已提交
668
            x = create_random_tensors([opt_C, height // opt_f, width // opt_f], seeds=seeds)
A
AUTOMATIC 已提交
669

A
AUTOMATIC 已提交
670
            samples_ddim = func_sample(init_data=init_data, x=x, conditioning=c, unconditional_conditioning=uc)
A
first  
AUTOMATIC 已提交
671

A
AUTOMATIC 已提交
672 673
            x_samples_ddim = model.decode_first_stage(samples_ddim)
            x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
A
first  
AUTOMATIC 已提交
674

A
AUTOMATIC 已提交
675
            if prompt_matrix or opts.samples_save or opts.grid_save:
A
AUTOMATIC 已提交
676 677 678 679 680 681 682 683 684
                for i, x_sample in enumerate(x_samples_ddim):
                    x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c')
                    x_sample = x_sample.astype(np.uint8)

                    if use_GFPGAN and GFPGAN is not None:
                        cropped_faces, restored_faces, restored_img = GFPGAN.enhance(x_sample, has_aligned=False, only_center_face=False, paste_back=True)
                        x_sample = restored_img

                    image = Image.fromarray(x_sample)
A
AUTOMATIC 已提交
685
                    save_image(image, sample_path, f"{base_count:05}", seeds[i], prompts[i], opts.samples_format, info=infotext())
A
AUTOMATIC 已提交
686 687 688

                    output_images.append(image)
                    base_count += 1
A
first  
AUTOMATIC 已提交
689

A
AUTOMATIC 已提交
690
        if (prompt_matrix or opts.grid_save) and not do_not_save_grid:
A
AUTOMATIC 已提交
691
            grid = image_grid(output_images, batch_size, round_down=prompt_matrix)
692 693

            if prompt_matrix:
A
AUTOMATIC 已提交
694 695 696 697 698 699 700 701

                try:
                    grid = draw_prompt_matrix(grid, width, height, prompt_matrix_parts)
                except Exception:
                    import traceback
                    print("Error creating prompt_matrix text:", file=sys.stderr)
                    print(traceback.format_exc(), file=sys.stderr)

702 703
                output_images.insert(0, grid)

A
AUTOMATIC 已提交
704
            save_image(grid, outpath, f"grid-{grid_count:04}", seed, prompt, opts.grid_format, info=infotext(), short_filename=not opts.grid_extended_filename)
A
first  
AUTOMATIC 已提交
705 706 707
            grid_count += 1


A
AUTOMATIC 已提交
708

A
AUTOMATIC 已提交
709 710
    torch_gc()
    return output_images, seed, infotext()
D
dogewanwan 已提交
711

A
AUTOMATIC 已提交
712

A
AUTOMATIC 已提交
713
def txt2img(prompt: str, ddim_steps: int, sampler_index: int, use_GFPGAN: bool, prompt_matrix: bool, ddim_eta: float, n_iter: int, batch_size: int, cfg_scale: float, seed: int, height: int, width: int):
A
AUTOMATIC 已提交
714
    outpath = opts.outdir or "outputs/txt2img-samples"
D
dogewanwan 已提交
715

A
AUTOMATIC 已提交
716
    sampler = samplers[sampler_index].constructor(model)
A
AUTOMATIC 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730

    def init():
        pass

    def sample(init_data, x, conditioning, unconditional_conditioning):
        samples_ddim, _ = sampler.sample(S=ddim_steps, conditioning=conditioning, batch_size=int(x.shape[0]), shape=x[0].shape, verbose=False, unconditional_guidance_scale=cfg_scale, unconditional_conditioning=unconditional_conditioning, eta=ddim_eta, x_T=x)
        return samples_ddim

    output_images, seed, info = process_images(
        outpath=outpath,
        func_init=init,
        func_sample=sample,
        prompt=prompt,
        seed=seed,
A
AUTOMATIC 已提交
731
        sampler_index=sampler_index,
A
AUTOMATIC 已提交
732 733 734 735 736 737 738 739 740 741 742 743
        batch_size=batch_size,
        n_iter=n_iter,
        steps=ddim_steps,
        cfg_scale=cfg_scale,
        width=width,
        height=height,
        prompt_matrix=prompt_matrix,
        use_GFPGAN=use_GFPGAN
    )

    del sampler

A
AUTOMATIC 已提交
744
    return output_images, seed, plaintext_to_html(info)
A
AUTOMATIC 已提交
745 746


A
AUTOMATIC 已提交
747 748 749 750 751
class Flagging(gr.FlaggingCallback):

    def setup(self, components, flagging_dir: str):
        pass

752 753 754
    def flag(self, flag_data, flag_option=None, flag_index=None, username=None):
        import csv

A
AUTOMATIC 已提交
755 756
        os.makedirs("log/images", exist_ok=True)

A
AUTOMATIC 已提交
757
        # those must match the "txt2img" function
A
AUTOMATIC 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
        prompt, ddim_steps, sampler_name, use_GFPGAN, prompt_matrix, ddim_eta, n_iter, n_samples, cfg_scale, request_seed, height, width, images, seed, comment = flag_data

        filenames = []

        with open("log/log.csv", "a", encoding="utf8", newline='') as file:
            import time
            import base64

            at_start = file.tell() == 0
            writer = csv.writer(file)
            if at_start:
                writer.writerow(["prompt", "seed", "width", "height", "cfgs", "steps", "filename"])

            filename_base = str(int(time.time() * 1000))
            for i, filedata in enumerate(images):
                filename = "log/images/"+filename_base + ("" if len(images) == 1 else "-"+str(i+1)) + ".png"

                if filedata.startswith("data:image/png;base64,"):
                    filedata = filedata[len("data:image/png;base64,"):]

                with open(filename, "wb") as imgfile:
                    imgfile.write(base64.decodebytes(filedata.encode('utf-8')))

                filenames.append(filename)

            writer.writerow([prompt, seed, width, height, cfg_scale, ddim_steps, filenames[0]])

        print("Logged:", filenames[0])

A
first  
AUTOMATIC 已提交
787

A
AUTOMATIC 已提交
788
txt2img_interface = gr.Interface(
A
AUTOMATIC 已提交
789
    wrap_gradio_call(txt2img),
A
first  
AUTOMATIC 已提交
790 791 792
    inputs=[
        gr.Textbox(label="Prompt", placeholder="A corgi wearing a top hat as an oil painting.", lines=1),
        gr.Slider(minimum=1, maximum=150, step=1, label="Sampling Steps", value=50),
A
AUTOMATIC 已提交
793
        gr.Radio(label='Sampling method', choices=[x.name for x in samplers], value=samplers[0].name, type="index"),
A
first  
AUTOMATIC 已提交
794
        gr.Checkbox(label='Fix faces using GFPGAN', value=False, visible=GFPGAN is not None),
A
AUTOMATIC 已提交
795
        gr.Checkbox(label='Create prompt matrix (separate multiple prompts using |, and get all combinations of them)', value=False),
A
first  
AUTOMATIC 已提交
796
        gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="DDIM ETA", value=0.0, visible=False),
A
AUTOMATIC 已提交
797
        gr.Slider(minimum=1, maximum=cmd_opts.max_batch_count, step=1, label='Batch count (how many batches of images to generate)', value=1),
798
        gr.Slider(minimum=1, maximum=8, step=1, label='Batch size (how many images are in a batch; memory-hungry)', value=1),
799
        gr.Slider(minimum=1.0, maximum=15.0, step=0.5, label='Classifier Free Guidance Scale (how strongly the image should follow the prompt)', value=7.0),
A
first  
AUTOMATIC 已提交
800 801 802 803 804 805 806
        gr.Number(label='Seed', value=-1),
        gr.Slider(minimum=64, maximum=2048, step=64, label="Height", value=512),
        gr.Slider(minimum=64, maximum=2048, step=64, label="Width", value=512),
    ],
    outputs=[
        gr.Gallery(label="Images"),
        gr.Number(label='Seed'),
A
AUTOMATIC 已提交
807
        gr.HTML(),
A
first  
AUTOMATIC 已提交
808
    ],
A
AUTOMATIC 已提交
809
    title="Stable Diffusion Text-to-Image",
A
AUTOMATIC 已提交
810
    flagging_callback=Flagging()
A
first  
AUTOMATIC 已提交
811 812 813
)


A
AUTOMATIC 已提交
814
def img2img(prompt: str, init_img, ddim_steps: int, sampler_index: int, use_GFPGAN: bool, prompt_matrix, loopback: bool, n_iter: int, batch_size: int, cfg_scale: float, denoising_strength: float, seed: int, height: int, width: int, resize_mode: int):
A
AUTOMATIC 已提交
815
    outpath = opts.outdir or "outputs/img2img-samples"
D
dogewanwan 已提交
816

A
AUTOMATIC 已提交
817
    sampler = samplers_for_img2img[sampler_index].constructor(model)
A
first  
AUTOMATIC 已提交
818

A
AUTOMATIC 已提交
819
    assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'
A
first  
AUTOMATIC 已提交
820

A
AUTOMATIC 已提交
821 822
    def init():
        image = init_img.convert("RGB")
A
AUTOMATIC 已提交
823
        image = resize_image(resize_mode, image, width, height)
A
AUTOMATIC 已提交
824 825 826
        image = np.array(image).astype(np.float32) / 255.0
        image = image[None].transpose(0, 3, 1, 2)
        image = torch.from_numpy(image)
A
first  
AUTOMATIC 已提交
827

828 829 830 831
        init_image = 2. * image - 1.
        init_image = init_image.to(device)
        init_image = repeat(init_image, '1 ... -> b ...', b=batch_size)
        init_latent = model.get_first_stage_encoding(model.encode_first_stage(init_image))  # move to latent space
A
AUTOMATIC 已提交
832

A
AUTOMATIC 已提交
833 834 835
        return init_latent,

    def sample(init_data, x, conditioning, unconditional_conditioning):
A
AUTOMATIC 已提交
836 837
        t_enc = int(denoising_strength * ddim_steps)

A
AUTOMATIC 已提交
838 839 840 841 842 843 844 845
        x0, = init_data

        sigmas = sampler.model_wrap.get_sigmas(ddim_steps)
        noise = x * sigmas[ddim_steps - t_enc - 1]

        xi = x0 + noise
        sigma_sched = sigmas[ddim_steps - t_enc - 1:]
        model_wrap_cfg = CFGDenoiser(sampler.model_wrap)
A
AUTOMATIC 已提交
846
        samples_ddim = sampler.func(model_wrap_cfg, xi, sigma_sched, extra_args={'cond': conditioning, 'uncond': unconditional_conditioning, 'cond_scale': cfg_scale}, disable=False)
A
AUTOMATIC 已提交
847 848
        return samples_ddim

A
AUTOMATIC 已提交
849 850 851 852 853 854 855 856 857 858 859 860
    if loopback:
        output_images, info = None, None
        history = []
        initial_seed = None

        for i in range(n_iter):
            output_images, seed, info = process_images(
                outpath=outpath,
                func_init=init,
                func_sample=sample,
                prompt=prompt,
                seed=seed,
A
AUTOMATIC 已提交
861
                sampler_index=0,
A
AUTOMATIC 已提交
862 863 864 865 866 867 868 869
                batch_size=1,
                n_iter=1,
                steps=ddim_steps,
                cfg_scale=cfg_scale,
                width=width,
                height=height,
                prompt_matrix=prompt_matrix,
                use_GFPGAN=use_GFPGAN,
870
                do_not_save_grid=True,
A
AUTOMATIC 已提交
871
                extra_generation_params={"Denoising Strength": denoising_strength},
A
AUTOMATIC 已提交
872 873 874 875 876 877 878 879 880 881 882 883
            )

            if initial_seed is None:
                initial_seed = seed

            init_img = output_images[0]
            seed = seed + 1
            denoising_strength = max(denoising_strength * 0.95, 0.1)
            history.append(init_img)

        grid_count = len(os.listdir(outpath)) - 1
        grid = image_grid(history, batch_size, force_n_rows=1)
884

A
AUTOMATIC 已提交
885
        save_image(grid, outpath, f"grid-{grid_count:04}", initial_seed, prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename)
A
AUTOMATIC 已提交
886 887 888 889 890 891 892 893 894 895 896

        output_images = history
        seed = initial_seed

    else:
        output_images, seed, info = process_images(
            outpath=outpath,
            func_init=init,
            func_sample=sample,
            prompt=prompt,
            seed=seed,
A
AUTOMATIC 已提交
897
            sampler_index=0,
A
AUTOMATIC 已提交
898 899 900 901 902 903 904
            batch_size=batch_size,
            n_iter=n_iter,
            steps=ddim_steps,
            cfg_scale=cfg_scale,
            width=width,
            height=height,
            prompt_matrix=prompt_matrix,
905
            use_GFPGAN=use_GFPGAN,
A
AUTOMATIC 已提交
906
            extra_generation_params={"Denoising Strength": denoising_strength},
A
AUTOMATIC 已提交
907
        )
A
AUTOMATIC 已提交
908

A
AUTOMATIC 已提交
909
    del sampler
A
first  
AUTOMATIC 已提交
910

A
AUTOMATIC 已提交
911
    return output_images, seed, plaintext_to_html(info)
A
first  
AUTOMATIC 已提交
912 913


914 915 916
sample_img2img = "assets/stable-samples/img2img/sketch-mountains-input.jpg"
sample_img2img = sample_img2img if os.path.exists(sample_img2img) else None

A
first  
AUTOMATIC 已提交
917
img2img_interface = gr.Interface(
A
AUTOMATIC 已提交
918
    wrap_gradio_call(img2img),
A
first  
AUTOMATIC 已提交
919 920
    inputs=[
        gr.Textbox(placeholder="A fantasy landscape, trending on artstation.", lines=1),
921
        gr.Image(value=sample_img2img, source="upload", interactive=True, type="pil"),
A
first  
AUTOMATIC 已提交
922
        gr.Slider(minimum=1, maximum=150, step=1, label="Sampling Steps", value=50),
A
AUTOMATIC 已提交
923
        gr.Radio(label='Sampling method', choices=[x.name for x in samplers_for_img2img], value=samplers_for_img2img[0].name, type="index"),
924
        gr.Checkbox(label='Fix faces using GFPGAN', value=False, visible=GFPGAN is not None),
A
AUTOMATIC 已提交
925
        gr.Checkbox(label='Create prompt matrix (separate multiple prompts using |, and get all combinations of them)', value=False),
A
AUTOMATIC 已提交
926
        gr.Checkbox(label='Loopback (use images from previous batch when creating next batch)', value=False),
A
AUTOMATIC 已提交
927
        gr.Slider(minimum=1, maximum=cmd_opts.max_batch_count, step=1, label='Batch count (how many batches of images to generate)', value=1),
928
        gr.Slider(minimum=1, maximum=8, step=1, label='Batch size (how many images are in a batch; memory-hungry)', value=1),
929
        gr.Slider(minimum=1.0, maximum=15.0, step=0.5, label='Classifier Free Guidance Scale (how strongly the image should follow the prompt)', value=7.0),
A
first  
AUTOMATIC 已提交
930 931
        gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising Strength', value=0.75),
        gr.Number(label='Seed', value=-1),
932 933
        gr.Slider(minimum=64, maximum=2048, step=64, label="Height", value=512),
        gr.Slider(minimum=64, maximum=2048, step=64, label="Width", value=512),
A
AUTOMATIC 已提交
934
        gr.Radio(label="Resize mode", choices=["Just resize", "Crop and resize", "Resize and fill"], type="index", value="Just resize")
A
first  
AUTOMATIC 已提交
935 936 937
    ],
    outputs=[
        gr.Gallery(),
A
AUTOMATIC 已提交
938
        gr.Number(label='Seed'),
A
AUTOMATIC 已提交
939
        gr.HTML(),
A
first  
AUTOMATIC 已提交
940
    ],
941
    allow_flagging="never",
A
first  
AUTOMATIC 已提交
942 943
)

A
AUTOMATIC 已提交
944

A
AUTOMATIC 已提交
945
def run_extras(image, GFPGAN_strength, RealESRGAN_upscaling, RealESRGAN_model_index):
946 947
    image = image.convert("RGB")

A
AUTOMATIC 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
    outpath = opts.outdir or "outputs/extras-samples"

    if GFPGAN is not None and GFPGAN_strength > 0:
        cropped_faces, restored_faces, restored_img = GFPGAN.enhance(np.array(image, dtype=np.uint8), has_aligned=False, only_center_face=False, paste_back=True)
        res = Image.fromarray(restored_img)

        if GFPGAN_strength < 1.0:
            res = Image.blend(image, res, GFPGAN_strength)

        image = res

    if have_realesrgan and RealESRGAN_upscaling != 1.0:
        info = realesrgan_models[RealESRGAN_model_index]

        model = info.model()
        upsampler = RealESRGANer(
            scale=info.netscale,
            model_path=info.location,
            model=model,
            half=True
        )

        upsampled = upsampler.enhance(np.array(image), outscale=RealESRGAN_upscaling)[0]

        image = Image.fromarray(upsampled)
973

A
AUTOMATIC 已提交
974 975 976 977
    os.makedirs(outpath, exist_ok=True)
    base_count = len(os.listdir(outpath))

    save_image(image, outpath, f"{base_count:05}", None, '', opts.samples_format, short_filename=True)
978

A
AUTOMATIC 已提交
979
    return image, 0, ''
980 981


A
AUTOMATIC 已提交
982 983
extras_interface = gr.Interface(
    wrap_gradio_call(run_extras),
A
AUTOMATIC 已提交
984 985
    inputs=[
        gr.Image(label="Source", source="upload", interactive=True, type="pil"),
A
AUTOMATIC 已提交
986 987 988
        gr.Slider(minimum=0.0, maximum=1.0, step=0.001, label="GFPGAN strength", value=1, interactive=GFPGAN is not None),
        gr.Slider(minimum=1.0, maximum=4.0, step=0.05, label="Real-ESRGAN upscaling", value=2, interactive=have_realesrgan),
        gr.Radio(label='Real-ESRGAN model', choices=[x.name for x in realesrgan_models], value=realesrgan_models[0].name, type="index", interactive=have_realesrgan),
A
AUTOMATIC 已提交
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    ],
    outputs=[
        gr.Image(label="Result"),
        gr.Number(label='Seed', visible=False),
        gr.HTML(),
    ],
    allow_flagging="never",
)

opts = Options()
if os.path.exists(config_filename):
    opts.load(config_filename)


def run_settings(*args):
    up = []

    for key, value, comp in zip(opts.data_labels.keys(), args, settings_interface.input_components):
        opts.data[key] = value
        up.append(comp.update(value=value))

    opts.save(config_filename)

    return 'Settings saved.', ''


def create_setting_component(key):
    def fun():
        return opts.data[key] if key in opts.data else opts.data_labels[key][0]

    labelinfo = opts.data_labels[key]
    t = type(labelinfo[0])
    label = labelinfo[1]
    if t == str:
        item = gr.Textbox(label=label, value=fun, lines=1)
    elif t == int:
        if len(labelinfo) == 4:
            item = gr.Slider(minimum=labelinfo[2], maximum=labelinfo[3], step=1, label=label, value=fun)
        else:
            item = gr.Number(label=label, value=fun)
    elif t == bool:
        item = gr.Checkbox(label=label, value=fun)
    else:
        raise Exception(f'bad options item type: {str(t)} for key {key}')

    return item


settings_interface = gr.Interface(
    run_settings,
    inputs=[create_setting_component(key) for key in opts.data_labels.keys()],
    outputs=[
        gr.Textbox(label='Result'),
        gr.HTML(),
    ],
    title=None,
    description=None,
    allow_flagging="never",
)

interfaces = [
    (txt2img_interface, "txt2img"),
    (img2img_interface, "img2img"),
A
AUTOMATIC 已提交
1052
    (extras_interface, "Extras"),
A
AUTOMATIC 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    (settings_interface, "Settings"),
]

config = OmegaConf.load(cmd_opts.config)
model = load_model_from_config(config, cmd_opts.ckpt)

device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model = (model if cmd_opts.no_half else model.half()).to(device)
text_inversion_embeddings = TextInversionEmbeddings()

if os.path.exists(cmd_opts.embeddings_dir):
    text_inversion_embeddings.hijack(model)
1065

1066 1067 1068
demo = gr.TabbedInterface(
    interface_list=[x[0] for x in interfaces],
    tab_names=[x[1] for x in interfaces],
A
AUTOMATIC 已提交
1069
    css=("" if cmd_opts.no_progressbar_hiding else css_hide_progressbar) + """
A
AUTOMATIC 已提交
1070 1071 1072
.output-html p {margin: 0 0.5em;}
.performance { font-size: 0.85em; color: #444; }
"""
1073
)
A
first  
AUTOMATIC 已提交
1074

A
AUTOMATIC 已提交
1075
demo.launch()