-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (87 loc) · 3.67 KB
/
script.js
File metadata and controls
94 lines (87 loc) · 3.67 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
// script.js
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('predictionForm');
const resultDiv = document.getElementById('result');
const predictionOutput = document.getElementById('predictionOutput');
const pastPredictions = document.getElementById('pastPredictions');
// Real-time previews
const inputs = {
sepalLength: document.getElementById('sepalLength'),
sepalWidth: document.getElementById('sepalWidth'),
petalLength: document.getElementById('petalLength'),
petalWidth: document.getElementById('petalWidth')
};
Object.keys(inputs).forEach(key => {
inputs[key].addEventListener('input', (e) => {
document.getElementById(`${key}Preview`).textContent = e.target.value || 0;
});
});
// Form submission
form.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(form);
try {
const response = await fetch('predict.php', {
method: 'POST',
body: formData
});
// Robustly parse JSON and handle non-JSON errors
const contentType = response.headers.get('content-type') || '';
let data;
if (contentType.includes('application/json')) {
data = await response.json();
} else {
const text = await response.text();
throw new Error(`Unexpected response. Status ${response.status}. Body: ${text?.slice(0, 200)}`);
}
if (response.ok && data && data.success) {
const species = data.prediction;
const iconClass = species.toLowerCase();
predictionOutput.innerHTML = `
<div class="result-icon ${iconClass}">🌸</div>
<p><strong>Predicted Species:</strong> Iris ${species}</p>
`;
resultDiv.classList.remove('d-none');
loadPastPredictions();
} else {
const errMsg = (data && data.error) ? data.error : 'Unknown error';
alert('Error: ' + errMsg);
}
} catch (error) {
alert('Failed to predict: ' + error.message);
}
});
// Load past predictions
async function loadPastPredictions() {
try {
const response = await fetch('predict.php?action=get_past');
const contentType = response.headers.get('content-type') || '';
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to load history (${response.status}): ${text?.slice(0, 200)}`);
}
if (!contentType.includes('application/json')) {
const text = await response.text();
throw new Error(`Unexpected response for history. Body: ${text?.slice(0, 200)}`);
}
const data = await response.json();
pastPredictions.innerHTML = '';
data.forEach(row => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${row.id}</td>
<td>${row.sepal_length}</td>
<td>${row.sepal_width}</td>
<td>${row.petal_length}</td>
<td>${row.petal_width}</td>
<td>${row.prediction}</td>
<td>${row.timestamp}</td>
`;
pastPredictions.appendChild(tr);
});
} catch (error) {
console.error('Failed to load past predictions:', error);
}
}
loadPastPredictions();
});