Spaces:
Sleeping
Sleeping
| <html lang="en" data-bs-theme="dark"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>AI Image Classifier</title> | |
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> | |
| <style> | |
| #drop-area { | |
| border: 2px dashed #6c757d; | |
| border-radius: 10px; | |
| padding: 30px; | |
| text-align: center; | |
| color: #adb5bd; | |
| background-color: #343a40; | |
| cursor: pointer; | |
| } | |
| #drop-area.hover { | |
| border-color: #0d6efd; | |
| color: #fff; | |
| } | |
| #preview-container img { | |
| max-width: 100px; | |
| max-height: 100px; | |
| margin: 10px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container py-5"> | |
| <h2 class="text-center mb-4 text-info">Upload Images for Classification</h2> | |
| <form id="uploadForm"> | |
| <div id="drop-area" class="mb-3"> | |
| <p>Drag & Drop Images Here or Click to Select</p> | |
| <input type="file" id="imageInput" class="form-control d-none" accept="image/*" multiple> | |
| </div> | |
| <button type="submit" class="btn btn-primary">Predict</button> | |
| </form> | |
| <div id="preview-container" class="d-flex flex-wrap justify-content-start mt-4"></div> | |
| <div id="results-container" class="mt-4"></div> | |
| <h5 class="text-warning mt-5">Upload History</h5> | |
| <canvas id="uploadChart" height="100"></canvas> | |
| </div> | |
| <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | |
| <script> | |
| const form = document.getElementById('uploadForm'); | |
| const imageInput = document.getElementById('imageInput'); | |
| const dropArea = document.getElementById('drop-area'); | |
| const previewContainer = document.getElementById('preview-container'); | |
| const resultsContainer = document.getElementById('results-container'); | |
| const chartData = []; | |
| dropArea.addEventListener('click', () => imageInput.click()); | |
| dropArea.addEventListener('dragover', (e) => { | |
| e.preventDefault(); | |
| dropArea.classList.add('hover'); | |
| }); | |
| dropArea.addEventListener('dragleave', () => dropArea.classList.remove('hover')); | |
| dropArea.addEventListener('drop', (e) => { | |
| e.preventDefault(); | |
| dropArea.classList.remove('hover'); | |
| imageInput.files = e.dataTransfer.files; | |
| displayPreviews(imageInput.files); | |
| }); | |
| imageInput.addEventListener('change', () => displayPreviews(imageInput.files)); | |
| function displayPreviews(files) { | |
| previewContainer.innerHTML = ''; | |
| for (const file of files) { | |
| const reader = new FileReader(); | |
| reader.onload = () => { | |
| const img = document.createElement('img'); | |
| img.src = reader.result; | |
| img.classList.add('img-thumbnail'); | |
| previewContainer.appendChild(img); | |
| }; | |
| reader.readAsDataURL(file); | |
| } | |
| } | |
| form.addEventListener('submit', async (e) => { | |
| e.preventDefault(); | |
| const files = imageInput.files; | |
| if (!files.length) return; | |
| resultsContainer.innerHTML = ''; | |
| for (const file of files) { | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| const resDiv = document.createElement('div'); | |
| resDiv.className = 'alert alert-info mt-2'; | |
| resDiv.textContent = `Predicting ${file.name}...`; | |
| resultsContainer.appendChild(resDiv); | |
| try { | |
| const response = await fetch(`${window.location.origin}/predict`, { | |
| method: 'POST', | |
| body: formData | |
| }); | |
| const data = await response.json(); | |
| resDiv.classList.replace('alert-info', 'alert-success'); | |
| resDiv.textContent = `${file.name}: ${data.prediction}`; | |
| chartData.push({ name: file.name, label: data.prediction }); | |
| updateChart(); | |
| } catch (err) { | |
| resDiv.classList.replace('alert-info', 'alert-danger'); | |
| resDiv.textContent = `${file.name}: Error occurred`; | |
| } | |
| } | |
| }); | |
| const ctx = document.getElementById('uploadChart').getContext('2d'); | |
| const uploadChart = new Chart(ctx, { | |
| type: 'bar', | |
| data: { | |
| labels: [], | |
| datasets: [{ | |
| label: 'Predicted Labels', | |
| data: [], | |
| backgroundColor: 'rgba(13, 110, 253, 0.6)' | |
| }] | |
| }, | |
| options: { | |
| scales: { | |
| y: { beginAtZero: true, ticks: { stepSize: 1, precision: 0 } } | |
| } | |
| } | |
| }); | |
| function updateChart() { | |
| const labelCounts = {}; | |
| chartData.forEach(item => labelCounts[item.label] = (labelCounts[item.label] || 0) + 1); | |
| uploadChart.data.labels = Object.keys(labelCounts); | |
| uploadChart.data.datasets[0].data = Object.values(labelCounts); | |
| uploadChart.update(); | |
| } | |
| </script> | |
| </body> | |
| </html> | |