Skip to content

Commit 5ae104b

Browse files
authored
Merge pull request AUTOMATIC1111#3 from ShinkoNet/master
added support for words in seeds
2 parents 56a93f3 + e460676 commit 5ae104b

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

webui.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,13 @@ def image_grid(imgs, batch_size, round_down=False, force_n_rows=None):
258258

259259
return grid
260260

261-
261+
def seed_to_int(s):
262+
if s == 'random':
263+
return random.randint(0,2**32)
264+
n = abs(int(s) if s.isdigit() else hash(s))
265+
while n > 2**32:
266+
n = n >> 32
267+
return n
262268

263269
def draw_prompt_matrix(im, width, height, all_prompts):
264270
def wrap(text, d, font, line_length):
@@ -390,10 +396,6 @@ def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name,
390396
mem_mon = MemUsageMonitor('MemMon')
391397
mem_mon.start()
392398

393-
if seed == -1:
394-
seed = random.randrange(4294967294)
395-
seed = int(seed)
396-
397399
os.makedirs(outpath, exist_ok=True)
398400

399401
sample_path = os.path.join(outpath, "samples")
@@ -468,6 +470,7 @@ def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name,
468470
cropped_faces, restored_faces, restored_img = GFPGAN.enhance(x_sample, has_aligned=False, only_center_face=False, paste_back=True)
469471
x_sample = restored_img
470472

473+
471474
image = Image.fromarray(x_sample)
472475
filename = f"{base_count:05}-{seeds[i]}_{prompts[i].replace(' ', '_').translate({ord(x): '' for x in invalid_filename_chars})[:128]}.png"
473476
if not skip_save:
@@ -518,6 +521,7 @@ def process_images(outpath, func_init, func_sample, prompt, seed, sampler_name,
518521
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):
519522
outpath = opt.outdir or "outputs/txt2img-samples"
520523
err = False
524+
seed = seed_to_int(seed)
521525

522526
if sampler_name == 'PLMS':
523527
sampler = PLMSSampler(model)
@@ -624,7 +628,7 @@ def flag(self, flag_data, flag_option=None, flag_index=None, username=None):
624628
gr.Slider(minimum=1, maximum=16, step=1, label='Batch count (how many batches of images to generate)', value=1),
625629
gr.Slider(minimum=1, maximum=8, step=1, label='Batch size (how many images are in a batch; memory-hungry)', value=1),
626630
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),
627-
gr.Number(label='Seed', value=-1),
631+
gr.Textbox(label="Seed ('random' to randomize)", lines=1, value="random"),
628632
gr.Slider(minimum=64, maximum=2048, step=64, label="Height", value=512),
629633
gr.Slider(minimum=64, maximum=2048, step=64, label="Width", value=512),
630634
],
@@ -644,6 +648,7 @@ def flag(self, flag_data, flag_option=None, flag_index=None, username=None):
644648
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):
645649
outpath = opt.outdir or "outputs/img2img-samples"
646650
err = False
651+
seed = seed_to_int(seed)
647652

648653
if sampler_name == 'DDIM':
649654
sampler = DDIMSampler(model)
@@ -725,12 +730,14 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
725730
seed = seed + 1
726731
denoising_strength = max(denoising_strength * 0.95, 0.1)
727732
history.append(init_img)
733+
728734
if not skip_grid:
729735
grid_count = len(os.listdir(outpath)) - 1
730736
grid = image_grid(history, batch_size, force_n_rows=1)
731737
grid_file = f"grid-{grid_count:05}-{seed}_{prompt.replace(' ', '_').translate({ord(x): '' for x in invalid_filename_chars})[:128]}.jpg"
732738
grid.save(os.path.join(outpath, grid_file), 'jpeg', quality=100, optimize=True)
733739

740+
734741
output_images = history
735742
seed = initial_seed
736743

@@ -787,7 +794,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
787794
gr.Slider(minimum=1, maximum=8, step=1, label='Batch size (how many images are in a batch; memory-hungry)', value=1),
788795
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),
789796
gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising Strength', value=0.75),
790-
gr.Number(label='Seed', value=-1),
797+
gr.Textbox(label="Seed ('random' to randomize)", lines=1, value="random"),
791798
gr.Slider(minimum=64, maximum=2048, step=64, label="Height", value=512),
792799
gr.Slider(minimum=64, maximum=2048, step=64, label="Width", value=512),
793800
gr.Radio(label="Resize mode", choices=["Just resize", "Crop and resize", "Resize and fill"], type="index", value="Just resize")

0 commit comments

Comments
 (0)