Spaces:
Runtime error
Runtime error
File size: 9,968 Bytes
aa5c395 | 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 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Batik Classifier - Test Web Interface</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
border-radius: 20px;
padding: 40px;
max-width: 600px;
width: 100%;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
h1 {
text-align: center;
color: #333;
margin-bottom: 10px;
}
.subtitle {
text-align: center;
color: #666;
margin-bottom: 30px;
}
.upload-area {
border: 3px dashed #667eea;
border-radius: 10px;
padding: 40px;
text-align: center;
cursor: pointer;
transition: all 0.3s;
margin-bottom: 20px;
}
.upload-area:hover {
border-color: #764ba2;
background: #f8f9ff;
}
.upload-area.dragover {
background: #e8ecff;
border-color: #5568d3;
}
input[type="file"] {
display: none;
}
.upload-icon {
font-size: 48px;
margin-bottom: 10px;
}
.btn {
width: 100%;
padding: 15px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 10px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: transform 0.2s;
}
.btn:hover {
transform: translateY(-2px);
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.preview {
text-align: center;
margin: 20px 0;
}
.preview img {
max-width: 100%;
max-height: 300px;
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.result {
margin-top: 20px;
padding: 20px;
background: #f8f9ff;
border-radius: 10px;
display: none;
}
.result.show {
display: block;
}
.prediction {
font-size: 24px;
font-weight: bold;
color: #667eea;
margin-bottom: 10px;
}
.confidence {
font-size: 18px;
color: #666;
margin-bottom: 20px;
}
.top-predictions {
margin-top: 20px;
}
.top-predictions h3 {
margin-bottom: 10px;
color: #333;
}
.pred-item {
display: flex;
justify-content: space-between;
padding: 10px;
background: white;
border-radius: 5px;
margin-bottom: 5px;
}
.loading {
text-align: center;
display: none;
}
.loading.show {
display: block;
}
.spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid #667eea;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 20px auto;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.error {
color: #e74c3c;
background: #ffe8e8;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
display: none;
}
.error.show {
display: block;
}
</style>
</head>
<body>
<div class="container">
<h1>🎨 Batik Classifier</h1>
<p class="subtitle">AI-Powered Batik Motif Recognition (95% Accuracy)</p>
<div class="upload-area" id="uploadArea">
<div class="upload-icon">📸</div>
<p><strong>Click to upload</strong> or drag and drop</p>
<p style="font-size: 14px; color: #999; margin-top: 5px;">PNG, JPG up to 10MB</p>
<input type="file" id="fileInput" accept="image/*">
</div>
<div class="preview" id="preview"></div>
<button class="btn" id="predictBtn" disabled>🔍 Analyze Batik</button>
<div class="loading" id="loading">
<div class="spinner"></div>
<p>Analyzing image...</p>
</div>
<div class="error" id="error"></div>
<div class="result" id="result">
<div class="prediction">
<span id="predictionText"></span>
</div>
<div class="confidence">
Confidence: <strong id="confidenceText"></strong>
</div>
<div class="top-predictions">
<h3>📊 Top 5 Predictions:</h3>
<div id="topPredictions"></div>
</div>
</div>
</div>
<script>
const API_URL = 'http://192.168.0.106:5000';
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
const predictBtn = document.getElementById('predictBtn');
const preview = document.getElementById('preview');
const loading = document.getElementById('loading');
const result = document.getElementById('result');
const error = document.getElementById('error');
let selectedFile = null;
// Click to upload
uploadArea.addEventListener('click', () => fileInput.click());
// File selection
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
handleFile(file);
}
});
// Drag and drop
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
const file = e.dataTransfer.files[0];
if (file && file.type.startsWith('image/')) {
handleFile(file);
}
});
// Handle file
function handleFile(file) {
selectedFile = file;
// Show preview
const reader = new FileReader();
reader.onload = (e) => {
preview.innerHTML = `<img src="${e.target.result}" alt="Preview">`;
};
reader.readAsDataURL(file);
// Enable predict button
predictBtn.disabled = false;
// Hide previous results
result.classList.remove('show');
error.classList.remove('show');
}
// Predict
predictBtn.addEventListener('click', async () => {
if (!selectedFile) return;
// Show loading
loading.classList.add('show');
result.classList.remove('show');
error.classList.remove('show');
predictBtn.disabled = true;
try {
const formData = new FormData();
formData.append('image', selectedFile);
const response = await fetch(`${API_URL}/predict`, {
method: 'POST',
body: formData
});
const data = await response.json();
if (data.success) {
// Show result
document.getElementById('predictionText').textContent = data.prediction;
document.getElementById('confidenceText').textContent = data.percentage;
const topPredDiv = document.getElementById('topPredictions');
topPredDiv.innerHTML = data.top_5_predictions.map((pred, idx) => `
<div class="pred-item">
<span>${idx + 1}. ${pred.class}</span>
<strong>${pred.percentage}</strong>
</div>
`).join('');
result.classList.add('show');
} else {
error.textContent = data.error || 'Prediction failed';
error.classList.add('show');
}
} catch (err) {
error.textContent = `Error: ${err.message}. Make sure the API server is running.`;
error.classList.add('show');
} finally {
loading.classList.remove('show');
predictBtn.disabled = false;
}
});
</script>
</body>
</html>
|