File size: 33,967 Bytes
9883bdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
def generate_slides_html(image_paths, image_ids, desc1, desc2, desc3, output_file='gallery_with_descriptions.html'):
    # Start of HTML content
    html_content = '''<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Image Gallery</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                max-width: 900px;
                margin: 0 auto;
                padding: 20px;
                background: #f5f5f5;
            }
            .gallery-container {
                position: relative;
                background: white;
                border-radius: 8px;
                padding: 30px;
                margin-bottom: 30px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
                min-height: 700px;
            }
            .gallery-container img {
                max-width: 700px;
                max-height: 500px;
                height: auto;
                border-radius: 4px;
                display: block;
                margin: 0 auto 20px;
            }
            .slide {
                display: none;
            }
            .slide.active {
                display: block;
                animation: fadeIn 0.5s;
            }
            @keyframes fadeIn {
                from { opacity: 0; }
                to { opacity: 1; }
            }
            .nav-buttons {
                display: flex;
                justify-content: space-between;
                margin: 20px 0;
            }
            .nav-button {
                padding: 10px 20px;
                background: #007bff;
                color: white;
                border: none;
                border-radius: 4px;
                cursor: pointer;
                font-size: 16px;
            }
            .nav-button:disabled {
                background: #cccccc;
                cursor: not-allowed;
            }
            .image-counter {
                text-align: center;
                font-weight: bold;
                margin-bottom: 20px;
                color: #555;
            }
            .description {
                background: #f8f8f8;
                padding: 15px;
                margin: 15px 0;
                border-radius: 4px;
                white-space: pre-line;
            }
            .image-title {
                font-size: 1.5em;
                font-weight: bold;
                margin-bottom: 15px;
                color: #333;
                text-align: center;
            }
            .model-title {
                font-weight: bold;
                color: #666;
                margin-bottom: 5px;
            }
            .random-button {
                padding: 10px 20px;
                background: #28a745;
                color: white;
                border: none;
                border-radius: 4px;
                cursor: pointer;
                font-size: 16px;
            }
            .random-button:hover {
                background: #218838;
            }
        </style>
    </head>
    <body>
        <div class="gallery-container" id="gallery">
            <div class="image-counter">Image <span id="current-index">1</span> of <span id="total-images">0</span></div>
            
            <!-- Slides will be generated here -->
    '''
    
    # Generate content for each image
    for i in range(len(image_paths)):
        # Process descriptions to handle line breaks
        desc1_html = desc1[i].replace('\n', '<br>')
        desc2_html = desc2[i].replace('\n', '<br>')
        desc3_html = desc3[i].replace('\n', '<br>')
        
        html_content += f'''
            <div class="slide" data-image-id="{image_ids[i]}" id="slide-{i}">
                <div class="image-title">Image ID: {image_ids[i]}</div>
                <img src="{image_paths[i]}" alt="Image {image_ids[i]}">
                
                <div class="description">
                    <div class="model-title">Model 1</div>
                    {desc1_html}
                </div>

                <div class="description">
                    <div class="model-title">Model 2</div>
                    {desc2_html}
                </div>
                
                <div class="description">
                    <div class="model-title">Model 3</div>
                    {desc3_html}
                </div>
            </div>
        '''

    html_content += '''
            <div class="nav-buttons">
                <button id="prev-button" class="nav-button">Previous</button>
                <button id="next-button" class="nav-button">Next</button>
            </div>
        </div>
        
        <script>
        // Variables to track current slide and history
        let currentSlide = 0;
        const slides = document.querySelectorAll('.slide');
        const totalSlides = slides.length;
        const viewedSlides = new Set([0]); // Track which slides have been viewed
        const slideHistory = [0]; // Track navigation history
        let historyPosition = 0; // Current position in history
        
        // Update total images counter
        document.getElementById('total-images').textContent = totalSlides;
        
        // Function to show a specific slide
        function goToSlide(index) {
            // Hide all slides
            slides.forEach(slide => {
                slide.classList.remove('active');
            });
            
            // Show the selected slide
            slides[index].classList.add('active');
            currentSlide = index;
            
            // Add to viewed slides
            viewedSlides.add(index);
            
            // Update the counter
            document.getElementById('current-index').textContent = index + 1;
            
            // Update button states
            document.getElementById('prev-button').disabled = slideHistory.length <= 1;
        }
        
        // Function to go to a random slide and track in history
        function goToRandomSlide() {
            // Get array of unviewed slide indices
            const unviewedSlides = Array.from(Array(totalSlides).keys())
                .filter(index => !viewedSlides.has(index) && index !== currentSlide);
            
            // If we've seen all slides except the current one, reset
            if (unviewedSlides.length === 0) {
                viewedSlides.clear();
                // Don't add current slide to viewed set so we don't repeat it immediately
                
                // Recalculate unviewed slides (now all except current)
                const allSlides = Array.from(Array(totalSlides).keys())
                    .filter(index => index !== currentSlide);
                
                // Select a random slide from all slides except current
                const randomIndex = Math.floor(Math.random() * allSlides.length);
                const newSlideIndex = allSlides[randomIndex];
                
                // Add to history and update position
                slideHistory.push(newSlideIndex);
                historyPosition = slideHistory.length - 1;
                
                goToSlide(newSlideIndex);
            } else {
                // Select a random unviewed slide
                const randomIndex = Math.floor(Math.random() * unviewedSlides.length);
                const newSlideIndex = unviewedSlides[randomIndex];
                
                // Add to history and update position
                slideHistory.push(newSlideIndex);
                historyPosition = slideHistory.length - 1;
                
                goToSlide(newSlideIndex);
            }
        }
        
        // Function to go to previous slide in history
        function goToPreviousSlide() {
            if (slideHistory.length > 1 && historyPosition > 0) {
                historyPosition--;
                goToSlide(slideHistory[historyPosition]);
            }
        }
        
        // Function for next slide (completely random, no repeats until all seen)
        function goToNextSlide() {
            goToRandomSlide();
        }
        
        // Function for previous slide (removed history navigation)
        function goToPrevSlide() {
            // No longer tracking history - just go to a random slide
            goToRandomSlide();
        }
        
        // Initialize the first slide
        goToSlide(0);
        
        // Event listeners for navigation buttons
        document.getElementById('next-button').addEventListener('click', goToRandomSlide);
        document.getElementById('prev-button').addEventListener('click', goToPreviousSlide);
        
        // Add keyboard navigation
        document.addEventListener('keydown', (e) => {
            if (e.key === 'ArrowRight' || e.key === ' ' || e.key === 'Enter') {
                goToRandomSlide(); // Right arrow, space, or enter goes to next random
            } else if (e.key === 'ArrowLeft') {
                goToPreviousSlide(); // Left arrow goes to previous
            }
        });
        
        // Initialize - disable previous button at start
        document.getElementById('prev-button').disabled = true;
        </script>
    </body>
    </html>
    '''
    
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(html_content)
    
    print(f"Gallery with three model outputs has been generated as {output_file}")



