jasonod888 commited on
Commit ·
e86d966
1
Parent(s): 8d53463
Fix: remove unsupported target_properties kwarg; add previews; robust inputs
Browse files- handler.py +99 -128
handler.py
CHANGED
|
@@ -1,153 +1,124 @@
|
|
| 1 |
import base64
|
| 2 |
import io
|
|
|
|
| 3 |
import os
|
| 4 |
-
from typing import Any, Dict,
|
| 5 |
|
| 6 |
-
import numpy as np
|
| 7 |
-
from PIL import Image
|
| 8 |
import torch
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def _b64_to_pil(b64: str) -> Image.Image:
|
| 13 |
-
"""Decode base64 (optionally data URL) → PIL RGB."""
|
| 14 |
-
s = b64.strip()
|
| 15 |
-
if s.startswith("data:image"):
|
| 16 |
-
s = s.split(",", 1)[1]
|
| 17 |
-
raw = base64.b64decode(s, validate=False)
|
| 18 |
-
return Image.open(io.BytesIO(raw)).convert("RGB")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def _pil_to_b64_png(img: Image.Image) -> str:
|
| 22 |
-
"""PIL → base64 PNG (no data URL prefix)."""
|
| 23 |
-
buf = io.BytesIO()
|
| 24 |
-
img.save(buf, format="PNG")
|
| 25 |
-
return base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 26 |
|
| 27 |
|
| 28 |
class EndpointHandler:
|
| 29 |
"""
|
| 30 |
-
Hugging Face Inference
|
| 31 |
-
|
| 32 |
-
Expects request JSON:
|
| 33 |
-
{
|
| 34 |
-
"inputs": "<base64 image or data URL>",
|
| 35 |
-
"parameters": {
|
| 36 |
-
"processing_res": 768,
|
| 37 |
-
"targets": ["albedo","diffuse_shading","residual"], # optional subset
|
| 38 |
-
"num_inference_steps": <int>, # optional
|
| 39 |
-
"match_input_resolution": true # default true
|
| 40 |
-
}
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
Returns:
|
| 44 |
-
{
|
| 45 |
-
"outputs": {
|
| 46 |
-
"targets": [...],
|
| 47 |
-
"tensors_info": {"shape":[H,W,C], "dtype":"float32"},
|
| 48 |
-
"uncertainty_available": <bool>,
|
| 49 |
-
"visualizations": ["<b64_png_albedo>", "<b64_png_diffuse>", "<b64_png_residual>"],
|
| 50 |
-
"visualizations_error": null | "<msg>"
|
| 51 |
-
}
|
| 52 |
-
}
|
| 53 |
"""
|
| 54 |
|
| 55 |
-
def __init__(self,
|
| 56 |
-
#
|
| 57 |
-
self.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
-
# decode base64 image
|
| 92 |
-
try:
|
| 93 |
-
if isinstance(raw_inputs, (bytes, bytearray)):
|
| 94 |
-
raw_inputs = raw_inputs.decode("utf-8")
|
| 95 |
-
image = _b64_to_pil(raw_inputs)
|
| 96 |
-
except Exception as e:
|
| 97 |
-
return {"error": f"Failed to decode 'inputs': {e}"}
|
| 98 |
-
|
| 99 |
-
processing_res = int(params.get("processing_res", getattr(self.pipe, "default_processing_resolution", 768) or 768))
|
| 100 |
-
num_steps = params.get("num_inference_steps", None)
|
| 101 |
-
match_input_resolution = bool(params.get("match_input_resolution", True))
|
| 102 |
-
|
| 103 |
-
# requested targets, keep order, intersect with supported
|
| 104 |
-
req_targets = params.get("targets", self.supported_targets)
|
| 105 |
-
targets = [t for t in req_targets if t in self.supported_targets]
|
| 106 |
-
if not targets:
|
| 107 |
-
targets = self.supported_targets
|
| 108 |
-
|
| 109 |
-
# target_properties override so the pipeline outputs only what we want, in order
|
| 110 |
-
target_props = dict(self.pipe.target_properties) if getattr(self.pipe, "target_properties", None) else {}
|
| 111 |
-
target_props["target_names"] = targets
|
| 112 |
-
|
| 113 |
-
# ---- Inference ----
|
| 114 |
with torch.inference_mode():
|
| 115 |
-
|
| 116 |
-
image
|
| 117 |
-
processing_resolution=processing_res,
|
| 118 |
-
match_input_resolution=match_input_resolution,
|
| 119 |
num_inference_steps=num_steps,
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
#
|
| 131 |
-
|
| 132 |
-
vis_err = None
|
| 133 |
try:
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
)
|
| 139 |
-
# to base64 PNGs (no data URL prefix to keep payload compact)
|
| 140 |
-
vis_b64 = [_pil_to_b64_png(im) for im in vis_imgs]
|
| 141 |
-
except Exception as e:
|
| 142 |
-
vis_b64 = None
|
| 143 |
-
vis_err = f"Failed to visualize: {e}"
|
| 144 |
|
| 145 |
-
|
| 146 |
"outputs": {
|
| 147 |
-
"targets":
|
| 148 |
-
"tensors_info": {
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
| 152 |
}
|
| 153 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import base64
|
| 2 |
import io
|
| 3 |
+
import json
|
| 4 |
import os
|
| 5 |
+
from typing import Any, Dict, Tuple, Optional
|
| 6 |
|
|
|
|
|
|
|
| 7 |
import torch
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from diffusers import MarigoldIntrinsicsPipeline, utils as diffusers_utils
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
class EndpointHandler:
|
| 13 |
"""
|
| 14 |
+
Hugging Face Inference Endpoints custom handler for Marigold Intrinsics (Lighting).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
|
| 17 |
+
def __init__(self, path: str = "."):
|
| 18 |
+
# Load the Lighting checkpoint
|
| 19 |
+
self.pipe = MarigoldIntrinsicsPipeline.from_pretrained(
|
| 20 |
+
"prs-eth/marigold-iid-lighting-v1-1",
|
| 21 |
+
variant="fp16",
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
+
|
| 25 |
+
# Optional: reduced steps (user can override via inputs)
|
| 26 |
+
self.default_steps = 4
|
| 27 |
+
|
| 28 |
+
# ------- helpers --------
|
| 29 |
+
def _load_image_from_any(self, obj: Any) -> Image.Image:
|
| 30 |
+
"""
|
| 31 |
+
Accepts:
|
| 32 |
+
- str URL (http/https)
|
| 33 |
+
- base64 string (png/jpg)
|
| 34 |
+
- dict {"image_url": "..."}
|
| 35 |
+
Returns PIL.Image
|
| 36 |
+
"""
|
| 37 |
+
if isinstance(obj, dict):
|
| 38 |
+
if "image_url" in obj:
|
| 39 |
+
return diffusers_utils.load_image(obj["image_url"])
|
| 40 |
+
# if someone sent {"image_b64": "..."}:
|
| 41 |
+
if "image_b64" in obj:
|
| 42 |
+
return Image.open(io.BytesIO(base64.b64decode(obj["image_b64"]))).convert("RGB")
|
| 43 |
+
raise ValueError("Unsupported dict format for 'inputs'. Provide 'image_url' or 'image_b64'.")
|
| 44 |
+
|
| 45 |
+
if isinstance(obj, str):
|
| 46 |
+
s = obj.strip()
|
| 47 |
+
if s.startswith("http://") or s.startswith("https://"):
|
| 48 |
+
return diffusers_utils.load_image(s)
|
| 49 |
+
# else assume base64 image
|
| 50 |
+
return Image.open(io.BytesIO(base64.b64decode(s))).convert("RGB")
|
| 51 |
+
|
| 52 |
+
raise ValueError("Unsupported 'inputs' type. Provide URL string, base64 string, or {'image_url': ...} dict.")
|
| 53 |
+
|
| 54 |
+
def _png_b64(self, pil_img: Image.Image) -> str:
|
| 55 |
+
buf = io.BytesIO()
|
| 56 |
+
pil_img.save(buf, format="PNG")
|
| 57 |
+
return base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 58 |
+
|
| 59 |
+
# ------- main entry --------
|
| 60 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 61 |
+
# Inference Endpoints expects the request JSON to have an "inputs" key.
|
| 62 |
+
if "inputs" not in data:
|
| 63 |
+
raise ValueError("Body must include an 'inputs' key")
|
| 64 |
|
| 65 |
+
# Parse options (all optional)
|
| 66 |
+
params = data if isinstance(data, dict) else {}
|
| 67 |
+
num_steps = int(params.get("num_inference_steps", self.default_steps))
|
| 68 |
|
| 69 |
+
# Note: do NOT pass unsupported kwargs like target_properties or processing_res
|
| 70 |
+
image = self._load_image_from_any(data["inputs"])
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with torch.inference_mode():
|
| 73 |
+
intrinsics = self.pipe(
|
| 74 |
+
image,
|
|
|
|
|
|
|
| 75 |
num_inference_steps=num_steps,
|
| 76 |
+
# If you later want to expose these (and they’re supported),
|
| 77 |
+
# you can add conditionals:
|
| 78 |
+
# processing_resolution=params.get("processing_resolution", None),
|
| 79 |
+
# match_input_resolution=params.get("match_input_resolution", None),
|
| 80 |
)
|
| 81 |
|
| 82 |
+
# intrinsics.prediction is a tensor; lighting model yields 3 targets
|
| 83 |
+
pred = intrinsics.prediction # shape: (T, H, W, 3) typically; T=3 for lighting
|
| 84 |
+
if isinstance(pred, torch.Tensor):
|
| 85 |
+
t = pred
|
| 86 |
+
else:
|
| 87 |
+
# Just in case (diffusers usually returns a dataclass with .prediction tensor)
|
| 88 |
+
raise RuntimeError("Unexpected intrinsics.prediction type")
|
| 89 |
|
| 90 |
+
# Gather target names from pipeline properties
|
| 91 |
+
# For lighting these should be: ["albedo", "shading", "residual"]
|
|
|
|
| 92 |
try:
|
| 93 |
+
target_names = list(self.pipe.target_properties.keys())
|
| 94 |
+
except Exception:
|
| 95 |
+
# Fallback (expected order for lighting model)
|
| 96 |
+
target_names = ["albedo", "shading", "residual"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
out: Dict[str, Any] = {
|
| 99 |
"outputs": {
|
| 100 |
+
"targets": target_names,
|
| 101 |
+
"tensors_info": {
|
| 102 |
+
"shape": list(t.shape),
|
| 103 |
+
"dtype": str(t.dtype).replace("torch.", ""),
|
| 104 |
+
},
|
| 105 |
+
"uncertainty_available": False,
|
| 106 |
}
|
| 107 |
}
|
| 108 |
+
|
| 109 |
+
# Try to attach base64 previews per target
|
| 110 |
+
try:
|
| 111 |
+
vis_list = self.pipe.image_processor.visualize_intrinsics(t, self.pipe.target_properties)
|
| 112 |
+
# vis_list is a list over batch (we run 1 image), where each item is a dict of PIL images per target
|
| 113 |
+
previews: Dict[str, str] = {}
|
| 114 |
+
if isinstance(vis_list, (list, tuple)) and len(vis_list) >= 1 and isinstance(vis_list[0], dict):
|
| 115 |
+
for name, pil_img in vis_list[0].items():
|
| 116 |
+
# only include known target previews to avoid surprises
|
| 117 |
+
if name in target_names and isinstance(pil_img, Image.Image):
|
| 118 |
+
previews[name] = self._png_b64(pil_img)
|
| 119 |
+
if previews:
|
| 120 |
+
out["outputs"]["visualizations_png_b64"] = previews
|
| 121 |
+
except Exception as e:
|
| 122 |
+
out["outputs"]["visualizations_error"] = f"Failed to visualize: {e.__class__.__name__}: {e}"
|
| 123 |
+
|
| 124 |
+
return out
|