@@ -11,7 +11,7 @@ class WebUIApiResult:
11
11
images : list
12
12
parameters : dict
13
13
info : dict
14
-
14
+
15
15
@property
16
16
def image (self ):
17
17
return self .images [0 ]
@@ -35,28 +35,28 @@ def __init__(self,
35
35
baseurl = f'https://{ host } :{ port } /sdapi/v1'
36
36
else :
37
37
baseurl = f'http://{ host } :{ port } /sdapi/v1'
38
-
38
+
39
39
self .baseurl = baseurl
40
40
self .default_sampler = sampler
41
41
self .default_steps = steps
42
-
42
+
43
43
self .session = requests .Session ()
44
-
44
+
45
45
def set_auth (self , username , password ):
46
46
self .session .auth = (username , password )
47
-
47
+
48
48
def _to_api_result (self , response ):
49
-
49
+
50
50
if response .status_code != 200 :
51
51
raise RuntimeError (response .status_code , response .text )
52
-
52
+
53
53
r = response .json ()
54
54
images = []
55
55
if 'images' in r .keys ():
56
56
images = [Image .open (io .BytesIO (base64 .b64decode (i ))) for i in r ['images' ]]
57
57
elif 'image' in r .keys ():
58
58
images = [Image .open (io .BytesIO (base64 .b64decode (r ['image' ])))]
59
-
59
+
60
60
info = ''
61
61
if 'info' in r .keys ():
62
62
try :
@@ -71,8 +71,8 @@ def _to_api_result(self, response):
71
71
parameters = r ['parameters' ]
72
72
73
73
return WebUIApiResult (images , parameters , info )
74
-
75
- def txt2img (self ,
74
+
75
+ def txt2img (self ,
76
76
enable_hr = False ,
77
77
denoising_strength = 0.0 ,
78
78
firstphase_width = 0 ,
@@ -99,18 +99,22 @@ def txt2img(self,
99
99
s_noise = 1 ,
100
100
override_settings = {},
101
101
override_settings_restore_afterwards = True ,
102
- sampler_name = None , # use this instead of sampler_index
102
+ sampler_name = None , # use this instead of sampler_index
103
103
sampler_index = None ,
104
104
steps = None ,
105
- ):
105
+ script_name = None ,
106
+ script_args = None # List of arguments for the script "script_name"
107
+ ):
106
108
if sampler_index is None :
107
109
sampler_index = self .default_sampler
108
110
if sampler_name is None :
109
111
sampler_name = self .default_sampler
110
112
if steps is None :
111
113
steps = self .default_steps
114
+ if script_args is None :
115
+ script_args = []
112
116
113
- payload = {
117
+ payload = {
114
118
"enable_hr" : enable_hr ,
115
119
"denoising_strength" : denoising_strength ,
116
120
"firstphase_width" : firstphase_width ,
@@ -140,14 +144,15 @@ def txt2img(self,
140
144
"override_settings_restore_afterwards" : override_settings_restore_afterwards ,
141
145
"sampler_name" : sampler_name ,
142
146
"sampler_index" : sampler_index ,
147
+ "script_name" : script_name ,
148
+ "script_args" : script_args
143
149
}
144
150
response = self .session .post (url = f'{ self .baseurl } /txt2img' , json = payload )
145
151
return self ._to_api_result (response )
146
152
147
-
148
153
def img2img (self ,
149
- images = [], # list of PIL Image
150
- mask_image = None , # PIL Image mask
154
+ images = [], # list of PIL Image
155
+ mask_image = None , # PIL Image mask
151
156
resize_mode = 0 ,
152
157
denoising_strength = 0.75 ,
153
158
mask_blur = 4 ,
@@ -180,15 +185,19 @@ def img2img(self,
180
185
override_settings_restore_afterwards = True ,
181
186
include_init_images = False ,
182
187
steps = None ,
183
- sampler_name = None , # use this instead of sampler_index
188
+ sampler_name = None , # use this instead of sampler_index
184
189
sampler_index = None ,
185
- ):
190
+ script_name = None ,
191
+ script_args = None # List of arguments for the script "script_name"
192
+ ):
186
193
if sampler_name is None :
187
194
sampler_name = self .default_sampler
188
195
if sampler_index is None :
189
196
sampler_index = self .default_sampler
190
197
if steps is None :
191
198
steps = self .default_steps
199
+ if script_args is None :
200
+ script_args = []
192
201
193
202
payload = {
194
203
"init_images" : [b64_img (x ) for x in images ],
@@ -226,15 +235,17 @@ def img2img(self,
226
235
"sampler_name" : sampler_name ,
227
236
"sampler_index" : sampler_index ,
228
237
"include_init_images" : include_init_images ,
238
+ "script_name" : script_name ,
239
+ "script_args" : script_args
229
240
}
230
241
if mask_image is not None :
231
- payload ['mask' ]= b64_img (mask_image )
232
-
242
+ payload ['mask' ] = b64_img (mask_image )
243
+
233
244
response = self .session .post (url = f'{ self .baseurl } /img2img' , json = payload )
234
245
return self ._to_api_result (response )
235
246
236
247
def extra_single_image (self ,
237
- image , # PIL Image
248
+ image , # PIL Image
238
249
resize_mode = 0 ,
239
250
show_extras_results = True ,
240
251
gfpgan_visibility = 0 ,
@@ -248,7 +259,7 @@ def extra_single_image(self,
248
259
upscaler_2 = "None" ,
249
260
extras_upscaler_2_visibility = 0 ,
250
261
upscale_first = False ,
251
- ):
262
+ ):
252
263
payload = {
253
264
"resize_mode" : resize_mode ,
254
265
"show_extras_results" : show_extras_results ,
@@ -265,13 +276,13 @@ def extra_single_image(self,
265
276
"upscale_first" : upscale_first ,
266
277
"image" : b64_img (image ),
267
278
}
268
-
279
+
269
280
response = self .session .post (url = f'{ self .baseurl } /extra-single-image' , json = payload )
270
281
return self ._to_api_result (response )
271
282
272
283
def extra_batch_images (self ,
273
- images , # list of PIL images
274
- name_list = None , # list of image names
284
+ images , # list of PIL images
285
+ name_list = None , # list of image names
275
286
resize_mode = 0 ,
276
287
show_extras_results = True ,
277
288
gfpgan_visibility = 0 ,
@@ -285,21 +296,21 @@ def extra_batch_images(self,
285
296
upscaler_2 = "None" ,
286
297
extras_upscaler_2_visibility = 0 ,
287
298
upscale_first = False ,
288
- ):
299
+ ):
289
300
if name_list is not None :
290
301
if len (name_list ) != len (images ):
291
302
raise RuntimeError ('len(images) != len(name_list)' )
292
303
else :
293
- name_list = [f'image{ i + 1 :05} ' for i in range (len (images ))]
304
+ name_list = [f'image{ i + 1 :05} ' for i in range (len (images ))]
294
305
images = [b64_img (x ) for x in images ]
295
-
306
+
296
307
image_list = []
297
308
for name , image in zip (name_list , images ):
298
309
image_list .append ({
299
310
"data" : image ,
300
311
"name" : name
301
312
})
302
-
313
+
303
314
payload = {
304
315
"resize_mode" : resize_mode ,
305
316
"show_extras_results" : show_extras_results ,
@@ -316,16 +327,16 @@ def extra_batch_images(self,
316
327
"upscale_first" : upscale_first ,
317
328
"imageList" : image_list ,
318
329
}
319
-
330
+
320
331
response = self .session .post (url = f'{ self .baseurl } /extra-batch-images' , json = payload )
321
332
return self ._to_api_result (response )
322
-
333
+
323
334
# XXX 500 error (2022/12/26)
324
335
def png_info (self , image ):
325
336
payload = {
326
337
"image" : b64_img (image ),
327
338
}
328
-
339
+
329
340
response = self .session .post (url = f'{ self .baseurl } /png-info' , json = payload )
330
341
return self ._to_api_result (response )
331
342
@@ -334,20 +345,20 @@ def interrogate(self, image):
334
345
payload = {
335
346
"image" : b64_img (image ),
336
347
}
337
-
348
+
338
349
response = self .session .post (url = f'{ self .baseurl } /interrogate' , json = payload )
339
350
return self ._to_api_result (response )
340
351
341
- def get_options (self ):
352
+ def get_options (self ):
342
353
response = self .session .get (url = f'{ self .baseurl } /options' )
343
354
return response .json ()
344
355
345
356
# working (2022/11/21)
346
- def set_options (self , options ):
357
+ def set_options (self , options ):
347
358
response = self .session .post (url = f'{ self .baseurl } /options' , json = options )
348
359
return response .json ()
349
360
350
- def get_cmd_flags (self ):
361
+ def get_cmd_flags (self ):
351
362
response = self .session .get (url = f'{ self .baseurl } /cmd-flags' )
352
363
return response .json ()
353
364
def get_samplers (self ):
@@ -380,7 +391,7 @@ def get_artists(self):
380
391
def refresh_checkpoints (self ):
381
392
response = self .session .post (url = f'{ self .baseurl } /refresh-checkpoints' )
382
393
return response .json ()
383
-
394
+
384
395
def get_endpoint (self , endpoint , baseurl ):
385
396
if baseurl :
386
397
return f'{ self .baseurl } /{ endpoint } '
@@ -431,7 +442,7 @@ def util_get_current_model(self):
431
442
return self .get_options ()['sd_model_checkpoint' ]
432
443
433
444
434
- class Upscaler (str , Enum ):
445
+ class Upscaler (str , Enum ):
435
446
none = 'None'
436
447
Lanczos = 'Lanczos'
437
448
Nearest = 'Nearest'
0 commit comments