def generate_rating_html(image_paths, image_ids, desc1, desc2, desc3, desc4, desc5, output_file='gallery_with_ratings.html'):
    # Start of HTML content with linebreaks
    html_content = '''<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Image Gallery with Ratings</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                max-width: 800px;
                margin: 0 auto;
                padding: 20px;
                background: #f5f5f5;
            }
            .image-container {
                background: white;
                border-radius: 8px;
                padding: 20px;
                margin-bottom: 30px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .image-container img {
                max-width: 500px;
                height: auto;
                border-radius: 4px;
                display: block;
                margin: 0 auto;
            }
            .description {
                background: #f8f8f8;
                padding: 15px;
                margin: 10px 0;
                border-radius: 4px;
                white-space: pre-line; /* This helps preserve line breaks */
            }
            .image-title {
                font-size: 1.5em;
                font-weight: bold;
                margin-bottom: 15px;
                color: #333;
                text-align: center;
            }
            .model-title {
                font-weight: bold;
                color: #666;
                margin-bottom: 5px;
            }
            .rating {
                display: flex;
                align-items: center;
                margin-top: 10px;
                padding: 10px;
                background: #fff;
                border-radius: 4px;
            }
            .rating-label {
                margin-right: 10px;
                font-weight: bold;
            }
            .rating-group {
                display: flex;
                gap: 10px;
            }
            .rating-radio {
                display: none;
            }
            .rating-button {
                padding: 8px 12px;
                border: 1px solid #ccc;
                border-radius: 4px;
                cursor: pointer;
                transition: all 0.2s;
            }
            .rating-radio:checked + .rating-button {
                background: #007bff;
                color: white;
                border-color: #0056b3;
            }
            .save-button {
                position: fixed;
                bottom: 20px;
                right: 20px;
                padding: 10px 20px;
                background: #007bff;
                color: white;
                border: none;
                border-radius: 4px;
                cursor: pointer;
            }
            .save-button:disabled {
                background: #cccccc;
                cursor: not-allowed;
            }
            #images-container {
                /* Container for all image blocks */
            }
        </style>
    </head>
    <body>
        <div id="rater-name-container">
            <label for="rater-name" style="font-weight: bold;">Labeller id:</label>
            <input type="text" id="rater-name" style="margin: 10px 0; padding: 5px; width: 200px;">
        </div>
        
        <div id="images-container">
            <!-- Image containers will be inserted here dynamically -->
        </div>
    '''
    
    # Create JavaScript arrays for each piece of data
    js_image_paths = []
    js_image_ids = []
    js_desc1 = []
    js_desc2 = []
    js_desc3 = []
    js_desc4 = []
    js_desc5 = []
    
    for i in range(len(image_paths)):
        js_image_paths.append(f'"{image_paths[i]}"')
        js_image_ids.append(f'"{image_ids[i]}"')
        
        # Process descriptions to properly handle line breaks - replace \n with <br> for HTML rendering
        desc1_html = desc1[i].replace('"', '\\"').replace('\n', '<br>')
        desc2_html = desc2[i].replace('"', '\\"').replace('\n', '<br>')
        desc3_html = desc3[i].replace('"', '\\"').replace('\n', '<br>')
        desc4_html = desc4[i].replace('"', '\\"').replace('\n', '<br>')
        desc5_html = desc5[i].replace('"', '\\"').replace('\n', '<br>')
        
        js_desc1.append(f'"{desc1_html}"')
        js_desc2.append(f'"{desc2_html}"')
        js_desc3.append(f'"{desc3_html}"')
        js_desc4.append(f'"{desc4_html}"')
        js_desc5.append(f'"{desc5_html}"')
    
    # Add JavaScript to handle randomization and image display
    html_content += f'''
        <button onclick="saveRatings()" class="save-button" id="save-button">Save Ratings as CSV</button>
        
        <script>
        // Store all image data as separate arrays
        const imagePaths = [{', '.join(js_image_paths)}];
        const imageIds = [{', '.join(js_image_ids)}];
        const desc1 = [{', '.join(js_desc1)}];
        const desc2 = [{', '.join(js_desc2)}];
        const desc3 = [{', '.join(js_desc3)}];
        const desc4 = [{', '.join(js_desc4)}];
        const desc5 = [{', '.join(js_desc5)}];
        
        // Create an array of indices to shuffle for images
        let indices = [];
        for (let i = 0; i < imageIds.length; i++) {{
            indices.push(i);
        }}
        
        // Function to render all images in randomized order with randomized model order
        function renderImages() {{
            const container = document.getElementById('images-container');
            
            // Shuffle the indices (this randomizes image order)
            shuffleArray(indices);
            
            // Clear the container
            container.innerHTML = '';
            
            // Add each image in shuffled order
            indices.forEach((originalIndex, newIndex) => {{
                // For each image, we'll create a different random order for the models
                let modelOrder = [1, 2, 3, 4, 5];
                shuffleArray(modelOrder);
                
                // Start building the image container HTML
                let imageHtml = `
                <div class="image-container" data-image-id="${{imageIds[originalIndex]}}">
                    <div class="image-title">Image ID: ${{imageIds[originalIndex]}}</div>
                    <img src="${{imagePaths[originalIndex]}}" alt="Image ${{imageIds[originalIndex]}}">
                `;
                
                // Add descriptions and rating UI for each model in the randomized order
                modelOrder.forEach((modelNum, modelIndex) => {{
                    // Get the description data for this model
                    let descData;
                    if (modelNum === 1) descData = desc1[originalIndex];
                    else if (modelNum === 2) descData = desc2[originalIndex];
                    else if (modelNum === 3) descData = desc3[originalIndex];
                    else if (modelNum === 4) descData = desc4[originalIndex];
                    else if (modelNum === 5) descData = desc5[originalIndex];
                    
                    // Create HTML for this model's description and rating
                    imageHtml += `
                    <div class="description">
                        <div class="model-title">Model ${{modelNum}}</div>
                        ${{descData}}
                        <div class="rating" data-model="${{modelNum}}">
                            <span class="rating-label">Rating:</span>
                            <div class="rating-group">
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="1" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-1">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-1">1</label>
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="2" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-2">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-2">2</label>
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="3" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-3">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-3">3</label>
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="4" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-4">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-4">4</label>
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="5" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-5">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-5">5</label>
                            </div>
                        </div>
                    </div>`;
                }});
                
                // Close the image container div
                imageHtml += `</div>`;
                
                // Add the complete HTML for this image to the page
                container.innerHTML += imageHtml;
            }});
        }}
        
        // Fisher-Yates shuffle algorithm
        function shuffleArray(array) {{
            for (let i = array.length - 1; i > 0; i--) {{
                const j = Math.floor(Math.random() * (i + 1));
                [array[i], array[j]] = [array[j], array[i]];
            }}
            return array;
        }}
        
        function saveRatings() {{
            const labellerId = document.getElementById('rater-name').value.trim();
            
            if (!labellerId) {{
                alert('Please enter a Labeller ID before saving');
                return;
            }}
            
            const ratings = [];
            
            // Collect all ratings
            document.querySelectorAll('.rating-radio:checked').forEach(radio => {{
                ratings.push({{
                    model: radio.dataset.model,
                    image_id: radio.dataset.image,
                    rating: radio.value
                }});
            }});
            
            // Convert to CSV
            const headers = ['model', 'image_id', 'rating'];
            const csvContent = [
                headers.join(','),
                ...ratings.map(row => [
                    row.model,
                    row.image_id,
                    row.rating
                ].join(','))
            ].join('\\n');
            
            // Create and trigger download with labeller ID in filename
            const blob = new Blob([csvContent], {{ type: 'text/csv;charset=utf-8;' }});
            const link = document.createElement('a');
            link.href = URL.createObjectURL(blob);
            link.download = `ratings_${{labellerId}}.csv`;
            link.click();
        }}

        // Add event listener to enable/disable save button based on labeller ID
        document.getElementById('rater-name').addEventListener('input', function() {{
            const saveButton = document.getElementById('save-button');
            saveButton.disabled = !this.value.trim();
        }});

        // Initially disable save button
        document.getElementById('save-button').disabled = true;
        
        // Render images when the page loads
        document.addEventListener('DOMContentLoaded', function() {{
            renderImages();
        }});
        </script>
    </body>
    </html>
    '''
    
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(html_content)
    
    print(f"Rating form with 5 models has been generated as {output_file}")


