|
| 1 | +# This file is used to verify your http server acts as expected |
| 2 | +# Run it with `python3 test.py`` |
| 3 | + |
| 4 | +import requests |
| 5 | +import base64 |
| 6 | +import os |
| 7 | +import json |
| 8 | +from io import BytesIO |
| 9 | +from PIL import Image |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +TESTS = "tests" |
| 13 | +FIXTURES = TESTS + os.sep + "fixtures" |
| 14 | +OUTPUT = TESTS + os.sep + "output" |
| 15 | +Path(OUTPUT).mkdir(parents=True, exist_ok=True) |
| 16 | + |
| 17 | + |
| 18 | +def b64encode_file(filename: str): |
| 19 | + with open(os.path.join(FIXTURES, filename), "rb") as file: |
| 20 | + return base64.b64encode(file.read()) |
| 21 | + |
| 22 | + |
| 23 | +def output_path(filename: str): |
| 24 | + return os.path.join(OUTPUT, filename) |
| 25 | + |
| 26 | + |
| 27 | +def decode_and_save(image_byte_string: str, name: str): |
| 28 | + image_encoded = image_byte_string.encode("utf-8") |
| 29 | + image_bytes = BytesIO(base64.b64decode(image_encoded)) |
| 30 | + image = Image.open(image_bytes) |
| 31 | + fp = output_path(name + ".png") |
| 32 | + image.save(fp) |
| 33 | + print("Saved " + fp) |
| 34 | + |
| 35 | + |
| 36 | +def test(name, inputs): |
| 37 | + print("Running test: " + name) |
| 38 | + response = requests.post("http://localhost:8000/", json=inputs) |
| 39 | + result = response.json() |
| 40 | + |
| 41 | + if result.get("images_base64", None) == "None": |
| 42 | + print(json.dumps(result, indent=4)) |
| 43 | + print() |
| 44 | + return |
| 45 | + |
| 46 | + images_base64 = result.get("images_base64", None) |
| 47 | + if images_base64: |
| 48 | + for idx, image_byte_string in enumerate(images_base64): |
| 49 | + decode_and_save(image_byte_string, f"{name}_{idx}") |
| 50 | + else: |
| 51 | + decode_and_save(result["image_base64"], name) |
| 52 | + |
| 53 | + print() |
| 54 | + |
| 55 | + |
| 56 | +test( |
| 57 | + "outpaint", |
| 58 | + { |
| 59 | + "modelInputs": { |
| 60 | + "prompt": "girl with a pearl earing standing in a big room", |
| 61 | + "init_image": b64encode_file("girl_with_pearl_earing_outpainting_in.png"), |
| 62 | + }, |
| 63 | + "callInputs": { |
| 64 | + "MODEL_ID": "CompVis/stable-diffusion-v1-4", |
| 65 | + "PIPELINE": "StableDiffusionInpaintPipelineLegacy", |
| 66 | + "SCHEDULER": "DDIM", # Note, as of diffusers 0.3.0, no LMS yet |
| 67 | + "FILL_MODE": "patchmatch", |
| 68 | + }, |
| 69 | + }, |
| 70 | +) |
0 commit comments