txt2img.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import modules.scripts
  2. from modules import sd_samplers, processing
  3. from modules.generation_parameters_copypaste import create_override_settings_dict
  4. from modules.shared import opts, cmd_opts
  5. import modules.shared as shared
  6. from modules.ui import plaintext_to_html
  7. def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, steps: int, sampler_index: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, enable_hr: bool, denoising_strength: float, hr_scale: float, hr_upscaler: str, hr_second_pass_steps: int, hr_resize_x: int, hr_resize_y: int, hr_sampler_index: int, hr_prompt: str, hr_negative_prompt, override_settings_texts, *args):
  8. override_settings = create_override_settings_dict(override_settings_texts)
  9. p = processing.StableDiffusionProcessingTxt2Img(
  10. sd_model=shared.sd_model,
  11. outpath_samples=opts.outdir_samples or opts.outdir_txt2img_samples,
  12. outpath_grids=opts.outdir_grids or opts.outdir_txt2img_grids,
  13. prompt=prompt,
  14. styles=prompt_styles,
  15. negative_prompt=negative_prompt,
  16. seed=seed,
  17. subseed=subseed,
  18. subseed_strength=subseed_strength,
  19. seed_resize_from_h=seed_resize_from_h,
  20. seed_resize_from_w=seed_resize_from_w,
  21. seed_enable_extras=seed_enable_extras,
  22. sampler_name=sd_samplers.samplers[sampler_index].name,
  23. batch_size=batch_size,
  24. n_iter=n_iter,
  25. steps=steps,
  26. cfg_scale=cfg_scale,
  27. width=width,
  28. height=height,
  29. restore_faces=restore_faces,
  30. tiling=tiling,
  31. enable_hr=enable_hr,
  32. denoising_strength=denoising_strength if enable_hr else None,
  33. hr_scale=hr_scale,
  34. hr_upscaler=hr_upscaler,
  35. hr_second_pass_steps=hr_second_pass_steps,
  36. hr_resize_x=hr_resize_x,
  37. hr_resize_y=hr_resize_y,
  38. hr_sampler_name=sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name if hr_sampler_index != 0 else None,
  39. hr_prompt=hr_prompt,
  40. hr_negative_prompt=hr_negative_prompt,
  41. override_settings=override_settings,
  42. )
  43. p.scripts = modules.scripts.scripts_txt2img
  44. p.script_args = args
  45. if cmd_opts.enable_console_prompts:
  46. print(f"\ntxt2img: {prompt}", file=shared.progress_print_out)
  47. processed = modules.scripts.scripts_txt2img.run(p, *args)
  48. if processed is None:
  49. processed = processing.process_images(p)
  50. p.close()
  51. shared.total_tqdm.clear()
  52. generation_info_js = processed.js()
  53. if opts.samples_log_stdout:
  54. print(generation_info_js)
  55. if opts.do_not_show_images:
  56. processed.images = []
  57. return processed.images, generation_info_js, plaintext_to_html(processed.info), plaintext_to_html(processed.comments)