File size: 5,787 Bytes
4ed9f59 283bed4 4ed9f59 2ef631e 4ed9f59 af61ad1 283bed4 abb6db1 4d4df77 abb6db1 b6af06b abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 797b969 4d4df77 abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 4d4df77 abb6db1 b6af06b 4d4df77 797b969 4d4df77 797b969 2ef631e 797b969 b6af06b 797b969 b6af06b 4d4df77 797b969 4d4df77 abb6db1 4d4df77 2ef631e 283bed4 4d4df77 abb6db1 4d4df77 b6af06b 4d4df77 797b969 4d4df77 797b969 b6af06b abb6db1 af61ad1 |
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import os
import io
import torch
import base64
import requests
import numpy as np
from PIL import Image
from realesrgan import RealESRGANer
from basicsr.archs.rrdbnet_arch import RRDBNet
class EndpointHandler:
def __init__(self, path="."):
print("π [INIT] Starting EndpointHandler initialization...")
print(f"π Working directory: {os.getcwd()}")
print(f"π Model path root: {path}")
self.model_url = (
"https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/"
"RealESRGAN_x4plus.pth"
)
self.model_path = os.path.join(path, "RealESRGAN_x4plus.pth")
if not os.path.exists(self.model_path):
print(f"π₯ [DOWNLOAD] Fetching model weights from {self.model_url}")
r = requests.get(self.model_url)
r.raise_for_status()
with open(self.model_path, "wb") as f:
f.write(r.content)
print(f"β
[DOWNLOAD] Saved model to {self.model_path}")
else:
print(f"β
[CACHE] Model already exists at {self.model_path}")
print("π§ [MODEL] Building RRDBNet...")
model = RRDBNet(
num_in_ch=3,
num_out_ch=3,
num_feat=64,
num_block=23,
num_grow_ch=32,
scale=4,
)
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"π» [DEVICE] Using device: {device}")
self.upsampler = RealESRGANer(
scale=4,
model_path=self.model_path,
model=model,
half=False,
device=device,
)
print("β
[INIT DONE] Real-ESRGAN model initialized and ready.\n\n")
# ==========================================================
# MAIN CALLABLE
# ==========================================================
def __call__(self, data):
print("π°οΈ [CALL] Endpoint invoked!")
print(f"π¦ [CALL] Raw data type: {type(data)}")
print(f"π [CALL] Data preview: {str(data)[:300]}...")
try:
print("β‘οΈ [STEP] Preprocessing input...")
image = self.preprocess(data)
print(f"β
[STEP] Preprocessing complete! Image size: {image.size}")
print("β‘οΈ [STEP] Running inference...")
output = self.inference(image)
print("β
[STEP] Inference complete!")
print("β‘οΈ [STEP] Encoding output image...")
result = self.postprocess(output)
print("β
[STEP] Postprocessing complete!")
return result
except Exception as e:
print("π₯ [ERROR] Exception during inference:", str(e))
return {"error": str(e)}
# ==========================================================
# PREPROCESS
# ==========================================================
def preprocess(self, data):
print(f"π§ [PREPROCESS] Type received: {type(data)}")
if isinstance(data, dict):
print("π§© [PREPROCESS] Detected dict input.")
if "inputs" in data:
data = data["inputs"]
print(f"π¨ [PREPROCESS] Found 'inputs' key: {type(data)}")
if isinstance(data, Image.Image):
print("πΌοΈ [PREPROCESS] Got PIL.Image.Image directly.")
return data.convert("RGB")
if isinstance(data, (bytes, bytearray)):
print("π§Ύ [PREPROCESS] Treating input as raw bytes.")
return Image.open(io.BytesIO(data)).convert("RGB")
if isinstance(data, str):
print(f"π§Ύ [PREPROCESS] Treating input as base64 string, len={len(data)}")
decoded = base64.b64decode(data)
return Image.open(io.BytesIO(decoded)).convert("RGB")
if isinstance(data, list) and len(data) > 0:
item = data[0]
if isinstance(item, Image.Image):
return item.convert("RGB")
if isinstance(item, (bytes, bytearray)):
return Image.open(io.BytesIO(item)).convert("RGB")
if isinstance(item, str):
return Image.open(io.BytesIO(base64.b64decode(item))).convert("RGB")
raise ValueError("Unsupported input type. Expected image, bytes, or base64 data.")
# ==========================================================
# INFERENCE
# ==========================================================
def inference(self, image):
print("π― [INFERENCE] Running ESRGAN upscaling...")
print(f"π [INFERENCE] Input image size: {image.size}")
# Convert PIL -> NumPy BGR for RealESRGAN
img_np = np.array(image)[:, :, ::-1] # RGB -> BGR
print(f"π [INFERENCE] Converted to NumPy: shape={img_np.shape}, dtype={img_np.dtype}")
output, _ = self.upsampler.enhance(img_np, outscale=4)
print(f"β
[INFERENCE] Output NumPy shape: {output.shape}")
# Convert back to PIL RGB
output_rgb = Image.fromarray(output[:, :, ::-1])
print(f"β
[INFERENCE] Converted back to PIL: size={output_rgb.size}")
return output_rgb
# ==========================================================
# POSTPROCESS
# ==========================================================
def postprocess(self, output_image):
print("π€ [POSTPROCESS] Encoding image to base64...")
buf = io.BytesIO()
output_image.save(buf, format="PNG")
raw_bytes = buf.getvalue()
print(f"π [POSTPROCESS] Output byte size: {len(raw_bytes)}")
encoded = base64.b64encode(raw_bytes).decode("utf-8")
print(f"β
[POSTPROCESS] Encoded base64 length: {len(encoded)}")
buf.close()
return {"image": encoded}
|