Spaces:
Sleeping
Sleeping
File size: 30,828 Bytes
5371cce | 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 | <!DOCTYPE html>
<html lang="en" class="h-full bg-slate-900 text-slate-100">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI-Assisted Diagnostic Support Platform</title>
<!-- Tailwind CSS Play CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
theme: {
extend: {
colors: {
clinical: {
50: '#f0f7ff',
100: '#e0effe',
500: '#0284c7',
600: '#0369a1',
700: '#075985',
800: '#0c4a6e',
900: '#0a3a5c',
}
}
}
}
}
</script>
<!-- Google Fonts Outfit & Inter -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Outfit:wght@400;600;700;800&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', sans-serif;
}
h1, h2, h3, .font-display {
font-family: 'Outfit', sans-serif;
}
</style>
</head>
<body class="min-h-full flex flex-col justify-between selection:bg-sky-500/30">
<!-- Header -->
<header class="border-b border-slate-800 bg-slate-950/80 backdrop-blur-md sticky top-0 z-50 px-6 py-4 flex items-center justify-between">
<div class="flex items-center gap-3">
<div class="p-2 bg-sky-500/20 text-sky-400 rounded-lg border border-sky-500/30">
<!-- Pulse diagnostic icon (SVG) -->
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 animate-pulse" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
</div>
<div>
<h1 class="text-xl font-bold tracking-tight bg-gradient-to-r from-sky-400 to-indigo-400 bg-clip-text text-transparent">
AI-Assisted Diagnostic Support Platform
</h1>
<p class="text-xs text-slate-400 font-medium">Biomedical Engineering Graduation Project</p>
</div>
</div>
<!-- Health Status -->
<div class="flex items-center gap-2 bg-slate-900 px-3 py-1.5 rounded-full border border-slate-800 text-xs">
<span id="api-status-dot" class="h-2.5 w-2.5 rounded-full bg-emerald-500 animate-pulse"></span>
<span id="api-status-text" class="text-slate-300">Backend API: Online</span>
</div>
</header>
<!-- Main Container -->
<main class="max-w-7xl mx-auto p-6 w-full flex-grow grid grid-cols-1 lg:grid-cols-12 gap-6">
<!-- Left Column: Module & Upload -->
<section class="lg:col-span-5 flex flex-col gap-6">
<!-- Module Selector -->
<div class="bg-slate-950/40 border border-slate-800 rounded-2xl p-5 backdrop-blur-sm">
<h2 class="text-sm font-semibold text-slate-400 mb-4 uppercase tracking-wider flex items-center gap-2">
<svg class="h-4 w-4 text-sky-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path></svg>
Select Diagnostic Module
</h2>
<div class="grid grid-cols-2 gap-3">
<button id="btn-module-blood" onclick="switchModule('blood_cell')" class="p-4 rounded-xl border text-left transition-all border-sky-500 bg-sky-500/10 text-white shadow-[0_0_15px_rgba(14,165,233,0.15)]">
<div class="font-bold text-sm">WBC Blood Smear</div>
<div class="text-[10px] text-slate-400 mt-1">ResNet-18 (8 Classes)</div>
<div class="text-[10px] text-sky-400 mt-2 font-medium">98.07% Accuracy</div>
</button>
<button id="btn-module-skin" onclick="switchModule('skin_lesion')" class="p-4 rounded-xl border text-left transition-all border-slate-800 bg-slate-950/60 text-slate-400 hover:border-slate-700">
<div class="font-bold text-sm">Skin Cancer ViT</div>
<div class="text-[10px] text-slate-400 mt-1">Transformer (7 Classes)</div>
<div class="text-[10px] text-sky-400 mt-2 font-medium">96.95% Accuracy</div>
</button>
</div>
<div class="mt-4 p-3 bg-slate-900/60 rounded-lg border border-slate-800 flex gap-2.5 text-xs text-slate-400">
<svg class="h-4 w-4 text-sky-400 shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
<div id="module-desc">
Loads standard ResNet-18 network fine-tuned on PBC dataset. Identifies: Basophils, Eosinophils, Erythroblasts, Lymphocytes, Monocytes, Neutrophils, and Platelets.
</div>
</div>
</div>
<!-- Upload Box -->
<div id="drop-zone" class="bg-slate-950/60 border-2 border-dashed border-slate-800 rounded-2xl p-8 text-center backdrop-blur-sm transition-all flex flex-col items-center justify-center min-h-[300px]">
<div id="upload-prompt" class="flex flex-col items-center">
<div class="p-4 bg-slate-900 border border-slate-800 rounded-2xl mb-4 text-slate-400">
<svg class="h-8 w-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"></path></svg>
</div>
<h3 class="font-bold text-sm mb-1 text-slate-200">Upload diagnostic image</h3>
<p class="text-xs text-slate-400 mb-6 max-w-[280px]">
Drag and drop your clinical image here, or browse local files
</p>
<label class="px-5 py-2.5 bg-slate-900 hover:bg-slate-800 border border-slate-700 rounded-xl text-xs font-semibold text-slate-200 cursor-pointer transition-colors shadow-md">
Browse Files
<input type="file" id="file-input" onchange="handleFileSelect(event)" accept="image/*" class="hidden">
</label>
</div>
<div id="upload-preview" class="hidden relative w-full flex flex-col items-center gap-4">
<img id="img-preview" src="#" alt="Preview" class="max-h-[220px] rounded-lg object-contain border border-slate-800 shadow-lg">
<div class="flex gap-2">
<button onclick="clearUpload()" class="px-4 py-2 border border-slate-700 rounded-lg text-xs hover:bg-slate-800 transition-colors">
Remove
</button>
<button id="btn-run" onclick="submitInference()" class="px-6 py-2 bg-sky-500 hover:bg-sky-600 text-white text-xs font-semibold rounded-lg shadow-lg hover:shadow-sky-500/20 transition-all flex items-center gap-2">
<svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path></svg>
Run Diagnostic Inference
</button>
</div>
</div>
</div>
<!-- Quick Demo Images -->
<div class="bg-slate-950/40 border border-slate-800 rounded-2xl p-5 backdrop-blur-sm">
<h2 class="text-sm font-semibold text-slate-400 mb-3 uppercase tracking-wider flex items-center gap-2">
<svg class="h-4 w-4 text-indigo-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
Demo Sample Images
</h2>
<p class="text-[11px] text-slate-400 mb-4">
Click a sample below to load and run instant diagnosis without file uploads.
</p>
<div class="grid grid-cols-2 gap-3">
<button onclick="loadDemoSample('blood_cell')" class="flex items-center gap-2.5 p-3 rounded-xl bg-slate-950/80 hover:bg-slate-900 border border-slate-800 text-left transition-colors">
<div class="w-8 h-8 rounded bg-sky-500/20 text-sky-400 flex items-center justify-center shrink-0 text-xs font-bold">WBC</div>
<div class="truncate">
<div class="text-xs font-bold text-slate-200 truncate">Blood Cell Smear</div>
<div class="text-[10px] text-slate-500">Run WBC ResNet</div>
</div>
</button>
<button onclick="loadDemoSample('skin_lesion')" class="flex items-center gap-2.5 p-3 rounded-xl bg-slate-950/80 hover:bg-slate-900 border border-slate-800 text-left transition-colors">
<div class="w-8 h-8 rounded bg-indigo-500/20 text-indigo-400 flex items-center justify-center shrink-0 text-xs font-bold">SKIN</div>
<div class="truncate">
<div class="text-xs font-bold text-slate-200 truncate">Skin Lesion</div>
<div class="text-[10px] text-slate-500">Run Cancer ViT</div>
</div>
</button>
</div>
</div>
</section>
<!-- Right Column: Diagnostic Results -->
<section class="lg:col-span-7 flex flex-col gap-6">
<div class="bg-slate-950/40 border border-slate-800 rounded-2xl p-6 backdrop-blur-sm flex-grow flex flex-col">
<h2 class="text-sm font-semibold text-slate-400 mb-6 uppercase tracking-wider flex items-center justify-between">
<span class="flex items-center gap-2">
<svg class="h-4 w-4 text-sky-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"></path></svg>
Diagnostic Results
</span>
<span id="inf-time-tag" class="text-xs text-slate-500 flex items-center gap-1.5 font-normal normal-case hidden">
<svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
Inference Time: <span id="inf-time-value">0.0</span> ms
</span>
</h2>
<!-- Loading State -->
<div id="results-loading" class="hidden flex-grow flex flex-col items-center justify-center p-12 text-center">
<svg class="h-10 w-10 text-sky-500 animate-spin mb-4" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
<h3 class="font-bold text-base mb-1 text-slate-200">Analyzing Medical Image...</h3>
<p class="text-xs text-slate-400 max-w-[280px]">
Extracting high-level features and running pre-trained neural networks. Please wait...
</p>
</div>
<!-- Initial Blank State -->
<div id="results-blank" class="flex-grow flex flex-col items-center justify-center p-12 text-center text-slate-400 border border-dashed border-slate-800 rounded-2xl bg-slate-950/20">
<svg class="h-10 w-10 text-slate-600 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
<h3 class="font-bold text-sm text-slate-300 mb-1">No scan evaluated</h3>
<p class="text-xs text-slate-500 max-w-[280px]">
Please choose a module, upload an image or click a sample, and press "Run Inference" to load diagnostic data.
</p>
</div>
<!-- Filled Results -->
<div id="results-filled" class="hidden flex-grow flex flex-col gap-6">
<!-- Summary card -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 bg-slate-900/60 p-5 rounded-2xl border border-slate-800">
<div>
<span class="text-xs text-slate-400 uppercase tracking-wider font-semibold">Predicted Class</span>
<div class="text-2xl font-black text-white mt-1 flex items-center gap-2">
<span id="predicted-label">Label</span>
<span class="text-emerald-500">
<svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
</span>
</div>
</div>
<div>
<span class="text-xs text-slate-400 uppercase tracking-wider font-semibold">Confidence Rating</span>
<div class="mt-1 flex items-center gap-3">
<div id="confidence-percentage" class="text-2xl font-black text-sky-400">0.00%</div>
<div class="flex-grow bg-slate-800 h-2.5 rounded-full overflow-hidden">
<div id="confidence-bar" class="bg-sky-500 h-full rounded-full" style="width: 0%"></div>
</div>
</div>
</div>
</div>
<!-- Interactive views -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 flex-grow">
<!-- Images Panel -->
<div class="flex flex-col gap-3">
<div class="flex items-center justify-between">
<span class="text-xs font-bold text-slate-400 uppercase">Image View</span>
<button id="btn-toggle-heatmap" onclick="toggleHeatmapView()" class="px-3 py-1 rounded text-[10px] font-bold border bg-sky-500/20 text-sky-300 border-sky-500/40 transition-colors">
Heatmap Overlay
</button>
</div>
<div class="bg-slate-900 border border-slate-800 rounded-xl p-3 flex items-center justify-center min-h-[220px] shadow-inner relative overflow-hidden">
<img id="img-display" src="" alt="Diagnostic Output" class="max-h-[220px] rounded object-contain">
</div>
<p id="image-caption" class="text-[10px] text-slate-400 text-center italic">
Heatmap highlights diagnostic regions of interest.
</p>
</div>
<!-- Probability breakdown -->
<div class="flex flex-col gap-3">
<span class="text-xs font-bold text-slate-400 uppercase">Probability Distribution</span>
<div id="prob-container" class="space-y-3 bg-slate-900/40 p-4 rounded-xl border border-slate-800 flex-grow overflow-y-auto max-h-[260px]">
<!-- Bars dynamically created here -->
</div>
</div>
</div>
</div>
</div>
</section>
</main>
<!-- Database History Log -->
<section class="max-w-7xl mx-auto px-6 pb-6 w-full">
<div class="bg-slate-950/40 border border-slate-800 rounded-2xl p-6 backdrop-blur-sm">
<div class="flex items-center justify-between mb-4">
<h2 class="text-sm font-semibold text-slate-400 uppercase tracking-wider flex items-center gap-2">
<svg class="h-4 w-4 text-sky-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4m0 5c0 2.21-3.582 4-8 4s-8-1.79-8-4"></path></svg>
Database History Logs
</h2>
<button onclick="fetchHistory()" class="p-2 bg-slate-900 border border-slate-800 hover:bg-slate-800 hover:border-slate-700 rounded-lg text-slate-400 hover:text-white transition-all">
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 1121.21 15H19"></path></svg>
</button>
</div>
<div class="overflow-x-auto border border-slate-800 rounded-xl">
<table class="w-full text-left text-xs border-collapse">
<thead>
<tr class="bg-slate-900 text-slate-400 border-b border-slate-800 uppercase tracking-wider font-semibold">
<th class="p-3">Time Stamp</th>
<th class="p-3">Diagnostic Module</th>
<th class="p-3">Uploaded Filename</th>
<th class="p-3">Diagnostic Outcome</th>
<th class="p-3 text-right">Confidence</th>
</tr>
</thead>
<tbody id="history-table-body" class="divide-y divide-slate-800 bg-slate-950/20">
<!-- Table rows populated dynamically -->
</tbody>
</table>
</div>
</div>
</section>
<!-- Legal & Disclaimers -->
<footer class="bg-slate-950 border-t border-slate-800 px-6 py-6 text-center text-slate-400 text-xs">
<div class="max-w-4xl mx-auto flex flex-col gap-4">
<div class="flex items-center justify-center gap-2 text-rose-400 font-bold bg-rose-500/10 border border-rose-500/20 rounded-xl px-4 py-3 max-w-2xl mx-auto shadow-md">
<svg class="h-5 w-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg>
<span>
ACADEMIC DISCLAIMER: For laboratory evaluation and demonstration only. This system is not cleared by FDA/CDSCO and must not be used for clinical decision-making or diagnosis of human subjects.
</span>
</div>
<div class="text-[10px] text-slate-500 leading-relaxed">
<p class="font-semibold text-slate-400 mb-1">DATASET LIMITATIONS & BIAS STATEMENT</p>
<p>
The models integrated are trained on public datasets (PBC & HAM10000) which contain inherent demographic and technical biases. For instance, the skin lesion classifier exhibits higher accuracy on fair skin types due to underrepresentation of dark skin phototypes in HAM10000. The WBC classifier was trained on high-quality microscopy slides, which may result in degraded accuracy when processing images captured under varying illumination, magnification levels, or camera parameters.
</p>
</div>
<p class="text-[10px] text-slate-600 mt-2">
© 2026 AI-Assisted Diagnostic Platform. Built for Graduation Project Evaluation.
</p>
</div>
</footer>
<script>
// Configuration: Set to your public backend URL when hosting online (e.g., "https://username-space.hf.space")
const API_BASE = "https://ram0894-diagnostic-platform-api.hf.space";
let activeModule = 'blood_cell';
let currentPrediction = null;
let originalImageURL = null;
let showingHeatmap = true;
let selectedFileObject = null;
// Switch between WBC and Skin modules
function switchModule(module) {
activeModule = module;
const btnBlood = document.getElementById('btn-module-blood');
const btnSkin = document.getElementById('btn-module-skin');
const desc = document.getElementById('module-desc');
if (module === 'blood_cell') {
btnBlood.className = "p-4 rounded-xl border text-left transition-all border-sky-500 bg-sky-500/10 text-white shadow-[0_0_15px_rgba(14,165,233,0.15)]";
btnSkin.className = "p-4 rounded-xl border text-left transition-all border-slate-800 bg-slate-950/60 text-slate-400 hover:border-slate-700";
desc.innerText = "Loads standard ResNet-18 network fine-tuned on PBC dataset. Identifies: Basophils, Eosinophils, Erythroblasts, Lymphocytes, Monocytes, Neutrophils, and Platelets.";
} else {
btnBlood.className = "p-4 rounded-xl border text-left transition-all border-slate-800 bg-slate-950/60 text-slate-400 hover:border-slate-700";
btnSkin.className = "p-4 rounded-xl border text-left transition-all border-sky-500 bg-sky-500/10 text-white shadow-[0_0_15px_rgba(14,165,233,0.15)]";
desc.innerText = "Loads Vision Transformer (ViT) model trained on the HAM10000 skin dataset. Identifies: Melanocytic Nevi, Melanoma, Carcinoma, and other lesion types.";
}
clearUpload();
}
// Handle Drag & Drop
const dropZone = document.getElementById('drop-zone');
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('border-sky-500', 'bg-sky-500/5');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('border-sky-500', 'bg-sky-500/5');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('border-sky-500', 'bg-sky-500/5');
if (e.dataTransfer.files && e.dataTransfer.files[0]) {
loadImageFile(e.dataTransfer.files[0]);
}
});
function handleFileSelect(e) {
if (e.target.files && e.target.files[0]) {
loadImageFile(e.target.files[0]);
}
}
function loadImageFile(file) {
selectedFileObject = file;
originalImageURL = URL.createObjectURL(file);
document.getElementById('upload-prompt').classList.add('hidden');
document.getElementById('upload-preview').classList.remove('hidden');
document.getElementById('img-preview').src = originalImageURL;
// Hide previous results
document.getElementById('results-filled').classList.add('hidden');
document.getElementById('results-blank').classList.remove('hidden');
document.getElementById('inf-time-tag').classList.add('hidden');
}
function clearUpload() {
selectedFileObject = null;
if (originalImageURL) {
URL.revokeObjectURL(originalImageURL);
originalImageURL = null;
}
document.getElementById('file-input').value = '';
document.getElementById('upload-preview').classList.add('hidden');
document.getElementById('upload-prompt').classList.remove('hidden');
document.getElementById('results-filled').classList.add('hidden');
document.getElementById('results-blank').classList.remove('hidden');
document.getElementById('inf-time-tag').classList.add('hidden');
currentPrediction = null;
}
// Submit for prediction
async function submitInference() {
if (!selectedFileObject) return;
document.getElementById('results-blank').classList.add('hidden');
document.getElementById('results-filled').classList.add('hidden');
document.getElementById('results-loading').classList.remove('hidden');
const formData = new FormData();
formData.append("file", selectedFileObject);
formData.append("module_type", activeModule);
try {
const response = await fetch(API_BASE + "/predict", {
method: "POST",
body: formData
});
if (!response.ok) {
const err = await response.json();
throw new Error(err.detail || "Diagnosis failed");
}
currentPrediction = await response.json();
renderResults();
fetchHistory();
} catch (err) {
alert(err.message);
document.getElementById('results-blank').classList.remove('hidden');
} finally {
document.getElementById('results-loading').classList.add('hidden');
}
}
// Render prediction response
function renderResults() {
if (!currentPrediction) return;
document.getElementById('results-filled').classList.remove('hidden');
// Labels & Confidence
document.getElementById('predicted-label').innerText = currentPrediction.prediction;
document.getElementById('confidence-percentage').innerText = (currentPrediction.confidence * 100).toFixed(2) + "%";
document.getElementById('confidence-bar').style.width = (currentPrediction.confidence * 100) + "%";
// Inf speed
document.getElementById('inf-time-tag').classList.remove('hidden');
document.getElementById('inf-time-value').innerText = (currentPrediction.inference_time_sec * 1000).toFixed(1);
// Setup image views
showingHeatmap = true;
const btnToggle = document.getElementById('btn-toggle-heatmap');
if (currentPrediction.heatmap_base64) {
btnToggle.style.display = 'inline-block';
btnToggle.className = "px-3 py-1 rounded text-[10px] font-bold border bg-sky-500/20 text-sky-300 border-sky-500/40 transition-colors";
btnToggle.innerText = activeModule === 'blood_cell' ? 'Grad-CAM Overlay' : 'Attention Heatmap';
document.getElementById('img-display').src = "data:image/jpeg;base64," + currentPrediction.heatmap_base64;
document.getElementById('image-caption').innerText = activeModule === 'blood_cell'
? "Grad-CAM highlights regions influencing the decision."
: "Attention rollout shows the ViT's self-attention patterns.";
} else {
btnToggle.style.display = 'none';
document.getElementById('img-display').src = originalImageURL;
document.getElementById('image-caption').innerText = "Original input image resized to 224x224.";
}
// Probability bar charts
const container = document.getElementById('prob-container');
container.innerHTML = '';
const sortedProbs = Object.entries(currentPrediction.class_probabilities)
.sort((a, b) => b[1] - a[1]);
sortedProbs.forEach(([className, prob]) => {
const isPred = className.toLowerCase() === currentPrediction.prediction.toLowerCase();
const barColor = isPred ? 'bg-sky-500' : 'bg-slate-700';
const itemHtml = `
<div class="text-xs">
<div class="flex justify-between text-slate-300 mb-1">
<span class="font-medium truncate max-w-[170px]">${className}</span>
<span class="text-slate-400">${(prob * 100).toFixed(2)}%</span>
</div>
<div class="w-full bg-slate-800 h-1.5 rounded-full overflow-hidden">
<div class="h-full rounded-full ${barColor}" style="width: ${prob * 100}%"></div>
</div>
</div>
`;
container.insertAdjacentHTML('beforeend', itemHtml);
});
}
// Toggle overlay view
function toggleHeatmapView() {
if (!currentPrediction || !currentPrediction.heatmap_base64) return;
showingHeatmap = !showingHeatmap;
const btnToggle = document.getElementById('btn-toggle-heatmap');
const imgDisp = document.getElementById('img-display');
const caption = document.getElementById('image-caption');
if (showingHeatmap) {
btnToggle.className = "px-3 py-1 rounded text-[10px] font-bold border bg-sky-500/20 text-sky-300 border-sky-500/40 transition-colors";
imgDisp.src = "data:image/jpeg;base64," + currentPrediction.heatmap_base64;
caption.innerText = activeModule === 'blood_cell'
? "Grad-CAM highlights regions influencing the decision."
: "Attention rollout shows the ViT's self-attention patterns.";
} else {
btnToggle.className = "px-3 py-1 rounded text-[10px] font-bold border bg-slate-900 text-slate-400 border-slate-800 hover:border-slate-700 transition-colors";
imgDisp.src = originalImageURL;
caption.innerText = "Original input image resized to 224x224.";
}
}
// Load Demo Image
async function loadDemoSample(sampleType) {
if (sampleType !== activeModule) {
switchModule(sampleType);
}
const filename = sampleType === 'blood_cell' ? 'sample_wbc_test.jpg' : 'sample_skin_test.jpg';
document.getElementById('results-blank').classList.add('hidden');
document.getElementById('results-filled').classList.add('hidden');
document.getElementById('results-loading').classList.remove('hidden');
try {
const response = await fetch(API_BASE + "/static/" + filename);
if (!response.ok) throw new Error("Demo sample file missing");
const blob = await response.blob();
const file = new File([blob], filename, { type: "image/jpeg" });
loadImageFile(file);
// Auto run diagnosis
await submitInference();
} catch (err) {
alert("Could not fetch demo sample image: " + err.message);
document.getElementById('results-loading').classList.add('hidden');
document.getElementById('results-blank').classList.remove('hidden');
}
}
// Fetch Database Logs
async function fetchHistory() {
try {
const response = await fetch(API_BASE + "/history");
if (!response.ok) throw new Error("Failed to load logs");
const historyList = await response.json();
const tbody = document.getElementById('history-table-body');
tbody.innerHTML = '';
if (historyList.length === 0) {
tbody.innerHTML = `
<tr>
<td colspan="5" class="p-8 text-center text-slate-500">
No records saved in database. Run inference to log items.
</td>
</tr>
`;
return;
}
historyList.slice(0, 8).forEach(log => {
const rowHtml = `
<tr class="hover:bg-slate-900/40 text-slate-300 transition-colors">
<td class="p-3 text-slate-400">
${new Date(log.timestamp).toLocaleString()}
</td>
<td class="p-3 font-semibold text-sky-400">${log.module}</td>
<td class="p-3 truncate max-w-[180px]" title="${log.filename}">${log.filename}</td>
<td class="p-3 text-slate-100 font-bold">${log.prediction}</td>
<td class="p-3 text-right font-semibold text-slate-300">
${(log.confidence * 100).toFixed(1)}%
</td>
</tr>
`;
tbody.insertAdjacentHTML('beforeend', rowHtml);
});
} catch (err) {
console.error(err.message);
}
}
// Initialize on load
window.onload = function() {
fetchHistory();
};
</script>
</body>
</html>
|