1
+ import inspect
1
2
import json
2
3
from contextlib import closing
3
4
11
12
import gradio as gr
12
13
13
14
14
- def txt2img_create_processing (id_task : str , request : gr .Request , prompt : str , negative_prompt : str , prompt_styles , steps : int , sampler_name : str , n_iter : int , batch_size : int , cfg_scale : float , 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_checkpoint_name : str , hr_sampler_name : str , hr_prompt : str , hr_negative_prompt , override_settings_texts , * args , force_enable_hr = False ):
15
+ def txt2img_create_processing (id_task : str , request : gr .Request , prompt : str , negative_prompt : str , prompt_styles , steps : int , sampler_name : str , n_iter : int , batch_size : int , cfg_scale : float , 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_checkpoint_name : str , hr_sampler_name : str , hr_prompt : str , hr_negative_prompt , override_settings_texts , * args , force_enable_hr = False , firstpass_image = None ):
15
16
override_settings = create_override_settings_dict (override_settings_texts )
16
17
17
18
if force_enable_hr :
@@ -43,6 +44,7 @@ def txt2img_create_processing(id_task: str, request: gr.Request, prompt: str, ne
43
44
hr_prompt = hr_prompt ,
44
45
hr_negative_prompt = hr_negative_prompt ,
45
46
override_settings = override_settings ,
47
+ firstpass_image = firstpass_image ,
46
48
)
47
49
48
50
p .scripts = modules .scripts .scripts_txt2img
@@ -60,21 +62,33 @@ def txt2img_upscale(id_task: str, request: gr.Request, gallery, gallery_index, g
60
62
assert len (gallery ) > 0 , 'No image to upscale'
61
63
assert 0 <= gallery_index < len (gallery ), f'Bad image index: { gallery_index } '
62
64
63
- p = txt2img_create_processing (id_task , request , * args )
64
- p .enable_hr = True
65
- p .batch_size = 1
66
- p .n_iter = 1
67
- p .txt2img_upscale = True
65
+ # get firstpass image from gallery
66
+ firstpass_image = infotext_utils .image_from_url_text (gallery [gallery_index ] if 0 <= gallery_index < len (gallery ) else gallery [0 ])
68
67
69
- geninfo = json .loads (generation_info )
68
+ bind_args = inspect .signature (txt2img_create_processing ).bind (id_task , request , * args , force_enable_hr = True , firstpass_image = firstpass_image )
69
+ bind_args .apply_defaults ()
70
70
71
- image_info = gallery [gallery_index ] if 0 <= gallery_index < len (gallery ) else gallery [0 ]
72
- p .firstpass_image = infotext_utils .image_from_url_text (image_info )
71
+ # set batch_size and n_iter to 1 for hr button, overrides batch_size and n_iter from ui
72
+ bind_args .arguments ['batch_size' ] = 1
73
+ bind_args .arguments ['n_iter' ] = 1
73
74
75
+ geninfo = json .loads (generation_info )
74
76
parameters = parse_generation_parameters (geninfo .get ('infotexts' )[gallery_index ], [])
75
- p .seed = parameters .get ('Seed' , - 1 )
76
- p .subseed = parameters .get ('Variation seed' , - 1 )
77
77
78
+ # bind_args.arguments['args'] is same p.script_args
79
+ # update seed script args
80
+ seed_script_args = list (modules .scripts .scripts_txt2img .get_script_args (bind_args .arguments ['args' ], 'seed' ))
81
+ for param , index in [('Seed' , 0 ), ('Variation seed' , 2 ), ('Variation seed strength' , 3 ), ('Seed resize from-1' , 4 ), ('Seed resize from-2' , 5 )]:
82
+ seed_script_args [index ] = parameters .get (param , seed_script_args [index ])
83
+ seed_script_args [1 ] = 'Variation seed' in parameters or 'Seed resize from-1' in parameters
84
+ bind_args .arguments ['args' ] = modules .scripts .scripts_txt2img .update_script_args (bind_args .arguments ['args' ], 'seed' , seed_script_args )
85
+
86
+ p = txt2img_create_processing (* bind_args .args , ** bind_args .kwargs )
87
+
88
+ # txt2img_upscale attribute that signifies this is called by txt2img_upscale
89
+ p .txt2img_upscale = True
90
+
91
+ # disable save_images_before_highres_fix as it is already saved from firstpass
78
92
p .override_settings ['save_images_before_highres_fix' ] = False
79
93
80
94
with closing (p ):
0 commit comments