Create handler.py
Browse files- handler.py +29 -0
handler.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import DiffusionPipeline
|
| 2 |
+
|
| 3 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 4 |
+
if device.type != 'cuda':
|
| 5 |
+
raise ValueError("need to run on GPU")
|
| 6 |
+
# set mixed precision dtype
|
| 7 |
+
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16
|
| 8 |
+
|
| 9 |
+
class EndpointHandler():
|
| 10 |
+
def __init__(self, path=""):
|
| 11 |
+
self.pipeline = DiffusionPipeline.from_pretrained("CompVis/ldm-super-resolution-4x-openimages", torch_dtype=dtype).to(device)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
| 15 |
+
image = data.pop("image", None)
|
| 16 |
+
low_res_img = image.resize((128, 128))
|
| 17 |
+
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
upscaled_image = self.pipeline(low_res_img, num_inference_steps=100, eta=1).images[0]
|
| 20 |
+
|
| 21 |
+
return upscaled_image
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# helper to decode input image
|
| 25 |
+
def decode_base64_image(self, image_string):
|
| 26 |
+
base64_image = base64.b64decode(image_string)
|
| 27 |
+
buffer = BytesIO(base64_image)
|
| 28 |
+
image = Image.open(buffer)
|
| 29 |
+
return image
|