File size: 10,365 Bytes
03e275e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Single Classification</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>

        html.dark {

            --bg-color: #1a202c;

            --text-color: #e2e8f0;

            --card-bg-color: #2d3748;

            --card-border-color: #4a5568;

            --btn-bg-color: #4a5568;

            --btn-hover-bg-color: #2c5282;

            --btn-text-color: #e2e8f0;

        }



        html.light {

            --bg-color: #f7fafc;

            --text-color: #2d3748;

            --card-bg-color: #ffffff;

            --card-border-color: #e2e8f0;

            --btn-bg-color: #3182ce;

            --btn-hover-bg-color: #2c5282;

            --btn-text-color: #ffffff;

        }



        body {

            background-color: var(--bg-color);

            color: var(--text-color);

        }



        .card {

            background-color: var(--card-bg-color);

            border: 1px solid var(--card-border-color);

        }



        .btn {

            background-color: var(--btn-bg-color);

            color: var(--btn-text-color);

        }



        .btn:hover {

            background-color: var(--btn-hover-bg-color);

        }



        .loading {

            display: none;

            position: fixed;

            top: 0;

            left: 0;

            width: 100%;

            height: 100%;

            background: rgba(0, 0, 0, 0.5);

            z-index: 1000;

        }



        .result-table {

            font-family: monospace;

            white-space: pre;

        }



        .classification-text {

            font-size: 1.5rem;

            font-weight: bold;

        }



        .btn-classify {

            background-color: #10b981;

            color: white;

            font-size: 1.1rem;

            padding: 0.75rem 1.5rem;

            border-radius: 9999px;

        }



        .btn-classify:hover {

            background-color: #059669;

        }



        .btn-remove {

            background-color: #ef4444;

            color: white;

            font-size: 1.1rem;

            padding: 0.75rem 1.5rem;

            border-radius: 9999px;

        }



        .btn-remove:hover {

            background-color: #dc2626;

        }



        .results-container {

            display: flex;

            flex-direction: column;

            align-items: center;

            text-align: center;

        }



        .classification-result {

            margin: 1.5rem 0;

            text-align: center;

        }



        .table-container {

            width: 100%;

            display: flex;

            justify-content: center;

            text-align: left;

        }



        /* Loading overlay styles */

        .loading-overlay {

            background: rgba(0, 0, 0, 0.7);

            color: white;

        }



        .loading-text {

            color: white;

            font-size: 1.2rem;

            font-weight: 500;

        }

    </style>
