Spaces:
Running
Running
Fix: inline styles (no CSS dependency), remove GT images (licensing), muted badge colors
Browse files
app.py
CHANGED
|
@@ -48,89 +48,6 @@ NO_GPU_MSG = (
|
|
| 48 |
"or run the app locally with a GPU: `python app.py`"
|
| 49 |
)
|
| 50 |
|
| 51 |
-
# ββ Custom CSS βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 52 |
-
CUSTOM_CSS = """
|
| 53 |
-
.header-container {
|
| 54 |
-
text-align: center;
|
| 55 |
-
padding: 1.5rem 1rem 0.5rem 1rem;
|
| 56 |
-
}
|
| 57 |
-
.header-container h1 {
|
| 58 |
-
font-size: 1.8rem;
|
| 59 |
-
font-weight: 700;
|
| 60 |
-
margin-bottom: 0.3rem;
|
| 61 |
-
}
|
| 62 |
-
.header-subtitle {
|
| 63 |
-
text-align: center;
|
| 64 |
-
color: #555;
|
| 65 |
-
font-size: 0.95rem;
|
| 66 |
-
margin-bottom: 0.8rem;
|
| 67 |
-
}
|
| 68 |
-
.badge-row {
|
| 69 |
-
display: flex;
|
| 70 |
-
justify-content: center;
|
| 71 |
-
gap: 0.6rem;
|
| 72 |
-
flex-wrap: wrap;
|
| 73 |
-
margin-bottom: 1rem;
|
| 74 |
-
}
|
| 75 |
-
.badge {
|
| 76 |
-
display: inline-block;
|
| 77 |
-
padding: 0.25rem 0.75rem;
|
| 78 |
-
border-radius: 999px;
|
| 79 |
-
font-size: 0.8rem;
|
| 80 |
-
font-weight: 600;
|
| 81 |
-
background: #e8eaf6;
|
| 82 |
-
color: #3949ab;
|
| 83 |
-
}
|
| 84 |
-
.badge-green {
|
| 85 |
-
background: #e8f5e9;
|
| 86 |
-
color: #2e7d32;
|
| 87 |
-
}
|
| 88 |
-
.badge-purple {
|
| 89 |
-
background: #f3e5f5;
|
| 90 |
-
color: #7b1fa2;
|
| 91 |
-
}
|
| 92 |
-
.badge-orange {
|
| 93 |
-
background: #fff3e0;
|
| 94 |
-
color: #e65100;
|
| 95 |
-
}
|
| 96 |
-
.gpu-notice {
|
| 97 |
-
background: #fff8e1;
|
| 98 |
-
border: 1px solid #ffe082;
|
| 99 |
-
border-radius: 8px;
|
| 100 |
-
padding: 0.75rem 1rem;
|
| 101 |
-
margin-bottom: 1rem;
|
| 102 |
-
font-size: 0.9rem;
|
| 103 |
-
color: #6d4c00;
|
| 104 |
-
}
|
| 105 |
-
.section-heading {
|
| 106 |
-
font-size: 1.05rem;
|
| 107 |
-
font-weight: 600;
|
| 108 |
-
color: #333;
|
| 109 |
-
margin-bottom: 0.5rem;
|
| 110 |
-
border-bottom: 2px solid #e0e0e0;
|
| 111 |
-
padding-bottom: 0.3rem;
|
| 112 |
-
}
|
| 113 |
-
.gallery-info {
|
| 114 |
-
background: #f5f5f5;
|
| 115 |
-
border-radius: 8px;
|
| 116 |
-
padding: 0.6rem 1rem;
|
| 117 |
-
margin-bottom: 0.8rem;
|
| 118 |
-
font-size: 0.88rem;
|
| 119 |
-
color: #555;
|
| 120 |
-
}
|
| 121 |
-
.about-section {
|
| 122 |
-
max-width: 800px;
|
| 123 |
-
margin: 0 auto;
|
| 124 |
-
padding: 1rem;
|
| 125 |
-
}
|
| 126 |
-
footer {
|
| 127 |
-
text-align: center;
|
| 128 |
-
padding: 1rem;
|
| 129 |
-
color: #999;
|
| 130 |
-
font-size: 0.8rem;
|
| 131 |
-
}
|
| 132 |
-
"""
|
| 133 |
-
|
| 134 |
|
| 135 |
# ββ Lazy imports (avoid crash if no GPU) βββββββββββββββββββββββββββββ
|
| 136 |
_model_cache = {"model": None, "uni_model": None, "spatial_pool_size": 32}
|
|
@@ -315,30 +232,26 @@ def load_gallery():
|
|
| 315 |
def _make_gallery_label(key, entry):
|
| 316 |
"""Create a human-readable label for a gallery entry."""
|
| 317 |
source = entry.get("source", "")
|
| 318 |
-
gt_stain = entry.get("gt_stain", "")
|
| 319 |
-
# Extract a short sample ID from the key
|
| 320 |
parts = key.split("_")
|
| 321 |
if source == "BCI":
|
| 322 |
-
# e.g. BCI_HER2_3+_00277_test_3+ -> "BCI - HER2 3+ (#00277)"
|
| 323 |
her2_class = parts[2] if len(parts) > 2 else ""
|
| 324 |
sample_id = parts[3] if len(parts) > 3 else ""
|
| 325 |
return f"BCI - HER2 {her2_class} (#{sample_id})"
|
| 326 |
else:
|
| 327 |
-
# e.g. MIST_Ki67_10M2102916_10_20 -> "MIST - Ki67 (10M2102916)"
|
| 328 |
stain = parts[1] if len(parts) > 1 else ""
|
| 329 |
sample_id = parts[2] if len(parts) > 2 else ""
|
| 330 |
return f"MIST - {stain} ({sample_id})"
|
| 331 |
|
| 332 |
|
| 333 |
def show_gallery(display_name, gallery, name_map):
|
| 334 |
-
"""Show a gallery example by its display name."""
|
| 335 |
key = name_map.get(display_name)
|
| 336 |
if not key or not gallery or key not in gallery:
|
| 337 |
-
return None, None, None, None, None,
|
| 338 |
entry = gallery[key]
|
| 339 |
base = GALLERY_DIR / "images"
|
|
|
|
| 340 |
he = Image.open(base / entry["he"]).convert("RGB") if "he" in entry else None
|
| 341 |
-
gt = Image.open(base / entry["gt"]).convert("RGB") if "gt" in entry else None
|
| 342 |
gen_her2 = Image.open(base / entry["gen_her2"]).convert("RGB") if "gen_her2" in entry else None
|
| 343 |
gen_ki67 = Image.open(base / entry["gen_ki67"]).convert("RGB") if "gen_ki67" in entry else None
|
| 344 |
gen_er = Image.open(base / entry["gen_er"]).convert("RGB") if "gen_er" in entry else None
|
|
@@ -346,8 +259,8 @@ def show_gallery(display_name, gallery, name_map):
|
|
| 346 |
|
| 347 |
source = entry.get("source", "Unknown")
|
| 348 |
gt_stain = entry.get("gt_stain", "Unknown")
|
| 349 |
-
info = f"**
|
| 350 |
-
return he,
|
| 351 |
|
| 352 |
|
| 353 |
# ββ Build Gradio App βββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -361,25 +274,29 @@ if gallery_data:
|
|
| 361 |
gallery_name_map[label] = key
|
| 362 |
gallery_display_names.append(label)
|
| 363 |
|
| 364 |
-
with gr.Blocks(title="UNIStainNet
|
| 365 |
|
| 366 |
-
# ββ Header ββ
|
| 367 |
gr.HTML("""
|
| 368 |
-
<div
|
| 369 |
-
<h1>UNIStainNet</h1>
|
| 370 |
-
<p style="font-size:1.
|
| 371 |
Foundation-Model-Guided Virtual Staining of H&E to IHC
|
| 372 |
</p>
|
| 373 |
</div>
|
| 374 |
-
<
|
| 375 |
Translate H&E histopathology images into immunohistochemistry (IHC) stains
|
| 376 |
for breast cancer biomarkers using a single unified deep learning model.
|
| 377 |
-
</
|
| 378 |
-
<div
|
| 379 |
-
<span
|
| 380 |
-
|
| 381 |
-
<span
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
</div>
|
| 384 |
""")
|
| 385 |
|
|
@@ -388,12 +305,10 @@ with gr.Blocks(title="UNIStainNet β Virtual IHC Staining") as demo:
|
|
| 388 |
if not gallery_display_names:
|
| 389 |
gr.Markdown("No pre-computed gallery available.")
|
| 390 |
else:
|
| 391 |
-
gr.
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
</div>
|
| 396 |
-
""")
|
| 397 |
gallery_dropdown = gr.Dropdown(
|
| 398 |
choices=gallery_display_names,
|
| 399 |
value=gallery_display_names[0] if gallery_display_names else None,
|
|
@@ -401,15 +316,14 @@ with gr.Blocks(title="UNIStainNet β Virtual IHC Staining") as demo:
|
|
| 401 |
)
|
| 402 |
gallery_info_box = gr.Markdown(value="")
|
| 403 |
|
| 404 |
-
gr.
|
| 405 |
-
|
| 406 |
-
gal_he = gr.Image(type="pil", label="H&E Input", height=280)
|
| 407 |
-
gal_gt = gr.Image(type="pil", label="Ground Truth IHC", height=280)
|
| 408 |
|
| 409 |
-
gr.
|
| 410 |
with gr.Row():
|
| 411 |
gal_her2 = gr.Image(type="pil", label="Generated HER2", height=280)
|
| 412 |
gal_ki67 = gr.Image(type="pil", label="Generated Ki67", height=280)
|
|
|
|
| 413 |
gal_er = gr.Image(type="pil", label="Generated ER", height=280)
|
| 414 |
gal_pr = gr.Image(type="pil", label="Generated PR", height=280)
|
| 415 |
|
|
@@ -419,26 +333,28 @@ with gr.Blocks(title="UNIStainNet β Virtual IHC Staining") as demo:
|
|
| 419 |
gallery_dropdown.change(
|
| 420 |
fn=_show_gallery_wrapper,
|
| 421 |
inputs=[gallery_dropdown],
|
| 422 |
-
outputs=[gal_he,
|
| 423 |
)
|
| 424 |
|
| 425 |
# Auto-load first example
|
| 426 |
demo.load(
|
| 427 |
-
fn=lambda: _show_gallery_wrapper(gallery_display_names[0]) if gallery_display_names else (None,) *
|
| 428 |
-
outputs=[gal_he,
|
| 429 |
)
|
| 430 |
|
| 431 |
# ββ Tab 2: Single Stain ββββββββββββββββββββββββββββββββββββββ
|
| 432 |
with gr.Tab("Virtual Staining", id="inference"):
|
| 433 |
if not GPU_AVAILABLE and not HAS_SPACES:
|
| 434 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
else:
|
| 436 |
-
gr.
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
</div>
|
| 441 |
-
""")
|
| 442 |
with gr.Row():
|
| 443 |
with gr.Column(scale=1):
|
| 444 |
input_image = gr.Image(type="pil", label="Upload H&E Image", height=380)
|
|
@@ -466,14 +382,16 @@ with gr.Blocks(title="UNIStainNet β Virtual IHC Staining") as demo:
|
|
| 466 |
# ββ Tab 3: Cross-Stain βββββββββββββββββββββββββββββββββββββββ
|
| 467 |
with gr.Tab("Cross-Stain Comparison", id="cross-stain"):
|
| 468 |
if not GPU_AVAILABLE and not HAS_SPACES:
|
| 469 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
else:
|
| 471 |
-
gr.
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
</div>
|
| 476 |
-
""")
|
| 477 |
with gr.Row():
|
| 478 |
with gr.Column(scale=1):
|
| 479 |
cross_input = gr.Image(type="pil", label="Upload H&E Image", height=300)
|
|
@@ -486,7 +404,7 @@ with gr.Blocks(title="UNIStainNet β Virtual IHC Staining") as demo:
|
|
| 486 |
cross_btn = gr.Button("Generate All 4 Stains", variant="primary", size="lg")
|
| 487 |
cross_time = gr.Textbox(label="Status", interactive=False)
|
| 488 |
|
| 489 |
-
gr.
|
| 490 |
with gr.Row():
|
| 491 |
cross_he_out = gr.Image(type="pil", label="H&E Input", height=250)
|
| 492 |
cross_her2 = gr.Image(type="pil", label="HER2", height=250)
|
|
@@ -504,72 +422,68 @@ with gr.Blocks(title="UNIStainNet β Virtual IHC Staining") as demo:
|
|
| 504 |
with gr.Tab("About", id="about"):
|
| 505 |
gr.Markdown(
|
| 506 |
"""
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
This is a **research tool** for exploratory analysis. It is not intended for clinical diagnosis
|
| 561 |
-
and has not undergone regulatory validation. Generated stains should not be used for treatment decisions.
|
| 562 |
-
|
| 563 |
-
</div>
|
| 564 |
"""
|
| 565 |
)
|
| 566 |
|
| 567 |
# ββ Footer βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 568 |
gr.HTML("""
|
| 569 |
-
<
|
| 570 |
UNIStainNet | Built with Gradio
|
| 571 |
-
</
|
| 572 |
""")
|
| 573 |
|
| 574 |
if __name__ == "__main__":
|
| 575 |
-
demo.launch(
|
|
|
|
| 48 |
"or run the app locally with a GPU: `python app.py`"
|
| 49 |
)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# ββ Lazy imports (avoid crash if no GPU) βββββββββββββββββββββββββββββ
|
| 53 |
_model_cache = {"model": None, "uni_model": None, "spatial_pool_size": 32}
|
|
|
|
| 232 |
def _make_gallery_label(key, entry):
|
| 233 |
"""Create a human-readable label for a gallery entry."""
|
| 234 |
source = entry.get("source", "")
|
|
|
|
|
|
|
| 235 |
parts = key.split("_")
|
| 236 |
if source == "BCI":
|
|
|
|
| 237 |
her2_class = parts[2] if len(parts) > 2 else ""
|
| 238 |
sample_id = parts[3] if len(parts) > 3 else ""
|
| 239 |
return f"BCI - HER2 {her2_class} (#{sample_id})"
|
| 240 |
else:
|
|
|
|
| 241 |
stain = parts[1] if len(parts) > 1 else ""
|
| 242 |
sample_id = parts[2] if len(parts) > 2 else ""
|
| 243 |
return f"MIST - {stain} ({sample_id})"
|
| 244 |
|
| 245 |
|
| 246 |
def show_gallery(display_name, gallery, name_map):
|
| 247 |
+
"""Show a gallery example by its display name. Only model-generated outputs shown."""
|
| 248 |
key = name_map.get(display_name)
|
| 249 |
if not key or not gallery or key not in gallery:
|
| 250 |
+
return None, None, None, None, None, ""
|
| 251 |
entry = gallery[key]
|
| 252 |
base = GALLERY_DIR / "images"
|
| 253 |
+
# Only show H&E input and model-generated stains (no ground truth β licensing)
|
| 254 |
he = Image.open(base / entry["he"]).convert("RGB") if "he" in entry else None
|
|
|
|
| 255 |
gen_her2 = Image.open(base / entry["gen_her2"]).convert("RGB") if "gen_her2" in entry else None
|
| 256 |
gen_ki67 = Image.open(base / entry["gen_ki67"]).convert("RGB") if "gen_ki67" in entry else None
|
| 257 |
gen_er = Image.open(base / entry["gen_er"]).convert("RGB") if "gen_er" in entry else None
|
|
|
|
| 259 |
|
| 260 |
source = entry.get("source", "Unknown")
|
| 261 |
gt_stain = entry.get("gt_stain", "Unknown")
|
| 262 |
+
info = f"**Source:** {source} | **Original IHC stain:** {gt_stain}"
|
| 263 |
+
return he, gen_her2, gen_ki67, gen_er, gen_pr, info
|
| 264 |
|
| 265 |
|
| 266 |
# ββ Build Gradio App βββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 274 |
gallery_name_map[label] = key
|
| 275 |
gallery_display_names.append(label)
|
| 276 |
|
| 277 |
+
with gr.Blocks(title="UNIStainNet -- Virtual IHC Staining") as demo:
|
| 278 |
|
| 279 |
+
# ββ Header (all inline styles for Gradio 6.x compatibility) ββ
|
| 280 |
gr.HTML("""
|
| 281 |
+
<div style="text-align:center; padding:1.5rem 1rem 0.5rem 1rem;">
|
| 282 |
+
<h1 style="font-size:1.8rem; font-weight:700; margin-bottom:0.3rem;">UNIStainNet</h1>
|
| 283 |
+
<p style="font-size:1.05rem; color:#555; margin-top:0.2rem;">
|
| 284 |
Foundation-Model-Guided Virtual Staining of H&E to IHC
|
| 285 |
</p>
|
| 286 |
</div>
|
| 287 |
+
<p style="text-align:center; color:#555; font-size:0.95rem; margin-bottom:0.8rem;">
|
| 288 |
Translate H&E histopathology images into immunohistochemistry (IHC) stains
|
| 289 |
for breast cancer biomarkers using a single unified deep learning model.
|
| 290 |
+
</p>
|
| 291 |
+
<div style="display:flex; justify-content:center; gap:0.6rem; flex-wrap:wrap; margin-bottom:1rem;">
|
| 292 |
+
<span style="display:inline-block; padding:0.25rem 0.75rem; border-radius:999px;
|
| 293 |
+
font-size:0.8rem; font-weight:600; background:#dce3f9; color:#1a3a8a;">42M Parameters</span>
|
| 294 |
+
<span style="display:inline-block; padding:0.25rem 0.75rem; border-radius:999px;
|
| 295 |
+
font-size:0.8rem; font-weight:600; background:#d4edda; color:#155724;">4 IHC Stains</span>
|
| 296 |
+
<span style="display:inline-block; padding:0.25rem 0.75rem; border-radius:999px;
|
| 297 |
+
font-size:0.8rem; font-weight:600; background:#e8d5f5; color:#5b1a8a;">UNI Foundation Model</span>
|
| 298 |
+
<span style="display:inline-block; padding:0.25rem 0.75rem; border-radius:999px;
|
| 299 |
+
font-size:0.8rem; font-weight:600; background:#f5ddc4; color:#7a3b10;">Single Forward Pass</span>
|
| 300 |
</div>
|
| 301 |
""")
|
| 302 |
|
|
|
|
| 305 |
if not gallery_display_names:
|
| 306 |
gr.Markdown("No pre-computed gallery available.")
|
| 307 |
else:
|
| 308 |
+
gr.Markdown(
|
| 309 |
+
"Browse pre-computed virtual staining results -- **no GPU required**. "
|
| 310 |
+
"Each example shows the H&E input and all 4 generated IHC stains from our unified model."
|
| 311 |
+
)
|
|
|
|
|
|
|
| 312 |
gallery_dropdown = gr.Dropdown(
|
| 313 |
choices=gallery_display_names,
|
| 314 |
value=gallery_display_names[0] if gallery_display_names else None,
|
|
|
|
| 316 |
)
|
| 317 |
gallery_info_box = gr.Markdown(value="")
|
| 318 |
|
| 319 |
+
gr.Markdown("### H&E Input")
|
| 320 |
+
gal_he = gr.Image(type="pil", label="H&E Input", height=300)
|
|
|
|
|
|
|
| 321 |
|
| 322 |
+
gr.Markdown("### Generated IHC Stains")
|
| 323 |
with gr.Row():
|
| 324 |
gal_her2 = gr.Image(type="pil", label="Generated HER2", height=280)
|
| 325 |
gal_ki67 = gr.Image(type="pil", label="Generated Ki67", height=280)
|
| 326 |
+
with gr.Row():
|
| 327 |
gal_er = gr.Image(type="pil", label="Generated ER", height=280)
|
| 328 |
gal_pr = gr.Image(type="pil", label="Generated PR", height=280)
|
| 329 |
|
|
|
|
| 333 |
gallery_dropdown.change(
|
| 334 |
fn=_show_gallery_wrapper,
|
| 335 |
inputs=[gallery_dropdown],
|
| 336 |
+
outputs=[gal_he, gal_her2, gal_ki67, gal_er, gal_pr, gallery_info_box],
|
| 337 |
)
|
| 338 |
|
| 339 |
# Auto-load first example
|
| 340 |
demo.load(
|
| 341 |
+
fn=lambda: _show_gallery_wrapper(gallery_display_names[0]) if gallery_display_names else (None,) * 6,
|
| 342 |
+
outputs=[gal_he, gal_her2, gal_ki67, gal_er, gal_pr, gallery_info_box],
|
| 343 |
)
|
| 344 |
|
| 345 |
# ββ Tab 2: Single Stain ββββββββββββββββββββββββββββββββββββββ
|
| 346 |
with gr.Tab("Virtual Staining", id="inference"):
|
| 347 |
if not GPU_AVAILABLE and not HAS_SPACES:
|
| 348 |
+
gr.HTML(
|
| 349 |
+
'<div style="background:#fff8e1; border:1px solid #ffe082; border-radius:8px; '
|
| 350 |
+
'padding:0.75rem 1rem; margin-bottom:1rem; font-size:0.9rem; color:#6d4c00;">'
|
| 351 |
+
f'{NO_GPU_MSG}</div>'
|
| 352 |
+
)
|
| 353 |
else:
|
| 354 |
+
gr.Markdown(
|
| 355 |
+
"Upload an H&E image and select a target IHC stain. "
|
| 356 |
+
"The model generates the virtual stain in a single forward pass (~1 second on GPU)."
|
| 357 |
+
)
|
|
|
|
|
|
|
| 358 |
with gr.Row():
|
| 359 |
with gr.Column(scale=1):
|
| 360 |
input_image = gr.Image(type="pil", label="Upload H&E Image", height=380)
|
|
|
|
| 382 |
# ββ Tab 3: Cross-Stain βββββββββββββββββββββββββββββββββββββββ
|
| 383 |
with gr.Tab("Cross-Stain Comparison", id="cross-stain"):
|
| 384 |
if not GPU_AVAILABLE and not HAS_SPACES:
|
| 385 |
+
gr.HTML(
|
| 386 |
+
'<div style="background:#fff8e1; border:1px solid #ffe082; border-radius:8px; '
|
| 387 |
+
'padding:0.75rem 1rem; margin-bottom:1rem; font-size:0.9rem; color:#6d4c00;">'
|
| 388 |
+
f'{NO_GPU_MSG}</div>'
|
| 389 |
+
)
|
| 390 |
else:
|
| 391 |
+
gr.Markdown(
|
| 392 |
+
"Generate **all 4 IHC stains** from a single H&E input. "
|
| 393 |
+
"This demonstrates the unified multi-stain capability of UNIStainNet."
|
| 394 |
+
)
|
|
|
|
|
|
|
| 395 |
with gr.Row():
|
| 396 |
with gr.Column(scale=1):
|
| 397 |
cross_input = gr.Image(type="pil", label="Upload H&E Image", height=300)
|
|
|
|
| 404 |
cross_btn = gr.Button("Generate All 4 Stains", variant="primary", size="lg")
|
| 405 |
cross_time = gr.Textbox(label="Status", interactive=False)
|
| 406 |
|
| 407 |
+
gr.Markdown("### Results")
|
| 408 |
with gr.Row():
|
| 409 |
cross_he_out = gr.Image(type="pil", label="H&E Input", height=250)
|
| 410 |
cross_her2 = gr.Image(type="pil", label="HER2", height=250)
|
|
|
|
| 422 |
with gr.Tab("About", id="about"):
|
| 423 |
gr.Markdown(
|
| 424 |
"""
|
| 425 |
+
## UNIStainNet: Foundation-Model-Guided Virtual Staining
|
| 426 |
+
|
| 427 |
+
UNIStainNet is a deep learning model for **virtual immunohistochemistry (IHC) staining**
|
| 428 |
+
from standard hematoxylin & eosin (H&E) histopathology images. It translates routine H&E
|
| 429 |
+
slides into IHC stains for four clinically important breast cancer biomarkers:
|
| 430 |
+
**HER2**, **Ki67**, **ER**, and **PR**.
|
| 431 |
+
|
| 432 |
+
### Why Virtual Staining?
|
| 433 |
+
|
| 434 |
+
- **Tissue conservation** -- eliminates the need for additional serial sections
|
| 435 |
+
- **Faster turnaround** -- results in seconds instead of hours/days
|
| 436 |
+
- **Cost reduction** -- one H&E slide replaces multiple IHC tests for screening
|
| 437 |
+
- **Consistency** -- no batch-to-batch staining variability
|
| 438 |
+
|
| 439 |
+
### How It Works
|
| 440 |
+
|
| 441 |
+
The model uses a **SPADE-UNet generator** conditioned on dense spatial features from a
|
| 442 |
+
frozen [UNI](https://github.com/mahmoodlab/UNI) pathology foundation model (ViT-L/16,
|
| 443 |
+
pretrained on 100M+ histopathology patches). A FiLM-based stain embedding allows a
|
| 444 |
+
**single unified model** to generate all 4 IHC stains.
|
| 445 |
+
|
| 446 |
+
| Component | Details |
|
| 447 |
+
|-----------|---------|
|
| 448 |
+
| **Generator** | SPADE-UNet with UNI spatial conditioning + FiLM stain embeddings |
|
| 449 |
+
| **Foundation Model** | UNI ViT-L/16 (frozen, 303M parameters) |
|
| 450 |
+
| **Spatial Tokens** | 4x4 sub-crop tiling of H&E input, yielding 32x32 = 1,024 tokens |
|
| 451 |
+
| **Generator Parameters** | 42M |
|
| 452 |
+
| **Inference** | Single forward pass (~1 second on GPU) |
|
| 453 |
+
|
| 454 |
+
### Quantitative Results (MIST Dataset, Unified Model)
|
| 455 |
+
|
| 456 |
+
| Stain | FID | KID x1k | Pearson-R | DAB KL |
|
| 457 |
+
|-------|-----|---------|-----------|--------|
|
| 458 |
+
| HER2 | 34.5 | 2.2 | 0.929 | 0.166 |
|
| 459 |
+
| Ki67 | 27.2 | 1.8 | 0.927 | 0.119 |
|
| 460 |
+
| ER | 29.2 | 1.8 | 0.949 | 0.182 |
|
| 461 |
+
| PR | 29.0 | 1.1 | 0.943 | 0.171 |
|
| 462 |
+
|
| 463 |
+
### Key Innovations
|
| 464 |
+
|
| 465 |
+
- **Dense UNI spatial conditioning**: Unlike prior methods that use global image features,
|
| 466 |
+
UNIStainNet extracts spatially-resolved features at 32x32 resolution, enabling the generator
|
| 467 |
+
to leverage fine-grained morphological context from the pathology foundation model.
|
| 468 |
+
- **Misalignment-aware training**: Because H&E and IHC are cut from consecutive tissue sections
|
| 469 |
+
(not the same section), there are inherent spatial shifts. Our loss suite (perceptual loss,
|
| 470 |
+
DAB intensity supervision, unconditional discriminator) is designed to handle this misalignment.
|
| 471 |
+
- **Classifier-free guidance (CFG)**: 10% class dropout and 10% UNI dropout during training
|
| 472 |
+
enables tunable generation strength at inference time.
|
| 473 |
+
|
| 474 |
+
### Disclaimer
|
| 475 |
+
|
| 476 |
+
This is a **research tool** for exploratory analysis. It is not intended for clinical diagnosis
|
| 477 |
+
and has not undergone regulatory validation. Generated stains should not be used for treatment decisions.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
"""
|
| 479 |
)
|
| 480 |
|
| 481 |
# ββ Footer βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 482 |
gr.HTML("""
|
| 483 |
+
<p style="text-align:center; padding:1rem; color:#999; font-size:0.8rem;">
|
| 484 |
UNIStainNet | Built with Gradio
|
| 485 |
+
</p>
|
| 486 |
""")
|
| 487 |
|
| 488 |
if __name__ == "__main__":
|
| 489 |
+
demo.launch()
|