upload handler
Browse files- handler.py +24 -31
handler.py
CHANGED
|
@@ -1,39 +1,32 @@
|
|
| 1 |
-
from typing import
|
| 2 |
import torch
|
| 3 |
from torch import autocast
|
| 4 |
-
from diffusers import
|
| 5 |
import base64
|
| 6 |
from io import BytesIO
|
| 7 |
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
class EndpointHandler():
|
| 13 |
-
def __init__(self, path=""):
|
| 14 |
-
# load the optimized model
|
| 15 |
-
self.pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
|
| 16 |
-
self.pipe = self.pipe.to(device)
|
| 17 |
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# postprocess the prediction
|
| 39 |
-
return {"image": img_str}
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
import torch
|
| 3 |
from torch import autocast
|
| 4 |
+
from diffusers import StableDiffusionAdapterPipeline
|
| 5 |
import base64
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else 'cpu')
|
| 9 |
|
| 10 |
+
if device.type != "cuda":
|
| 11 |
+
raise ValueError('need to run on gpu')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
+
class EndpointHandler():
|
| 15 |
+
def __init__(self, path="") :
|
| 16 |
+
self.pipe = StableDiffusionAdapterPipeline.from_pretrained(path, torch_dtype=torch.float16)
|
| 17 |
+
self.pipe = self.pipe.to(device)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def __call__(self, data:Any) -> List[List[Dict[str, float]]]:
|
| 21 |
+
inputs = data.pop("inputs", data)
|
| 22 |
+
|
| 23 |
+
with autocast(device.type):
|
| 24 |
+
image = self.pipe(inputs, guidance_scale=7.5)["samples"][0]
|
| 25 |
+
|
| 26 |
+
buffered = BytesIO()
|
| 27 |
+
image.save(buffered, format="JPEG")
|
| 28 |
+
img_str = base64.base64encode(buffered.getvalue())
|
| 29 |
+
|
| 30 |
+
return { "image" : img_str.decode()}
|
| 31 |
+
|
| 32 |
+
|
|
|
|
|
|