lymon commited on
Commit
785e158
·
1 Parent(s): e2c01cb
Files changed (1) hide show
  1. handler.py +22 -7
handler.py CHANGED
@@ -23,14 +23,29 @@ class EndpointHandler:
23
  )
24
  self.pipe.to("cuda")
25
 
26
- def __call__(self, data: Dict[str, Any]) -> List[Dict[str, str]]:
27
- # 获取控制图像(支持 URL 或二进制上传)
28
- control = data.get("control_image")
29
- if isinstance(control, str):
30
- resp = requests.get(control)
31
- control_image = Image.open(BytesIO(resp.content)).convert("RGB")
 
 
 
 
 
 
 
 
32
  else:
33
- control_image = Image.open(BytesIO(control)).convert("RGB")
 
 
 
 
 
 
 
34
 
35
  # 可选:调整尺寸(默认 4× 放大)
36
  w, h = control_image.size
 
23
  )
24
  self.pipe.to("cuda")
25
 
26
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
27
+ # 兼容包装与非包装请求体
28
+ payload = data.get("inputs", data) # 支持 UI 及直接 POST 两种格式
29
+
30
+ # 读取图像字节
31
+ img_bytes = None
32
+ url = payload.get("control_image_url")
33
+ b64 = payload.get("control_image_base64")
34
+ if b64:
35
+ img_bytes = base64.b64decode(b64)
36
+ elif url:
37
+ resp = requests.get(url)
38
+ resp.raise_for_status() # 捕获 4xx/5xx 错误 :contentReference[oaicite:3]{index=3}
39
+ img_bytes = resp.content
40
  else:
41
+ raise ValueError("请在 inputs 中提供 control_image_url 或 control_image_base64")
42
+
43
+ # 用 PIL 解析并转换
44
+ try:
45
+ control_image = Image.open(BytesIO(img_bytes)).convert("RGB")
46
+ except Exception as e:
47
+ # 可能是数据损坏或格式不符
48
+ raise ValueError(f"无法识别图像文件: {str(e)}")
49
 
50
  # 可选:调整尺寸(默认 4× 放大)
51
  w, h = control_image.size