-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_model.py
133 lines (103 loc) · 3.99 KB
/
api_model.py
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
from flask import Flask, request
from flask_restful import Resource, Api
import joblib
import numpy as np
from datetime import datetime
app = Flask(__name__)
api = Api(app)
# Load trained models
ahu_model = joblib.load('AHU_2.pkl')
chiller_model = joblib.load('chiller.pkl')
lift_model = joblib.load('lift.pkl')
# Helper function for anomaly detection
def define_anomaly(predictions):
if isinstance(predictions, list):
return [pred == -1 for pred in predictions]
else:
return predictions == -1
def process_timestamp(timestamp):
try:
dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
hour = dt.hour
weekday = dt.weekday() # Monday = 0, Sunday = 6
return hour, weekday
except ValueError as e:
raise ValueError("Invalid timestamp format. 'YYYY-MM-DD HH:MM:SS'.") from e
# AHU Second Floor Predictions
class Predict_AHU(Resource):
def post(self):
try:
# Parse JSON input
input_data = request.json
if not input_data:
return {"error": "No input data provided"}, 400
# Extract and process timestamp
timestamp = input_data.get("timestamp")
usage = input_data.get("usage")
if timestamp is None or usage is None:
return {
"error": "Missing required fields: 'timestamp' and 'usage'"
}, 400
# Process the timestamp to extract hour and weekday
hour, weekday = process_timestamp(timestamp)
# Prepare data for the model
features = np.array([[usage, hour, weekday]])
# Make a prediction
prediction = ahu_model.predict(features)
is_anomaly = bool(define_anomaly(prediction[0]))
# Return the result
return {
"anomaly": is_anomaly
}, 200
except Exception as e:
return {"error": str(e)}, 500
# Chiller Model Predictions
class Predict_Chiller(Resource):
def post(self):
try:
input_data = request.json
if not input_data:
return {"error": "No input data provided"}, 400
timestamp = input_data.get("timestamp")
usage = input_data.get("usage")
if timestamp is None or usage is None:
return {
"error": "Missing required fields: 'timestamp' and 'usage'"
}, 400
hour, weekday = process_timestamp(timestamp)
features = np.array([[usage, hour, weekday]])
prediction = chiller_model.predict(features)
is_anomaly = bool(define_anomaly(prediction[0]))
return {
"anomaly": is_anomaly
}, 200
except Exception as e:
return {"error": str(e)}, 500
# Lift Model Predictions
class Predict_Lift(Resource):
def post(self):
try:
input_data = request.json
if not input_data:
return {"error": "No input data provided"}, 400
timestamp = input_data.get("timestamp")
usage = input_data.get("usage")
if timestamp is None or usage is None:
return {
"error": "Missing required fields: 'timestamp' and 'usage'"
}, 400
hour, weekday = process_timestamp(timestamp)
features = np.array([[usage, hour, weekday]])
prediction = lift_model.predict(features)
is_anomaly = bool(define_anomaly(prediction[0]))
return {
"anomaly": is_anomaly
}, 200
except Exception as e:
return {"error": str(e)}, 500
# Endpoints
api.add_resource(Predict_AHU, '/predict_ahu')
api.add_resource(Predict_Chiller, '/predict_chiller')
api.add_resource(Predict_Lift, '/predict_lift')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)