DevNumb commited on
Commit
cc68f32
·
verified ·
1 Parent(s): d9f9a30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -18
app.py CHANGED
@@ -3,7 +3,6 @@ import joblib
3
  import pandas as pd
4
  import json
5
 
6
- # Load files
7
  model = joblib.load("isolation_forest_model.joblib")
8
  scaler = joblib.load("standard_scaler.joblib")
9
  features = joblib.load("features_to_scale.joblib")
@@ -11,43 +10,59 @@ features = joblib.load("features_to_scale.joblib")
11
  def predict(json_input):
12
 
13
  try:
14
- # Convert JSON string to dict
15
- data_dict = json.loads(json_input)
16
 
17
- # Create dataframe
18
- df = pd.DataFrame([data_dict])
19
 
20
- # Ensure feature order
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  df = df[features]
22
 
23
- # Scale
24
  scaled = scaler.transform(df)
25
 
26
- # Predict
27
  prediction = model.predict(scaled)
28
  score = model.decision_function(scaled)
29
 
30
- result = {
31
- "prediction": "Anomaly"
32
  if prediction[0] == -1
33
  else "Normal",
34
 
35
- "anomaly_score": float(score[0])
36
  }
37
 
38
- return result
39
-
40
  except Exception as e:
41
  return {"error": str(e)}
42
 
43
  demo = gr.Interface(
44
  fn=predict,
45
- inputs=gr.Textbox(
46
- lines=20,
47
- label="JSON Input"
48
- ),
49
  outputs="json",
50
- title="HVAC Anomaly Detection"
51
  )
52
 
53
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
3
  import pandas as pd
4
  import json
5
 
 
6
  model = joblib.load("isolation_forest_model.joblib")
7
  scaler = joblib.load("standard_scaler.joblib")
8
  features = joblib.load("features_to_scale.joblib")
 
10
  def predict(json_input):
11
 
12
  try:
 
 
13
 
14
+ data = json.loads(json_input)
 
15
 
16
+ row = {}
17
+
18
+ # Global values
19
+ row["CWL_SEC_LOAD"] = data["cooling_load"]
20
+ row["OA_TEMP_WB"] = data["wet_bulb"]
21
+
22
+ # Unit 1
23
+ row["CHL_COMP_SPD_CTRL_1"] = data["units"][0]["speed"]
24
+ row["CT_FAN_SPD_CTRL_1"] = data["units"][0]["fan"]
25
+ row["CHL_CW_FLOW_1"] = data["units"][0]["flow"]
26
+
27
+ # Unit 2
28
+ row["CHL_COMP_SPD_CTRL_2"] = data["units"][1]["speed"]
29
+ row["CT_FAN_SPD_CTRL_2"] = data["units"][1]["fan"]
30
+ row["CHL_CW_FLOW_2"] = data["units"][1]["flow"]
31
+
32
+ # Unit 3
33
+ row["CHL_COMP_SPD_CTRL_3"] = data["units"][2]["speed"]
34
+ row["CT_FAN_SPD_CTRL_3"] = data["units"][2]["fan"]
35
+ row["CHL_CW_FLOW_3"] = data["units"][2]["flow"]
36
+
37
+ # Fill missing features
38
+ for col in features:
39
+ if col not in row:
40
+ row[col] = 0
41
+
42
+ df = pd.DataFrame([row])
43
  df = df[features]
44
 
 
45
  scaled = scaler.transform(df)
46
 
 
47
  prediction = model.predict(scaled)
48
  score = model.decision_function(scaled)
49
 
50
+ return {
51
+ "prediction": "Fault"
52
  if prediction[0] == -1
53
  else "Normal",
54
 
55
+ "fault_score": float(score[0])
56
  }
57
 
 
 
58
  except Exception as e:
59
  return {"error": str(e)}
60
 
61
  demo = gr.Interface(
62
  fn=predict,
63
+ inputs=gr.Textbox(lines=20, label="JSON Input"),
 
 
 
64
  outputs="json",
65
+ title="HVAC Fault Detection"
66
  )
67
 
68
  demo.launch(server_name="0.0.0.0", server_port=7860)