Spaces:
Running on Zero
Running on Zero
Upload 2 files
Browse files- app.py +17 -8
- test_app.py +13 -0
app.py
CHANGED
|
@@ -126,12 +126,14 @@ def _ensure_repo(path: pathlib.Path, url: str) -> None:
|
|
| 126 |
_run(["git", "clone", "--depth", "1", url, str(path)])
|
| 127 |
|
| 128 |
|
| 129 |
-
def
|
| 130 |
-
"""
|
| 131 |
source = COMFY / "utils"
|
| 132 |
target = COMFY / "utilities"
|
| 133 |
-
if source.exists() and
|
| 134 |
-
|
|
|
|
|
|
|
| 135 |
for path in COMFY.rglob("*.py"):
|
| 136 |
if "__pycache__" in path.parts:
|
| 137 |
continue
|
|
@@ -139,8 +141,8 @@ def _apply_utils_namespace_fix() -> None:
|
|
| 139 |
text = path.read_text(encoding="utf-8")
|
| 140 |
except UnicodeDecodeError:
|
| 141 |
continue
|
| 142 |
-
updated = re.sub(r"\bfrom
|
| 143 |
-
updated = re.sub(r"\bimport
|
| 144 |
if updated != text:
|
| 145 |
path.write_text(updated, encoding="utf-8")
|
| 146 |
|
|
@@ -153,7 +155,7 @@ def _ensure_comfy() -> None:
|
|
| 153 |
_install_filtered_requirements(COMFY / "requirements.txt")
|
| 154 |
CUSTOM_NODES.mkdir(parents=True, exist_ok=True)
|
| 155 |
_ensure_repo(CUSTOM_NODES / "comfyui-krea2edit", KREA_EDIT_NODES)
|
| 156 |
-
|
| 157 |
for folder in ("diffusion_models", "text_encoders", "vae", "loras/krea"):
|
| 158 |
(MODELS / folder).mkdir(parents=True, exist_ok=True)
|
| 159 |
INPUT.mkdir(parents=True, exist_ok=True)
|
|
@@ -374,7 +376,14 @@ def _prepare_runtime(progress: gr.Progress | None = None) -> None:
|
|
| 374 |
_init_comfy_nodes()
|
| 375 |
|
| 376 |
|
| 377 |
-
def get_gpu_duration(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
if gen_budget and int(gen_budget) > 0:
|
| 379 |
return max(MIN_GPU_SECONDS, min(MAX_GPU_SECONDS, int(gen_budget)))
|
| 380 |
estimate = int(35 + (int(width) * int(height) / 1_000_000) * int(steps) * 3.0)
|
|
|
|
| 126 |
_run(["git", "clone", "--depth", "1", url, str(path)])
|
| 127 |
|
| 128 |
|
| 129 |
+
def _restore_utils_namespace() -> None:
|
| 130 |
+
"""Undo the old root ``utils`` rename if a persistent Space has one."""
|
| 131 |
source = COMFY / "utils"
|
| 132 |
target = COMFY / "utilities"
|
| 133 |
+
if not source.exists() and target.exists():
|
| 134 |
+
target.rename(source)
|
| 135 |
+
if not source.exists():
|
| 136 |
+
return
|
| 137 |
for path in COMFY.rglob("*.py"):
|
| 138 |
if "__pycache__" in path.parts:
|
| 139 |
continue
|
|
|
|
| 141 |
text = path.read_text(encoding="utf-8")
|
| 142 |
except UnicodeDecodeError:
|
| 143 |
continue
|
| 144 |
+
updated = re.sub(r"\bfrom utilities\b", "from utils", text)
|
| 145 |
+
updated = re.sub(r"\bimport utilities\b", "import utils", updated)
|
| 146 |
if updated != text:
|
| 147 |
path.write_text(updated, encoding="utf-8")
|
| 148 |
|
|
|
|
| 155 |
_install_filtered_requirements(COMFY / "requirements.txt")
|
| 156 |
CUSTOM_NODES.mkdir(parents=True, exist_ok=True)
|
| 157 |
_ensure_repo(CUSTOM_NODES / "comfyui-krea2edit", KREA_EDIT_NODES)
|
| 158 |
+
_restore_utils_namespace()
|
| 159 |
for folder in ("diffusion_models", "text_encoders", "vae", "loras/krea"):
|
| 160 |
(MODELS / folder).mkdir(parents=True, exist_ok=True)
|
| 161 |
INPUT.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 376 |
_init_comfy_nodes()
|
| 377 |
|
| 378 |
|
| 379 |
+
def get_gpu_duration(*args: Any, **kwargs: Any) -> int:
|
| 380 |
+
"""Estimate GPU time for Spaces' positional or keyword callback forms."""
|
| 381 |
+
# spaces.zero invokes a duration callback with the complete generate()
|
| 382 |
+
# argument list positionally. Keep the indexes aligned with generate().
|
| 383 |
+
steps = kwargs.get("steps", args[11] if len(args) > 11 else DEFAULT_STEPS)
|
| 384 |
+
width = kwargs.get("width", args[5] if len(args) > 5 else DEFAULT_WIDTH)
|
| 385 |
+
height = kwargs.get("height", args[6] if len(args) > 6 else DEFAULT_HEIGHT)
|
| 386 |
+
gen_budget = kwargs.get("gen_budget", args[17] if len(args) > 17 else 0)
|
| 387 |
if gen_budget and int(gen_budget) > 0:
|
| 388 |
return max(MIN_GPU_SECONDS, min(MAX_GPU_SECONDS, int(gen_budget)))
|
| 389 |
estimate = int(35 + (int(width) * int(height) / 1_000_000) * int(steps) * 3.0)
|
test_app.py
CHANGED
|
@@ -55,6 +55,19 @@ class WorkflowTests(unittest.TestCase):
|
|
| 55 |
app._validate_request("edit", "", "", "source.png")
|
| 56 |
app._validate_request("edit", "", "change the color", "source.png")
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
unittest.main()
|
|
|
|
| 55 |
app._validate_request("edit", "", "", "source.png")
|
| 56 |
app._validate_request("edit", "", "change the color", "source.png")
|
| 57 |
|
| 58 |
+
def test_gpu_duration_accepts_spaces_positional_callback(self):
|
| 59 |
+
values = [
|
| 60 |
+
"text2image", "prompt", "", None, None,
|
| 61 |
+
1024, 1024, 1.4, 768, 1.0, 1.0,
|
| 62 |
+
8, 1.0, "euler", "beta", 2, False, 0, None,
|
| 63 |
+
]
|
| 64 |
+
duration = app.get_gpu_duration(*values)
|
| 65 |
+
self.assertGreaterEqual(duration, app.MIN_GPU_SECONDS)
|
| 66 |
+
|
| 67 |
+
def test_gpu_duration_accepts_keyword_callback(self):
|
| 68 |
+
duration = app.get_gpu_duration(steps=8, width=1024, height=1024, gen_budget=120)
|
| 69 |
+
self.assertEqual(duration, 120)
|
| 70 |
+
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
unittest.main()
|