txt2img.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import modules.scripts
  2. from modules import sd_samplers
  3. from modules.generation_parameters_copypaste import create_override_settings_dict
  4. from modules.processing import StableDiffusionProcessing, Processed, StableDiffusionProcessingTxt2Img, \
  5. StableDiffusionProcessingImg2Img, process_images
  6. from modules.shared import opts, cmd_opts
  7. import modules.shared as shared
  8. import modules.processing as processing
  9. from modules.ui import plaintext_to_html
  10. 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, override_settings_texts, *args):
  11. override_settings = create_override_settings_dict(override_settings_texts)
  12. p = StableDiffusionProcessingTxt2Img(
  13. sd_model=shared.sd_model,
  14. outpath_samples=opts.outdir_samples or opts.outdir_txt2img_samples,
  15. outpath_grids=opts.outdir_grids or opts.outdir_txt2img_grids,
  16. prompt=prompt,
  17. styles=prompt_styles,
  18. negative_prompt=negative_prompt,
  19. seed=seed,
  20. subseed=subseed,
  21. subseed_strength=subseed_strength,
  22. seed_resize_from_h=seed_resize_from_h,
  23. seed_resize_from_w=seed_resize_from_w,
  24. seed_enable_extras=seed_enable_extras,
  25. sampler_name=sd_samplers.samplers[sampler_index].name,
  26. batch_size=batch_size,
  27. n_iter=n_iter,
  28. steps=steps,
  29. cfg_scale=cfg_scale,
  30. width=width,
  31. height=height,
  32. restore_faces=restore_faces,
  33. tiling=tiling,
  34. enable_hr=enable_hr,
  35. denoising_strength=denoising_strength if enable_hr else None,
  36. hr_scale=hr_scale,
  37. hr_upscaler=hr_upscaler,
  38. hr_second_pass_steps=hr_second_pass_steps,
  39. hr_resize_x=hr_resize_x,
  40. hr_resize_y=hr_resize_y,
  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 = 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)