File size: 7,835 Bytes
a9af1c1
 
 
 
 
 
0877ca8
0c10715
a9af1c1
 
 
 
 
 
0c10715
a9af1c1
 
 
 
0c10715
a9af1c1
 
 
 
 
 
 
 
 
 
 
0877ca8
a9af1c1
 
 
 
 
 
0877ca8
a9af1c1
 
 
 
 
 
 
 
0c10715
 
 
 
 
a9af1c1
 
 
 
 
 
 
 
0c10715
 
 
 
 
 
 
 
 
 
 
 
a9af1c1
 
 
0c10715
 
 
 
 
a9af1c1
0c10715
a9af1c1
0c10715
a9af1c1
0c10715
 
 
 
 
a9af1c1
 
 
0877ca8
0c10715
 
 
 
 
 
 
 
 
 
 
 
 
 
a9af1c1
 
 
 
 
 
0877ca8
a9af1c1
0877ca8
a9af1c1
0c10715
 
 
 
a9af1c1
0877ca8
a9af1c1
0877ca8
a9af1c1
0c10715
a9af1c1
0877ca8
a9af1c1
0c10715
 
 
 
 
 
 
a9af1c1
0c10715
a9af1c1
0c10715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9af1c1
0877ca8
a9af1c1
0877ca8
a9af1c1
0877ca8
 
a9af1c1
0c10715
 
 
0877ca8
a9af1c1
0877ca8
 
 
 
a9af1c1
0c10715
 
 
 
 
 
 
0877ca8
a9af1c1
0877ca8
a9af1c1
0877ca8
a9af1c1
0877ca8
 
a9af1c1
0c10715
 
 
 
 
0877ca8
 
 
0c10715
0877ca8
 
a9af1c1
0c10715
 
 
 
 
 
 
a9af1c1
0c10715
 
 
 
 
 
 
 
 
 
a9af1c1
0c10715
 
 
 
 
 
 
 
 
 
0877ca8
0c10715
 
0877ca8
0c10715
0877ca8
0c10715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0877ca8
a9af1c1
0c10715
 
 
0877ca8
a9af1c1
0877ca8
0c10715
0877ca8
 
0c10715
0877ca8
a9af1c1
0877ca8
0c10715
 
 
 
 
 
a9af1c1
0877ca8
a9af1c1
0c10715
a9af1c1
0c10715
 
a9af1c1
0c10715
a9af1c1
0c10715
a9af1c1
0c10715
 
 
 
 
 
 
 
 
 
 
a9af1c1
0c10715
 
a9af1c1
0c10715
a9af1c1
0c10715
 
a9af1c1
0c10715
 
a9af1c1
0c10715
 
a9af1c1
0c10715
a9af1c1
0c10715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9af1c1
0877ca8
a9af1c1
0c10715
 
0877ca8
 
0c10715
0877ca8
0c10715
 
0877ca8
0c10715
 
 
a9af1c1
0877ca8
a9af1c1
 
