stable-diffusion-webui
191 строка · 6.4 Кб
1import copy
2import random
3import shlex
4
5import modules.scripts as scripts
6import gradio as gr
7
8from modules import sd_samplers, errors, sd_models
9from modules.processing import Processed, process_images
10from modules.shared import state
11
12
13def process_model_tag(tag):
14info = sd_models.get_closet_checkpoint_match(tag)
15assert info is not None, f'Unknown checkpoint: {tag}'
16return info.name
17
18
19def process_string_tag(tag):
20return tag
21
22
23def process_int_tag(tag):
24return int(tag)
25
26
27def process_float_tag(tag):
28return float(tag)
29
30
31def process_boolean_tag(tag):
32return True if (tag == "true") else False
33
34
35prompt_tags = {
36"sd_model": process_model_tag,
37"outpath_samples": process_string_tag,
38"outpath_grids": process_string_tag,
39"prompt_for_display": process_string_tag,
40"prompt": process_string_tag,
41"negative_prompt": process_string_tag,
42"styles": process_string_tag,
43"seed": process_int_tag,
44"subseed_strength": process_float_tag,
45"subseed": process_int_tag,
46"seed_resize_from_h": process_int_tag,
47"seed_resize_from_w": process_int_tag,
48"sampler_index": process_int_tag,
49"sampler_name": process_string_tag,
50"batch_size": process_int_tag,
51"n_iter": process_int_tag,
52"steps": process_int_tag,
53"cfg_scale": process_float_tag,
54"width": process_int_tag,
55"height": process_int_tag,
56"restore_faces": process_boolean_tag,
57"tiling": process_boolean_tag,
58"do_not_save_samples": process_boolean_tag,
59"do_not_save_grid": process_boolean_tag
60}
61
62
63def cmdargs(line):
64args = shlex.split(line)
65pos = 0
66res = {}
67
68while pos < len(args):
69arg = args[pos]
70
71assert arg.startswith("--"), f'must start with "--": {arg}'
72assert pos+1 < len(args), f'missing argument for command line option {arg}'
73
74tag = arg[2:]
75
76if tag == "prompt" or tag == "negative_prompt":
77pos += 1
78prompt = args[pos]
79pos += 1
80while pos < len(args) and not args[pos].startswith("--"):
81prompt += " "
82prompt += args[pos]
83pos += 1
84res[tag] = prompt
85continue
86
87
88func = prompt_tags.get(tag, None)
89assert func, f'unknown commandline option: {arg}'
90
91val = args[pos+1]
92if tag == "sampler_name":
93val = sd_samplers.samplers_map.get(val.lower(), None)
94
95res[tag] = func(val)
96
97pos += 2
98
99return res
100
101
102def load_prompt_file(file):
103if file is None:
104return None, gr.update(), gr.update(lines=7)
105else:
106lines = [x.strip() for x in file.decode('utf8', errors='ignore').split("\n")]
107return None, "\n".join(lines), gr.update(lines=7)
108
109
110class Script(scripts.Script):
111def title(self):
112return "Prompts from file or textbox"
113
114def ui(self, is_img2img):
115checkbox_iterate = gr.Checkbox(label="Iterate seed every line", value=False, elem_id=self.elem_id("checkbox_iterate"))
116checkbox_iterate_batch = gr.Checkbox(label="Use same random seed for all lines", value=False, elem_id=self.elem_id("checkbox_iterate_batch"))
117prompt_position = gr.Radio(["start", "end"], label="Insert prompts at the", elem_id=self.elem_id("prompt_position"), value="start")
118
119prompt_txt = gr.Textbox(label="List of prompt inputs", lines=1, elem_id=self.elem_id("prompt_txt"))
120file = gr.File(label="Upload prompt inputs", type='binary', elem_id=self.elem_id("file"))
121
122file.change(fn=load_prompt_file, inputs=[file], outputs=[file, prompt_txt, prompt_txt], show_progress=False)
123
124# We start at one line. When the text changes, we jump to seven lines, or two lines if no \n.
125# We don't shrink back to 1, because that causes the control to ignore [enter], and it may
126# be unclear to the user that shift-enter is needed.
127prompt_txt.change(lambda tb: gr.update(lines=7) if ("\n" in tb) else gr.update(lines=2), inputs=[prompt_txt], outputs=[prompt_txt], show_progress=False)
128return [checkbox_iterate, checkbox_iterate_batch, prompt_position, prompt_txt]
129
130def run(self, p, checkbox_iterate, checkbox_iterate_batch, prompt_position, prompt_txt: str):
131lines = [x for x in (x.strip() for x in prompt_txt.splitlines()) if x]
132
133p.do_not_save_grid = True
134
135job_count = 0
136jobs = []
137
138for line in lines:
139if "--" in line:
140try:
141args = cmdargs(line)
142except Exception:
143errors.report(f"Error parsing line {line} as commandline", exc_info=True)
144args = {"prompt": line}
145else:
146args = {"prompt": line}
147
148job_count += args.get("n_iter", p.n_iter)
149
150jobs.append(args)
151
152print(f"Will process {len(lines)} lines in {job_count} jobs.")
153if (checkbox_iterate or checkbox_iterate_batch) and p.seed == -1:
154p.seed = int(random.randrange(4294967294))
155
156state.job_count = job_count
157
158images = []
159all_prompts = []
160infotexts = []
161for args in jobs:
162state.job = f"{state.job_no + 1} out of {state.job_count}"
163
164copy_p = copy.copy(p)
165for k, v in args.items():
166if k == "sd_model":
167copy_p.override_settings['sd_model_checkpoint'] = v
168else:
169setattr(copy_p, k, v)
170
171if args.get("prompt") and p.prompt:
172if prompt_position == "start":
173copy_p.prompt = args.get("prompt") + " " + p.prompt
174else:
175copy_p.prompt = p.prompt + " " + args.get("prompt")
176
177if args.get("negative_prompt") and p.negative_prompt:
178if prompt_position == "start":
179copy_p.negative_prompt = args.get("negative_prompt") + " " + p.negative_prompt
180else:
181copy_p.negative_prompt = p.negative_prompt + " " + args.get("negative_prompt")
182
183proc = process_images(copy_p)
184images += proc.images
185
186if checkbox_iterate:
187p.seed = p.seed + (p.batch_size * p.n_iter)
188all_prompts += proc.all_prompts
189infotexts += proc.infotexts
190
191return Processed(p, images, p.seed, "", all_prompts=all_prompts, infotexts=infotexts)
192