-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
47 lines (34 loc) · 1.36 KB
/
Copy pathapp.py
File metadata and controls
47 lines (34 loc) · 1.36 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
from flask import Flask, request, jsonify
import joblib
import numpy as np
import pandas as pd
app = Flask(__name__)
# Load the model and scaler
model = joblib.load('lstm_model.pkl')
scaler = joblib.load('scaler.pkl')
@app.route('/detect', methods=['POST'])
def predict():
# Get the data from the request
data = request.json
input_data = pd.DataFrame(data['input'])
# Ensure 'Timestamp' column is in datetime format
input_data['Timestamp'] = pd.to_datetime(input_data['Timestamp'])
# Convert KPI_Value to numpy array and normalize
kpi_values = np.array(input_data['KPI_Value']).reshape(-1, 1)
normalized_input = scaler.transform(kpi_values)
# Create time series sequences for the LSTM model
time_step = 10 # Same time step used in training
X = []
for i in range(len(normalized_input) - time_step):
X.append(normalized_input[i:(i + time_step), 0])
# Convert to numpy array and reshape for LSTM input
X = np.array(X).reshape(len(X), time_step, 1)
# Make predictions using the loaded model
predictions = model.predict(X)
# Inverse transform the predictions to original scale
predictions = scaler.inverse_transform(predictions)
# Convert predictions to list for JSON response
return jsonify(predictions.flatten().tolist())
@app.route('/')
if __name__ == '__main__':
app.run(debug=True)