-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyropredictor.html
More file actions
116 lines (100 loc) · 5 KB
/
pyropredictor.html
File metadata and controls
116 lines (100 loc) · 5 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pyro Predictor</title>
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../styles.css"> <!-- Link to your external CSS file -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.22.0/dist/tf.min.js"></script>
</head>
<body>
<br>
<br>
<h1 id="pageTitle">
Pyro Predictor
</h1>
<div class="container">
<button id="checkDangerLevel" onclick="fetchAndPredictVisualCrossing()">Predict</button>
<h2>7-Day Fire Danger Forecast</h2>
<div id="forecast-results">
<p id="result-day-1">Loading day 1...</p>
<p id="result-day-2">Loading day 2...</p>
<p id="result-day-3">Loading day 3...</p>
<p id="result-day-4">Loading day 4...</p>
<p id="result-day-5">Loading day 5...</p>
<p id="result-day-6">Loading day 6...</p>
<p id="result-day-7">Loading day 7...</p>
</div>
</div>
<script>
let model;
let scalerStats;
const labels = ['Low', 'Moderate', 'High', 'Very High', 'Extreme'];
async function loadEverything() {
try {
console.log("Loading model :)");
model = await tf.loadGraphModel('model.json');
console.log("Model loaded, loading scaler");
const res = await fetch('scaler_stats.json');
scalerStats = await res.json();
console.log("Model and scaler loaded.");
} catch (e) {
console.error("Failed to load model or scaler:", e);
}
}
function normalizeInput(inputArray, mins, maxs) {
return inputArray.map((val, i) => {
return (val - mins[i]) / (maxs[i] - mins[i]);
});
}
async function predictFireDanger(input) {
if (!model || !scalerStats) {
alert("Model or scaler not loaded yet!");
return;
}
const normInput = normalizeInput(input, scalerStats.mins, scalerStats.maxs);
const inputTensor = tf.tensor2d([normInput]); // shape [1, 9]
const prediction = model.predict(inputTensor);
const result = prediction.argMax(-1); // get index of highest probability
const predictedClass = (await result.data())[0]; // value 0-4
return predictedClass;
}
async function fetchAndPredictVisualCrossing() {
try {
const response = await fetch('https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/los%20angeles/next7days?unitGroup=us&include=days&key=3DAZML9PJ9STXC6VMGBYDMDGW&contentType=json');
const data = await response.json();
const days = data.days.slice(0, 7); // Get next 7 days
for (let i = 0; i < days.length; i++) {
const day = days[i];
const tempmax = day.tempmax || 0;
const dew = day.dew || 0;
const humidity = day.humidity || 0;
const precip = day.precip || 0;
const precipcover = day.precipcover || 0;
const windgust = day.windgust || 0;
const windspeed = day.windspeed || 0;
const solarradiation = day.solarradiation || 0;
const season = getSeasonFromMonth(new Date(day.datetime).getMonth() + 1);
const features = [tempmax, dew, humidity, precip, precipcover, windgust, windspeed, solarradiation, season];
console.log(`Day ${i + 1} features:`, features);
const predictedClass = await predictFireDanger(features);
const label = predictedClass !== undefined ? labels[predictedClass] : "Unavailable";
const resultDiv = document.getElementById(`result-day-${i + 1}`);
resultDiv.innerText = `${day.datetime}: ${label}`;
}
} catch (err) {
console.error('Error fetching weather from Visual Crossing:', err);
}
}
function getSeasonFromMonth(month) {
// 1: Winter, 2: Spring, 3: Summer, 4: Fall (just like your Python logic)
return Math.floor(((month % 12) + 3) / 3);
}
console.log("About to load model");
window.addEventListener('load', loadEverything);
</script>
<script src="loadHeader.js"></script>
<script src="animations.js"></script>
</body>
</html>