3f1ca09
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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Image Classification - Model Comparison</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            background: #eef2ff;
            text-align: center;
            padding: 25px;
        }

        .container {
            background: white;
            max-width: 1000px;
            margin: auto;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 4px 15px #999;
        }

        h1 {
            color: #4f46e5;
        }

        input {
            margin: 20px;
        }

        button {
            background: #4f46e5;
            color: white;
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
        }

        button:hover {
            background: #3730a3;
        }

        button:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }

        #preview {
            max-width: 250px;
            max-height: 250px;
            margin: 20px auto;
            border-radius: 10px;
            display: none;
        }

        .models {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 20px;
            margin-top: 30px;
        }

        .model-card {
            background: #f8faff;
            border: 1px solid #dbeafe;
            padding: 20px;
            border-radius: 12px;
            text-align: left;
        }

        .model-card h2 {
            color: #4f46e5;
            text-align: center;
        }

        .prediction {
            background: white;
            padding: 12px;
            margin: 10px 0;
            border-radius: 8px;
            border: 1px solid #ddd;
        }

        .prediction strong {
            color: #333;
        }

        #status {
            margin: 20px;
            color: #444;
        }

        #comparison {
            background: #eef2ff;
            padding: 20px;
            border-radius: 10px;
            margin-top: 25px;
        }

        @media(max-width: 650px) {
            .models {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>

<body>

<div class="container">

    <h1>Image Classification</h1>

    <h3>Compare Two AI Models</h3>

    <p>Upload one image and compare the predictions
       of two Hugging Face models.</p>

    <input type="file" id="imageInput" accept="image/*">

    <br>

    <img id="preview" alt="Uploaded Image">

    <br>

    <button id="classifyBtn">
        Compare Models
    </button>

    <p id="status">Please upload an image.</p>

    <div class="models">

        <!-- MODEL 1 -->

        <div class="model-card">

            <h2>Model 1 - ViT</h2>

            <p>Vision Transformer</p>

            <p><small>google/vit-base-patch16-224</small></p>

            <div id="result1">
                Predictions will appear here.
            </div>

        </div>

        <!-- MODEL 2 -->

        <div class="model-card">

            <h2>Model 2 - ConvNeXt</h2>

            <p>Convolutional Neural Network</p>

            <p><small>facebook/convnext-tiny-224</small></p>

            <div id="result2">
                Predictions will appear here.
            </div>

        </div>

    </div>

    <div id="comparison"></div>

</div>

<script type="module">

import { pipeline } from
'https://cdn.jsdelivr.net/npm/@huggingface/transformers';

let model1 = null;
let model2 = null;

let selectedImage = null;

const imageInput = document.getElementById("imageInput");
const preview = document.getElementById("preview");
const status = document.getElementById("status");
const button = document.getElementById("classifyBtn");

const result1 = document.getElementById("result1");
const result2 = document.getElementById("result2");
const comparison = document.getElementById("comparison");


// Upload and preview image

imageInput.addEventListener("change", function() {

    selectedImage = this.files[0];

    if (selectedImage) {

        preview.src = URL.createObjectURL(selectedImage);
        preview.style.display = "block";

        result1.textContent = "Ready to classify.";
        result2.textContent = "Ready to classify.";

        comparison.innerHTML = "";

        status.innerText = "Image uploaded successfully!";

    }

});


// Load first model

async function loadModel1() {

    if (!model1) {

        status.innerText = "Loading Model 1 - ViT...";

        model1 = await pipeline(
            "image-classification",
            "Xenova/vit-base-patch16-224",
            { dtype: "q8" }
        );

    }

    return model1;
}


// Load second model

async function loadModel2() {

    if (!model2) {

        status.innerText = "Loading Model 2 - ConvNeXt...";

        model2 = await pipeline(
            "image-classification",
            "Xenova/convnext-tiny-224",
            { dtype: "q8" }
        );

    }

    return model2;
}


// Display predictions

function displayResults(container, predictions) {

    container.innerHTML = "";

    predictions.forEach((item, index) => {

        const card = document.createElement("div");
        card.className = "prediction";

        const label = document.createElement("strong");
        label.textContent = (index + 1) + ". " + item.label;

        const score = document.createElement("p");
        score.textContent =
            "Confidence: " + (item.score * 100).toFixed(2) + "%";

        card.appendChild(label);
        card.appendChild(score);

        container.appendChild(card);

    });

}


// Compare both models

button.addEventListener("click", async function() {

    if (!selectedImage) {

        status.innerText = "Please upload an image first!";
        return;

    }

    button.disabled = true;

    result1.textContent = "Processing...";
    result2.textContent = "Processing...";
    comparison.innerHTML = "";

    let imageURL = URL.createObjectURL(selectedImage);

    try {

        // Load both models

        await loadModel1();
        await loadModel2();

        status.innerText = "Both models loaded. Classifying image...";

        // Give the same image to both models

        const [predictions1, predictions2] = await Promise.all([
            model1(imageURL, { top_k: 5 }),
            model2(imageURL, { top_k: 5 })
        ]);

        // Display results

        displayResults(result1, predictions1);
        displayResults(result2, predictions2);

        // Compare top predictions

        const label1 = predictions1[0].label;
        const label2 = predictions2[0].label;

        comparison.innerHTML = "";

        const heading = document.createElement("h2");
        heading.textContent = "Model Comparison";

        const first = document.createElement("p");
        first.textContent = "ViT Prediction: " + label1;

        const second = document.createElement("p");
        second.textContent = "ConvNeXt Prediction: " + label2;

        const conclusion = document.createElement("h3");

        if (label1 === label2) {
            conclusion.textContent =
                "Both models predicted the same category!";
        } else {
            conclusion.textContent =
                "The two models predicted different categories.";
        }

        comparison.append(heading, first, second, conclusion);

        status.innerText = "Comparison completed successfully!";

    }

    catch(error) {

        status.innerText = "Error: " + error.message;

        console.error(error);

    }

    finally {

        URL.revokeObjectURL(imageURL);
        button.disabled = false;

    }

});

</script>

</body>
</html>