prompts_from_file.py 7.2 KB

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