Spaces:
Runtime error
Runtime error
File size: 27,054 Bytes
67f1c25 b23df0d 67f1c25 621b022 67f1c25 621b022 67f1c25 621b022 67f1c25 621b022 67f1c25 621b022 67f1c25 621b022 67f1c25 621b022 67f1c25 621b022 67f1c25 621b022 a335483 5b27f23 a335483 5b27f23 a335483 5b27f23 a335483 eda3bb5 5b27f23 eda3bb5 a335483 eda3bb5 5b27f23 eda3bb5 a335483 621b022 67f1c25 621b022 67f1c25 621b022 a335483 621b022 a335483 621b022 a335483 621b022 a335483 621b022 a335483 621b022 a335483 621b022 eda3bb5 621b022 a335483 621b022 67f1c25 b23df0d eda3bb5 5b27f23 eda3bb5 621b022 a335483 621b022 a335483 621b022 b23df0d 67f1c25 e9f2fc6 67f1c25 621b022 67f1c25 a990302 a335483 eda3bb5 a335483 621b022 67f1c25 5b27f23 eda3bb5 5b27f23 eda3bb5 5b27f23 eda3bb5 5b27f23 eda3bb5 a335483 621b022 67f1c25 e9f2fc6 621b022 5b27f23 eda3bb5 5b27f23 eda3bb5 e9f2fc6 9ee2c7e 5aa52c3 621b022 a990302 5aa52c3 67f1c25 621b022 5aa52c3 e9f2fc6 b23df0d 9ee2c7e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 | import json
from pathlib import Path
from typing import Any
import gradio as gr
from src.inference import (
CASE_OPTIONS,
DEFAULT_CASE_NAME,
DEFAULT_GRADCAM_TARGET_LAYER,
DEVICE,
GRADCAM_TARGET_LAYER_OPTIONS,
batch_predict_with_xdl_stream,
)
from src.utils import clean_drive_downloads, normalize_uploaded_paths
UI_DEFAULTS_PATH = Path(__file__).resolve().parent.parent / "config" / "ui_defaults.json"
UI_DEFAULTS_FALLBACK = {
"selected_case": DEFAULT_CASE_NAME,
"confidence_threshold": 0.60,
"smoothgrad_samples": 50,
"smoothgrad_noise": 0.05,
"gradcam_target_layer": DEFAULT_GRADCAM_TARGET_LAYER,
"save_xdl_results": False,
"save_xdl_dir": "xdl_results",
}
GRADCAM_TARGET_LAYER_DROPDOWN_CHOICES = [
("DenseBlock 3 (Default, balanced)", "denseblock3"),
("Transition 2 (Broad, stable)", "transition2"),
("Transition 1 (Earlier, detailed/noisier)", "transition1"),
("DenseBlock 4 (Late, center-heavy)", "denseblock4"),
("Transition 3 (Late, center-heavy)", "transition3"),
("Norm5 Last (Legacy behavior)", "norm5_last"),
]
CUSTOM_CSS = """
.app-shell {
--xdl-bg: #ffffff;
--xdl-surface: #f8fafc;
--xdl-text: #0f172a;
--xdl-muted: #475569;
--xdl-border: #d1d5db;
--xdl-primary: #0f766e;
--xdl-primary-strong: #0f4f4a;
--xdl-accent: #0ea5e9;
--xdl-success-bg: #dcfce7;
--xdl-success-text: #166534;
--xdl-warning-bg: #fef3c7;
--xdl-warning-text: #92400e;
max-width: 1120px;
margin: 0 auto;
color: var(--xdl-text);
}
.hero {
border: 1px solid var(--xdl-border);
background: linear-gradient(135deg, #f0fdfa 0%, #ecfeff 44%, #ffffff 100%);
border-radius: 18px;
padding: 20px 22px;
margin-bottom: 12px;
color: var(--xdl-text);
box-shadow: 0 18px 45px rgba(15, 23, 42, 0.08);
}
.hero-top {
display: flex;
justify-content: space-between;
gap: 18px;
align-items: flex-start;
}
.hero-kicker {
color: var(--xdl-primary);
font-size: 12px;
font-weight: 900;
letter-spacing: 0.12em;
margin-bottom: 6px;
text-transform: uppercase;
}
.hero h1 {
margin: 0;
font-size: 30px;
line-height: 1.1;
color: var(--xdl-text);
}
.hero p {
margin: 8px 0 0 0;
color: #334155;
font-size: 15px;
line-height: 1.5;
max-width: 720px;
}
.hero-pills {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
min-width: 220px;
}
.hero-pill {
border: 1px solid #99f6e4;
border-radius: 999px;
background: #ffffff;
color: var(--xdl-primary-strong);
font-size: 12px;
font-weight: 900;
padding: 7px 10px;
white-space: nowrap;
}
.panel {
border: 1px solid #e2e8f0;
border-radius: 12px;
background: var(--xdl-bg);
color: var(--xdl-text);
padding: 12px;
}
.dashboard-shell {
align-items: flex-start;
flex-direction: row-reverse;
gap: 16px;
}
.sidebar-panel {
border-radius: 20px;
padding: 16px;
position: sticky;
top: 12px;
}
.workspace-panel {
border-radius: 20px;
padding: 18px;
min-height: 680px;
}
.control-section {
border: 1px solid #e2e8f0;
border-radius: 15px;
background: #f8fafc;
margin-bottom: 12px;
padding: 12px;
}
.control-section-title {
color: var(--xdl-primary-strong);
font-size: 12px;
font-weight: 900;
letter-spacing: 0.08em;
margin-bottom: 10px;
text-transform: uppercase;
}
.workspace-title {
display: flex;
align-items: center !important;
justify-content: space-between;
gap: 14px;
margin-bottom: 14px;
}
.workspace-title-copy {
display: flex;
align-items: center;
flex: 1 1 auto;
min-height: 36px;
min-width: 0;
}
.workspace-title h2 {
color: var(--xdl-text);
font-size: 20px;
line-height: 1.2;
margin: 0;
}
.presentation-toggle {
align-self: center !important;
flex: 0 0 auto !important;
min-width: fit-content;
}
.presentation-toggle button {
align-items: center !important;
border: 1px solid #99f6e4 !important;
border-radius: 999px !important;
background: linear-gradient(135deg, #ffffff, #f0fdfa) !important;
color: var(--xdl-primary-strong) !important;
display: inline-flex !important;
font-size: 12px !important;
font-weight: 900 !important;
justify-content: center !important;
line-height: 1 !important;
margin: 0 !important;
min-height: 36px !important;
padding: 0 14px !important;
white-space: nowrap;
}
.presentation-toggle button:hover {
border-color: var(--xdl-primary) !important;
box-shadow: 0 8px 20px rgba(15, 118, 110, 0.16) !important;
}
.empty-state {
border: 1px dashed #cbd5e1;
border-radius: 18px;
background: linear-gradient(135deg, #ffffff, #f8fafc);
color: var(--xdl-muted);
padding: 28px;
text-align: center;
}
.empty-state-title {
color: var(--xdl-text);
font-size: 18px;
font-weight: 900;
margin-bottom: 8px;
}
.empty-state-copy {
font-size: 14px;
line-height: 1.5;
margin: 0 auto;
max-width: 520px;
}
.summary-card {
border: 1px solid #d1d5db;
border-radius: 18px;
background: #ffffff;
color: var(--xdl-text);
padding: 16px;
box-shadow: 0 14px 35px rgba(15, 23, 42, 0.06);
}
.summary-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 14px;
}
.summary-title h3 {
color: var(--xdl-text);
font-size: 18px;
margin: 0;
}
.summary-title span {
border-radius: 999px;
background: #ecfeff;
color: var(--xdl-primary-strong);
font-size: 12px;
font-weight: 900;
padding: 6px 10px;
}
.metric-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 10px;
margin-bottom: 12px;
}
.metric-card {
border: 1px solid #e2e8f0;
border-radius: 14px;
background: #f8fafc;
padding: 12px;
}
.metric-label {
color: #64748b;
font-size: 12px;
font-weight: 800;
margin-bottom: 6px;
}
.metric-value {
color: var(--xdl-text);
font-size: 20px;
font-weight: 900;
line-height: 1.15;
word-break: break-word;
}
.metric-value.small {
font-size: 16px;
}
.summary-details {
border-top: 1px solid #e5e7eb;
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 8px 16px;
padding-top: 12px;
}
.summary-detail {
color: #334155;
font-size: 13px;
line-height: 1.4;
}
.summary-detail b {
color: var(--xdl-text);
}
.panel :is(h1, h2, h3, h4, h5, h6, p, label, span, th, td) {
color: inherit;
}
.panel .secondary-wrap,
.panel .prose,
.panel .markdown {
color: var(--xdl-text);
}
.xdl-report {
display: flex;
flex-direction: column;
gap: 14px;
color: var(--xdl-text);
}
.processing-status {
border: 1px solid #dbeafe;
background: #eff6ff;
border-radius: 14px;
color: var(--xdl-text);
padding: 14px;
margin: 12px 0;
}
.processing-status-title {
font-size: 16px;
font-weight: 800;
margin-bottom: 10px;
}
.processing-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
}
.progress-card {
border: 1px solid #bfdbfe;
border-radius: 12px;
background: #ffffff;
color: var(--xdl-text);
padding: 12px;
}
.progress-card-head {
display: flex;
justify-content: space-between;
gap: 12px;
align-items: center;
font-size: 14px;
margin-bottom: 8px;
}
.progress-card-head span {
color: #075985;
font-weight: 800;
}
.progress-message {
color: #334155;
font-size: 13px;
line-height: 1.4;
margin-bottom: 10px;
}
.progress-track {
height: 10px;
border-radius: 999px;
background: #dbeafe;
overflow: hidden;
}
.progress-fill {
height: 100%;
border-radius: 999px;
background: linear-gradient(90deg, var(--xdl-primary), var(--xdl-accent));
transition: width 180ms ease;
}
.xdl-guide {
border: 1px solid #bae6fd;
background: #f0f9ff;
border-radius: 12px;
padding: 14px;
color: var(--xdl-text);
}
.xdl-guide-title {
font-size: 16px;
font-weight: 700;
color: var(--xdl-text);
margin-bottom: 10px;
}
.xdl-guide-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 10px;
}
.xdl-guide-grid div {
border: 1px solid #dbeafe;
background: var(--xdl-bg);
color: var(--xdl-text);
border-radius: 10px;
padding: 10px;
}
.xdl-guide-grid b {
display: block;
color: #075985;
font-size: 14px;
margin-bottom: 4px;
}
.xdl-guide-grid span {
display: block;
color: #334155;
font-size: 13px;
line-height: 1.4;
}
.xdl-report-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: var(--xdl-text);
font-size: 17px;
font-weight: 700;
}
.xdl-report-heading span {
border-radius: 999px;
background: #e0f2fe;
color: #075985;
font-size: 12px;
font-weight: 700;
padding: 5px 10px;
}
.xdl-scroll {
max-height: 820px;
overflow-y: auto;
padding: 2px 8px 2px 2px;
}
.xdl-card {
border: 1px solid #cbd5e1;
border-radius: 18px;
background: linear-gradient(180deg, #ffffff 0%, #fbfdff 100%);
color: var(--xdl-text);
box-shadow: 0 16px 36px rgba(15, 23, 42, 0.09);
margin-bottom: 18px;
padding: 18px;
}
.xdl-card-header {
display: flex;
justify-content: space-between;
gap: 14px;
align-items: flex-start;
margin-bottom: 14px;
}
.xdl-filename {
color: var(--xdl-text);
font-size: 18px;
font-weight: 800;
word-break: break-word;
}
.xdl-card-subtitle {
color: #64748b;
font-size: 13px;
margin-top: 3px;
}
.xdl-meta {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
.xdl-badge,
.xdl-confidence {
border-radius: 999px;
font-size: 13px;
font-weight: 800;
padding: 7px 11px;
white-space: nowrap;
}
.xdl-badge {
background: var(--xdl-success-bg);
color: var(--xdl-success-text);
}
.xdl-confidence {
background: var(--xdl-warning-bg);
color: var(--xdl-warning-text);
}
.xdl-index {
color: var(--xdl-primary);
font-size: 12px;
font-weight: 900;
letter-spacing: 0.08em;
margin-bottom: 5px;
text-transform: uppercase;
}
.xdl-acceptance-note {
border-left: 3px solid var(--xdl-primary);
background: #f0fdfa;
border-radius: 10px;
color: #334155;
font-size: 13px;
line-height: 1.4;
margin: 12px 0 14px 0;
padding: 9px 11px;
}
.xdl-panel-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 12px;
}
.xdl-panel {
border: 1px solid #e2e8f0;
border-radius: 14px;
background: var(--xdl-surface);
color: var(--xdl-text);
overflow: hidden;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.75);
}
.xdl-panel-title {
background: #0f172a;
color: #ffffff;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.02em;
padding: 8px 10px;
text-align: center;
}
.xdl-panel img {
display: block;
width: 100%;
height: auto;
background: #ffffff;
transition: transform 160ms ease, filter 160ms ease;
}
.xdl-image-link {
color: inherit;
display: block;
position: relative;
text-decoration: none;
}
.xdl-image-link:hover img {
filter: saturate(1.05) contrast(1.03);
transform: scale(1.01);
}
.xdl-image-hint {
border: 1px solid rgba(255, 255, 255, 0.45);
border-radius: 999px;
background: rgba(15, 23, 42, 0.78);
bottom: 9px;
color: #ffffff !important;
font-size: 11px;
font-weight: 900;
opacity: 0;
padding: 5px 8px;
pointer-events: none;
position: absolute;
right: 9px;
transform: translateY(4px);
transition: opacity 160ms ease, transform 160ms ease;
}
.xdl-image-link:hover .xdl-image-hint {
opacity: 1;
transform: translateY(0);
}
.xdl-empty {
border: 1px dashed #cbd5e1;
border-radius: 14px;
background: var(--xdl-surface);
color: var(--xdl-muted);
padding: 24px;
text-align: center;
}
.xdl-empty-title {
color: var(--xdl-text);
font-size: 16px;
font-weight: 800;
margin-bottom: 6px;
}
.xdl-status {
color: var(--xdl-muted);
font-size: 13px;
text-align: right;
}
@media (max-width: 900px) {
.sidebar-panel {
position: static;
}
.metric-grid,
.summary-details {
grid-template-columns: 1fr;
}
.hero-top {
flex-direction: column;
}
.hero-pills {
justify-content: flex-start;
}
.processing-grid,
.xdl-guide-grid,
.xdl-panel-grid {
grid-template-columns: 1fr;
}
.xdl-card-header {
flex-direction: column;
}
.xdl-meta {
justify-content: flex-start;
}
}
.action-stack {
display: flex;
flex-direction: column;
gap: 8px;
}
.action-row {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 8px;
align-items: stretch;
}
.run-action button {
font-weight: 900 !important;
min-height: 44px;
}
.clean-action button {
min-height: 44px;
}
.action-note {
border: 1px solid #ccfbf1;
border-radius: 10px;
background: #f0fdfa;
color: #0f4f4a;
font-size: 12px;
font-weight: 700;
line-height: 1.35;
padding: 9px 10px;
}
"""
def _as_float(value: Any, fallback: float) -> float:
try:
return float(value)
except (TypeError, ValueError):
return float(fallback)
def _as_int(value: Any, fallback: int) -> int:
try:
return int(value)
except (TypeError, ValueError):
return int(fallback)
def _as_bool(value: Any, fallback: bool) -> bool:
if isinstance(value, bool):
return value
if isinstance(value, str):
return value.strip().lower() in {"1", "true", "yes", "y", "on"}
if value is None:
return fallback
return bool(value)
def _load_ui_defaults() -> dict[str, Any]:
defaults = dict(UI_DEFAULTS_FALLBACK)
try:
raw_text = UI_DEFAULTS_PATH.read_text(encoding="utf-8")
raw = json.loads(raw_text)
if isinstance(raw, dict):
for key in defaults:
if key in raw:
defaults[key] = raw[key]
except Exception:
pass
selected_case = str(defaults.get("selected_case", DEFAULT_CASE_NAME))
defaults["selected_case"] = selected_case if selected_case in CASE_OPTIONS else DEFAULT_CASE_NAME
defaults["confidence_threshold"] = min(
1.0,
max(0.0, _as_float(defaults.get("confidence_threshold"), UI_DEFAULTS_FALLBACK["confidence_threshold"])),
)
defaults["smoothgrad_samples"] = max(
1,
_as_int(defaults.get("smoothgrad_samples"), UI_DEFAULTS_FALLBACK["smoothgrad_samples"]),
)
defaults["smoothgrad_noise"] = min(
1.0,
max(0.0, _as_float(defaults.get("smoothgrad_noise"), UI_DEFAULTS_FALLBACK["smoothgrad_noise"])),
)
gradcam_target_layer = str(defaults.get("gradcam_target_layer", DEFAULT_GRADCAM_TARGET_LAYER)).strip().lower()
defaults["gradcam_target_layer"] = (
gradcam_target_layer if gradcam_target_layer in GRADCAM_TARGET_LAYER_OPTIONS else DEFAULT_GRADCAM_TARGET_LAYER
)
defaults["save_xdl_results"] = _as_bool(defaults.get("save_xdl_results"), UI_DEFAULTS_FALLBACK["save_xdl_results"])
defaults["save_xdl_dir"] = str(defaults.get("save_xdl_dir") or UI_DEFAULTS_FALLBACK["save_xdl_dir"])
return defaults
def _toggle_save_dir(enabled: bool):
is_enabled = bool(enabled)
return gr.update(visible=is_enabled, interactive=is_enabled)
def _toggle_presentation_view(enabled: bool):
next_enabled = not bool(enabled)
label = "Exit Presentation View" if next_enabled else "Enter Presentation View"
variant = "primary" if next_enabled else "secondary"
return next_enabled, gr.update(visible=not next_enabled), gr.update(value=label, variant=variant)
def _loaded_images_from_upload(uploaded_files):
image_paths = normalize_uploaded_paths(uploaded_files)
names = [path.name for path in image_paths]
stored_paths = [str(path) for path in image_paths]
return gr.update(choices=names, value=names[0] if names else None, interactive=bool(names)), stored_paths
def _empty_summary_html() -> str:
return (
"<div class='empty-state'>"
"<div class='empty-state-title'>No Results Yet</div>"
"<div class='empty-state-copy'>Upload an image folder or enter a public Google Drive folder link, "
"then run inference to see the batch diagnosis summary.</div>"
"</div>"
)
def _empty_processing_html() -> str:
return (
"<div class='empty-state'>"
"<div class='empty-state-title'>Processing Status Will Appear Here</div>"
"<div class='empty-state-copy'>Input preparation, Drive download progress, and XDL generation progress "
"will stream here while the model runs.</div>"
"</div>"
)
def _empty_xdl_html() -> str:
return (
"<div class='empty-state'>"
"<div class='empty-state-title'>XDL Cards Not Generated Yet</div>"
"<div class='empty-state-copy'>Classified images will appear as presentation-ready Original, GradCAM, "
"and SmoothGrad cards after inference completes.</div>"
"</div>"
)
def _reset_demo():
clean_drive_downloads()
ui_defaults = _load_ui_defaults()
save_enabled = bool(ui_defaults["save_xdl_results"])
return (
ui_defaults["selected_case"],
None,
[],
"",
gr.update(choices=[], value=None, interactive=False),
ui_defaults["confidence_threshold"],
ui_defaults["smoothgrad_samples"],
ui_defaults["smoothgrad_noise"],
ui_defaults["gradcam_target_layer"],
ui_defaults["save_xdl_results"],
gr.update(
value=ui_defaults["save_xdl_dir"],
visible=save_enabled,
interactive=save_enabled,
),
_empty_summary_html(),
_empty_processing_html(),
_empty_xdl_html(),
[],
)
def build_demo() -> gr.Blocks:
ui_defaults = _load_ui_defaults()
with gr.Blocks(title="XDL Colitis Demo") as demo:
gr.HTML(
f"""
<style>{CUSTOM_CSS}</style>
<div class="app-shell">
<div class="hero">
<div class="hero-top">
<div class="hero-copy">
<div class="hero-kicker">Medical AI Explanation Demo</div>
<h1>XDL Colitis Workbench</h1>
<p>DenseNet-based colitis classification with GradCAM and SmoothGrad explanations. Upload a directory or use a public Google Drive folder link, then run batch inference.</p>
</div>
<div class="hero-pills">
<span class="hero-pill">Device: {DEVICE.type}</span>
<span class="hero-pill">3 Case Modes</span>
<span class="hero-pill">Folder / Drive Input</span>
</div>
</div>
</div>
</div>
"""
)
with gr.Row(elem_classes=["app-shell", "dashboard-shell"]):
with gr.Column(scale=1, min_width=330, elem_classes=["panel", "sidebar-panel"]) as sidebar_panel:
gr.HTML("<div class='control-section-title'>Data Source</div>")
with gr.Group(elem_classes=["control-section"]):
upload_input = gr.UploadButton(
label="Select Image Folder",
variant="secondary",
file_count="directory",
type="filepath",
)
google_drive_link = gr.Textbox(
label="Google Drive Link (Optional)",
placeholder="Public Google Drive folder link",
)
loaded_images = gr.Dropdown(
choices=[],
label="Loaded Images",
info="Filenames detected from upload or Google Drive after processing starts.",
interactive=False,
)
uploaded_files_state = gr.State([])
folder_path = gr.State("")
gr.HTML("<div class='control-section-title'>Model Case</div>")
with gr.Group(elem_classes=["control-section"]):
selected_case = gr.Dropdown(
choices=CASE_OPTIONS,
value=ui_defaults["selected_case"],
label="Problem Case",
info="Choose the model group that matches your diagnosis scenario.",
)
gr.HTML("<div class='control-section-title'>Inference Settings</div>")
with gr.Group(elem_classes=["control-section"]):
threshold = gr.Number(
value=ui_defaults["confidence_threshold"],
minimum=0.0,
maximum=1.0,
step=0.01,
precision=2,
label="Confidence Threshold",
info="Range: 0.00 to 1.00",
)
gradcam_target_layer = gr.Dropdown(
choices=GRADCAM_TARGET_LAYER_DROPDOWN_CHOICES,
value=ui_defaults["gradcam_target_layer"],
label="GradCAM Target Layer",
info="Try `transition2` or `denseblock3` if CAM looks too centered.",
)
smoothgrad_samples = gr.Number(
value=ui_defaults["smoothgrad_samples"],
minimum=1,
maximum=1000,
step=1,
precision=0,
label="SmoothGrad Samples",
info="Higher values improve stability but increase runtime.",
)
smoothgrad_noise = gr.Number(
value=ui_defaults["smoothgrad_noise"],
minimum=0.0,
maximum=1.0,
step=0.01,
precision=2,
label="SmoothGrad Noise Level",
info="Typical range: 0.01 to 0.20",
)
gr.HTML("<div class='control-section-title'>Export</div>")
with gr.Group(elem_classes=["control-section"]):
save_xdl_results = gr.Checkbox(
label="Save XDL Results Locally",
value=ui_defaults["save_xdl_results"],
)
save_xdl_dir = gr.Textbox(
label="Save Folder",
value=ui_defaults["save_xdl_dir"],
placeholder="xdl_results",
visible=bool(ui_defaults["save_xdl_results"]),
interactive=bool(ui_defaults["save_xdl_results"]),
)
with gr.Column(elem_classes=["action-stack"]):
with gr.Row(elem_classes=["action-row"]):
run_btn = gr.Button(
"Run / Rerun Inference",
variant="primary",
elem_classes=["run-action"],
)
clean_btn = gr.Button("Clean", variant="secondary", elem_classes=["clean-action"])
gr.HTML(
"<div class='action-note'>Runs classification first, then streams XDL overlays as each image finishes.</div>"
)
with gr.Column(scale=3, elem_classes=["panel", "workspace-panel"]):
with gr.Row(elem_classes=["workspace-title"]):
gr.HTML(
"<div class='workspace-title-copy'><h2>Results Workspace</h2></div>"
)
presentation_state = gr.State(False)
presentation_btn = gr.Button(
"Enter Presentation View",
variant="secondary",
elem_classes=["presentation-toggle"],
scale=0,
min_width=178,
)
summary_out = gr.HTML(value=_empty_summary_html(), label="Summary")
processing_status_out = gr.HTML(value=_empty_processing_html(), label="Processing Status")
xdl_report_out = gr.HTML(value=_empty_xdl_html(), label="Classified XDL Results")
with gr.Accordion("Detailed Per-Image Table", open=False):
table_out = gr.Dataframe(
headers=["filename", "status", "predicted_label", "confidence_or_error"],
datatype=["str", "str", "str", "str"],
interactive=False,
label="Per-image Results",
)
save_xdl_results.change(
fn=_toggle_save_dir,
inputs=[save_xdl_results],
outputs=[save_xdl_dir],
)
upload_input.upload(
fn=_loaded_images_from_upload,
inputs=[upload_input],
outputs=[loaded_images, uploaded_files_state],
)
presentation_btn.click(
fn=_toggle_presentation_view,
inputs=[presentation_state],
outputs=[presentation_state, sidebar_panel, presentation_btn],
)
run_btn.click(
fn=batch_predict_with_xdl_stream,
inputs=[
uploaded_files_state,
selected_case,
folder_path,
threshold,
smoothgrad_samples,
smoothgrad_noise,
save_xdl_results,
save_xdl_dir,
gradcam_target_layer,
google_drive_link,
],
outputs=[summary_out, processing_status_out, xdl_report_out, loaded_images, table_out],
)
clean_btn.click(
fn=_reset_demo,
inputs=[],
outputs=[
selected_case,
upload_input,
uploaded_files_state,
google_drive_link,
loaded_images,
threshold,
smoothgrad_samples,
smoothgrad_noise,
gradcam_target_layer,
save_xdl_results,
save_xdl_dir,
summary_out,
processing_status_out,
xdl_report_out,
table_out,
],
)
return demo.queue()
|