remove some code duplication

remove calls to locals()
add a test for img2img with script
This commit is contained in:
AUTOMATIC 2023-01-08 16:14:38 +03:00
parent e7f2f1e1b6
commit 137ce534b2
2 changed files with 22 additions and 17 deletions

View File

@ -148,14 +148,20 @@ class Api:
raise HTTPException(status_code=401, detail="Incorrect username or password", headers={"WWW-Authenticate": "Basic"})
def text2imgapi(self, txt2imgreq: StableDiffusionTxt2ImgProcessingAPI):
if txt2imgreq.script_name is not None:
if scripts.scripts_txt2img.scripts == []:
scripts.scripts_txt2img.initialize_scripts(False)
ui.create_ui()
def get_script(self, script_name, script_runner):
if script_name is None:
return None, None
script_idx = script_name_to_index(txt2imgreq.script_name, scripts.scripts_txt2img.selectable_scripts)
script = scripts.scripts_txt2img.selectable_scripts[script_idx]
if not script_runner.scripts:
script_runner.initialize_scripts(False)
ui.create_ui()
script_idx = script_name_to_index(script_name, script_runner.selectable_scripts)
script = script_runner.selectable_scripts[script_idx]
return script, script_idx
def text2imgapi(self, txt2imgreq: StableDiffusionTxt2ImgProcessingAPI):
script, script_idx = self.get_script(txt2imgreq.script_name, scripts.scripts_txt2img)
populate = txt2imgreq.copy(update={ # Override __init__ params
"sampler_name": validate_sampler_name(txt2imgreq.sampler_name or txt2imgreq.sampler_index),
@ -173,7 +179,7 @@ class Api:
p = StableDiffusionProcessingTxt2Img(sd_model=shared.sd_model, **args)
shared.state.begin()
if 'script' in locals():
if script is not None:
p.outpath_grids = opts.outdir_txt2img_grids
p.outpath_samples = opts.outdir_txt2img_samples
p.script_args = [script_idx + 1] + [None] * (script.args_from - 1) + p.script_args
@ -182,7 +188,6 @@ class Api:
processed = process_images(p)
shared.state.end()
b64images = list(map(encode_pil_to_base64, processed.images))
return TextToImageResponse(images=b64images, parameters=vars(txt2imgreq), info=processed.js())
@ -192,13 +197,7 @@ class Api:
if init_images is None:
raise HTTPException(status_code=404, detail="Init image not found")
if img2imgreq.script_name is not None:
if scripts.scripts_img2img.scripts == []:
scripts.scripts_img2img.initialize_scripts(True)
ui.create_ui()
script_idx = script_name_to_index(img2imgreq.script_name, scripts.scripts_img2img.selectable_scripts)
script = scripts.scripts_img2img.selectable_scripts[script_idx]
script, script_idx = self.get_script(img2imgreq.script_name, scripts.scripts_img2img)
mask = img2imgreq.mask
if mask:
@ -223,7 +222,7 @@ class Api:
p.init_images = [decode_base64_to_image(x) for x in init_images]
shared.state.begin()
if 'script' in locals():
if script is not None:
p.outpath_grids = opts.outdir_img2img_grids
p.outpath_samples = opts.outdir_img2img_samples
p.script_args = [script_idx + 1] + [None] * (script.args_from - 1) + p.script_args

View File

@ -50,6 +50,12 @@ class TestImg2ImgWorking(unittest.TestCase):
self.simple_img2img["mask"] = encode_pil_to_base64(Image.open(r"test/test_files/mask_basic.png"))
self.assertEqual(requests.post(self.url_img2img, json=self.simple_img2img).status_code, 200)
def test_img2img_sd_upscale_performed(self):
self.simple_img2img["script_name"] = "sd upscale"
self.simple_img2img["script_args"] = ["", 8, "Lanczos", 2.0]
self.assertEqual(requests.post(self.url_img2img, json=self.simple_img2img).status_code, 200)
if __name__ == "__main__":
unittest.main()