Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Created on Mon Jun 9 11:22:33 2025 | |
| @author: amit | |
| """ | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>White Blood Cell Classifier</title> | |
| <style> | |
| * { | |
| margin: 0; | |
| padding: 0; | |
| box-sizing: border-box; | |
| } | |
| body { | |
| font-family: 'Arial', sans-serif; | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
| min-height: 100vh; | |
| padding: 20px; | |
| } | |
| .container { | |
| max-width: 1200px; | |
| margin: 0 auto; | |
| background: white; | |
| border-radius: 15px; | |
| box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1); | |
| overflow: hidden; | |
| } | |
| .header { | |
| background: linear-gradient(135deg, #2c3e50, #3498db); | |
| color: white; | |
| padding: 30px; | |
| text-align: center; | |
| } | |
| .header h1 { | |
| font-size: 2.5rem; | |
| margin-bottom: 10px; | |
| } | |
| .header p { | |
| font-size: 1.1rem; | |
| opacity: 0.9; | |
| } | |
| .main-content { | |
| padding: 40px; | |
| } | |
| .upload-section { | |
| background: #f8f9fa; | |
| border-radius: 10px; | |
| padding: 30px; | |
| margin-bottom: 30px; | |
| border: 2px dashed #dee2e6; | |
| transition: all 0.3s ease; | |
| } | |
| .upload-section:hover { | |
| border-color: #007bff; | |
| background: #f0f8ff; | |
| } | |
| .upload-area { | |
| text-align: center; | |
| padding: 20px; | |
| } | |
| .upload-icon { | |
| font-size: 3rem; | |
| color: #6c757d; | |
| margin-bottom: 20px; | |
| } | |
| .file-input { | |
| display: none; | |
| } | |
| .upload-btn { | |
| background: linear-gradient(135deg, #28a745, #20c997); | |
| color: white; | |
| padding: 12px 30px; | |
| border: none; | |
| border-radius: 25px; | |
| font-size: 1.1rem; | |
| cursor: pointer; | |
| transition: all 0.3s ease; | |
| } | |
| .upload-btn:hover { | |
| transform: translateY(-2px); | |
| box-shadow: 0 5px 15px rgba(40, 167, 69, 0.3); | |
| } | |
| .predict-btn { | |
| background: linear-gradient(135deg, #007bff, #0056b3); | |
| color: white; | |
| padding: 15px 40px; | |
| border: none; | |
| border-radius: 25px; | |
| font-size: 1.2rem; | |
| cursor: pointer; | |
| transition: all 0.3s ease; | |
| margin-top: 20px; | |
| } | |
| .predict-btn:hover { | |
| transform: translateY(-2px); | |
| box-shadow: 0 5px 15px rgba(0, 123, 255, 0.3); | |
| } | |
| .predict-btn:disabled { | |
| background: #6c757d; | |
| cursor: not-allowed; | |
| transform: none; | |
| } | |
| .preview-section { | |
| display: none; | |
| margin-top: 30px; | |
| } | |
| .image-preview { | |
| display: flex; | |
| flex-wrap: wrap; | |
| gap: 20px; | |
| margin-bottom: 20px; | |
| } | |
| .preview-item { | |
| position: relative; | |
| background: white; | |
| border-radius: 10px; | |
| box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); | |
| overflow: hidden; | |
| transition: transform 0.3s ease; | |
| } | |
| .preview-item:hover { | |
| transform: translateY(-5px); | |
| } | |
| .preview-item img { | |
| width: 200px; | |
| height: 200px; | |
| object-fit: cover; | |
| } | |
| .preview-info { | |
| padding: 15px; | |
| text-align: center; | |
| } | |
| .filename { | |
| font-weight: bold; | |
| color: #2c3e50; | |
| margin-bottom: 5px; | |
| } | |
| .filesize { | |
| color: #6c757d; | |
| font-size: 0.9rem; | |
| } | |
| .results-section { | |
| display: none; | |
| margin-top: 30px; | |
| } | |
| .result-item { | |
| background: white; | |
| border-radius: 10px; | |
| box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); | |
| margin-bottom: 20px; | |
| overflow: hidden; | |
| animation: slideIn 0.5s ease; | |
| } | |
| @keyframes slideIn { | |
| from { | |
| opacity: 0; | |
| transform: translateY(20px); | |
| } | |
| to { | |
| opacity: 1; | |
| transform: translateY(0); | |
| } | |
| } | |
| .result-header { | |
| background: linear-gradient(135deg, #17a2b8, #138496); | |
| color: white; | |
| padding: 15px 20px; | |
| display: flex; | |
| justify-content: space-between; | |
| align-items: center; | |
| } | |
| .result-body { | |
| padding: 20px; | |
| } | |
| .prediction-main { | |
| display: flex; | |
| align-items: center; | |
| gap: 20px; | |
| margin-bottom: 20px; | |
| } | |
| .prediction-main img { | |
| width: 100px; | |
| height: 100px; | |
| object-fit: cover; | |
| border-radius: 10px; | |
| } | |
| .prediction-details { | |
| flex: 1; | |
| } | |
| .predicted-class { | |
| font-size: 1.5rem; | |
| font-weight: bold; | |
| color: #2c3e50; | |
| margin-bottom: 10px; | |
| } | |
| .confidence { | |
| font-size: 1.2rem; | |
| color: #28a745; | |
| font-weight: bold; | |
| } | |
| .probabilities { | |
| margin-top: 20px; | |
| } | |
| .probability-item { | |
| display: flex; | |
| align-items: center; | |
| margin-bottom: 10px; | |
| } | |
| .probability-label { | |
| width: 120px; | |
| font-weight: bold; | |
| color: #495057; | |
| } | |
| .probability-bar { | |
| flex: 1; | |
| height: 20px; | |
| background: #e9ecef; | |
| border-radius: 10px; | |
| overflow: hidden; | |
| margin: 0 10px; | |
| } | |
| .probability-fill { | |
| height: 100%; | |
| background: linear-gradient(135deg, #007bff, #0056b3); | |
| transition: width 0.5s ease; | |
| } | |
| .probability-value { | |
| width: 60px; | |
| text-align: right; | |
| font-weight: bold; | |
| color: #495057; | |
| } | |
| .loading { | |
| display: none; | |
| text-align: center; | |
| margin: 20px 0; | |
| } | |
| .spinner { | |
| border: 4px solid #f3f3f3; | |
| border-top: 4px solid #007bff; | |
| border-radius: 50%; | |
| width: 50px; | |
| height: 50px; | |
| animation: spin 1s linear infinite; | |
| margin: 0 auto 20px; | |
| } | |
| @keyframes spin { | |
| 0% { transform: rotate(0deg); } | |
| 100% { transform: rotate(360deg); } | |
| } | |
| .error { | |
| background: #f8d7da; | |
| color: #721c24; | |
| padding: 15px; | |
| border-radius: 5px; | |
| margin: 10px 0; | |
| border: 1px solid #f5c6cb; | |
| } | |
| .wbc-info { | |
| background: #e3f2fd; | |
| border-radius: 10px; | |
| padding: 20px; | |
| margin-top: 30px; | |
| } | |
| .wbc-info h3 { | |
| color: #1976d2; | |
| margin-bottom: 15px; | |
| } | |
| .wbc-types { | |
| display: grid; | |
| grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); | |
| gap: 15px; | |
| } | |
| .wbc-type { | |
| background: white; | |
| padding: 15px; | |
| border-radius: 8px; | |
| box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); | |
| } | |
| .wbc-type-name { | |
| font-weight: bold; | |
| color: #1976d2; | |
| margin-bottom: 5px; | |
| } | |
| .wbc-type-desc { | |
| font-size: 0.9rem; | |
| color: #666; | |
| } | |
| @media (max-width: 768px) { | |
| .container { | |
| margin: 10px; | |
| } | |
| .main-content { | |
| padding: 20px; | |
| } | |
| .header h1 { | |
| font-size: 2rem; | |
| } | |
| .image-preview { | |
| justify-content: center; | |
| } | |
| .prediction-main { | |
| flex-direction: column; | |
| text-align: center; | |
| } | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <div class="header"> | |
| <h1>🩸 White Blood Cell Classifier</h1> | |
| <p>AI-Powered Classification of White Blood Cell Types</p> | |
| </div> | |
| <div class="main-content"> | |
| <div class="upload-section"> | |
| <div class="upload-area"> | |
| <div class="upload-icon">📤</div> | |
| <h3>Upload WBC Images</h3> | |
| <p>Select one or multiple images of white blood cells for classification</p> | |
| <input type="file" id="imageInput" class="file-input" multiple accept="image/*"> | |
| <button class="upload-btn" onclick="document.getElementById('imageInput').click()"> | |
| Choose Images | |
| </button> | |
| </div> | |
| </div> | |
| <div class="preview-section" id="previewSection"> | |
| <h3>Selected Images</h3> | |
| <div class="image-preview" id="imagePreview"></div> | |
| <div style="text-align: center;"> | |
| <button class="predict-btn" id="predictBtn" onclick="classifyImages()"> | |
| 🔬 Classify WBC Types | |
| </button> | |
| </div> | |
| </div> | |
| <div class="loading" id="loading"> | |
| <div class="spinner"></div> | |
| <p>Analyzing images... Please wait</p> | |
| </div> | |
| <div class="results-section" id="resultsSection"> | |
| <h3>Classification Results</h3> | |
| <div id="resultsContainer"></div> | |
| </div> | |
| <div class="wbc-info"> | |
| <h3>🧬 White Blood Cell Types</h3> | |
| <div class="wbc-types"> | |
| <div class="wbc-type"> | |
| <div class="wbc-type-name">Neutrophil</div> | |
| <div class="wbc-type-desc">Most abundant WBC, first responders to infection</div> | |
| </div> | |
| <div class="wbc-type"> | |
| <div class="wbc-type-name">Lymphocyte</div> | |
| <div class="wbc-type-desc">Key players in adaptive immunity</div> | |
| </div> | |
| <div class="wbc-type"> | |
| <div class="wbc-type-name">Monocyte</div> | |
| <div class="wbc-type-desc">Develop into macrophages and dendritic cells</div> | |
| </div> | |
| <div class="wbc-type"> | |
| <div class="wbc-type-name">Eosinophil</div> | |
| <div class="wbc-type-desc">Combat parasites and involved in allergic reactions</div> | |
| </div> | |
| <div class="wbc-type"> | |
| <div class="wbc-type-name">Basophil</div> | |
| <div class="wbc-type-desc">Least common, release histamine during allergic reactions</div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| let selectedFiles = []; | |
| document.getElementById('imageInput').addEventListener('change', function(e) { | |
| selectedFiles = Array.from(e.target.files); | |
| displayPreview(); | |
| }); | |
| function displayPreview() { | |
| const previewSection = document.getElementById('previewSection'); | |
| const imagePreview = document.getElementById('imagePreview'); | |
| if (selectedFiles.length === 0) { | |
| previewSection.style.display = 'none'; | |
| return; | |
| } | |
| previewSection.style.display = 'block'; | |
| imagePreview.innerHTML = ''; | |
| selectedFiles.forEach((file, index) => { | |
| const reader = new FileReader(); | |
| reader.onload = function(e) { | |
| const previewItem = document.createElement('div'); | |
| previewItem.className = 'preview-item'; | |
| previewItem.innerHTML = ` | |
| <img src="${e.target.result}" alt="Preview ${index + 1}"> | |
| <div class="preview-info"> | |
| <div class="filename">${file.name}</div> | |
| <div class="filesize">${(file.size / 1024).toFixed(1)} KB</div> | |
| </div> | |
| `; | |
| imagePreview.appendChild(previewItem); | |
| }; | |
| reader.readAsDataURL(file); | |
| }); | |
| } | |
| async function classifyImages() { | |
| if (selectedFiles.length === 0) { | |
| alert('Please select at least one image first!'); | |
| return; | |
| } | |
| const loading = document.getElementById('loading'); | |
| const resultsSection = document.getElementById('resultsSection'); | |
| const resultsContainer = document.getElementById('resultsContainer'); | |
| const predictBtn = document.getElementById('predictBtn'); | |
| // Show loading | |
| loading.style.display = 'block'; | |
| resultsSection.style.display = 'none'; | |
| predictBtn.disabled = true; | |
| try { | |
| const formData = new FormData(); | |
| selectedFiles.forEach(file => { | |
| formData.append('images', file); | |
| }); | |
| const response = await fetch('/api/predict_batch', { | |
| method: 'POST', | |
| body: formData | |
| }); | |
| const data = await response.json(); | |
| if (data.success) { | |
| displayResults(data.batch_results); | |
| } else { | |
| throw new Error(data.error || 'Classification failed'); | |
| } | |
| } catch (error) { | |
| console.error('Error:', error); | |
| resultsContainer.innerHTML = ` | |
| <div class="error"> | |
| <strong>Error:</strong> ${error.message} | |
| </div> | |
| `; | |
| resultsSection.style.display = 'block'; | |
| } finally { | |
| loading.style.display = 'none'; | |
| predictBtn.disabled = false; | |
| } | |
| } | |
| function displayResults(results) { | |
| const resultsContainer = document.getElementById('resultsContainer'); | |
| const resultsSection = document.getElementById('resultsSection'); | |
| resultsContainer.innerHTML = ''; | |
| results.forEach((result, index) => { | |
| const resultItem = document.createElement('div'); | |
| resultItem.className = 'result-item'; | |
| if (result.success) { | |
| const predictions = result.results; | |
| const imageUrl = URL.createObjectURL(selectedFiles[index]); | |
| resultItem.innerHTML = ` | |
| <div class="result-header"> | |
| <span><strong>${result.filename}</strong></span> | |
| <span>Classification Complete ✅</span> | |
| </div> | |
| <div class="result-body"> | |
| <div class="prediction-main"> | |
| <img src="${imageUrl}" alt="${result.filename}"> | |
| <div class="prediction-details"> | |
| <div class="predicted-class"> | |
| 🩸 ${predictions.predicted_class} | |
| </div> | |
| <div class="confidence"> | |
| Confidence: ${(predictions.confidence * 100).toFixed(1)}% | |
| </div> | |
| </div> | |
| </div> | |
| <div class="probabilities"> | |
| <h4>All Class Probabilities:</h4> | |
| ${Object.entries(predictions.all_probabilities) | |
| .map(([className, probability]) => ` | |
| <div class="probability-item"> | |
| <div class="probability-label">${className}</div> | |
| <div class="probability-bar"> | |
| <div class="probability-fill" style="width: ${probability * 100}%"></div> | |
| </div> | |
| <div class="probability-value">${(probability * 100).toFixed(1)}%</div> | |
| </div> | |
| `).join('')} | |
| </div> | |
| </div> | |
| `; | |
| } else { | |
| resultItem.innerHTML = ` | |
| <div class="result-header"> | |
| <span><strong>${result.filename}</strong></span> | |
| <span>Error ❌</span> | |
| </div> | |
| <div class="result-body"> | |
| <div class="error"> | |
| <strong>Classification failed:</strong> ${result.error} | |
| </div> | |
| </div> | |
| `; | |
| } | |
| resultsContainer.appendChild(resultItem); | |
| }); | |
| resultsSection.style.display = 'block'; | |
| } | |
| // Health check on page load | |
| window.addEventListener('load', async function() { | |
| try { | |
| const response = await fetch('/api/health'); | |
| const data = await response.json(); | |
| if (!data.model_loaded) { | |
| document.body.innerHTML = ` | |
| <div style="text-align: center; padding: 50px; color: red;"> | |
| <h1>⚠️ Model Loading Error</h1> | |
| <p>The classification model could not be loaded. Please check the server logs.</p> | |
| </div> | |
| `; | |
| } | |
| } catch (error) { | |
| console.error('Health check failed:', error); | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> |