asoderznik
commited on
Commit
•
22d7300
1
Parent(s):
be8f6aa
Update handler.py
Browse files- handler.py +12 -4
handler.py
CHANGED
@@ -23,9 +23,9 @@ class EndpointHandler():
|
|
23 |
def __init__(self, path=""):
|
24 |
self.path = path
|
25 |
# load the optimized model
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
|
30 |
|
31 |
def __call__(self, data) -> List[Dict[str, Any]]:
|
@@ -39,5 +39,13 @@ class EndpointHandler():
|
|
39 |
inputs = data.get("inputs")
|
40 |
logger.info('inputs received %s', inputs)
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# postprocess the prediction
|
43 |
-
return {"image":
|
|
|
23 |
def __init__(self, path=""):
|
24 |
self.path = path
|
25 |
# load the optimized model
|
26 |
+
model_id = "stabilityai/stable-diffusion-x4-upscaler"
|
27 |
+
self.pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
|
28 |
+
self.pipe = self.pipe.to(device)
|
29 |
|
30 |
|
31 |
def __call__(self, data) -> List[Dict[str, Any]]:
|
|
|
39 |
inputs = data.get("inputs")
|
40 |
logger.info('inputs received %s', inputs)
|
41 |
|
42 |
+
decoded_image = Image.open(BytesIO(base64.b64decode(inputs['image'])))
|
43 |
+
with autocast(device.type):
|
44 |
+
upscaled_image = self.pipe(prompt="", image = decoded_image).images[0]
|
45 |
+
|
46 |
+
buffered = BytesIO()
|
47 |
+
upscaled_image.save(buffered, format="JPEG")
|
48 |
+
img_str = base64.b64encode(buffered.getvalue())
|
49 |
+
|
50 |
# postprocess the prediction
|
51 |
+
return {"image": img_str}
|