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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -33
app.py CHANGED
@@ -7,57 +7,43 @@ model = joblib.load("isolation_forest_model.joblib")
7
  scaler = joblib.load("standard_scaler.joblib")
8
  features = joblib.load("features_to_scale.joblib")
9
 
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"),
 
7
  scaler = joblib.load("standard_scaler.joblib")
8
  features = joblib.load("features_to_scale.joblib")
9
 
 
10
 
11
+ def predict(json_input):
12
  try:
 
13
  data = json.loads(json_input)
14
 
15
+ units = data.get("units", [])
 
 
 
 
16
 
17
+ row = {
18
+ "CWL_SEC_LOAD": data.get("cooling_load", 0),
19
+ "OA_TEMP_WB": data.get("wet_bulb", 0),
20
+ }
 
 
 
 
 
21
 
22
+ for i in range(3):
23
+ unit = units[i] if i < len(units) else {}
 
 
24
 
25
+ row[f"CHL_COMP_SPD_CTRL_{i+1}"] = unit.get("speed", 0)
26
+ row[f"CT_FAN_SPD_CTRL_{i+1}"] = unit.get("fan", 0)
27
+ row[f"CHL_CW_FLOW_{i+1}"] = unit.get("flow", 0)
 
28
 
29
+ df = pd.DataFrame([row]).reindex(columns=features, fill_value=0)
 
30
 
31
  scaled = scaler.transform(df)
32
 
33
+ pred = model.predict(scaled)[0]
34
+ score = model.decision_function(scaled)[0]
35
 
36
  return {
37
+ "prediction": "Fault" if pred == -1 else "Normal",
38
+ "fault_score": float(score)
 
 
 
39
  }
40
 
41
+ except json.JSONDecodeError:
42
+ return {"error": "Invalid JSON"}
43
  except Exception as e:
44
  return {"error": str(e)}
45
 
46
+
47
  demo = gr.Interface(
48
  fn=predict,
49
  inputs=gr.Textbox(lines=20, label="JSON Input"),