File size: 8,505 Bytes
5a58b2b | 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 | let predictedRiskLevel = 1;
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('predictionForm');
const heightInput = document.getElementById('Height');
const weightInput = document.getElementById('Weight');
const bmiInput = document.getElementById('BMI');
const modal = document.getElementById('resultModal');
const closeModalBtn = document.querySelector('.close-modal');
// Live Preview Elements
const livePreview = document.getElementById('livePreview');
const liveRiskScore = document.getElementById('liveRiskScore');
const liveRiskLabel = document.getElementById('liveRiskLabel');
// Auto-calculate BMI
function calculateBMI() {
const heightCm = parseFloat(heightInput.value);
const weightKg = parseFloat(weightInput.value);
if (heightCm > 0 && weightKg > 0) {
const heightM = heightCm / 100;
const bmi = (weightKg / (heightM * heightM)).toFixed(2);
bmiInput.value = bmi;
} else {
bmiInput.value = '';
}
}
heightInput.addEventListener('input', calculateBMI);
weightInput.addEventListener('input', calculateBMI);
// Live Preview Logic (Debounced)
let debounceTimer;
function updateLivePreview() {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(async () => {
// Check if required fields are filled (basic check)
if (!heightInput.value || !weightInput.value || !document.getElementById('Age').value) return;
const formData = new FormData(form);
const data = {};
formData.forEach((value, key) => {
if (form.querySelector(`input[name="${key}"][type="checkbox"]`)) {
data[key] = "1";
} else {
data[key] = value;
}
});
const checkboxes = form.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(cb => {
if (!data.hasOwnProperty(cb.name)) data[cb.name] = "0";
});
if (!data['BMI']) return;
try {
const response = await fetch('/api/v1/predict', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
if (response.ok) {
const result = await response.json();
liveRiskScore.textContent = (result.probability * 100).toFixed(1) + '%';
liveRiskLabel.textContent = result.risk_label;
livePreview.classList.add('visible');
if (result.risk_level <= 2) liveRiskScore.style.color = '#10B981';
else if (result.risk_level === 3) liveRiskScore.style.color = '#F59E0B';
else liveRiskScore.style.color = '#EF4444';
}
} catch (e) {
console.error("Live preview failed", e);
}
}, 500);
}
form.addEventListener('change', updateLivePreview);
// Form Submission
form.addEventListener('submit', async (e) => {
e.preventDefault();
const submitBtn = form.querySelector('button[type="submit"]');
const originalBtnText = submitBtn.innerHTML;
submitBtn.innerHTML = '<i class="fa-solid fa-spinner fa-spin"></i> Analyzing...';
submitBtn.disabled = true;
const formData = new FormData(form);
const data = {};
formData.forEach((value, key) => {
if (form.querySelector(`input[name="${key}"][type="checkbox"]`)) {
data[key] = "1";
} else {
data[key] = value;
}
});
const checkboxes = form.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(cb => {
if (!data.hasOwnProperty(cb.name)) {
data[cb.name] = "0";
}
});
try {
const response = await fetch('/api/v1/predict', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const result = await response.json();
window.lastResult = result;
showResult(result);
} catch (error) {
console.error('Error:', error);
alert('An error occurred during analysis. Please try again.');
} finally {
submitBtn.innerHTML = originalBtnText;
submitBtn.disabled = false;
}
});
function showResult(result) {
const riskLevelEl = document.getElementById('riskLevel');
const riskIconEl = document.getElementById('riskIcon');
const probFill = document.getElementById('probFill');
const probValue = document.getElementById('probValue');
predictedRiskLevel = result.risk_level;
const probability = result.probability;
const percentage = (probability * 100).toFixed(1) + '%';
let colorStart, colorEnd, iconClass, text;
if (predictedRiskLevel <= 2) {
text = 'Low Risk';
iconClass = 'fa-heart-circle-check risk-low';
colorStart = '#34D399';
colorEnd = '#10B981';
riskLevelEl.style.color = '#10B981';
} else if (predictedRiskLevel === 3) {
text = 'Moderate Risk';
iconClass = 'fa-heart-circle-exclamation';
colorStart = '#FBBF24';
colorEnd = '#F59E0B';
riskLevelEl.style.color = '#F59E0B';
riskIconEl.innerHTML = `<i class="fa-solid ${iconClass}" style="color: #F59E0B"></i>`;
} else {
text = 'High Risk';
iconClass = 'fa-heart-crack risk-high';
colorStart = '#F87171';
colorEnd = '#EF4444';
riskLevelEl.style.color = '#EF4444';
}
riskLevelEl.textContent = text;
if (predictedRiskLevel !== 3) {
riskLevelEl.className = predictedRiskLevel <= 2 ? 'risk-low' : 'risk-high';
riskIconEl.innerHTML = `<i class="fa-solid ${iconClass}"></i>`;
}
probFill.style.background = `linear-gradient(90deg, ${colorStart}, ${colorEnd})`;
probFill.style.width = percentage;
probValue.textContent = percentage;
modal.classList.add('visible');
}
function closeModal() {
modal.classList.remove('visible');
}
closeModalBtn.addEventListener('click', closeModal);
window.onclick = function (event) {
if (event.target == modal) {
closeModal();
}
}
// smooth scroll to sections
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
if (targetId === '#') return;
const targetElement = document.querySelector(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth'
});
}
});
});
window.closeModal = closeModal;
});
function goToRecommendations() {
// Gather key parameters for recommendation personalization
const params = new URLSearchParams();
// 1. Pass the exact calculated probability
if (window.lastResult && window.lastResult.probability) {
params.append('risk', (window.lastResult.probability * 100).toFixed(1));
}
// 2. Selects and Inputs
const fields = [
'Age', 'BMI', 'Sex', 'Smoking', 'Diabetes',
'Alcohol', 'Fried_Potato', 'Fruit', 'Green_Vegetables', 'Exercise', 'General_Health'
];
fields.forEach(id => {
const el = document.getElementById(id);
if (el) params.append(id, el.value);
});
// 3. Checkboxes (Special handling)
const checkBoxes = ['Skin_Cancer', 'Other_Cancer', 'Depression', 'Arthritis'];
checkBoxes.forEach(id => {
const el = document.getElementById(id);
if (el) params.append(id, el.checked ? "1" : "0");
});
window.location.href = `/recommendation/${predictedRiskLevel}?${params.toString()}`;
}
|