Spaces:
Running on Zero
Running on Zero
Commit ·
ae8cd94
1
Parent(s): 7c2b4e0
Remember recent jobs in browser state
Browse files
app.py
CHANGED
|
@@ -293,6 +293,35 @@ def load_job(job_id: str):
|
|
| 293 |
return metrics_markdown(metadata), overlay, str(prediction_path), cleaned
|
| 294 |
|
| 295 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
if CHECKPOINT_PATH.exists():
|
| 297 |
try:
|
| 298 |
load_binary_model()
|
|
@@ -304,6 +333,11 @@ else:
|
|
| 304 |
|
| 305 |
|
| 306 |
with gr.Blocks(title="Brain Tumour Binary Segmentation MVP", delete_cache=(3600, 3600)) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
gr.Markdown(
|
| 308 |
"""
|
| 309 |
# Brain Tumour Binary Segmentation MVP
|
|
@@ -320,21 +354,38 @@ with gr.Blocks(title="Brain Tumour Binary Segmentation MVP", delete_cache=(3600,
|
|
| 320 |
overlay_out = gr.Image(label="Largest predicted-tumour slice overlay", type="numpy")
|
| 321 |
download_out = gr.File(label="Download predicted binary mask (.nii.gz)")
|
| 322 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
run_button.click(
|
| 324 |
fn=run_segmentation,
|
| 325 |
inputs=[upload],
|
| 326 |
outputs=[metrics_out, overlay_out, download_out, job_id_out],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
)
|
| 328 |
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
|
|
|
| 333 |
load_button.click(
|
| 334 |
fn=load_job,
|
| 335 |
inputs=[job_id_in],
|
| 336 |
outputs=[metrics_out, overlay_out, download_out, job_id_out],
|
| 337 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
|
| 340 |
if __name__ == "__main__":
|
|
|
|
| 293 |
return metrics_markdown(metadata), overlay, str(prediction_path), cleaned
|
| 294 |
|
| 295 |
|
| 296 |
+
def normalize_job_history(history) -> list[str]:
|
| 297 |
+
if not isinstance(history, list):
|
| 298 |
+
return []
|
| 299 |
+
normalized = []
|
| 300 |
+
seen = set()
|
| 301 |
+
for item in history:
|
| 302 |
+
if not isinstance(item, str):
|
| 303 |
+
continue
|
| 304 |
+
cleaned = item.strip()
|
| 305 |
+
if not JOB_ID_PATTERN.fullmatch(cleaned) or cleaned in seen:
|
| 306 |
+
continue
|
| 307 |
+
normalized.append(cleaned)
|
| 308 |
+
seen.add(cleaned)
|
| 309 |
+
return normalized[:20]
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
def remember_job(job_id: str, history) -> tuple[list[str], gr.Dropdown]:
|
| 313 |
+
cleaned = validate_job_id(job_id)
|
| 314 |
+
updated = [cleaned] + [item for item in normalize_job_history(history) if item != cleaned]
|
| 315 |
+
updated = updated[:20]
|
| 316 |
+
return updated, gr.update(choices=updated, value=cleaned)
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
def refresh_recent_jobs(history) -> gr.Dropdown:
|
| 320 |
+
normalized = normalize_job_history(history)
|
| 321 |
+
value = normalized[0] if normalized else None
|
| 322 |
+
return gr.update(choices=normalized, value=value)
|
| 323 |
+
|
| 324 |
+
|
| 325 |
if CHECKPOINT_PATH.exists():
|
| 326 |
try:
|
| 327 |
load_binary_model()
|
|
|
|
| 333 |
|
| 334 |
|
| 335 |
with gr.Blocks(title="Brain Tumour Binary Segmentation MVP", delete_cache=(3600, 3600)) as demo:
|
| 336 |
+
job_history = gr.BrowserState(
|
| 337 |
+
default_value=[],
|
| 338 |
+
storage_key="brain_tumour_segmentation_job_history_v1",
|
| 339 |
+
secret="brain-tumour-segmentation-history-v1",
|
| 340 |
+
)
|
| 341 |
gr.Markdown(
|
| 342 |
"""
|
| 343 |
# Brain Tumour Binary Segmentation MVP
|
|
|
|
| 354 |
overlay_out = gr.Image(label="Largest predicted-tumour slice overlay", type="numpy")
|
| 355 |
download_out = gr.File(label="Download predicted binary mask (.nii.gz)")
|
| 356 |
|
| 357 |
+
gr.Markdown("### Reload a completed result")
|
| 358 |
+
recent_jobs = gr.Dropdown(label="Recent jobs in this browser", choices=[], interactive=True)
|
| 359 |
+
load_recent_button = gr.Button("Load selected recent job")
|
| 360 |
+
with gr.Row():
|
| 361 |
+
job_id_in = gr.Textbox(label="Previous Job ID", placeholder="Paste a completed job_id")
|
| 362 |
+
load_button = gr.Button("Load previous job")
|
| 363 |
+
|
| 364 |
run_button.click(
|
| 365 |
fn=run_segmentation,
|
| 366 |
inputs=[upload],
|
| 367 |
outputs=[metrics_out, overlay_out, download_out, job_id_out],
|
| 368 |
+
).then(
|
| 369 |
+
fn=remember_job,
|
| 370 |
+
inputs=[job_id_out, job_history],
|
| 371 |
+
outputs=[job_history, recent_jobs],
|
| 372 |
)
|
| 373 |
|
| 374 |
+
load_recent_button.click(
|
| 375 |
+
fn=load_job,
|
| 376 |
+
inputs=[recent_jobs],
|
| 377 |
+
outputs=[metrics_out, overlay_out, download_out, job_id_out],
|
| 378 |
+
)
|
| 379 |
load_button.click(
|
| 380 |
fn=load_job,
|
| 381 |
inputs=[job_id_in],
|
| 382 |
outputs=[metrics_out, overlay_out, download_out, job_id_out],
|
| 383 |
)
|
| 384 |
+
demo.load(
|
| 385 |
+
fn=refresh_recent_jobs,
|
| 386 |
+
inputs=[job_history],
|
| 387 |
+
outputs=[recent_jobs],
|
| 388 |
+
)
|
| 389 |
|
| 390 |
|
| 391 |
if __name__ == "__main__":
|