Add full diagnostic prints for debugging
Browse files- handler.py +88 -35
handler.py
CHANGED
|
@@ -10,7 +10,9 @@ from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
| 10 |
|
| 11 |
class EndpointHandler:
|
| 12 |
def __init__(self, path="."):
|
| 13 |
-
print("
|
|
|
|
|
|
|
| 14 |
|
| 15 |
self.model_url = (
|
| 16 |
"https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/"
|
|
@@ -19,13 +21,16 @@ class EndpointHandler:
|
|
| 19 |
self.model_path = os.path.join(path, "RealESRGAN_x4plus.pth")
|
| 20 |
|
| 21 |
if not os.path.exists(self.model_path):
|
| 22 |
-
print(f"๐ฅ
|
| 23 |
r = requests.get(self.model_url)
|
| 24 |
r.raise_for_status()
|
| 25 |
with open(self.model_path, "wb") as f:
|
| 26 |
f.write(r.content)
|
| 27 |
-
print(f"โ
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
model = RRDBNet(
|
| 30 |
num_in_ch=3,
|
| 31 |
num_out_ch=3,
|
|
@@ -35,69 +40,117 @@ class EndpointHandler:
|
|
| 35 |
scale=4,
|
| 36 |
)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
self.upsampler = RealESRGANer(
|
| 39 |
scale=4,
|
| 40 |
model_path=self.model_path,
|
| 41 |
model=model,
|
| 42 |
half=False,
|
| 43 |
-
device=
|
| 44 |
)
|
| 45 |
|
| 46 |
-
print("โ
Real-ESRGAN model initialized and ready.")
|
| 47 |
|
| 48 |
# ==========================================================
|
| 49 |
-
#
|
| 50 |
# ==========================================================
|
| 51 |
def __call__(self, data):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
try:
|
|
|
|
| 53 |
image = self.preprocess(data)
|
|
|
|
|
|
|
|
|
|
| 54 |
output = self.inference(image)
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
-
print("
|
| 58 |
return {"error": str(e)}
|
| 59 |
|
| 60 |
# ==========================================================
|
| 61 |
-
#
|
| 62 |
# ==========================================================
|
| 63 |
def preprocess(self, data):
|
| 64 |
-
"
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if isinstance(data, (bytes, bytearray)):
|
|
|
|
|
|
|
| 67 |
return Image.open(io.BytesIO(data)).convert("RGB")
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
if isinstance(data,
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
if isinstance(
|
| 89 |
-
return Image.open(io.BytesIO(
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
def inference(self, image):
|
|
|
|
|
|
|
| 94 |
output, _ = self.upsampler.enhance(image, outscale=4)
|
|
|
|
| 95 |
return output
|
| 96 |
|
|
|
|
|
|
|
|
|
|
| 97 |
def postprocess(self, output_image):
|
|
|
|
| 98 |
buf = io.BytesIO()
|
| 99 |
output_image.save(buf, format="PNG")
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
buf.close()
|
| 102 |
return {"image": encoded}
|
| 103 |
|
|
|
|
| 10 |
|
| 11 |
class EndpointHandler:
|
| 12 |
def __init__(self, path="."):
|
| 13 |
+
print("๐ [INIT] Starting EndpointHandler initialization...")
|
| 14 |
+
print(f"๐ Working directory: {os.getcwd()}")
|
| 15 |
+
print(f"๐ Model path root: {path}")
|
| 16 |
|
| 17 |
self.model_url = (
|
| 18 |
"https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/"
|
|
|
|
| 21 |
self.model_path = os.path.join(path, "RealESRGAN_x4plus.pth")
|
| 22 |
|
| 23 |
if not os.path.exists(self.model_path):
|
| 24 |
+
print(f"๐ฅ [DOWNLOAD] Fetching model weights from {self.model_url}")
|
| 25 |
r = requests.get(self.model_url)
|
| 26 |
r.raise_for_status()
|
| 27 |
with open(self.model_path, "wb") as f:
|
| 28 |
f.write(r.content)
|
| 29 |
+
print(f"โ
[DOWNLOAD] Saved model to {self.model_path}")
|
| 30 |
+
else:
|
| 31 |
+
print(f"โ
[CACHE] Model already exists at {self.model_path}")
|
| 32 |
|
| 33 |
+
print("๐ง [MODEL] Building RRDBNet...")
|
| 34 |
model = RRDBNet(
|
| 35 |
num_in_ch=3,
|
| 36 |
num_out_ch=3,
|
|
|
|
| 40 |
scale=4,
|
| 41 |
)
|
| 42 |
|
| 43 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 44 |
+
print(f"๐ป [DEVICE] Using device: {device}")
|
| 45 |
+
|
| 46 |
self.upsampler = RealESRGANer(
|
| 47 |
scale=4,
|
| 48 |
model_path=self.model_path,
|
| 49 |
model=model,
|
| 50 |
half=False,
|
| 51 |
+
device=device,
|
| 52 |
)
|
| 53 |
|
| 54 |
+
print("โ
[INIT DONE] Real-ESRGAN model initialized and ready.\n\n")
|
| 55 |
|
| 56 |
# ==========================================================
|
| 57 |
+
# MAIN CALLABLE
|
| 58 |
# ==========================================================
|
| 59 |
def __call__(self, data):
|
| 60 |
+
print("๐ฐ๏ธ [CALL] Endpoint invoked!")
|
| 61 |
+
print(f"๐ฆ [CALL] Raw data type: {type(data)}")
|
| 62 |
+
print(f"๐ [CALL] Data preview: {str(data)[:300]}...")
|
| 63 |
+
|
| 64 |
try:
|
| 65 |
+
print("โก๏ธ [STEP] Preprocessing input...")
|
| 66 |
image = self.preprocess(data)
|
| 67 |
+
print("โ
[STEP] Preprocessing complete!")
|
| 68 |
+
|
| 69 |
+
print("โก๏ธ [STEP] Running inference...")
|
| 70 |
output = self.inference(image)
|
| 71 |
+
print("โ
[STEP] Inference complete!")
|
| 72 |
+
|
| 73 |
+
print("โก๏ธ [STEP] Encoding output image...")
|
| 74 |
+
result = self.postprocess(output)
|
| 75 |
+
print("โ
[STEP] Postprocessing complete!")
|
| 76 |
+
|
| 77 |
+
return result
|
| 78 |
except Exception as e:
|
| 79 |
+
print("๐ฅ [ERROR] Exception during inference:", str(e))
|
| 80 |
return {"error": str(e)}
|
| 81 |
|
| 82 |
# ==========================================================
|
| 83 |
+
# PREPROCESS
|
| 84 |
# ==========================================================
|
| 85 |
def preprocess(self, data):
|
| 86 |
+
print(f"๐ง [PREPROCESS] Type received: {type(data)}")
|
| 87 |
+
|
| 88 |
+
# 1๏ธโฃ Hugging Face JSON-wrapped dict {"inputs": ...}
|
| 89 |
+
if isinstance(data, dict):
|
| 90 |
+
print("๐งฉ [PREPROCESS] Detected dict input.")
|
| 91 |
+
if "inputs" in data:
|
| 92 |
+
print("๐จ [PREPROCESS] Found 'inputs' key in dict.")
|
| 93 |
+
data = data["inputs"]
|
| 94 |
+
elif "image" in data:
|
| 95 |
+
print("๐จ [PREPROCESS] Found 'image' key in dict.")
|
| 96 |
+
data = data["image"]
|
| 97 |
+
else:
|
| 98 |
+
print("โ ๏ธ [PREPROCESS] Dict has no 'inputs' or 'image' keys.")
|
| 99 |
+
|
| 100 |
+
# 2๏ธโฃ Bytes
|
| 101 |
if isinstance(data, (bytes, bytearray)):
|
| 102 |
+
print("๐ผ๏ธ [PREPROCESS] Treating input as raw bytes.")
|
| 103 |
+
print(f"๐งฎ [BYTES] Length: {len(data)}")
|
| 104 |
return Image.open(io.BytesIO(data)).convert("RGB")
|
| 105 |
|
| 106 |
+
# 3๏ธโฃ Base64 string
|
| 107 |
+
if isinstance(data, str):
|
| 108 |
+
print("๐งพ [PREPROCESS] Treating input as base64 string.")
|
| 109 |
+
print(f"๐งฎ [BASE64] Length: {len(data)}")
|
| 110 |
+
try:
|
| 111 |
+
decoded = base64.b64decode(data)
|
| 112 |
+
print(f"โ
[BASE64] Successfully decoded base64 -> {len(decoded)} bytes.")
|
| 113 |
+
return Image.open(io.BytesIO(decoded)).convert("RGB")
|
| 114 |
+
except Exception as e:
|
| 115 |
+
print("๐ฅ [BASE64] Decode failed:", str(e))
|
| 116 |
+
raise ValueError("Invalid base64 string in request body.")
|
| 117 |
+
|
| 118 |
+
# 4๏ธโฃ List of inputs (some HF payloads wrap inside a list)
|
| 119 |
+
if isinstance(data, list):
|
| 120 |
+
print(f"๐ [PREPROCESS] List input detected with length {len(data)}")
|
| 121 |
+
if len(data) == 0:
|
| 122 |
+
raise ValueError("Empty input list.")
|
| 123 |
+
first = data[0]
|
| 124 |
+
print(f"๐น [PREPROCESS] First element type: {type(first)}")
|
| 125 |
+
if isinstance(first, (bytes, bytearray)):
|
| 126 |
+
return Image.open(io.BytesIO(first)).convert("RGB")
|
| 127 |
+
if isinstance(first, str):
|
| 128 |
+
decoded = base64.b64decode(first)
|
| 129 |
+
return Image.open(io.BytesIO(decoded)).convert("RGB")
|
| 130 |
|
| 131 |
+
raise ValueError("Unsupported input type. Expected image bytes or base64 data.")
|
| 132 |
+
|
| 133 |
+
# ==========================================================
|
| 134 |
+
# INFERENCE
|
| 135 |
+
# ==========================================================
|
| 136 |
def inference(self, image):
|
| 137 |
+
print("๐ฏ [INFERENCE] Running ESRGAN upscaling...")
|
| 138 |
+
print(f"๐ [INFERENCE] Input image size: {image.size}")
|
| 139 |
output, _ = self.upsampler.enhance(image, outscale=4)
|
| 140 |
+
print(f"โ
[INFERENCE] Output image size: {output.size}")
|
| 141 |
return output
|
| 142 |
|
| 143 |
+
# ==========================================================
|
| 144 |
+
# POSTPROCESS
|
| 145 |
+
# ==========================================================
|
| 146 |
def postprocess(self, output_image):
|
| 147 |
+
print("๐ค [POSTPROCESS] Encoding image to base64...")
|
| 148 |
buf = io.BytesIO()
|
| 149 |
output_image.save(buf, format="PNG")
|
| 150 |
+
raw_bytes = buf.getvalue()
|
| 151 |
+
print(f"๐ [POSTPROCESS] Output image byte size: {len(raw_bytes)}")
|
| 152 |
+
encoded = base64.b64encode(raw_bytes).decode("utf-8")
|
| 153 |
+
print(f"โ
[POSTPROCESS] Base64 encoded length: {len(encoded)}")
|
| 154 |
buf.close()
|
| 155 |
return {"image": encoded}
|
| 156 |
|