Spaces:
Running on Zero
Running on Zero
Scroll Lens 1.1.0
Browse files
app.py
CHANGED
|
@@ -124,6 +124,11 @@ GPU_REFUSALS = ("ZeroGPU quota exceeded", "ZeroGPU illegal duration", "ZeroGPU p
|
|
| 124 |
CPU_SLOT = threading.Semaphore(1) # one CPU fallback run at a time, so the page keeps answering
|
| 125 |
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
def _is_gpu_refusal(exc):
|
| 128 |
return (str(getattr(exc, "title", "") or "") in GPU_REFUSALS or "GPU task aborted" in str(exc)
|
| 129 |
or "No GPU was available" in str(exc))
|
|
@@ -152,14 +157,14 @@ def run_models(cube, layers, tta, allow_cpu=True):
|
|
| 152 |
except Exception as exc: # noqa: BLE001
|
| 153 |
if not _is_gpu_refusal(exc):
|
| 154 |
if str(getattr(exc, "title", "")) == "ZeroGPU worker error":
|
| 155 |
-
raise LensError(f"the models failed on the GPU ({
|
| 156 |
"another point") from None
|
| 157 |
raise
|
| 158 |
if not allow_cpu:
|
| 159 |
-
raise LensError(f"ZeroGPU refused this call: {
|
| 160 |
f"the middle {CPU_SIZE}³ on the CPU instead.") from None
|
| 161 |
log(f"GPU refused ({exc}); running on the CPU")
|
| 162 |
-
note = (f"ZeroGPU refused the run ({
|
| 163 |
f"Space's CPU, on the middle {CPU_SIZE}³ voxels. ZeroGPU time comes back each day; signing in "
|
| 164 |
"to Hugging Face gives more.")
|
| 165 |
return cpu_run(eng, cube, layers), note
|
|
@@ -256,6 +261,15 @@ class Cube:
|
|
| 256 |
s = s - cos(negative)
|
| 257 |
return s.astype(np.float32)
|
| 258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
def vector(self, voxel):
|
| 260 |
"""The unit 864-number embedding of the patch holding a cube voxel (z, y, x)."""
|
| 261 |
import numpy as np
|
|
@@ -303,7 +317,7 @@ class Cube:
|
|
| 303 |
body = len(hjson).to_bytes(4, "little") + hjson
|
| 304 |
body += b"\0" * ((-len(body)) % 4)
|
| 305 |
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
| 306 |
-
tmp = Path(f"{path}.part")
|
| 307 |
tmp.write_bytes(gzip.compress(body + b"".join(blobs), compresslevel=3, mtime=0))
|
| 308 |
os.replace(tmp, path)
|
| 309 |
self.data_path = str(path)
|
|
@@ -584,12 +598,13 @@ def numbers(state):
|
|
| 584 |
lines.append(f"{src} {('Model time: ' + secs + '.') if secs else ''} {view['note']}".strip())
|
| 585 |
lines.append("**What each model saw of this scroll in training.** " + " ".join(
|
| 586 |
f"{lens.LAYER_TITLES[k]}: {v['role'][k]}" for k in lens.LAYERS))
|
| 587 |
-
ag =
|
| 588 |
if ag and ag.get("auc") is not None:
|
| 589 |
lines.append(f"**Check.** The organizers' ink reference picks out the patches the 3D ink model marks as ink "
|
| 590 |
-
f"with AUC {ag['auc']:.2f};
|
| 591 |
-
f"{ag['
|
| 592 |
-
f"{ag['ink_patch_fraction']:.1%} of patches. Agreement between two
|
|
|
|
| 593 |
if cube.ref_sim is not None:
|
| 594 |
lines.append(f"Patches at or above the organizers' threshold (cosine {lens.TAU}) to their reference: "
|
| 595 |
f"{float((cube.ref_sim >= lens.TAU).mean()):.1%}.")
|
|
@@ -600,12 +615,14 @@ def numbers(state):
|
|
| 600 |
a = lens.agreement(s, cube.result["ink"])
|
| 601 |
if a["auc"] is not None:
|
| 602 |
extra = (f" Against the 3D ink model your concept scores AUC {a['auc']:.2f} "
|
| 603 |
-
f"(
|
| 604 |
clicked = {c["cube"] for c in state["pos"] + state["neg"] if c["cube"] != "file"}
|
| 605 |
loaded = any(c["cube"] == "file" for c in state["pos"])
|
|
|
|
| 606 |
lines.append(f"**Your concept:** {len([c for c in state['pos'] if c['cube'] != 'file'])} positive and "
|
| 607 |
-
f"{len(state['neg'])} negative clicks from {len(clicked)}
|
| 608 |
-
f"{', plus your concept.npy' if loaded else ''}
|
|
|
|
| 609 |
lines.append("_Model output, not a reading. A cube is smaller than one letter._")
|
| 610 |
return "\n\n".join(lines)
|
| 611 |
|
|
@@ -1510,8 +1527,9 @@ def build_demo():
|
|
| 1510 |
if q.get("v"):
|
| 1511 |
try:
|
| 1512 |
vid = lens.volume_id(q["v"])
|
| 1513 |
-
|
| 1514 |
-
|
|
|
|
| 1515 |
if bad:
|
| 1516 |
problems.append(f"{', '.join(bad)} must be whole numbers of voxels")
|
| 1517 |
tab = gr.Tabs(selected="point")
|
|
@@ -1535,6 +1553,8 @@ def build_demo():
|
|
| 1535 |
if not q.get("v"):
|
| 1536 |
p = state["view"]["point"]
|
| 1537 |
fields = [state["view"]["volume"], p[0], p[1], p[2]]
|
|
|
|
|
|
|
| 1538 |
return shown(state) + fields + [size, cube_id, tab]
|
| 1539 |
|
| 1540 |
with gr.Blocks(title="Scroll Lens", delete_cache=(1800, 3600)) as demo:
|
|
|
|
| 124 |
CPU_SLOT = threading.Semaphore(1) # one CPU fallback run at a time, so the page keeps answering
|
| 125 |
|
| 126 |
|
| 127 |
+
def _gpu_text(exc):
|
| 128 |
+
"""A ZeroGPU error's own message (gr.Error's str() is its repr, with quotes)."""
|
| 129 |
+
return str(getattr(exc, "message", None) or exc).strip().strip("'\"").strip()
|
| 130 |
+
|
| 131 |
+
|
| 132 |
def _is_gpu_refusal(exc):
|
| 133 |
return (str(getattr(exc, "title", "") or "") in GPU_REFUSALS or "GPU task aborted" in str(exc)
|
| 134 |
or "No GPU was available" in str(exc))
|
|
|
|
| 157 |
except Exception as exc: # noqa: BLE001
|
| 158 |
if not _is_gpu_refusal(exc):
|
| 159 |
if str(getattr(exc, "title", "")) == "ZeroGPU worker error":
|
| 160 |
+
raise LensError(f"the models failed on the GPU ({_gpu_text(exc)[:200]}); try again, or try "
|
| 161 |
"another point") from None
|
| 162 |
raise
|
| 163 |
if not allow_cpu:
|
| 164 |
+
raise LensError(f"ZeroGPU refused this call: {_gpu_text(exc)[:300]} Pass allow_cpu=True to run "
|
| 165 |
f"the middle {CPU_SIZE}³ on the CPU instead.") from None
|
| 166 |
log(f"GPU refused ({exc}); running on the CPU")
|
| 167 |
+
note = (f"ZeroGPU refused the run ({_gpu_text(exc).rstrip('. ')[:200]}), so the models ran on the "
|
| 168 |
f"Space's CPU, on the middle {CPU_SIZE}³ voxels. ZeroGPU time comes back each day; signing in "
|
| 169 |
"to Hugging Face gives more.")
|
| 170 |
return cpu_run(eng, cube, layers), note
|
|
|
|
| 261 |
s = s - cos(negative)
|
| 262 |
return s.astype(np.float32)
|
| 263 |
|
| 264 |
+
def reference_agreement(self):
|
| 265 |
+
"""The organizers' reference against the 3D ink model for this cube, with the control range (lens.agreement),
|
| 266 |
+
worked out once from the arrays held here."""
|
| 267 |
+
if self.ref_sim is None or "ink" not in self.result:
|
| 268 |
+
return None
|
| 269 |
+
if getattr(self, "_ref_agreement", None) is None:
|
| 270 |
+
self._ref_agreement = lens.agreement(self.ref_sim, self.result["ink"])
|
| 271 |
+
return self._ref_agreement
|
| 272 |
+
|
| 273 |
def vector(self, voxel):
|
| 274 |
"""The unit 864-number embedding of the patch holding a cube voxel (z, y, x)."""
|
| 275 |
import numpy as np
|
|
|
|
| 317 |
body = len(hjson).to_bytes(4, "little") + hjson
|
| 318 |
body += b"\0" * ((-len(body)) % 4)
|
| 319 |
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
| 320 |
+
tmp = Path(f"{path}.{uuid.uuid4().hex[:8]}.part") # concurrent first opens never share it
|
| 321 |
tmp.write_bytes(gzip.compress(body + b"".join(blobs), compresslevel=3, mtime=0))
|
| 322 |
os.replace(tmp, path)
|
| 323 |
self.data_path = str(path)
|
|
|
|
| 598 |
lines.append(f"{src} {('Model time: ' + secs + '.') if secs else ''} {view['note']}".strip())
|
| 599 |
lines.append("**What each model saw of this scroll in training.** " + " ".join(
|
| 600 |
f"{lens.LAYER_TITLES[k]}: {v['role'][k]}" for k in lens.LAYERS))
|
| 601 |
+
ag = cube.reference_agreement()
|
| 602 |
if ag and ag.get("auc") is not None:
|
| 603 |
lines.append(f"**Check.** The organizers' ink reference picks out the patches the 3D ink model marks as ink "
|
| 604 |
+
f"with AUC {ag['auc']:.2f}; controls (the ink mask moved away from its place) give "
|
| 605 |
+
f"{ag['auc_control_p05']:.2f} to {ag['auc_control_p95']:.2f}, and the AUC means something only "
|
| 606 |
+
f"above that range. Ink covers {ag['ink_patch_fraction']:.1%} of patches. Agreement between two "
|
| 607 |
+
"models, not ground truth.")
|
| 608 |
if cube.ref_sim is not None:
|
| 609 |
lines.append(f"Patches at or above the organizers' threshold (cosine {lens.TAU}) to their reference: "
|
| 610 |
f"{float((cube.ref_sim >= lens.TAU).mean()):.1%}.")
|
|
|
|
| 615 |
a = lens.agreement(s, cube.result["ink"])
|
| 616 |
if a["auc"] is not None:
|
| 617 |
extra = (f" Against the 3D ink model your concept scores AUC {a['auc']:.2f} "
|
| 618 |
+
f"(controls {a.get('auc_control_p05', 0):.2f} to {a.get('auc_control_p95', 0):.2f}).")
|
| 619 |
clicked = {c["cube"] for c in state["pos"] + state["neg"] if c["cube"] != "file"}
|
| 620 |
loaded = any(c["cube"] == "file" for c in state["pos"])
|
| 621 |
+
loaded_not = any(c["cube"] == "file" for c in state["neg"])
|
| 622 |
lines.append(f"**Your concept:** {len([c for c in state['pos'] if c['cube'] != 'file'])} positive and "
|
| 623 |
+
f"{len([c for c in state['neg'] if c['cube'] != 'file'])} negative clicks from {len(clicked)} "
|
| 624 |
+
f"cube(s){', plus your concept.npy' if loaded else ''}"
|
| 625 |
+
f"{' and concept_not.npy' if loaded_not else ''}.{extra}")
|
| 626 |
lines.append("_Model output, not a reading. A cube is smaller than one letter._")
|
| 627 |
return "\n\n".join(lines)
|
| 628 |
|
|
|
|
| 1527 |
if q.get("v"):
|
| 1528 |
try:
|
| 1529 |
vid = lens.volume_id(q["v"])
|
| 1530 |
+
ok = {k: str(q.get(k, "")).isascii() and str(q.get(k, "")).isdigit() for k in "zyx"}
|
| 1531 |
+
fields = [vid] + [int(q[k]) if ok[k] else gr.skip() for k in "zyx"]
|
| 1532 |
+
bad = [k for k in "zyx" if not ok[k]]
|
| 1533 |
if bad:
|
| 1534 |
problems.append(f"{', '.join(bad)} must be whole numbers of voxels")
|
| 1535 |
tab = gr.Tabs(selected="point")
|
|
|
|
| 1553 |
if not q.get("v"):
|
| 1554 |
p = state["view"]["point"]
|
| 1555 |
fields = [state["view"]["volume"], p[0], p[1], p[2]]
|
| 1556 |
+
if not q.get("size"):
|
| 1557 |
+
size = gr.update(value=state["view"]["size"])
|
| 1558 |
return shown(state) + fields + [size, cube_id, tab]
|
| 1559 |
|
| 1560 |
with gr.Blocks(title="Scroll Lens", delete_cache=(1800, 3600)) as demo:
|