diff --git a/apps/stable_diffusion/web/index.py b/apps/stable_diffusion/web/index.py index a0cbd59a62..004a052e5f 100644 --- a/apps/stable_diffusion/web/index.py +++ b/apps/stable_diffusion/web/index.py @@ -44,6 +44,7 @@ def launch_app(address): img2img_api, upscaler_api, inpaint_api, + outpaint_api, ) from fastapi import FastAPI, APIRouter import uvicorn @@ -55,9 +56,7 @@ def launch_app(address): app.add_api_route("/sdapi/v1/txt2img", txt2img_api, methods=["post"]) app.add_api_route("/sdapi/v1/img2img", img2img_api, methods=["post"]) app.add_api_route("/sdapi/v1/inpaint", inpaint_api, methods=["post"]) - # app.add_api_route( - # "/sdapi/v1/outpaint", outpaint_api, methods=["post"] - # ) + app.add_api_route("/sdapi/v1/outpaint", outpaint_api, methods=["post"]) app.add_api_route("/sdapi/v1/upscaler", upscaler_api, methods=["post"]) app.include_router(APIRouter()) uvicorn.run(app, host="127.0.0.1", port=args.server_port) diff --git a/apps/stable_diffusion/web/ui/img2img_ui.py b/apps/stable_diffusion/web/ui/img2img_ui.py index 7df49bddec..6ed965cece 100644 --- a/apps/stable_diffusion/web/ui/img2img_ui.py +++ b/apps/stable_diffusion/web/ui/img2img_ui.py @@ -342,7 +342,7 @@ def img2img_api( ) # Converts generator type to subscriptable - res = list(res)[0] + res = next(res) return { "images": encode_pil_to_base64(res[0]), diff --git a/apps/stable_diffusion/web/ui/outpaint_ui.py b/apps/stable_diffusion/web/ui/outpaint_ui.py index 0401275f2d..d6b0d2b317 100644 --- a/apps/stable_diffusion/web/ui/outpaint_ui.py +++ b/apps/stable_diffusion/web/ui/outpaint_ui.py @@ -287,7 +287,7 @@ def outpaint_api( custom_model="None", hf_model_id=InputData["hf_model_id"] if "hf_model_id" in InputData.keys() - else "stabilityai/stable-diffusion-2-1-base", + else "stabilityai/stable-diffusion-2-inpainting", custom_vae="None", precision="fp16", device=available_devices[0], @@ -298,6 +298,10 @@ def outpaint_api( lora_hf_id="", ondemand=False, ) + + # Convert Generator to Subscriptable + res = next(res) + return { "images": encode_pil_to_base64(res[0]), "parameters": {}, diff --git a/apps/stable_diffusion/web/ui/txt2img_ui.py b/apps/stable_diffusion/web/ui/txt2img_ui.py index 14887aa145..6e9ff38860 100644 --- a/apps/stable_diffusion/web/ui/txt2img_ui.py +++ b/apps/stable_diffusion/web/ui/txt2img_ui.py @@ -265,6 +265,10 @@ def txt2img_api( lora_hf_id="", ondemand=False, ) + + # Convert Generator to Subscriptable + res = next(res) + return { "images": encode_pil_to_base64(res[0]), "parameters": {}, diff --git a/apps/stable_diffusion/web/ui/upscaler_ui.py b/apps/stable_diffusion/web/ui/upscaler_ui.py index 902af4feb8..7a4b6469c1 100644 --- a/apps/stable_diffusion/web/ui/upscaler_ui.py +++ b/apps/stable_diffusion/web/ui/upscaler_ui.py @@ -300,7 +300,7 @@ def upscaler_api( ondemand=False, ) # Converts generator type to subscriptable - res = list(res)[0] + res = next(res) return { "images": encode_pil_to_base64(res[0]), diff --git a/rest_api_tests/api_test.py b/rest_api_tests/api_test.py index 2265e06d26..365dc51dcf 100644 --- a/rest_api_tests/api_test.py +++ b/rest_api_tests/api_test.py @@ -89,10 +89,9 @@ def img2img_test(): print(f"response from server was : {res.status_code}") - print("Extracting response object") - # NOTE Uncomment below to save the picture + # print("Extracting response object") # response_obj = res.json() # img_b64 = response_obj.get("images", [False])[0] or response_obj.get( # "image" @@ -154,7 +153,91 @@ def inpainting_test(): print(f"[Inpainting] response from server was : {res.status_code}") +def outpainting_test(): + prompt = "Paint a rabbit riding on the dog" + negative_prompt = "ugly, bad art, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, watermark, grainy, tiling, signature, cut off, draft" + seed = 2121991605 + height = 512 + width = 512 + steps = 50 + cfg_scale = 7 + color_variation = 0.2 + noise_q = 0.2 + directions = ["up", "down", "right", "left"] + pixels = 32 + mask_blur = 64 + image_path = r"./rest_api_tests/dog.png" + + # Converting Image to Base64 + img_file = open(image_path, "rb") + init_images = [ + "data:image/png;base64," + base64.b64encode(img_file.read()).decode() + ] + + url = "http://127.0.0.1:8080/sdapi/v1/outpaint" + + headers = { + "User-Agent": "PythonTest", + "Accept": "*/*", + "Accept-Encoding": "gzip, deflate, br", + } + + data = { + "prompt": prompt, + "negative_prompt": negative_prompt, + "seed": seed, + "height": height, + "width": width, + "steps": steps, + "cfg_scale": cfg_scale, + "color_variation": color_variation, + "noise_q": noise_q, + "directions": directions, + "pixels": pixels, + "mask_blur": mask_blur, + "init_images": init_images, + } + + res = requests.post(url=url, json=data, headers=headers, timeout=1000) + + print(f"[Outpaint] response from server was : {res.status_code}") + + +def txt2img_test(): + prompt = "Paint a rabbit in a top hate" + negative_prompt = "ugly, bad art, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, watermark, grainy, tiling, signature, cut off, draft" + seed = 2121991605 + height = 512 + width = 512 + steps = 50 + cfg_scale = 7 + + url = "http://127.0.0.1:8080/sdapi/v1/txt2img" + + headers = { + "User-Agent": "PythonTest", + "Accept": "*/*", + "Accept-Encoding": "gzip, deflate, br", + } + + data = { + "prompt": prompt, + "negative_prompt": negative_prompt, + "seed": seed, + "height": height, + "width": width, + "steps": steps, + "cfg_scale": cfg_scale, + } + + res = requests.post(url=url, json=data, headers=headers, timeout=1000) + + print(f"[txt2img] response from server was : {res.status_code}") + + if __name__ == "__main__": + txt2img_test() img2img_test() upscaler_test() inpainting_test() + outpainting_test()