Skip to content

Commit

Permalink
Merge pull request AUTOMATIC1111#2 from hlky/repo-sync/stable-diffusi…
Browse files Browse the repository at this point in the history
…on-webui/default

🔄 Synced file(s) with hlky/stable-diffusion-webui
  • Loading branch information
hlky committed Aug 24, 2022
2 parents c8e6f2f + d48785f commit 950d14a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions scripts/webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def load_GFPGAN():
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model = (model if opt.no_half else model.half()).to(device)

def load_embeddings(fp):
if fp is not None and hasattr(model, "embedding_manager"):
model.embedding_manager.load(fp.name)

def image_grid(imgs, batch_size, round_down=False, force_n_rows=None):
if force_n_rows is not None:
Expand Down Expand Up @@ -386,7 +389,7 @@ def check_prompt_length(prompt, comments):
comments.append(f"Warning: too many input tokens; some ({len(overflowing_words)}) have been truncated:\n{overflowing_text}\n")


def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name, skip_grid, skip_save, batch_size, n_iter, steps, cfg_scale, width, height, prompt_matrix, use_GFPGAN, do_not_save_grid=False, normalize_prompt_weights=True):
def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name, skip_grid, skip_save, batch_size, n_iter, steps, cfg_scale, width, height, prompt_matrix, use_GFPGAN, fp, do_not_save_grid=False, normalize_prompt_weights=True):
"""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"""
assert prompt is not None
torch_gc()
Expand All @@ -396,6 +399,9 @@ def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name,
mem_mon = MemUsageMonitor('MemMon')
mem_mon.start()

if hasattr(model, "embedding_manager"):
load_embeddings(fp)

os.makedirs(outpath, exist_ok=True)

sample_path = os.path.join(outpath, "samples")
Expand Down Expand Up @@ -537,7 +543,7 @@ def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name,
return output_images, seed, info, stats


def txt2img(prompt: str, ddim_steps: int, sampler_name: str, use_GFPGAN: bool, prompt_matrix: bool, skip_grid: bool, skip_save: bool, ddim_eta: float, n_iter: int, batch_size: int, cfg_scale: float, seed: int, height: int, width: int, normalize_prompt_weights: bool):
def txt2img(prompt: str, ddim_steps: int, sampler_name: str, use_GFPGAN: bool, prompt_matrix: bool, skip_grid: bool, skip_save: bool, ddim_eta: float, n_iter: int, batch_size: int, cfg_scale: float, seed: int, height: int, width: int, normalize_prompt_weights: bool, fp):
outpath = opt.outdir or "outputs/txt2img-samples"
err = False
seed = seed_to_int(seed)
Expand Down Expand Up @@ -576,6 +582,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
height=height,
prompt_matrix=prompt_matrix,
use_GFPGAN=use_GFPGAN,
fp=fp,
normalize_prompt_weights=normalize_prompt_weights
)

Expand Down Expand Up @@ -651,6 +658,7 @@ def flag(self, flag_data, flag_option=None, flag_index=None, username=None):
gr.Slider(minimum=64, maximum=2048, step=64, label="Height", value=512),
gr.Slider(minimum=64, maximum=2048, step=64, label="Width", value=512),
gr.Checkbox(label="Normalize Prompt Weights (ensure sum of weights add up to 1.0)", value=True),
gr.File(label = "Embeddings file for textual inversion", visible=hasattr(model, "embedding_manager")),
],
outputs=[
gr.Gallery(label="Images"),
Expand All @@ -665,7 +673,7 @@ def flag(self, flag_data, flag_option=None, flag_index=None, username=None):
)


def img2img(prompt: str, init_img, ddim_steps: int, sampler_name: str, use_GFPGAN: bool, prompt_matrix, loopback: bool, skip_grid: bool, skip_save: bool, n_iter: int, batch_size: int, cfg_scale: float, denoising_strength: float, seed: int, height: int, width: int, resize_mode: int, normalize_prompt_weights: bool):
def img2img(prompt: str, init_img, ddim_steps: int, sampler_name: str, use_GFPGAN: bool, prompt_matrix, loopback: bool, skip_grid: bool, skip_save: bool, n_iter: int, batch_size: int, cfg_scale: float, denoising_strength: float, seed: int, height: int, width: int, resize_mode: int, normalize_prompt_weights: bool, fp):
outpath = opt.outdir or "outputs/img2img-samples"
err = False
seed = seed_to_int(seed)
Expand Down Expand Up @@ -740,6 +748,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
height=height,
prompt_matrix=prompt_matrix,
use_GFPGAN=use_GFPGAN,
fp=fp,
do_not_save_grid=True
)

Expand Down Expand Up @@ -779,7 +788,8 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
height=height,
prompt_matrix=prompt_matrix,
use_GFPGAN=use_GFPGAN,
normalize_prompt_weights=normalize_prompt_weights
fp=fp,
normalize_prompt_weights=normalize_prompt_weights
)

del sampler
Expand Down Expand Up @@ -820,6 +830,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
gr.Slider(minimum=64, maximum=2048, step=64, label="Width", value=512),
gr.Radio(label="Resize mode", choices=["Just resize", "Crop and resize", "Resize and fill"], type="index", value="Just resize"),
gr.Checkbox(label="Normalize Prompt Weights (ensure sum of weights add up to 1.0)", value=True),
gr.File(label = "Embeddings file for textual inversion", visible=hasattr(model, "embedding_manager")),
],
outputs=[
gr.Gallery(),
Expand Down Expand Up @@ -918,5 +929,5 @@ def run_GFPGAN(image, strength):
css=("" if opt.no_progressbar_hiding else css_hide_progressbar),
theme="default",
)

demo.launch(show_error=True, server_name='0.0.0.0')
demo.queue(concurrency_count=1)
demo.launch(show_error=True, server_name='0.0.0.0')

0 comments on commit 950d14a

Please sign in to comment.