faceless-void commited on
Commit
75be7d4
Β·
verified Β·
1 Parent(s): 4a80fb4

Fix: inline styles (no CSS dependency), remove GT images (licensing), muted badge colors

Browse files
Files changed (1) hide show
  1. app.py +107 -193
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, 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"**Dataset:** {source} | **Ground truth stain:** {gt_stain}"
350
- return he, gt, gen_her2, gen_ki67, gen_er, gen_pr, info
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 β€” Virtual IHC Staining") as demo:
365
 
366
- # ── Header ──────────────────────────────────────────────────────
367
  gr.HTML("""
368
- <div class="header-container">
369
- <h1>UNIStainNet</h1>
370
- <p style="font-size:1.1rem; color:#555; margin-top:0.2rem;">
371
  Foundation-Model-Guided Virtual Staining of H&amp;E to IHC
372
  </p>
373
  </div>
374
- <div class="header-subtitle">
375
  Translate H&amp;E histopathology images into immunohistochemistry (IHC) stains
376
  for breast cancer biomarkers using a single unified deep learning model.
377
- </div>
378
- <div class="badge-row">
379
- <span class="badge">42M Parameters</span>
380
- <span class="badge-green badge">4 IHC Stains</span>
381
- <span class="badge-purple badge">UNI Foundation Model</span>
382
- <span class="badge-orange badge">Single Forward Pass</span>
 
 
 
 
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.HTML("""
392
- <div class="gallery-info">
393
- Browse pre-computed virtual staining results β€” <strong>no GPU required</strong>.
394
- Each example shows the H&amp;E input, ground truth IHC, and all 4 generated stains from our unified model.
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.HTML('<p class="section-heading">Input & Ground Truth</p>')
405
- with gr.Row():
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.HTML('<p class="section-heading">Generated IHC Stains (all from the same H&E)</p>')
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, gal_gt, gal_her2, gal_ki67, gal_er, gal_pr, gallery_info_box],
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,) * 7,
428
- outputs=[gal_he, gal_gt, gal_her2, gal_ki67, gal_er, gal_pr, gallery_info_box],
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(f'<div class="gpu-notice">{NO_GPU_MSG}</div>')
 
 
 
 
435
  else:
436
- gr.HTML("""
437
- <div class="gallery-info">
438
- Upload an H&amp;E image and select a target IHC stain. The model generates
439
- the virtual stain in a single forward pass (~1 second on GPU).
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(f'<div class="gpu-notice">{NO_GPU_MSG}</div>')
 
 
 
 
470
  else:
471
- gr.HTML("""
472
- <div class="gallery-info">
473
- Generate <strong>all 4 IHC stains</strong> from a single H&amp;E input.
474
- This demonstrates the unified multi-stain capability of UNIStainNet.
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.HTML('<p class="section-heading">Results</p>')
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
- <div class="about-section">
508
-
509
- ## UNIStainNet: Foundation-Model-Guided Virtual Staining
510
-
511
- UNIStainNet is a deep learning model for **virtual immunohistochemistry (IHC) staining**
512
- from standard hematoxylin & eosin (H&E) histopathology images. It translates routine H&E
513
- slides into IHC stains for four clinically important breast cancer biomarkers:
514
- **HER2**, **Ki67**, **ER**, and **PR**.
515
-
516
- ### Why Virtual Staining?
517
-
518
- - **Tissue conservation** β€” eliminates the need for additional serial sections
519
- - **Faster turnaround** β€” results in seconds instead of hours/days
520
- - **Cost reduction** β€” one H&E slide replaces multiple IHC tests for screening
521
- - **Consistency** β€” no batch-to-batch staining variability
522
-
523
- ### How It Works
524
-
525
- The model uses a **SPADE-UNet generator** conditioned on dense spatial features from a
526
- frozen [UNI](https://github.com/mahmoodlab/UNI) pathology foundation model (ViT-L/16,
527
- pretrained on 100M+ histopathology patches). A FiLM-based stain embedding allows a
528
- **single unified model** to generate all 4 IHC stains.
529
-
530
- | Component | Details |
531
- |-----------|---------|
532
- | **Generator** | SPADE-UNet with UNI spatial conditioning + FiLM stain embeddings |
533
- | **Foundation Model** | UNI ViT-L/16 (frozen, 303M parameters) |
534
- | **Spatial Tokens** | 4x4 sub-crop tiling of H&E input, yielding 32x32 = 1,024 tokens |
535
- | **Generator Parameters** | 42M |
536
- | **Inference** | Single forward pass (~1 second on GPU) |
537
-
538
- ### Quantitative Results (MIST Dataset, Unified Model)
539
-
540
- | Stain | FID | KID x1k | Pearson-R | DAB KL |
541
- |-------|-----|---------|-----------|--------|
542
- | HER2 | 34.5 | 2.2 | 0.929 | 0.166 |
543
- | Ki67 | 27.2 | 1.8 | 0.927 | 0.119 |
544
- | ER | 29.2 | 1.8 | 0.949 | 0.182 |
545
- | PR | 29.0 | 1.1 | 0.943 | 0.171 |
546
-
547
- ### Key Innovations
548
-
549
- - **Dense UNI spatial conditioning**: Unlike prior methods that use global image features,
550
- UNIStainNet extracts spatially-resolved features at 32x32 resolution, enabling the generator
551
- to leverage fine-grained morphological context from the pathology foundation model.
552
- - **Misalignment-aware training**: Because H&E and IHC are cut from consecutive tissue sections
553
- (not the same section), there are inherent spatial shifts. Our loss suite (perceptual loss,
554
- DAB intensity supervision, unconditional discriminator) is designed to handle this misalignment.
555
- - **Classifier-free guidance (CFG)**: 10% class dropout and 10% UNI dropout during training
556
- enables tunable generation strength at inference time.
557
-
558
- ### Disclaimer
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
- <footer>
570
  UNIStainNet | Built with Gradio
571
- </footer>
572
  """)
573
 
574
  if __name__ == "__main__":
575
- demo.launch(theme=gr.themes.Soft(), css=CUSTOM_CSS)
 
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&amp;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&amp;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()