txt2img.py 3.1 KB

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