-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (43 loc) · 1.67 KB
/
app.py
File metadata and controls
62 lines (43 loc) · 1.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
from flask import Flask, request, jsonify,render_template
import pandas as pd
import numpy as np
from tensorflow.keras.models import load_model
import os
app = Flask(__name__)
# Cargar el modelo previamente entrenado
model = load_model("modelJSON/model.h5")
window_size = 25
def predict_human_behavior(csv_file_path):
dataset = pd.read_csv(csv_file_path)
dataset = dataset.drop(columns=['Timestamp', 'Key'], errors='ignore')
dataset = dataset.replace(',', '.', regex=True).astype(float)
X_data = dataset[["Interval_ms", "Chars_per_Second", "Error_Count"]].values
# Crear secuencias de entrada según el tamaño de ventana
X_seq = []
for i in range(len(X_data) - window_size + 1):
X_seq.append(X_data[i:i + window_size])
X_seq = np.array(X_seq).astype(np.float32)
predictions = model.predict(X_seq)
# Calcular el promedio de las predicciones
mean_prediction = np.mean(predictions)
result = "Human" if mean_prediction > 0.5 else "Not Human (you are being hacked, dude)"
return result
@app.route('/')
def index():
return render_template('index.html')
# Ruta principal para subir el archivo y obtener la predicción
@app.route('/predict', methods=['POST'])
def predict():
if 'file' not in request.files:
return jsonify({"error": "No file provided"}), 400
file = request.files['file']
file_path = os.path.join("uploads", file.filename)
file.save(file_path)
try:
result = predict_human_behavior(file_path)
finally:
os.remove(file_path)
return jsonify({"result": result})
if __name__ == '__main__':
os.makedirs("uploads", exist_ok=True)
app.run(host='0.0.0.0', port=5000)