Spaces:
Running on Zero
Running on Zero
fix stale batch dropdown when the slider moves; label regex hits as signatures
Browse files
app.py
CHANGED
|
@@ -144,7 +144,10 @@ def batch_label(batch, i, n_batches, runtime=None):
|
|
| 144 |
kind = ("marker regions" if n_marker == len(batch) else
|
| 145 |
"sweep of the document" if n_marker == 0 else
|
| 146 |
f"{n_marker} marker + {len(batch) - n_marker} sweep")
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
| 148 |
return (f"Batch {i + 1} of {n_batches} — {len(batch)} region(s), {kind} · {where}{tail} · "
|
| 149 |
f"~{fmt_eta(len(batch), runtime)}")
|
| 150 |
|
|
@@ -162,7 +165,13 @@ def on_upload(path, per_batch, cover_all, runtime):
|
|
| 162 |
gr.update(choices=[], value=None, interactive=False))
|
| 163 |
|
| 164 |
groups = batches_of(candidates, per_batch)
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
lines = [
|
| 168 |
f"**{len(data):,} bytes** on disk, rendered to a **{len(skeleton):,}-character skeleton**"
|
|
@@ -201,7 +210,7 @@ def on_upload(path, per_batch, cover_all, runtime):
|
|
| 201 |
f"batches as you like, one at a time.")
|
| 202 |
|
| 203 |
return ((skeleton, candidates), "\n\n".join(lines),
|
| 204 |
-
gr.update(choices=choices, value=0, interactive=True))
|
| 205 |
|
| 206 |
|
| 207 |
def resolve_batch(value, n_batches: int) -> int:
|
|
@@ -411,9 +420,12 @@ with gr.Blocks(title="PDF Injection Detector") as demo:
|
|
| 411 |
"unlimited."))
|
| 412 |
per_batch = gr.Slider(1, MAX_PER_BATCH, value=DEFAULT_PER_BATCH, step=1,
|
| 413 |
label="Regions per batch",
|
| 414 |
-
info=f"
|
| 415 |
-
|
| 416 |
-
|
|
|
|
|
|
|
|
|
|
| 417 |
# allow_custom_value: the choices are empty until a PDF is uploaded, and without this
|
| 418 |
# Gradio validates any incoming value against that empty list and rejects it - which
|
| 419 |
# makes the batch un-selectable over the API even though the UI had populated it.
|
|
|
|
| 144 |
kind = ("marker regions" if n_marker == len(batch) else
|
| 145 |
"sweep of the document" if n_marker == 0 else
|
| 146 |
f"{n_marker} marker + {len(batch) - n_marker} sweep")
|
| 147 |
+
# "signature:" and not the bare family name. The regex has only seen a token like `/JS (` in
|
| 148 |
+
# the text; plenty of harmless PDFs contain one. Printing "javascript_injection" on its own
|
| 149 |
+
# before the model has read anything reads as a verdict the app has not made.
|
| 150 |
+
tail = f" · signature: {', '.join(fams)}" if fams else ""
|
| 151 |
return (f"Batch {i + 1} of {n_batches} — {len(batch)} region(s), {kind} · {where}{tail} · "
|
| 152 |
f"~{fmt_eta(len(batch), runtime)}")
|
| 153 |
|
|
|
|
| 165 |
gr.update(choices=[], value=None, interactive=False))
|
| 166 |
|
| 167 |
groups = batches_of(candidates, per_batch)
|
| 168 |
+
|
| 169 |
+
# Plain label strings, NOT (label, index) pairs. The dropdown allows custom values so the API
|
| 170 |
+
# can name a batch before any PDF has been uploaded, and that turns it into a free-text
|
| 171 |
+
# combobox: a programmatic integer value of 0 is falsy, so the displayed text refused to
|
| 172 |
+
# refresh and the box kept showing the previous split after the slider moved. Strings are
|
| 173 |
+
# never falsy here, and `resolve_batch` reads the index straight back out of "Batch N of M".
|
| 174 |
+
choices = [batch_label(b, i, len(groups), runtime) for i, b in enumerate(groups)]
|
| 175 |
|
| 176 |
lines = [
|
| 177 |
f"**{len(data):,} bytes** on disk, rendered to a **{len(skeleton):,}-character skeleton**"
|
|
|
|
| 210 |
f"batches as you like, one at a time.")
|
| 211 |
|
| 212 |
return ((skeleton, candidates), "\n\n".join(lines),
|
| 213 |
+
gr.update(choices=choices, value=choices[0], interactive=True))
|
| 214 |
|
| 215 |
|
| 216 |
def resolve_batch(value, n_batches: int) -> int:
|
|
|
|
| 420 |
"unlimited."))
|
| 421 |
per_batch = gr.Slider(1, MAX_PER_BATCH, value=DEFAULT_PER_BATCH, step=1,
|
| 422 |
label="Regions per batch",
|
| 423 |
+
info=(f"How many regions one run of the model reads — it just "
|
| 424 |
+
f"re-cuts the same list, so fewer per batch means more "
|
| 425 |
+
f"batches. A run reserves the same GPU time whatever this "
|
| 426 |
+
f"is set to, so lowering it inspects less of the file for "
|
| 427 |
+
f"the same quota. Leave it at {MAX_PER_BATCH} unless you "
|
| 428 |
+
f"want a faster single run."))
|
| 429 |
# allow_custom_value: the choices are empty until a PDF is uploaded, and without this
|
| 430 |
# Gradio validates any incoming value against that empty list and rejects it - which
|
| 431 |
# makes the batch un-selectable over the API even though the UI had populated it.
|