Spaces:
Sleeping
Sleeping
Create src/templates/index.html
Browse files- src/src/templates/index.html +132 -0
src/src/templates/index.html
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 2 |
+
|
| 3 |
+
// INITIALIZE ALL NUMBER INPUTS
|
| 4 |
+
const containers = document.querySelectorAll('.number-input-container');
|
| 5 |
+
|
| 6 |
+
containers.forEach(container => {
|
| 7 |
+
const input = container.querySelector('.number-input');
|
| 8 |
+
const decreaseBtn = container.querySelector('.decrease-btn');
|
| 9 |
+
const increaseBtn = container.querySelector('.increase-btn');
|
| 10 |
+
|
| 11 |
+
// Helper: Validate and clamp value based on min/max attributes
|
| 12 |
+
function validateAndClamp(value) {
|
| 13 |
+
const min = parseFloat(input.getAttribute('min')) || 0;
|
| 14 |
+
const max = parseFloat(input.getAttribute('max')) || Infinity;
|
| 15 |
+
const step = parseFloat(input.getAttribute('step')) || 1;
|
| 16 |
+
|
| 17 |
+
if (value > max) return max;
|
| 18 |
+
if (value < min) return min;
|
| 19 |
+
return value;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// Helper: Update value logic
|
| 23 |
+
function updateValue(change) {
|
| 24 |
+
let current = parseFloat(input.value) || 0;
|
| 25 |
+
let step = parseFloat(input.getAttribute('step')) || 1;
|
| 26 |
+
|
| 27 |
+
// Fix float math issues (e.g. 0.1 + 0.2)
|
| 28 |
+
let newValue = current + (change * step);
|
| 29 |
+
newValue = Math.round(newValue * 1000) / 1000;
|
| 30 |
+
|
| 31 |
+
const clamped = validateAndClamp(newValue);
|
| 32 |
+
input.value = clamped;
|
| 33 |
+
input.dispatchEvent(new Event('input')); // Trigger events
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// Button Listeners
|
| 37 |
+
decreaseBtn.addEventListener('click', () => updateValue(-1));
|
| 38 |
+
increaseBtn.addEventListener('click', () => updateValue(1));
|
| 39 |
+
|
| 40 |
+
// Validation on Blur (Flash Red if corrected)
|
| 41 |
+
input.addEventListener('blur', function() {
|
| 42 |
+
let current = parseFloat(this.value) || 0;
|
| 43 |
+
const clamped = validateAndClamp(current);
|
| 44 |
+
|
| 45 |
+
if (current !== clamped) {
|
| 46 |
+
this.value = clamped;
|
| 47 |
+
this.style.color = '#f87171';
|
| 48 |
+
setTimeout(() => this.style.color = '#ffffff', 300);
|
| 49 |
+
}
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
// Mouse Wheel Support
|
| 53 |
+
input.addEventListener('wheel', function(e) {
|
| 54 |
+
if (document.activeElement === input) {
|
| 55 |
+
e.preventDefault();
|
| 56 |
+
updateValue(e.deltaY < 0 ? 1 : -1);
|
| 57 |
+
}
|
| 58 |
+
});
|
| 59 |
+
});
|
| 60 |
+
});
|
| 61 |
+
|
| 62 |
+
// PREDICTION LOGIC (Sends data to Flask)
|
| 63 |
+
async function makePrediction() {
|
| 64 |
+
const submitBtn = document.querySelector('.submit-btn');
|
| 65 |
+
const originalText = submitBtn.innerText;
|
| 66 |
+
|
| 67 |
+
// Loading State
|
| 68 |
+
submitBtn.innerText = "Analyzing...";
|
| 69 |
+
submitBtn.disabled = true;
|
| 70 |
+
submitBtn.style.opacity = "0.7";
|
| 71 |
+
|
| 72 |
+
// Collect Data
|
| 73 |
+
const data = {
|
| 74 |
+
Pregnancies: Number(document.getElementById('Pregnancies').value),
|
| 75 |
+
Glucose: Number(document.getElementById('Glucose').value),
|
| 76 |
+
BloodPressure: Number(document.getElementById('BloodPressure').value),
|
| 77 |
+
SkinThickness: Number(document.getElementById('SkinThickness').value),
|
| 78 |
+
Insulin: Number(document.getElementById('Insulin').value),
|
| 79 |
+
BMI: Number(document.getElementById('BMI').value),
|
| 80 |
+
DiabetesPedigreeFunction: Number(document.getElementById('DiabetesPedigreeFunction').value),
|
| 81 |
+
Age: Number(document.getElementById('Age').value)
|
| 82 |
+
};
|
| 83 |
+
|
| 84 |
+
try {
|
| 85 |
+
// Send to Flask
|
| 86 |
+
const response = await fetch('/predict', {
|
| 87 |
+
method: 'POST',
|
| 88 |
+
headers: { 'Content-Type': 'application/json' },
|
| 89 |
+
body: JSON.stringify(data)
|
| 90 |
+
});
|
| 91 |
+
|
| 92 |
+
const result = await response.json();
|
| 93 |
+
|
| 94 |
+
// Update UI
|
| 95 |
+
const resultCard = document.getElementById('resultCard');
|
| 96 |
+
|
| 97 |
+
const isHighRisk = result.prediction === 1;
|
| 98 |
+
const colorClass = isHighRisk ? 'danger-color' : 'safe-color';
|
| 99 |
+
const barColor = isHighRisk ? '#f87171' : '#34d399';
|
| 100 |
+
const title = isHighRisk ? 'High Risk' : 'Low Risk';
|
| 101 |
+
const message = isHighRisk
|
| 102 |
+
? 'The model suggests a high probability of diabetes.'
|
| 103 |
+
: 'The model suggests a low probability of diabetes.';
|
| 104 |
+
|
| 105 |
+
resultCard.innerHTML = `
|
| 106 |
+
<div class="result-box">
|
| 107 |
+
<p style="color: #9ca3af; font-size: 0.9rem; letter-spacing: 1px; margin-bottom:10px;">PREDICTION RESULT</p>
|
| 108 |
+
<div class="risk-level ${colorClass}">${title}</div>
|
| 109 |
+
<p style="margin-bottom: 20px; color: #e0e0e0;">${message}</p>
|
| 110 |
+
|
| 111 |
+
<div style="text-align: left; width: 100%; background: #1c1c2e; padding: 15px; border-radius: 8px; border: 1px solid #2e2e42;">
|
| 112 |
+
<div style="display:flex; justify-content:space-between; font-size: 0.9rem; color: #ccc; margin-bottom: 8px;">
|
| 113 |
+
<span>Confidence Score</span>
|
| 114 |
+
<span style="color: #fff; font-weight: bold;">${result.confidence.toFixed(1)}%</span>
|
| 115 |
+
</div>
|
| 116 |
+
<div class="confidence-bar-bg">
|
| 117 |
+
<div class="confidence-bar-fill" style="width: ${result.confidence}%; background-color: ${barColor};"></div>
|
| 118 |
+
</div>
|
| 119 |
+
</div>
|
| 120 |
+
</div>
|
| 121 |
+
`;
|
| 122 |
+
|
| 123 |
+
} catch (error) {
|
| 124 |
+
console.error("Error:", error);
|
| 125 |
+
alert("Server Error: Make sure the Flask app is running.");
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
// Reset Button
|
| 129 |
+
submitBtn.innerText = originalText;
|
| 130 |
+
submitBtn.disabled = false;
|
| 131 |
+
submitBtn.style.opacity = "1";
|
| 132 |
+
}
|