def generate_rating_html4(image_paths, image_ids, desc1, desc2, desc3, desc4, output_file='gallery_with_ratings.html'):
    # Start of HTML content
    html_content = '''<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Image Gallery with Ratings</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                max-width: 800px;
                margin: 0 auto;
                padding: 20px;
                background: #f5f5f5;
            }
            .image-container {
                background: white;
                border-radius: 8px;
                padding: 20px;
                margin-bottom: 30px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .image-container img {
                max-width: 500px;
                height: auto;
                border-radius: 4px;
                display: block;
                margin: 0 auto;
            }
            .description {
                background: #f8f8f8;
                padding: 15px;
                margin: 10px 0;
                border-radius: 4px;
                white-space: pre-line; /* This helps preserve line breaks */
            }
            .image-title {
                font-size: 1.5em;
                font-weight: bold;
                margin-bottom: 15px;
                color: #333;
                text-align: center;
            }
            .model-title {
                font-weight: bold;
                color: #666;
                margin-bottom: 5px;
            }
            .rating {
                display: flex;
                align-items: center;
                margin-top: 10px;
                padding: 10px;
                background: #fff;
                border-radius: 4px;
            }
            .rating-label {
                margin-right: 10px;
                font-weight: bold;
            }
            .rating-group {
                display: flex;
                gap: 10px;
            }
            .rating-radio {
                display: none;
            }
            .rating-button {
                padding: 8px 12px;
                border: 1px solid #ccc;
                border-radius: 4px;
                cursor: pointer;
                transition: all 0.2s;
            }
            .rating-radio:checked + .rating-button {
                background: #007bff;
                color: white;
                border-color: #0056b3;
            }
            .save-button {
                position: fixed;
                bottom: 20px;
                right: 20px;
                padding: 10px 20px;
                background: #007bff;
                color: white;
                border: none;
                border-radius: 4px;
                cursor: pointer;
            }
            .save-button:disabled {
                background: #cccccc;
                cursor: not-allowed;
            }
            #images-container {
                /* Container for all image blocks */
            }
        </style>
    </head>
    <body>
        <div id="rater-name-container">
            <label for="rater-name" style="font-weight: bold;">Labeller id:</label>
            <input type="text" id="rater-name" style="margin: 10px 0; padding: 5px; width: 200px;">
        </div>
        
        <div id="images-container">
            <!-- Image containers will be inserted here dynamically -->
        </div>
    '''
    
    # Create JavaScript arrays for each piece of data
    js_image_paths = []
    js_image_ids = []
    js_desc1 = []
    js_desc2 = []
    js_desc3 = []
    js_desc4 = []
    
    for i in range(len(image_paths)):
        js_image_paths.append(f'"{image_paths[i]}"')
        js_image_ids.append(f'"{image_ids[i]}"')
        
        # Process descriptions to properly handle line breaks - replace \n with <br> for HTML rendering
        desc1_html = desc1[i].replace('"', '\\"').replace('\n', '<br>')
        desc2_html = desc2[i].replace('"', '\\"').replace('\n', '<br>')
        desc3_html = desc3[i].replace('"', '\\"').replace('\n', '<br>')
        desc4_html = desc4[i].replace('"', '\\"').replace('\n', '<br>')
        
        js_desc1.append(f'"{desc1_html}"')
        js_desc2.append(f'"{desc2_html}"')
        js_desc3.append(f'"{desc3_html}"')
        js_desc4.append(f'"{desc4_html}"')
    
    # Add JavaScript to handle randomization and image display
    html_content += f'''
        <button onclick="saveRatings()" class="save-button" id="save-button">Save Ratings as CSV</button>
        
        <script>
        // Store all image data as separate arrays
        const imagePaths = [{', '.join(js_image_paths)}];
        const imageIds = [{', '.join(js_image_ids)}];
        const desc1 = [{', '.join(js_desc1)}];
        const desc2 = [{', '.join(js_desc2)}];
        const desc3 = [{', '.join(js_desc3)}];
        const desc4 = [{', '.join(js_desc4)}];
        
        // Create an array of indices to shuffle for images
        let indices = [];
        for (let i = 0; i < imageIds.length; i++) {{
            indices.push(i);
        }}
        
        // Function to render all images in randomized order with randomized model order
        function renderImages() {{
            const container = document.getElementById('images-container');
            
            // Shuffle the indices (this randomizes image order)
            shuffleArray(indices);
            
            // Clear the container
            container.innerHTML = '';
            
            // Add each image in shuffled order
            indices.forEach((originalIndex, newIndex) => {{
                // For each image, we'll create a different random order for the models
                let modelOrder = [1, 2, 3, 4];
                shuffleArray(modelOrder);
                
                // Start building the image container HTML
                let imageHtml = `
                <div class="image-container" data-image-id="${{imageIds[originalIndex]}}">
                    <div class="image-title">Image ID: ${{imageIds[originalIndex]}}</div>
                    <img src="${{imagePaths[originalIndex]}}" alt="Image ${{imageIds[originalIndex]}}">
                `;
                
                // Add descriptions and rating UI for each model in the randomized order
                modelOrder.forEach((modelNum, modelIndex) => {{
                    // Get the description data for this model
                    let descData;
                    if (modelNum === 1) descData = desc1[originalIndex];
                    else if (modelNum === 2) descData = desc2[originalIndex];
                    else if (modelNum === 3) descData = desc3[originalIndex];
                    else if (modelNum === 4) descData = desc4[originalIndex];
                    
                    // Create HTML for this model's description and rating
                    imageHtml += `
                    <div class="description">
                        <div class="model-title">Model ${{modelNum}}</div>
                        ${{descData}}
                        <div class="rating" data-model="${{modelNum}}">
                            <span class="rating-label">Rating:</span>
                            <div class="rating-group">
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="1" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-1">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-1">1</label>
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="2" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-2">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-2">2</label>
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="3" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-3">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-3">3</label>
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="4" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-4">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-4">4</label>
                                <input type="radio" name="rating-${{newIndex}}-${{modelNum}}" value="5" class="rating-radio" data-image="${{imageIds[originalIndex]}}" data-model="${{modelNum}}" id="rating-${{newIndex}}-${{modelNum}}-5">
                                <label class="rating-button" for="rating-${{newIndex}}-${{modelNum}}-5">5</label>
                            </div>
                        </div>
                    </div>`;
                }});
                
                // Close the image container div
                imageHtml += `</div>`;
                
                // Add the complete HTML for this image to the page
                container.innerHTML += imageHtml;
            }});
        }}
        
        // Fisher-Yates shuffle algorithm
        function shuffleArray(array) {{
            for (let i = array.length - 1; i > 0; i--) {{
                const j = Math.floor(Math.random() * (i + 1));
                [array[i], array[j]] = [array[j], array[i]];
            }}
            return array;
        }}
        
        function saveRatings() {{
            const labellerId = document.getElementById('rater-name').value.trim();
            
            if (!labellerId) {{
                alert('Please enter a Labeller ID before saving');
                return;
            }}
            
            const ratings = [];
            
            // Collect all ratings
            document.querySelectorAll('.rating-radio:checked').forEach(radio => {{
                ratings.push({{
                    model: radio.dataset.model,
                    image_id: radio.dataset.image,
                    rating: radio.value
                }});
            }});
            
            // Convert to CSV
            const headers = ['model', 'image_id', 'rating'];
            const csvContent = [
                headers.join(','),
                ...ratings.map(row => [
                    row.model,
                    row.image_id,
                    row.rating
                ].join(','))
            ].join('\\n');
            
            // Create and trigger download with labeller ID in filename
            const blob = new Blob([csvContent], {{ type: 'text/csv;charset=utf-8;' }});
            const link = document.createElement('a');
            link.href = URL.createObjectURL(blob);
            link.download = `ratings_${{labellerId}}.csv`;
            link.click();
        }}

        // Add event listener to enable/disable save button based on labeller ID
        document.getElementById('rater-name').addEventListener('input', function() {{
            const saveButton = document.getElementById('save-button');
            saveButton.disabled = !this.value.trim();
        }});

        // Initially disable save button
        document.getElementById('save-button').disabled = true;
        
        // Render images when the page loads
        document.addEventListener('DOMContentLoaded', function() {{
            renderImages();
        }});
        </script>
    </body>
    </html>
    '''
    
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(html_content)
    
    print(f"Rating form with 4 models has been generated as {output_file}")