-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
574 lines (537 loc) · 26.1 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Image ELO Ranker: An open-source tool for efficient image ranking, aesthetic choices, and generating training data for deep learning models.">
<meta name="keywords" content="image ranking, ELO system, open-source, decision making, deep learning, training data">
<meta name="author" content="Quentin Wach">
<meta property="og:title" content="Image ELO Ranker - Make Better Decisions">
<meta property="og:description" content="Efficiently rank images, make aesthetic choices, and generate training data for deep learning models with this open-source tool.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://your-website-url.com">
<meta property="og:image" content="https://your-website-url.com/path-to-your-logo.png">
<title>Image Ranker</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
<style>
.material-symbols-rounded {
font-variation-settings:
'FILL' 0,
'wght' 300,
'GRAD' 0,
'opsz' 24
}
</style>
<!--
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8803344579810006"
crossorigin="anonymous"></script>
-->
</head>
<body>
<div class="container">
<audio id="clickSound" src="{{ url_for('static', filename='click.mp3') }}" preload="auto"></audio>
<div class="progress-bar">
<div id="progress-fill"></div>
</div>
<!-- PROGRESS BAR -->
<div id="progress-text"></div>
<!-- IMAGE COMPARISON -->
<div class="image-comparison">
<div class="images">
<img id="image1" src="" alt="Image 1">
<img id="image2" src="" alt="Image 2">
</div>
<div class="image-names">
<span id="image1-name"></span>
<span id="image2-name"></span>
</div>
<div class="buttons">
<div class="button-group">
<div class="action-buttons">
<button id="image1-btn" onclick="selectWinner('image1')">Image A is better <span class="shortcut">(Press 1 or left arrow)</span></button>
<button id="remove-image1-btn" onclick="removeImage(currentImages['image1'])" class="major-icon-btn" title="Remove Image A">
<span class="material-symbols-rounded">delete</span>
</button>
</div>
</div>
<div class="button-group">
<div class="action-buttons">
<button id="image2-btn" onclick="selectWinner('image2')">Image B is better <span class="shortcut">(Press 2 or right arrow)</span></button>
<button id="remove-image2-btn" onclick="removeImage(currentImages['image2'])" class="major-icon-btn" title="Remove Image B">
<span class="material-symbols-rounded">delete</span>
</button>
</div>
</div>
</div>
</div>
<!-- RANKING LIST HEADER -->
<div class="rankings">
<h2 style="opacity: 0.2;">Ranking</h2>
<ul id="rankingsList"></ul>
</div>
<!-- LEFT -->
<div class="left-buttons">
<div class="button-row">
<button id="selectDirectoryBtn" class="main-btn" title="Select a local directory to begin ranking images.">
<span class="material-symbols-rounded">upload</span>
Select Local Directory
</button>
<button id="import-comparison-history-btn" class="major-icon-btn" title="Import previous comparisons to continue a ranking session.">
<span class="material-symbols-rounded">history</span>
</button>
</div>
<div id="currentDirectory" class="current-directory">Not selected</div>
</div>
<!-- RIGHT -->
<div class="right-buttons">
<div class="button-row">
<button id="exclude-downvoted-btn" class="major-icon-btn" title="Exclude downvoted images from future comparisons.">
<span class="material-symbols-rounded">block</span>
</button>
<button id="smartShuffleBtn" class="major-icon-btn" title="Smart shuffle: Randomly select the next pair of images from the top of the ranking.">
<span class="material-symbols-rounded">shuffle</span>
</button>
<button id="auto-shuffle-btn" class="major-icon-btn" title="Auto-shuffle every three comparisons.">
<span class="material-symbols-rounded">category</span>
</button>
<button id="export-comparisons-btn" class="major-icon-btn" title="Export the current comparison history to a CSV file.">
<span class="material-symbols-rounded">download</span>
</button>
<button id="exportRankingsBtn" class="major-icon-btn" title="Export the current rankings to a CSV file.">
<span class="material-symbols-rounded">save</span>
</button>
</div>
<!--
<div class="social-buttons">
<a href="https://github.com/QuentinWach/image-ranker" target="_blank" rel="noopener noreferrer" class="social-button github-button">
<span class="material-symbols-rounded">star</span>
Star on GitHub
</a>
<a href="https://twitter.com/intent/tweet?text=Check%20out%20this%20awesome%20Image%20Ranker%20project!%20https://github.com/QuentinWach/image-ranker" target="_blank" rel="noopener noreferrer" class="social-button twitter-button">
<span class="material-symbols-rounded">share</span>
Share on X
</a>
</div>
-->
</div>
</div>
<!-- FOOTER -->
<footer class="footer">
<div class="footer-content">
<div class="footer-section">
<h3>About</h3>
<p>An open-source tool for fast yet precise image ranking of large image datasets. Use it to generate training data for deep learning models and RLHF, conduct A/B testing, use it for studies, or simply personal decision making.</p>
</div>
<div class="footer-section">
<h3>Quick Links</h3>
<ul>
<li><a href="https://github.com/QuentinWach/image-ranker" target="_blank">GitHub Repository</a></li>
<li><a href="https://x.com/QuentinWach">Contact</a></li>
</ul>
</div>
<div class="footer-section">
<h3>Connect</h3>
<p>If you have any questions or suggestions, please reach out!</p>
<div class="social-icons">
<a href="https://github.com/QuentinWach/image-ranker" target="_blank" class="social-icon github">
<span class="material-symbols-rounded">code</span>
</a>
<a href="https://x.com/QuentinWach" target="_blank" class="social-icon twitter">
<span class="material-symbols-rounded">alternate_email</span>
</a>
</div>
</div>
</div>
<div class="footer-bottom">
<p>© 2024 Image Ranker.</p>
</div>
</footer>
<script>
let currentImages = {};
let max_display = 30;
function loadImages() {
fetch('/get_images')
.then(response => response.json())
.then(data => {
if (data.error) {
alert(data.error);
return;
}
console.log('Received image data:', data);
currentImages = data;
document.getElementById('image1').src = '/serve_image?path=' + data.image1;
document.getElementById('image2').src = '/serve_image?path=' + data.image2;
console.log('Image1 src:', data.image1);
console.log('Image2 src:', data.image2);
document.getElementById('image1-name').textContent = `Image A: ${decodeURIComponent(data.image1.split('=').pop().split('/').pop())}`;
document.getElementById('image2-name').textContent = `Image B: ${decodeURIComponent(data.image2.split('=').pop().split('/').pop())}`;
updateProgress(data.progress);
})
.catch(error => {
console.error('Error loading images:', error);
});
}
let excludeDownvoted = false;
let excludedImages = new Set();
function selectWinner(winnerImage) {
playClickSound();
const winner = currentImages[winnerImage];
const loser = winnerImage === 'image1' ? currentImages.image2 : currentImages.image1;
fetch('/update_elo', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
winner: winner,
loser: loser,
exclude_loser: excludeDownvoted
}),
})
.then(() => {
updateRankings();
checkAutoShuffle();
if (excludeDownvoted) {
excludedImages.add(loser);
}
loadImages();
});
}
function removeImage(img_path) {
const confirmation = confirm(`Are you sure you want to remove ${img_path.split('/').pop()}? This will remove all remaining pairs that include this image.`);
if (!confirmation) return;
if (!img_path) {
console.error(`Image ${img_path} not found`);
return;
}
if (confirmation) {
console.error("fetching")
fetch('/remove_image', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ del_img: img_path }),
})
.then(() => {
loadImages();
updateRankings();
})
.catch(error => {
console.error('Error removing image:', error);
});
}
}
function updateRankings() {
display_count = 0
fetch('/get_rankings')
.then(response => response.json())
.then(data => {
const rankingsList = document.getElementById('rankingsList');
rankingsList.innerHTML = '';
data.forEach((item, index) => {
display_count += 1
const li = document.createElement('li');
let imgHtml;
if (display_count <= max_display) {
imgHtml = `<img src="/serve_image?path=${item.image}" alt="Ranked image" class="ranking-image">`;
} else {
imgHtml = `<div class="empty-image-placeholder">
<span class="material-symbols-rounded">image</span>
</div>`;
}
li.innerHTML = `
<span class="rank">#${index + 1}</span>
<div class="image-container">
${imgHtml}
<div class="image-hover"></div>
</div>
<span class="image-name">${item.image.split('/').pop()}${item.excluded ? ' (Excluded)' : ''}</span>
<span class="elo"><i class="material-symbols-rounded">star</i><span class="elo-value">${Math.round(item.elo)}</span></span>
<span class="uncertainty">± ${Math.round(item.uncertainty)}</span>
<span class="votes">
<span class="up-arrow"><i class="material-symbols-rounded">arrow_upward</i>${item.upvotes}</span>
<span class="down-arrow"><i class="material-symbols-rounded">arrow_downward</i>${item.downvotes}</span>
</span>
<button class="remove-image-btn major-icon-btn" onclick="removeImage('${item.image}')" title="Remove this image.">
<span class="material-symbols-rounded">delete</span>
</button>
`;
rankingsList.appendChild(li);
<!--
// Add Google Ad after every 10th image
//if ((index + 1) % 10 === 0 && index !== data.length - 1) {
// const adLi = document.createElement('li');
// adLi.className = 'ad-container';
// adLi.innerHTML = `
// <ins class="adsbygoogle"
// style="display:block"
// data-ad-format="fluid"
// data-ad-layout-key="-6t+ed+2i-1n-4w"
// data-ad-client="ca-pub-8803344579810006"
// data-ad-slot="4041607466"></ins>
// `;
// rankingsList.appendChild(adLi);
// // Push the ad to Google
// if (typeof adsbygoogle !== 'undefined' && adsbygoogle.loaded === false) {
// (adsbygoogle = window.adsbygoogle || []).push({});
// }
//}
-->
// Add event listeners for hover effect
document.querySelectorAll('.image-container').forEach(container => {
const img = container.querySelector('.ranking-image');
const hoverDiv = container.querySelector('.image-hover');
if (img) {
img.addEventListener('mouseenter', () => {
hoverDiv.style.backgroundImage = `url(${img.src})`;
hoverDiv.style.display = 'block';
});
img.addEventListener('mouseleave', () => {
hoverDiv.style.display = 'none';
});
}
});
});
<!--
// Ensure ads are loaded after the DOM is updated
setTimeout(() => {
(adsbygoogle = window.adsbygoogle || []).push({});
}, 100);
-->
});
}
function updateProgress(progress) {
const progressFill = document.getElementById('progress-fill');
const progressText = document.getElementById('progress-text');
const percentage = (progress.current / progress.total) * 100;
progressFill.style.width = `${percentage}%`;
progressText.textContent = `Progress: ${progress.current} / ${progress.total} comparisons`;
}
loadImages();
updateRankings();
document.getElementById('selectDirectoryBtn').addEventListener('click', function() {
fetch('/select_directory', {
method: 'POST'
})
.then(response => {
if (!response.ok) {
return response.json().then(err => { throw err; });
}
return response.json();
})
.then(data => {
if (data.success) {
alert('Directory selected successfully. Restarting ranking process...');
currentImages = {};
document.getElementById('rankingsList').innerHTML = '';
document.getElementById('progress-fill').style.width = '0%';
document.getElementById('progress-text').textContent = 'Progress: 0 / 0 comparisons';
loadImages();
updateRankings();
// Update the current directory display
document.getElementById('currentDirectory').textContent = 'Image Directory: ' + data.directory;
} else {
throw new Error(data.error || 'Unknown error occurred');
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while selecting the directory: ' + (error.message || error));
});
});
// Function to load the current directory on page load
function loadCurrentDirectory() {
fetch('/get_current_directory')
.then(response => response.json())
.then(data => {
if (data.directory) {
document.getElementById('currentDirectory').textContent = 'Image Directory: ' + data.directory;
} else {
document.getElementById('currentDirectory').textContent = 'Image Directory: Not selected';
}
})
.catch(error => {
console.error('Error loading current directory:', error);
document.getElementById('currentDirectory').textContent = 'Image Directory: Error loading';
});
}
// Call the function when the page loads
window.addEventListener('load', loadCurrentDirectory);
document.getElementById('exportRankingsBtn').addEventListener('click', function() {
fetch('/export_rankings')
.then(response => {
console.log('Response status:', response.status);
console.log('Response headers:', response.headers);
if (!response.ok) {
return response.text().then(text => {
throw new Error(`HTTP error! status: ${response.status}, body: ${text}`);
});
}
return response.blob();
})
.then(blob => {
console.log('Blob received:', blob);
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'image_rankings.csv';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while exporting rankings: ' + error.message);
});
});
document.getElementById('export-comparisons-btn').addEventListener('click', function() {
fetch('/export_comparisons')
.then(response => {
console.log('Response status:', response.status);
console.log('Response headers:', response.headers);
if (!response.ok) {
return response.text().then(text => {
throw new Error(`HTTP error! status: ${response.status}, body: ${text}`);
});
}
return response.blob();
})
.then(blob => {
console.log('Blob received:', blob);
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'comparisons.csv';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while exporting comparisons: ' + error.message);
});
});
document.getElementById('import-comparison-history-btn').addEventListener('click', function() {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'text/csv';
fileInput.addEventListener('change', function() {
const file = fileInput.files[0];
const formData = new FormData();
formData.append('file', file);
const append = confirm("Do you want to append the imported history to the existing comparison history? If not, the existing history will be replaced.");
formData.append('append', append);
fetch('/import_comparison_history', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('Comparison history imported successfully!');
updateRankings(); // Update the ranking display
loadImages(); // Update the progress display
} else {
alert('Error importing comparison history!');
}
});
});
fileInput.click();
});
function playClickSound() {
const clickSound = document.getElementById('clickSound');
clickSound.currentTime = 0;
clickSound.play();
}
function animateButton(buttonId) {
const button = document.getElementById(buttonId);
button.classList.add('pressed');
setTimeout(() => {
button.classList.remove('pressed');
}, 100);
}
document.addEventListener('keydown', function(event) {
if (event.key === '1' || event.key === 'ArrowLeft') {
selectWinner('image1');
animateButton('image1-btn');
} else if (event.key === '2' || event.key === 'ArrowRight') {
selectWinner('image2');
animateButton('image2-btn');
}
});
document.getElementById('smartShuffleBtn').addEventListener('click', function() {
fetch('/smart_shuffle')
.then(response => response.json())
.then(data => {
if (data.success) {
alert('Smart shuffle applied successfully!');
loadImages();
} else {
alert('Error applying smart shuffle: ' + data.error);
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while applying smart shuffle: ' + error.message);
});
});
const autoShuffleToggle = document.getElementById('auto-shuffle-btn');
let comparisonCount = 0;
async function checkAutoShuffle() {
if (autoShuffleToggle.classList.contains('active')) {
comparisonCount++;
if (comparisonCount >= 3) {
comparisonCount = 0;
try {
const response = await fetch('/smart_shuffle');
const data = await response.json();
if (data.success) {
console.log('Auto smart shuffle applied successfully!');
current_pair_index = 0; // Set current_pair_index to 0 after shuffle
} else {
console.error('Error applying auto smart shuffle:', data.error);
}
} catch (error) {
console.error('Error applying auto smart shuffle:', error.message);
}
}
}
}
autoShuffleToggle.addEventListener('click', function() {
autoShuffleToggle.classList.toggle('active');
});
document.getElementById('exclude-downvoted-btn').addEventListener('click', function() {
this.classList.toggle('active');
excludeDownvoted = this.classList.contains('active');
if (excludeDownvoted) {
alert('Downvoted images will now be excluded from future comparisons.');
// Recalculate image pairs
fetch('/exclude_image', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ excluded_image: 'recalculate' })
}).then(() => {
loadImages();
updateRankings();
});
} else {
alert('Downvoted images will no longer be excluded from future comparisons.');
excludedImages.clear();
fetch('/clear_excluded_images', { method: 'POST' })
.then(() => {
loadImages();
updateRankings();
});
}
});
</script>
</body>
</html>