File size: 1,228 Bytes
3e46ff6 95b5eda 3e46ff6 074bf5a 3e46ff6 1bc1c8f c99f459 1bc1c8f 54b0e0a 1bc1c8f 54b0e0a a8a6ee4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
from diffusers import AutoPipelineForImage2Image
import torch
from typing import Dict, Any
from PIL import Image
from io import BytesIO
import base64
class EndpointHandler():
def __init__(self, path="."):
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
self._pipe = AutoPipelineForImage2Image.from_pretrained(path, torch_dtype=torch.float16).to(device)
def __call__(self, data: Dict[str, Any]) -> list[Dict[str, Any]]:
inputs = data.pop("inputs", data)
params = {"prompt": inputs.get("prompt", ""),
"image": Image.open(BytesIO(base64.b64decode(inputs['image']))),
"strength": float(inputs.get("strength", 0.3)),
"guidance_scale": float(inputs.get("guidance_scale", 10)),
"height": 768,
"width": 768}
img: Image = self._pipe(**params).images[0]
stream = BytesIO()
img.save(stream, format="jpeg")
res = {"status": 200,
"image": base64.b64encode(stream.getvalue()).decode("utf8")
}
return res
if __name__ == "__main__":
h = EndpointHandler()
v = h({})
print(v) |