Spaces:
Sleeping

File size: 12,268 Bytes
fb14bd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * ScratchNet — 7-Class Flower Classifier
 * Frontend Controller & Interactive Neural Network Client
 */

document.addEventListener('DOMContentLoaded', () => {
    // DOM Elements
    const dropzone = document.getElementById('dropzone');
    const dropzoneEmpty = document.getElementById('dropzoneEmpty');
    const dropzonePreview = document.getElementById('dropzonePreview');
    const fileInput = document.getElementById('fileInput');
    const browseFilesBtn = document.getElementById('browseFilesBtn');
    const previewImage = document.getElementById('previewImage');
    const previewFilename = document.getElementById('previewFilename');
    const previewFilesize = document.getElementById('previewFilesize');
    const clearImageBtn = document.getElementById('clearImageBtn');
    const analyzeBtn = document.getElementById('analyzeBtn');
    const btnSpinner = document.getElementById('btnSpinner');
    const analyzeBtnText = document.getElementById('analyzeBtnText');

    const resultPlaceholder = document.getElementById('resultPlaceholder');
    const resultContent = document.getElementById('resultContent');
    const winnerName = document.getElementById('winnerName');
    const winnerScientific = document.getElementById('winnerScientific');
    const winnerIcon = document.getElementById('winnerIcon');
    const winnerConfidence = document.getElementById('winnerConfidence');
    const winnerDescription = document.getElementById('winnerDescription');
    const winnerFamily = document.getElementById('winnerFamily');
    const winnerSeason = document.getElementById('winnerSeason');
    const probabilitiesList = document.getElementById('probabilitiesList');
    const jsonCodeOutput = document.getElementById('jsonCodeOutput');

    const hardwareStatusBadge = document.getElementById('hardwareStatusBadge');
    const hardwareStatusText = document.getElementById('hardwareStatusText');
    const inferenceDeviceBadge = document.getElementById('inferenceDeviceBadge');
    const samplesContainer = document.getElementById('samplesContainer');

    // State
    let currentFile = null;

    // 1. Initial Health Check
    fetch('/api/health')
        .then(res => res.json())
        .then(data => {
            if (data.cuda_available) {
                hardwareStatusText.textContent = `⚡ CUDA Acceleration (${data.device.toUpperCase()})`;
                hardwareStatusBadge.style.borderColor = 'rgba(16, 185, 129, 0.4)';
            } else {
                hardwareStatusText.textContent = `💻 CPU Active (${data.device.toUpperCase()})`;
                hardwareStatusBadge.style.borderColor = 'rgba(245, 158, 11, 0.4)';
            }
            inferenceDeviceBadge.textContent = `${data.num_classes} Classes • ${data.device.toUpperCase()}`;
        })
        .catch(() => {
            hardwareStatusText.textContent = '● Backend Connected';
        });

    // 2. Fetch & Render Quick Test Samples
    fetch('/api/samples')
        .then(res => res.json())
        .then(data => {
            if (!data.samples || data.samples.length === 0) {
                samplesContainer.innerHTML = '<span class="samples-hint">No pre-loaded samples found.</span>';
                return;
            }

            samplesContainer.innerHTML = '';
            data.samples.forEach(sample => {
                const card = document.createElement('div');
                card.className = 'sample-card';
                card.setAttribute('data-category', sample.category);
                card.innerHTML = `
                    <div class="sample-thumb-wrapper">
                        <img src="${sample.url}" alt="${sample.display_name}" class="sample-thumb" loading="lazy">
                    </div>
                    <div class="sample-name-badge">
                        <span>${sample.icon}</span>
                        <span>${sample.display_name}</span>
                    </div>
                `;

                card.addEventListener('click', () => loadSampleImage(sample.url, sample.category));
                samplesContainer.appendChild(card);
            });
        })
        .catch(err => {
            samplesContainer.innerHTML = '<span class="samples-hint">Could not load samples.</span>';
            console.error(err);
        });

    // 3. File Input Trigger
    browseFilesBtn.addEventListener('click', (e) => {
        e.stopPropagation();
        fileInput.click();
    });

    dropzone.addEventListener('click', () => {
        if (!currentFile) {
            fileInput.click();
        }
    });

    fileInput.addEventListener('change', (e) => {
        if (e.target.files && e.target.files[0]) {
            handleSelectedFile(e.target.files[0]);
        }
    });

    // 4. Drag & Drop Event Handlers
    ['dragenter', 'dragover'].forEach(eventName => {
        dropzone.addEventListener(eventName, (e) => {
            e.preventDefault();
            e.stopPropagation();
            dropzone.classList.add('dragover');
        });
    });

    ['dragleave', 'drop'].forEach(eventName => {
        dropzone.addEventListener(eventName, (e) => {
            e.preventDefault();
            e.stopPropagation();
            dropzone.classList.remove('dragover');
        });
    });

    dropzone.addEventListener('drop', (e) => {
        const dt = e.dataTransfer;
        if (dt.files && dt.files[0]) {
            handleSelectedFile(dt.files[0]);
        }
    });

    // 5. Global Clipboard Paste Handler (Ctrl+V)
    window.addEventListener('paste', (e) => {
        if (e.clipboardData && e.clipboardData.items) {
            const items = e.clipboardData.items;
            for (let i = 0; i < items.length; i++) {
                if (items[i].type.indexOf('image') !== -1) {
                    const blob = items[i].getAsFile();
                    handleSelectedFile(blob, 'pasted_image.png');
                    break;
                }
            }
        }
    });

    // 6. Clear Image Button
    clearImageBtn.addEventListener('click', (e) => {
        e.stopPropagation();
        resetDropzone();
    });

    function resetDropzone() {
        currentFile = null;
        fileInput.value = '';
        dropzoneEmpty.style.display = 'flex';
        dropzonePreview.style.display = 'none';
        previewImage.src = '';
        analyzeBtn.disabled = true;
        
        // Remove active state on sample cards
        document.querySelectorAll('.sample-card').forEach(c => c.classList.remove('active'));
    }

    function handleSelectedFile(file, overrideName = null) {
        if (!file.type.startsWith('image/')) {
            alert('Please select a valid image file (JPG, PNG, WEBP, BMP).');
            return;
        }

        currentFile = file;
        const name = overrideName || file.name || 'image.jpg';
        const sizeFormatted = (file.size / 1024).toFixed(1) + ' KB';

        previewFilename.textContent = name;
        previewFilesize.textContent = sizeFormatted;

        const reader = new FileReader();
        reader.onload = (e) => {
            previewImage.src = e.target.result;
            dropzoneEmpty.style.display = 'none';
            dropzonePreview.style.display = 'flex';
            analyzeBtn.disabled = false;
        };
        reader.readAsDataURL(file);
    }

    // 7. Load & Auto-Analyze a Sample Flower
    function loadSampleImage(url, category) {
        // Highlight active sample card
        document.querySelectorAll('.sample-card').forEach(c => {
            if (c.getAttribute('data-category') === category) {
                c.classList.add('active');
            } else {
                c.classList.remove('active');
            }
        });

        fetch(url)
            .then(res => res.blob())
            .then(blob => {
                const sampleFile = new File([blob], `${category}.jpg`, { type: 'image/jpeg' });
                handleSelectedFile(sampleFile, `${category}_sample.jpg`);
                // Auto trigger detection for quick feedback
                runInference(sampleFile);
            })
            .catch(err => {
                console.error('Failed to load sample image:', err);
            });
    }

    // 8. Analyze Button Trigger
    analyzeBtn.addEventListener('click', () => {
        if (currentFile) {
            runInference(currentFile);
        }
    });

    // 9. Inference Execution Function
    function runInference(file) {
        // UI Loading State
        analyzeBtn.disabled = true;
        btnSpinner.style.display = 'inline-block';
        analyzeBtnText.textContent = 'Classifying Flower...';

        const formData = new FormData();
        formData.append('file', file);

        fetch('/api/predict', {
            method: 'POST',
            body: formData,
        })
            .then(res => {
                if (!res.ok) {
                    throw new Error(`Inference error: HTTP ${res.status}`);
                }
                return res.json();
            })
            .then(data => {
                if (data.success) {
                    renderPredictionResults(data);
                } else {
                    alert('Prediction failed: ' + (data.error || 'Unknown error'));
                }
            })
            .catch(err => {
                console.error('Error during prediction:', err);
                alert('Could not complete prediction. Please verify the server is running.');
            })
            .finally(() => {
                analyzeBtn.disabled = false;
                btnSpinner.style.display = 'none';
                analyzeBtnText.textContent = 'Analyze Flower';
            });
    }

    // 10. Render Prediction Results & Progress Bars
    function renderPredictionResults(data) {
        resultPlaceholder.style.display = 'none';
        resultContent.style.display = 'flex';

        // Winner Card
        const topInfo = data.flower_info || {};
        winnerName.textContent = data.display_name;
        winnerScientific.textContent = topInfo.scientific_name || '';
        winnerIcon.textContent = topInfo.icon || '🌸';
        winnerConfidence.textContent = data.confidence_pct;
        winnerDescription.textContent = topInfo.description || '';
        winnerFamily.textContent = topInfo.family || 'Botanical';
        winnerSeason.textContent = topInfo.season || 'Annual';

        if (topInfo.color) {
            winnerConfidence.style.color = topInfo.color;
            winnerName.style.color = topInfo.color;
        }

        // Probabilities List
        probabilitiesList.innerHTML = '';
        if (data.ranked_list && data.ranked_list.length > 0) {
            data.ranked_list.forEach((item, index) => {
                const probItem = document.createElement('div');
                probItem.className = 'prob-item';

                const isWinner = index === 0;
                probItem.innerHTML = `
                    <div class="prob-info-row">
                        <div class="prob-name-group" style="${isWinner ? 'color: ' + item.color + ';' : ''}">
                            <span>${item.icon}</span>
                            <span>${item.display_name}</span>
                        </div>
                        <span class="prob-pct-val" style="${isWinner ? 'color: ' + item.color + ';' : ''}">
                            ${item.percentage}
                        </span>
                    </div>
                    <div class="prob-bar-track">
                        <div class="prob-bar-fill" id="probBar_${item.name}" style="background: ${item.color}; width: 0%;"></div>
                    </div>
                `;
                probabilitiesList.appendChild(probItem);

                // Animate progress bar fill width after insertion
                requestAnimationFrame(() => {
                    setTimeout(() => {
                        const bar = document.getElementById(`probBar_${item.name}`);
                        if (bar) {
                            bar.style.width = `${item.percent_value}%`;
                        }
                    }, 50 * index);
                });
            });
        }

        // Raw JSON output
        jsonCodeOutput.textContent = JSON.stringify(data, null, 2);
    }
});