Spaces:
Running on Zero
Running on Zero
Allow object removal with or without a mask outline.
Browse filesEmpty mask inputs were treated as required images, so chained workflow runs dropped the instruction and failed. Treat missing masks as optional and keep prompt-only removal working.
Co-authored-by: Cursor <cursoragent@cursor.com>
- app.py +38 -6
- workflow.json +9 -9
app.py
CHANGED
|
@@ -229,6 +229,33 @@ def _local_path(value) -> str:
|
|
| 229 |
return path
|
| 230 |
|
| 231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
def _open_rgb(value) -> Image.Image:
|
| 233 |
if isinstance(value, Image.Image):
|
| 234 |
return value.convert("RGB")
|
|
@@ -237,9 +264,12 @@ def _open_rgb(value) -> Image.Image:
|
|
| 237 |
|
| 238 |
def _open_mask(value):
|
| 239 |
"""A mask as grayscale, white = the object. Transparent pixels count as black. None when absent."""
|
| 240 |
-
if value
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
return None
|
| 242 |
-
image = value if isinstance(value, Image.Image) else ImageOps.exif_transpose(Image.open(_local_path(value)))
|
| 243 |
if "A" in image.getbands() or image.mode == "P":
|
| 244 |
image = Image.alpha_composite(Image.new("RGBA", image.size, (0, 0, 0, 255)), image.convert("RGBA"))
|
| 245 |
return image.convert("L")
|
|
@@ -273,11 +303,13 @@ def _as_media(path: str) -> dict:
|
|
| 273 |
# ------------------------------------------------------------------ workflow fn
|
| 274 |
|
| 275 |
|
| 276 |
-
def pq_remove(image,
|
| 277 |
-
"""Remove
|
| 278 |
-
if image
|
| 279 |
raise gr.Error("Connect a source image first.")
|
| 280 |
-
result, _, _, _ = _remove(
|
|
|
|
|
|
|
| 281 |
return _as_media(_publish(result))
|
| 282 |
|
| 283 |
|
|
|
|
| 229 |
return path
|
| 230 |
|
| 231 |
|
| 232 |
+
def _empty_media(value) -> bool:
|
| 233 |
+
"""True when the canvas/API sent no file: None, '', or a dict with no path/url."""
|
| 234 |
+
if value is None or value == "":
|
| 235 |
+
return True
|
| 236 |
+
if isinstance(value, Image.Image):
|
| 237 |
+
return False
|
| 238 |
+
if isinstance(value, str):
|
| 239 |
+
return not value.strip()
|
| 240 |
+
if isinstance(value, dict):
|
| 241 |
+
return not (value.get("path") or value.get("url"))
|
| 242 |
+
return True
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def _as_instruction(value) -> str:
|
| 246 |
+
if value is None or isinstance(value, bool):
|
| 247 |
+
return ""
|
| 248 |
+
if isinstance(value, str):
|
| 249 |
+
return value
|
| 250 |
+
if isinstance(value, dict):
|
| 251 |
+
for key in ("text", "value", "prompt", "out", "in"):
|
| 252 |
+
item = value.get(key)
|
| 253 |
+
if isinstance(item, str) and item.strip():
|
| 254 |
+
return item
|
| 255 |
+
return ""
|
| 256 |
+
return str(value)
|
| 257 |
+
|
| 258 |
+
|
| 259 |
def _open_rgb(value) -> Image.Image:
|
| 260 |
if isinstance(value, Image.Image):
|
| 261 |
return value.convert("RGB")
|
|
|
|
| 264 |
|
| 265 |
def _open_mask(value):
|
| 266 |
"""A mask as grayscale, white = the object. Transparent pixels count as black. None when absent."""
|
| 267 |
+
if _empty_media(value):
|
| 268 |
+
return None
|
| 269 |
+
try:
|
| 270 |
+
image = value if isinstance(value, Image.Image) else ImageOps.exif_transpose(Image.open(_local_path(value)))
|
| 271 |
+
except (gr.Error, OSError, ValueError, FileNotFoundError):
|
| 272 |
return None
|
|
|
|
| 273 |
if "A" in image.getbands() or image.mode == "P":
|
| 274 |
image = Image.alpha_composite(Image.new("RGBA", image.size, (0, 0, 0, 255)), image.convert("RGBA"))
|
| 275 |
return image.convert("L")
|
|
|
|
| 303 |
# ------------------------------------------------------------------ workflow fn
|
| 304 |
|
| 305 |
|
| 306 |
+
def pq_remove(image, prompt="", variant: str = DEFAULT_VARIANT, seed: str = "42", mask=None) -> dict:
|
| 307 |
+
"""Remove an object with a mask outline, a text instruction, or both (no CFG)."""
|
| 308 |
+
if _empty_media(image):
|
| 309 |
raise gr.Error("Connect a source image first.")
|
| 310 |
+
result, _, _, _ = _remove(
|
| 311 |
+
_open_rgb(image), _open_mask(mask), _as_instruction(prompt), variant, _seed(seed)
|
| 312 |
+
)
|
| 313 |
return _as_media(_publish(result))
|
| 314 |
|
| 315 |
|
workflow.json
CHANGED
|
@@ -17,11 +17,11 @@
|
|
| 17 |
},
|
| 18 |
{
|
| 19 |
"id": "ref_mask",
|
| 20 |
-
"label": "Mask (white = remove)",
|
| 21 |
"role": "reference",
|
| 22 |
"asset_type": "image",
|
| 23 |
-
"inputs": [{"id": "in", "label": "Image", "type": "image"}],
|
| 24 |
-
"outputs": [{"id": "out", "label": "Image", "type": "image"}],
|
| 25 |
"x": 40,
|
| 26 |
"y": 200,
|
| 27 |
"width": 220,
|
|
@@ -72,8 +72,8 @@
|
|
| 72 |
"label": "Second Mask (optional)",
|
| 73 |
"role": "reference",
|
| 74 |
"asset_type": "image",
|
| 75 |
-
"inputs": [{"id": "in", "label": "Image", "type": "image"}],
|
| 76 |
-
"outputs": [{"id": "out", "label": "Image", "type": "image"}],
|
| 77 |
"x": 620,
|
| 78 |
"y": 300,
|
| 79 |
"width": 220,
|
|
@@ -103,10 +103,10 @@
|
|
| 103 |
"fn": "pq_remove",
|
| 104 |
"inputs": [
|
| 105 |
{"id": "image", "label": "Image", "type": "image", "required": true},
|
| 106 |
-
{"id": "mask", "label": "Mask", "type": "image"},
|
| 107 |
{"id": "prompt", "label": "Removal Instruction", "type": "text"},
|
| 108 |
{"id": "variant", "label": "Variant", "type": "text", "choices": ["6-step", "8-step", "base"], "default_value": "6-step"},
|
| 109 |
-
{"id": "seed", "label": "Seed", "type": "text"}
|
|
|
|
| 110 |
],
|
| 111 |
"outputs": [{"id": "out_0", "label": "Image", "type": "image", "output_index": 0}],
|
| 112 |
"x": 660,
|
|
@@ -123,10 +123,10 @@
|
|
| 123 |
"fn": "pq_remove",
|
| 124 |
"inputs": [
|
| 125 |
{"id": "image", "label": "Image", "type": "image", "required": true},
|
| 126 |
-
{"id": "mask", "label": "Mask", "type": "image"},
|
| 127 |
{"id": "prompt", "label": "Removal Instruction", "type": "text"},
|
| 128 |
{"id": "variant", "label": "Variant", "type": "text", "choices": ["6-step", "8-step", "base"], "default_value": "6-step"},
|
| 129 |
-
{"id": "seed", "label": "Seed", "type": "text"}
|
|
|
|
| 130 |
],
|
| 131 |
"outputs": [{"id": "out_0", "label": "Image", "type": "image", "output_index": 0}],
|
| 132 |
"x": 960,
|
|
|
|
| 17 |
},
|
| 18 |
{
|
| 19 |
"id": "ref_mask",
|
| 20 |
+
"label": "Mask (white = remove, optional)",
|
| 21 |
"role": "reference",
|
| 22 |
"asset_type": "image",
|
| 23 |
+
"inputs": [{"id": "in", "label": "Image", "type": "image", "required": false}],
|
| 24 |
+
"outputs": [{"id": "out", "label": "Image", "type": "image", "required": false}],
|
| 25 |
"x": 40,
|
| 26 |
"y": 200,
|
| 27 |
"width": 220,
|
|
|
|
| 72 |
"label": "Second Mask (optional)",
|
| 73 |
"role": "reference",
|
| 74 |
"asset_type": "image",
|
| 75 |
+
"inputs": [{"id": "in", "label": "Image", "type": "image", "required": false}],
|
| 76 |
+
"outputs": [{"id": "out", "label": "Image", "type": "image", "required": false}],
|
| 77 |
"x": 620,
|
| 78 |
"y": 300,
|
| 79 |
"width": 220,
|
|
|
|
| 103 |
"fn": "pq_remove",
|
| 104 |
"inputs": [
|
| 105 |
{"id": "image", "label": "Image", "type": "image", "required": true},
|
|
|
|
| 106 |
{"id": "prompt", "label": "Removal Instruction", "type": "text"},
|
| 107 |
{"id": "variant", "label": "Variant", "type": "text", "choices": ["6-step", "8-step", "base"], "default_value": "6-step"},
|
| 108 |
+
{"id": "seed", "label": "Seed", "type": "text"},
|
| 109 |
+
{"id": "mask", "label": "Mask", "type": "image", "required": false}
|
| 110 |
],
|
| 111 |
"outputs": [{"id": "out_0", "label": "Image", "type": "image", "output_index": 0}],
|
| 112 |
"x": 660,
|
|
|
|
| 123 |
"fn": "pq_remove",
|
| 124 |
"inputs": [
|
| 125 |
{"id": "image", "label": "Image", "type": "image", "required": true},
|
|
|
|
| 126 |
{"id": "prompt", "label": "Removal Instruction", "type": "text"},
|
| 127 |
{"id": "variant", "label": "Variant", "type": "text", "choices": ["6-step", "8-step", "base"], "default_value": "6-step"},
|
| 128 |
+
{"id": "seed", "label": "Seed", "type": "text"},
|
| 129 |
+
{"id": "mask", "label": "Mask", "type": "image", "required": false}
|
| 130 |
],
|
| 131 |
"outputs": [{"id": "out_0", "label": "Image", "type": "image", "output_index": 0}],
|
| 132 |
"x": 960,
|