Create handler.py
Browse files- handler.py +24 -0
handler.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from image_gen_aux import UpscaleWithModel
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
|
7 |
+
class EndpointHandler():
|
8 |
+
def __init__(self, path=""):
|
9 |
+
self.upscaler = UpscaleWithModel.from_pretrained("DAT_x3.pth").to("cuda")
|
10 |
+
|
11 |
+
def __call__(self, data):
|
12 |
+
inputs = data.pop("inputs", data)
|
13 |
+
output = upscaler(inputs, tiling=True, tile_width=768, tile_height=768, overlap=8)
|
14 |
+
|
15 |
+
output = outputs.reconstruction.data.squeeze().float().cpu().clamp_(0, 1).numpy()
|
16 |
+
output = np.moveaxis(output, source=0, destination=-1)
|
17 |
+
output = (output * 255.0).round().astype(np.uint8)
|
18 |
+
|
19 |
+
img = Image.fromarray(output)
|
20 |
+
buffered = BytesIO()
|
21 |
+
img.save(buffered, format="JPEG")
|
22 |
+
img_str = base64.b64encode(buffered.getvalue())
|
23 |
+
|
24 |
+
return img_str.decode()
|