</head>
<body class="min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <div class="flex justify-end mb-4">
            <button id="themeToggle" class="bg-gray-800 text-white px-4 py-2 rounded hover:bg-gray-600">
                ☀️
            </button>
        </div>
        <h1 class="text-3xl font-bold text-center mb-8">Single Image Classification</h1>
        
        <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
            <!-- Left Column: Image Upload -->
            <div class="card p-6 rounded-lg shadow-md">
                <div id="uploadSection" class="mb-4">
                    <label class="block text-sm font-bold mb-2">Upload Image</label>
                    <input type="file" id="imageInput" accept=".jpg,.jpeg,.png" class="hidden">
                    <button onclick="document.getElementById('imageInput').click()" class="btn w-full py-2 px-4 rounded">
                        Select Image
                    </button>
                </div>
                
                <div id="imagePreview" class="mt-4 hidden">
                    <img id="preview" class="max-w-full h-auto rounded-lg">
                    <div class="mt-4 flex space-x-4">
                        <button onclick="removeImage()" class="btn btn-remove flex-1">
                            Remove
                        </button>
                        <button onclick="classifyImage()" class="btn btn-classify flex-1">
                            Classify
                        </button>
                    </div>
                </div>
            </div>
            
            <!-- Right Column: Results -->
            <div class="card p-6 rounded-lg shadow-md">
                <h2 class="text-xl font-bold mb-4 text-center">Classification Results</h2>
                <div id="results" class="results-container"></div>
            </div>
        </div>
    </div>

    <!-- Loading Overlay -->
    <div id="loading" class="loading flex items-center justify-center">
        <div class="loading-overlay p-8 rounded-lg text-center">
            <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-white mx-auto"></div>
            <p id="loadingText" class="mt-4 loading-text">Processing...</p>
        </div>
    </div>

    <script>

        // Theme handling

        const themeToggle = document.getElementById('themeToggle');

        const htmlElement = document.documentElement;

        

        // Initialize theme

        if (!localStorage.getItem('theme')) {

            localStorage.setItem('theme', 'dark');

        }

        

        // Apply saved theme or default to dark

        const currentTheme = localStorage.getItem('theme') || 'dark';

        htmlElement.classList.remove('light', 'dark');

        htmlElement.classList.add(currentTheme);

        themeToggle.textContent = currentTheme === 'dark' ? '☀️' : '🌙';



        // Theme toggle handler

        themeToggle.addEventListener('click', () => {

            const isDark = htmlElement.classList.contains('dark');

            htmlElement.classList.remove('dark', 'light');

            const newTheme = isDark ? 'light' : 'dark';

            htmlElement.classList.add(newTheme);

            localStorage.setItem('theme', newTheme);

            themeToggle.textContent = newTheme === 'dark' ? '☀️' : '🌙';

        });



        // File handling

        let currentFile = null;



        document.getElementById('imageInput').addEventListener('change', function(e) {

            const file = e.target.files[0];

            if (file) {

                uploadFile(file);

            }

        });



        function uploadFile(file) {

            const formData = new FormData();

            formData.append('file', file);

            

            showLoading('Uploading...');

            

            fetch('/upload_single', {

                method: 'POST',

                body: formData

            })

            .then(response => response.json())

            .then(data => {

                if (data.filename) {

                    currentFile = data.filename;

                    document.getElementById('preview').src = `/static/uploads/single/${data.filename}`;

                    document.getElementById('imagePreview').classList.remove('hidden');

                    document.getElementById('uploadSection').classList.add('hidden');

                } else {

                    alert(data.error || 'Upload failed');

                }

            })

            .catch(error => {

                console.error('Error:', error);

                alert('Upload failed');

            })

            .finally(() => {

                hideLoading();

            });

        }



        function removeImage() {

            document.getElementById('imageInput').value = '';

            document.getElementById('imagePreview').classList.add('hidden');

            document.getElementById('uploadSection').classList.remove('hidden');

            document.getElementById('results').innerHTML = '';

            currentFile = null;

        }



        function classifyImage() {

            if (!currentFile) {

                alert('Please upload an image first');

                return;

            }



            showLoading('Classifying...');



            fetch('/classify_single', {

                method: 'POST',

                headers: {

                    'Content-Type': 'application/json',

                },

                body: JSON.stringify({ filename: currentFile })

            })

            .then(response => response.json())

            .then(data => {

                if (data.error) {

                    throw new Error(data.error);

                }

                const resultsDiv = document.getElementById('results');

                resultsDiv.innerHTML = `

                    <div class="classification-result">

                        <span class="font-bold">Classification: </span>

                        <span class="classification-text ${data.classification === 'Pass' ? 'text-green-600' : 'text-red-600'}">

                            ${data.classification}

                        </span>

                    </div>

                    <div class="table-container">

                        <pre class="whitespace-pre-wrap font-mono text-sm">${data.result_table}</pre>

                    </div>

                `;

            })

            .catch(error => {

                console.error('Error:', error);

                alert('Classification failed: ' + error.message);

            })

            .finally(() => {

                hideLoading();

            });

        }



        function showLoading(text) {

            document.getElementById('loading').style.display = 'flex';

            document.getElementById('loadingText').textContent = text;

        }



        function hideLoading() {

            document.getElementById('loading').style.display = 'none';

        }

    </script>
</body>
</html>