prompts_from_file.py 6.4 KB

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