-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
399 lines (312 loc) · 15.1 KB
/
index.html
File metadata and controls
399 lines (312 loc) · 15.1 KB
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
<!DOCTYPE html>
<html>
<head>
<title>XAI Microservices Interface</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
/* Custom styles */
.hidden {
display: none;
}
.arrow {
cursor: pointer;
}
.loader {
border: 16px solid #f3f3f3;
border-top: 16px solid #3498db;
border-radius: 50%;
width: 120px;
height: 120px;
animation: spin 2s linear infinite;
margin: 20px auto;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-center">XAI Microservices Interface</h1>
<!-- Image Classification Prediction -->
<div class="section">
<h2>
<span class="arrow" onclick="toggleSection('predictForm') ">▶</span>
Image Classification Prediction
</h2>
<!-- API Endpoint: /predict/image -->
<form id="predictForm" enctype="multipart/form-data">
<div class="form-group">
<label for="imageFile">Image File:</label>
<input type="file" id="imageFile" name="file" accept=".jpg,.jpeg,.png" required>
</div>
<button type="submit" class="btn btn-primary">Predict</button>
</br> </br>
<table id="predictionResult" class="section-result">
<thead class="hidden">
<tr>
<th>Name </th>
<th> Probability</th>
</tr>
</thead>
<tbody></tbody>
</table>
</form>
<!-- <div id="predictionResult" class="section-result"></div>
</br> -->
</br>
</div>
<!-- LIME Explanation -->
<div class="section">
<h2>
<span class="arrow" onclick="toggleSection('limeForm')">▶</span>
LIME Explanation
</h2>
<!-- API Endpoint: /explain_lime/image -->
<form id="limeForm" enctype="multipart/form-data">
<div class="form-group">
<label for="limeImageFile">Image File:</label>
<input type="file" id="limeImageFile" name="file" accept=".jpg,.jpeg,.png" required>
</div>
<button type="submit" class="btn btn-primary">Explain</button> </br> </br>
<div id="limeExplanation" class="section-result"></div>
<div id="limesegments" class="section-result"></div>
<div id="limetop_T" class="section-result"></div>
<div id="top_T_plot_base64"></div>
<div id="limeImageResult"></div>
<div id="segment_overlay_base64"></div>
<div id="bar_plot_base64"></div>
</form>
</br>
</div>
<!-- SHAP Explanation -->
<div class="section">
<h2>
<span class="arrow" onclick="toggleSection('shapForm')">▶</span>
SHAP Explanation
</h2>
<!-- API Endpoint: /explain_shap/image -->
<form id="shapForm" enctype="multipart/form-data">
<div class="form-group">
<label for="shapImageFile">Image File:</label>
<input type="file" id="shapImageFile" name="file" accept=".jpg,.jpeg,.png" required>
</div>
<button type="submit" class="btn btn-primary">Explain</button> </br> </br>
<div id="shapExplanation" class="section-result"></div>
<div id="shappredictionResult"></div>
<div id="shapImageResult"></div>
<div id="shap_S_plot_base64"></div>
</form>
</br>
</div>
<!-- Occlusion Sensitivity Explanation -->
<div class="section">
<h2>
<span class="arrow" onclick="toggleSection('occlusionForm')">▶</span>
Occlusion Sensitivity Explanation
</h2>
<!-- API Endpoint: /explain_occlusion/image -->
<form id="occlusionForm" enctype="multipart/form-data">
<div class="form-group">
<label for="occlusionImageFile">Image File:</label>
<input type="file" id="occlusionImageFile" name="file" accept=".jpg,.jpeg,.png" required>
</div>
<div class="form-group">
<label for="occlusionClassLabel">Class Label:</label>
<select id="occlusionClassLabel" required class="form-control">
<option value="cardboard">Cardboard</option>
<option value="glass">Glass</option>
<option value="metal">Metal</option>
<option value="paper">Paper</option>
<option value="plastic">Plastic</option>
<option value="trash">Trash</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Explain</button> </br> </br>
<div id="occlusionExplanation" class="section-result"></div>
<div id="occlusionImageResult"></div>
</form>
</br>
</div>
</div>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
// Image Classification Prediction
const predictForm = document.getElementById('predictForm');
const imageFileInput = document.getElementById('imageFile');
// const predictionResult = document.getElementById('predictionResult');
const predictionResultTable = document.getElementById('predictionResult');
const predictionResultTbody = predictionResultTable.getElementsByTagName('tbody')[0];
const predictionResultThead = predictionResultTable.getElementsByTagName('thead')[0];
predictForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData();
formData.append('file', imageFileInput.files[0]);
const response = await fetch('/predict/image', {
method: 'POST',
body: formData
});
const prediction = await response.json();
// predictionResult.innerHTML = JSON.stringify(prediction);
// predictionResult.innerHTML = 'Name: ' + prediction[0] + ', Probability: ' + prediction[1];
// Clear the existing prediction results
predictionResultTbody.innerHTML = '';
if (prediction.length > 0) {
// Show the table headers
predictionResultThead.classList.remove('hidden');
// Create a new row in the table
const newRow = predictionResultTbody.insertRow();
// Create cells for the name and probability
const nameCell = newRow.insertCell();
const probCell = newRow.insertCell();
// Set the text content of the cells
nameCell.textContent = prediction[0];
probCell.textContent = prediction[1];
} else {
// Hide the table headers
predictionResultThead.classList.add('hidden');
}
// Clear the file input field
imageFileInput.value = '';
});
// LIME Explanation
const limeForm = document.getElementById('limeForm');
const limeImageFileInput = document.getElementById('limeImageFile');
const limeExplanation = document.getElementById('limeExplanation');
const limetop_T = document.getElementById('limetop_T');
const top_T_plot_base64 = document.getElementById('top_T_plot_base64');
const limesegments = document.getElementById('limesegments');
const limeImageResult = document.getElementById('limeImageResult');
const bar_plot_base64 = document.getElementById('bar_plot_base64');
const segment_overlay_base64 = document.getElementById('segment_overlay_base64');
limeForm.addEventListener('submit', async (e) => {
e.preventDefault();
// Display the loading spinner
limeExplanation.innerHTML = '<div class="loader"></div>';
const formData = new FormData();
formData.append('file', limeImageFileInput.files[0]);
const response = await fetch('/explain_lime/image', {
method: 'POST',
body: formData
});
const result = await response.json();
const lime_explanation = result.lime_explanation;
const top_T = result.top_T;
const top_T_plot = result.top_T_plot_base64;
const segments = result.segments;
const dataURL = result.image_base64;
const bar_plot = result.bar_plot_base64;
const segment_overlay = result.segment_overlay_base64;
// Display explanations
limeExplanation.innerHTML = `<b> Explanation :</b> ${lime_explanation}`;
// Display segments
limesegments.innerHTML = `<b>Segments :</b> ${segments}`;
// Display top_T
limetop_T.innerHTML = `<b>Top Lables :</b> ${top_T}`;
// Create an <img> element for the image
const image = document.createElement('img');
image.src = `data:image/jpeg;base64,${dataURL}`;
limeImageResult.innerHTML = '';
limeImageResult.appendChild(image);
// Create an <img> element for the segment_overlay_base64
const image2 = document.createElement('img');
image2.src = `data:image/png;base64,${segment_overlay}`;
segment_overlay_base64.innerHTML = '';
segment_overlay_base64.appendChild(image2);
// // Create an <img> element for the bar_plot_base64
const image3 = document.createElement('img');
image3.src = `data:image/png;base64,${bar_plot}`;
bar_plot_base64.innerHTML = '';
bar_plot_base64.appendChild(image3);
// Create an <img> element for the top_T_plot_base64
const image4 = document.createElement('img');
image4.src = `data:image/png;base64,${top_T_plot}`;
top_T_plot_base64.innerHTML = '';
top_T_plot_base64.appendChild(image4);
});
// SHAP Explanation
const shapForm = document.getElementById('shapForm');
const shapImageFileInput = document.getElementById('shapImageFile');
const shapExplanation = document.getElementById('shapExplanation');
const shapImageResult = document.getElementById('shapImageResult');
const shap_S_plot_base64 = document.getElementById('shap_S_plot_base64');
const shappredictionResult = document.getElementById('shappredictionResult');
shapForm.addEventListener('submit', async (e) => {
e.preventDefault();
// Display the loading spinner
shapExplanation.innerHTML = '<div class="loader"></div>';
const formData = new FormData();
formData.append('file', shapImageFileInput.files[0]);
const response = await fetch('/explain_shap/image', {
method: 'POST',
body: formData
});
const result = await response.json();
// shappredictionResult.innerHTML = JSON.stringify(result.prediction);
// shappredictionResult.innerHTML = `<b> Prediction :</b> ${result.prediction}`
shappredictionResult.innerHTML = '<b> Prediction : </b>' + result.prediction[0] + ',<b> Probability : </b>' + result.prediction[1];
shapExplanation.innerHTML = `<b> Explanation :</b> ${result.explaination}`
const dataURL = result.shap_V_plot_base64;
const shap_S__base64 = result.shap_S_plot_base64;
console.log("shap_S__base64");
console.log(shap_S__base64);
// Create an <img> element for the image
const image = document.createElement('img');
image.src = `data:image/jpeg;base64,${dataURL}`;
shapImageResult.innerHTML = '';
shapImageResult.appendChild(image);
// Create an <img> element for the image
const image2 = document.createElement('img');
image2.src = `data:image/jpeg;base64,${shap_S__base64}`;
shap_S_plot_base64.innerHTML = '';
shap_S_plot_base64.appendChild(image2);
});
// Occlusion Sensitivity Explanation
const occlusionForm = document.getElementById('occlusionForm');
const occlusionImageFileInput = document.getElementById('occlusionImageFile');
const occlusionClassLabel = document.getElementById('occlusionClassLabel');
const occlusionExplanation = document.getElementById('occlusionExplanation');
const occlusionImageResult = document.getElementById('occlusionImageResult');
occlusionForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData();
formData.append('file', occlusionImageFileInput.files[0]);
const selectedLabel = occlusionClassLabel.value;
const queryParams = new URLSearchParams();
queryParams.append('imagetype', selectedLabel);
const queryString = queryParams.toString();
const response = await fetch(`/explain_occlusion/image?${queryString}`, {
method: 'POST',
body: formData
});
const result = await response.json();
occlusionExplanation.innerHTML = JSON.stringify(result.explanation);
occlusionExplanation.innerHTML = `<b> Explanation :</b> ${result.explanation}`
console.log(result);
console.log(result.explanation);
const dataURL = result.image_base64;
// Create an <img> element for the image
const image = document.createElement('img');
image.src = `data:image/jpeg;base64,${dataURL}`;
occlusionImageResult.innerHTML = '';
occlusionImageResult.appendChild(image);
});
function toggleSection(sectionId) {
const section = document.getElementById(sectionId);
const arrow = section.previousElementSibling.firstChild;
if (section.classList.contains('hidden')) {
section.classList.remove('hidden');
arrow.innerHTML = '▼';
} else {
section.classList.add('hidden');
arrow.innerHTML = '▶';
}
}
</script>
</body>
</html>