Spaces:
Running
Running
: changed the run flow to execute ResNet, ViT, and CLIP together, then render each result in its own panel
Browse files
app.js
CHANGED
|
@@ -3,8 +3,10 @@ import { MODEL_OPTIONS, classifyImage, getModelOption } from './classifier.js';
|
|
| 3 |
const imageInput = document.querySelector('#image-input');
|
| 4 |
const modelSelect = document.querySelector('#model-select-a');
|
| 5 |
const modelSelectAlt = document.querySelector('#model-select-b');
|
|
|
|
| 6 |
const modelNote = document.querySelector('#model-note-a');
|
| 7 |
const modelNoteAlt = document.querySelector('#model-note-b');
|
|
|
|
| 8 |
const clipControls = document.querySelector('#clip-controls');
|
| 9 |
const candidateLabels = document.querySelector('#candidate-labels');
|
| 10 |
const topK = document.querySelector('#top-k');
|
|
@@ -18,8 +20,10 @@ const previewImage = document.querySelector('#preview-image');
|
|
| 18 |
const summary = document.querySelector('#summary');
|
| 19 |
const summaryA = document.querySelector('#summary-a');
|
| 20 |
const summaryB = document.querySelector('#summary-b');
|
|
|
|
| 21 |
const tbody = document.querySelector('#predictions-body-a');
|
| 22 |
const tbodyAlt = document.querySelector('#predictions-body-b');
|
|
|
|
| 23 |
const runtimeBadge = document.querySelector('#runtime-badge');
|
| 24 |
const sampleGrid = document.querySelector('#sample-grid');
|
| 25 |
const shuffleButton = document.querySelector('#shuffle-examples');
|
|
@@ -31,6 +35,7 @@ const historyBody = document.querySelector('#history-body');
|
|
| 31 |
const clearHistory = document.querySelector('#clear-history');
|
| 32 |
const modelAName = document.querySelector('#model-a-name');
|
| 33 |
const modelBName = document.querySelector('#model-b-name');
|
|
|
|
| 34 |
|
| 35 |
const state = { dataUrl: null, sourceLabel: null, running: false, history: [] };
|
| 36 |
|
|
@@ -149,8 +154,10 @@ function resetResults() {
|
|
| 149 |
summary.textContent = 'No predictions yet.';
|
| 150 |
summaryA.textContent = 'No predictions yet.';
|
| 151 |
summaryB.textContent = 'No predictions yet.';
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
| 154 |
runMeta.hidden = true;
|
| 155 |
}
|
| 156 |
|
|
@@ -164,6 +171,7 @@ function setRunning(running) {
|
|
| 164 |
runButton.disabled = running || !state.dataUrl;
|
| 165 |
modelSelect.disabled = running;
|
| 166 |
modelSelectAlt.disabled = running;
|
|
|
|
| 167 |
imageInput.disabled = running;
|
| 168 |
topK.disabled = running;
|
| 169 |
candidateLabels.disabled = running;
|
|
@@ -210,7 +218,7 @@ async function loadExample(example, button) {
|
|
| 210 |
imageInput.value = '';
|
| 211 |
document.querySelectorAll('.sample-card').forEach((card) => card.classList.remove('selected'));
|
| 212 |
button.classList.add('selected');
|
| 213 |
-
setSelectedImage(dataUrl, example.label, `${example.label} example ready. Click Run
|
| 214 |
} catch (error) {
|
| 215 |
console.error(error);
|
| 216 |
statusEl.textContent = 'Could not load that example. Try another image.';
|
|
@@ -316,22 +324,26 @@ function renderPredictions(predictions, option, targetBody, targetSummary) {
|
|
| 316 |
}
|
| 317 |
|
| 318 |
function renderComparison(results) {
|
| 319 |
-
const [firstResult, secondResult] = results;
|
| 320 |
-
if (!firstResult || !secondResult) return;
|
| 321 |
|
| 322 |
renderPredictions(firstResult.predictions, firstResult.option, tbody, summaryA);
|
| 323 |
renderPredictions(secondResult.predictions, secondResult.option, tbodyAlt, summaryB);
|
|
|
|
| 324 |
|
| 325 |
const firstBest = firstResult.predictions[0];
|
| 326 |
const secondBest = secondResult.predictions[0];
|
|
|
|
| 327 |
summary.textContent = [
|
| 328 |
firstBest ? `${firstResult.option.label} predicts “${firstBest.label}” at ${(Number(firstBest.score) * 100).toFixed(1)}%.` : `${firstResult.option.label} returned no predictions.`,
|
| 329 |
secondBest ? `${secondResult.option.label} predicts “${secondBest.label}” at ${(Number(secondBest.score) * 100).toFixed(1)}%.` : `${secondResult.option.label} returned no predictions.`,
|
|
|
|
| 330 |
].join(' ');
|
| 331 |
|
| 332 |
modelAName.textContent = firstResult.option.label;
|
| 333 |
modelBName.textContent = secondResult.option.label;
|
| 334 |
-
|
|
|
|
| 335 |
metaImage.textContent = state.sourceLabel || 'Selected image';
|
| 336 |
metaRuntime.textContent = `${results.reduce((total, item) => total + item.seconds, 0).toFixed(1)}s`;
|
| 337 |
runMeta.hidden = false;
|
|
@@ -340,7 +352,7 @@ function renderComparison(results) {
|
|
| 340 |
function renderHistory() {
|
| 341 |
clearHistory.disabled = state.history.length === 0;
|
| 342 |
if (!state.history.length) {
|
| 343 |
-
historyBody.innerHTML = '<tr class="empty-row"><td colspan="6">Run
|
| 344 |
return;
|
| 345 |
}
|
| 346 |
|
|
@@ -389,18 +401,22 @@ topK.addEventListener('input', () => { topKValue.value = topK.value; });
|
|
| 389 |
function syncModelNotes() {
|
| 390 |
const optionA = getModelOption(modelSelect.value);
|
| 391 |
const optionB = getModelOption(modelSelectAlt.value);
|
|
|
|
| 392 |
modelNote.textContent = optionA.note;
|
| 393 |
modelNoteAlt.textContent = optionB.note;
|
|
|
|
| 394 |
modelAName.textContent = optionA.label;
|
| 395 |
modelBName.textContent = optionB.label;
|
| 396 |
-
|
| 397 |
-
|
|
|
|
| 398 |
resetResults();
|
| 399 |
-
if (state.dataUrl) statusEl.textContent = `Models updated. Run
|
| 400 |
}
|
| 401 |
|
| 402 |
modelSelect.addEventListener('change', syncModelNotes);
|
| 403 |
modelSelectAlt.addEventListener('change', syncModelNotes);
|
|
|
|
| 404 |
|
| 405 |
imageInput.addEventListener('change', () => {
|
| 406 |
const file = imageInput.files?.[0];
|
|
@@ -418,7 +434,7 @@ imageInput.addEventListener('change', () => {
|
|
| 418 |
|
| 419 |
document.querySelectorAll('.sample-card').forEach((card) => card.classList.remove('selected'));
|
| 420 |
const reader = new FileReader();
|
| 421 |
-
reader.onload = () => setSelectedImage(reader.result, 'Uploaded image', 'Your image is ready. Click Run
|
| 422 |
reader.onerror = () => {
|
| 423 |
statusEl.textContent = 'Could not read that image.';
|
| 424 |
setBadge('Image error', 'error');
|
|
@@ -443,8 +459,9 @@ runButton.addEventListener('click', async () => {
|
|
| 443 |
|
| 444 |
const optionA = getModelOption(modelSelect.value);
|
| 445 |
const optionB = getModelOption(modelSelectAlt.value);
|
|
|
|
| 446 |
const labels = parseCandidateLabels();
|
| 447 |
-
const selectedModels = [optionA, optionB];
|
| 448 |
|
| 449 |
if (selectedModels.some((option) => option.task === 'zero-shot-image-classification') && (labels.length < 2 || labels.length > 20)) {
|
| 450 |
statusEl.textContent = 'CLIP needs 2–20 candidate labels.';
|
|
@@ -459,7 +476,7 @@ runButton.addEventListener('click', async () => {
|
|
| 459 |
const started = performance.now();
|
| 460 |
|
| 461 |
try {
|
| 462 |
-
statusEl.textContent = `Running ${optionA.label} and ${
|
| 463 |
const results = await Promise.all(selectedModels.map(async (option) => {
|
| 464 |
const runStarted = performance.now();
|
| 465 |
const predictions = await classifyImage(state.dataUrl, {
|
|
@@ -478,11 +495,11 @@ runButton.addEventListener('click', async () => {
|
|
| 478 |
results.forEach(({ option, predictions, seconds }) => recordRun(option, predictions, seconds));
|
| 479 |
renderComparison(results);
|
| 480 |
const totalSeconds = (performance.now() - started) / 1000;
|
| 481 |
-
statusEl.textContent = `Comparison complete in ${totalSeconds.toFixed(1)}s. ${optionA.label} and ${
|
| 482 |
setBadge(`${results.length} models`, 'ready');
|
| 483 |
} catch (error) {
|
| 484 |
console.error(error);
|
| 485 |
-
statusEl.textContent = `${optionA.label} and ${
|
| 486 |
setBadge('Error', 'error');
|
| 487 |
} finally {
|
| 488 |
progressWrap.hidden = true;
|
|
@@ -492,6 +509,7 @@ runButton.addEventListener('click', async () => {
|
|
| 492 |
|
| 493 |
renderModelChoices(modelSelect, modelNote, MODEL_OPTIONS[0].key);
|
| 494 |
renderModelChoices(modelSelectAlt, modelNoteAlt, MODEL_OPTIONS[1].key);
|
|
|
|
| 495 |
syncModelNotes();
|
| 496 |
renderExampleChoices();
|
| 497 |
renderHistory();
|
|
|
|
| 3 |
const imageInput = document.querySelector('#image-input');
|
| 4 |
const modelSelect = document.querySelector('#model-select-a');
|
| 5 |
const modelSelectAlt = document.querySelector('#model-select-b');
|
| 6 |
+
const modelSelectThird = document.querySelector('#model-select-c');
|
| 7 |
const modelNote = document.querySelector('#model-note-a');
|
| 8 |
const modelNoteAlt = document.querySelector('#model-note-b');
|
| 9 |
+
const modelNoteThird = document.querySelector('#model-note-c');
|
| 10 |
const clipControls = document.querySelector('#clip-controls');
|
| 11 |
const candidateLabels = document.querySelector('#candidate-labels');
|
| 12 |
const topK = document.querySelector('#top-k');
|
|
|
|
| 20 |
const summary = document.querySelector('#summary');
|
| 21 |
const summaryA = document.querySelector('#summary-a');
|
| 22 |
const summaryB = document.querySelector('#summary-b');
|
| 23 |
+
const summaryC = document.querySelector('#summary-c');
|
| 24 |
const tbody = document.querySelector('#predictions-body-a');
|
| 25 |
const tbodyAlt = document.querySelector('#predictions-body-b');
|
| 26 |
+
const tbodyThird = document.querySelector('#predictions-body-c');
|
| 27 |
const runtimeBadge = document.querySelector('#runtime-badge');
|
| 28 |
const sampleGrid = document.querySelector('#sample-grid');
|
| 29 |
const shuffleButton = document.querySelector('#shuffle-examples');
|
|
|
|
| 35 |
const clearHistory = document.querySelector('#clear-history');
|
| 36 |
const modelAName = document.querySelector('#model-a-name');
|
| 37 |
const modelBName = document.querySelector('#model-b-name');
|
| 38 |
+
const modelCName = document.querySelector('#model-c-name');
|
| 39 |
|
| 40 |
const state = { dataUrl: null, sourceLabel: null, running: false, history: [] };
|
| 41 |
|
|
|
|
| 154 |
summary.textContent = 'No predictions yet.';
|
| 155 |
summaryA.textContent = 'No predictions yet.';
|
| 156 |
summaryB.textContent = 'No predictions yet.';
|
| 157 |
+
summaryC.textContent = 'No predictions yet.';
|
| 158 |
+
tbody.innerHTML = '<tr class="empty-row"><td colspan="3">Run all three models to compare details.</td></tr>';
|
| 159 |
+
tbodyAlt.innerHTML = '<tr class="empty-row"><td colspan="3">Run all three models to compare details.</td></tr>';
|
| 160 |
+
tbodyThird.innerHTML = '<tr class="empty-row"><td colspan="3">Run all three models to compare details.</td></tr>';
|
| 161 |
runMeta.hidden = true;
|
| 162 |
}
|
| 163 |
|
|
|
|
| 171 |
runButton.disabled = running || !state.dataUrl;
|
| 172 |
modelSelect.disabled = running;
|
| 173 |
modelSelectAlt.disabled = running;
|
| 174 |
+
modelSelectThird.disabled = running;
|
| 175 |
imageInput.disabled = running;
|
| 176 |
topK.disabled = running;
|
| 177 |
candidateLabels.disabled = running;
|
|
|
|
| 218 |
imageInput.value = '';
|
| 219 |
document.querySelectorAll('.sample-card').forEach((card) => card.classList.remove('selected'));
|
| 220 |
button.classList.add('selected');
|
| 221 |
+
setSelectedImage(dataUrl, example.label, `${example.label} example ready. Click Run 3-Model Comparison.`);
|
| 222 |
} catch (error) {
|
| 223 |
console.error(error);
|
| 224 |
statusEl.textContent = 'Could not load that example. Try another image.';
|
|
|
|
| 324 |
}
|
| 325 |
|
| 326 |
function renderComparison(results) {
|
| 327 |
+
const [firstResult, secondResult, thirdResult] = results;
|
| 328 |
+
if (!firstResult || !secondResult || !thirdResult) return;
|
| 329 |
|
| 330 |
renderPredictions(firstResult.predictions, firstResult.option, tbody, summaryA);
|
| 331 |
renderPredictions(secondResult.predictions, secondResult.option, tbodyAlt, summaryB);
|
| 332 |
+
renderPredictions(thirdResult.predictions, thirdResult.option, tbodyThird, summaryC);
|
| 333 |
|
| 334 |
const firstBest = firstResult.predictions[0];
|
| 335 |
const secondBest = secondResult.predictions[0];
|
| 336 |
+
const thirdBest = thirdResult.predictions[0];
|
| 337 |
summary.textContent = [
|
| 338 |
firstBest ? `${firstResult.option.label} predicts “${firstBest.label}” at ${(Number(firstBest.score) * 100).toFixed(1)}%.` : `${firstResult.option.label} returned no predictions.`,
|
| 339 |
secondBest ? `${secondResult.option.label} predicts “${secondBest.label}” at ${(Number(secondBest.score) * 100).toFixed(1)}%.` : `${secondResult.option.label} returned no predictions.`,
|
| 340 |
+
thirdBest ? `${thirdResult.option.label} predicts “${thirdBest.label}” at ${(Number(thirdBest.score) * 100).toFixed(1)}%.` : `${thirdResult.option.label} returned no predictions.`,
|
| 341 |
].join(' ');
|
| 342 |
|
| 343 |
modelAName.textContent = firstResult.option.label;
|
| 344 |
modelBName.textContent = secondResult.option.label;
|
| 345 |
+
modelCName.textContent = thirdResult.option.label;
|
| 346 |
+
metaModel.textContent = `${firstResult.option.label} + ${secondResult.option.label} + ${thirdResult.option.label}`;
|
| 347 |
metaImage.textContent = state.sourceLabel || 'Selected image';
|
| 348 |
metaRuntime.textContent = `${results.reduce((total, item) => total + item.seconds, 0).toFixed(1)}s`;
|
| 349 |
runMeta.hidden = false;
|
|
|
|
| 352 |
function renderHistory() {
|
| 353 |
clearHistory.disabled = state.history.length === 0;
|
| 354 |
if (!state.history.length) {
|
| 355 |
+
historyBody.innerHTML = '<tr class="empty-row"><td colspan="6">Run three models on the same image to start a comparison.</td></tr>';
|
| 356 |
return;
|
| 357 |
}
|
| 358 |
|
|
|
|
| 401 |
function syncModelNotes() {
|
| 402 |
const optionA = getModelOption(modelSelect.value);
|
| 403 |
const optionB = getModelOption(modelSelectAlt.value);
|
| 404 |
+
const optionC = getModelOption(modelSelectThird.value);
|
| 405 |
modelNote.textContent = optionA.note;
|
| 406 |
modelNoteAlt.textContent = optionB.note;
|
| 407 |
+
modelNoteThird.textContent = optionC.note;
|
| 408 |
modelAName.textContent = optionA.label;
|
| 409 |
modelBName.textContent = optionB.label;
|
| 410 |
+
modelCName.textContent = optionC.label;
|
| 411 |
+
clipControls.hidden = !(optionA.task === 'zero-shot-image-classification' || optionB.task === 'zero-shot-image-classification' || optionC.task === 'zero-shot-image-classification');
|
| 412 |
+
setBadge(`Models: ${optionA.label} + ${optionB.label} + ${optionC.label}`);
|
| 413 |
resetResults();
|
| 414 |
+
if (state.dataUrl) statusEl.textContent = `Models updated. Run all three to compare predictions.`;
|
| 415 |
}
|
| 416 |
|
| 417 |
modelSelect.addEventListener('change', syncModelNotes);
|
| 418 |
modelSelectAlt.addEventListener('change', syncModelNotes);
|
| 419 |
+
modelSelectThird.addEventListener('change', syncModelNotes);
|
| 420 |
|
| 421 |
imageInput.addEventListener('change', () => {
|
| 422 |
const file = imageInput.files?.[0];
|
|
|
|
| 434 |
|
| 435 |
document.querySelectorAll('.sample-card').forEach((card) => card.classList.remove('selected'));
|
| 436 |
const reader = new FileReader();
|
| 437 |
+
reader.onload = () => setSelectedImage(reader.result, 'Uploaded image', 'Your image is ready. Click Run 3-Model Comparison.');
|
| 438 |
reader.onerror = () => {
|
| 439 |
statusEl.textContent = 'Could not read that image.';
|
| 440 |
setBadge('Image error', 'error');
|
|
|
|
| 459 |
|
| 460 |
const optionA = getModelOption(modelSelect.value);
|
| 461 |
const optionB = getModelOption(modelSelectAlt.value);
|
| 462 |
+
const optionC = getModelOption(modelSelectThird.value);
|
| 463 |
const labels = parseCandidateLabels();
|
| 464 |
+
const selectedModels = [optionA, optionB, optionC];
|
| 465 |
|
| 466 |
if (selectedModels.some((option) => option.task === 'zero-shot-image-classification') && (labels.length < 2 || labels.length > 20)) {
|
| 467 |
statusEl.textContent = 'CLIP needs 2–20 candidate labels.';
|
|
|
|
| 476 |
const started = performance.now();
|
| 477 |
|
| 478 |
try {
|
| 479 |
+
statusEl.textContent = `Running ${optionA.label}, ${optionB.label}, and ${optionC.label} on the same image.`;
|
| 480 |
const results = await Promise.all(selectedModels.map(async (option) => {
|
| 481 |
const runStarted = performance.now();
|
| 482 |
const predictions = await classifyImage(state.dataUrl, {
|
|
|
|
| 495 |
results.forEach(({ option, predictions, seconds }) => recordRun(option, predictions, seconds));
|
| 496 |
renderComparison(results);
|
| 497 |
const totalSeconds = (performance.now() - started) / 1000;
|
| 498 |
+
statusEl.textContent = `Comparison complete in ${totalSeconds.toFixed(1)}s. ${optionA.label}, ${optionB.label}, and ${optionC.label} are ready to compare.`;
|
| 499 |
setBadge(`${results.length} models`, 'ready');
|
| 500 |
} catch (error) {
|
| 501 |
console.error(error);
|
| 502 |
+
statusEl.textContent = `${optionA.label}, ${optionB.label}, and ${optionC.label} failed to run. Check the network connection or try ResNet-18.`;
|
| 503 |
setBadge('Error', 'error');
|
| 504 |
} finally {
|
| 505 |
progressWrap.hidden = true;
|
|
|
|
| 509 |
|
| 510 |
renderModelChoices(modelSelect, modelNote, MODEL_OPTIONS[0].key);
|
| 511 |
renderModelChoices(modelSelectAlt, modelNoteAlt, MODEL_OPTIONS[1].key);
|
| 512 |
+
renderModelChoices(modelSelectThird, modelNoteThird, MODEL_OPTIONS[2].key);
|
| 513 |
syncModelNotes();
|
| 514 |
renderExampleChoices();
|
| 515 |
renderHistory();
|