-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (71 loc) · 2.97 KB
/
script.js
File metadata and controls
81 lines (71 loc) · 2.97 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
let model;
const windowSize = 25; // Asegúrate de usar el mismo tamaño de ventana que tu modelo
async function loadModel() {
model = await tf.loadLayersModel('modelJSON/model.json');
console.log("Model loaded successfully.");
}
async function predictFromCSV() {
if (!model) {
alert("Model is not loaded yet. Please wait.");
return;
}
const fileInput = document.getElementById('csvFile');
const file = fileInput.files[0];
const loadingElement = document.getElementById('loading');
const resultElement = document.getElementById('result');
if (!file) {
alert("Please upload a CSV file first.");
return;
}
loadingElement.style.display = 'block';
resultElement.innerText = '';
try {
const text = await file.text();
const data = parseCSV(text);
const processedData = processData(data);
const predictionTensor = model.predict(tf.tensor(processedData, [processedData.length, windowSize, 3]));
const predictions = await predictionTensor.array();
const meanPrediction = predictions.flat().reduce((acc, val) => acc + val) / predictions.length;
const result = meanPrediction > 0.5 ? "Human" : "Not Human (you are being hacked dude)";
try {
const response = await fetch('https://hendpoint.onrender.com/api/insert/' + result, { method: "POST" });
const data = await response.json();
console.log(data);
} catch (error) {
console.error("Error sending message to Telegram:", error);
}
resultElement.innerText = "Prediction: " + result;
} catch (error) {
console.error("Error during prediction:", error);
resultElement.innerText = "An error occurred during prediction. Please try again.";
} finally {
loadingElement.style.display = 'none';
}
}
function parseCSV(text) {
const lines = text.split('\n');
return lines.slice(1).map(line => {
const [, interval, charsPerSecond, errorCount] = line.split(',');
return {
Interval_ms: parseFloat(interval.replace(',', '.')),
Chars_per_Second: parseFloat(charsPerSecond.replace(',', '.')),
Error_Count: parseFloat(errorCount.replace(',', '.'))
};
});
}
function processData(data) {
return data.map((row, i, arr) =>
i + windowSize <= arr.length ? arr.slice(i, i + windowSize).map(r => [r.Interval_ms, r.Chars_per_Second, r.Error_Count]) : null
).filter(seq => seq);
}
loadModel();
loadModel().then(() => {
document.getElementById('csvFile').addEventListener('change', function(e) {
const fileName = e.target.files[0]?.name || 'Choose CSV File';
document.querySelector('.file-input-label').textContent = fileName;
});
});
document.getElementById('csvFile').addEventListener('change', function(e) {
const fileName = e.target.files[0]?.name || 'Choose CSV File';
document.querySelector('.file-input-label').textContent = fileName;
});