Skip to content

Commit

Permalink
Final REST API Fixes (#1590)
Browse files Browse the repository at this point in the history
* fixed outpaint api and added tests

* fixed text2img api

* more elegant generator to subscriptable conversion

* final fixes
  • Loading branch information
AyaanShah2204 authored Jun 23, 2023
1 parent 726d73d commit 8cdb384
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 8 deletions.
5 changes: 2 additions & 3 deletions apps/stable_diffusion/web/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def launch_app(address):
img2img_api,
upscaler_api,
inpaint_api,
outpaint_api,
)
from fastapi import FastAPI, APIRouter
import uvicorn
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion apps/stable_diffusion/web/ui/img2img_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
6 changes: 5 additions & 1 deletion apps/stable_diffusion/web/ui/outpaint_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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": {},
Expand Down
4 changes: 4 additions & 0 deletions apps/stable_diffusion/web/ui/txt2img_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down
2 changes: 1 addition & 1 deletion apps/stable_diffusion/web/ui/upscaler_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
87 changes: 85 additions & 2 deletions rest_api_tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

0 comments on commit 8cdb384

Please sign in to comment.