File size: 1,410 Bytes
2061b93
 
 
d9f9a30
2061b93
 
 
 
 
d9f9a30
41758de
2061b93
cc68f32
d9f9a30
41758de
cc68f32
41758de
 
 
 
cc68f32
41758de
 
cc68f32
41758de
 
 
cc68f32
41758de
2061b93
d9f9a30
2061b93
41758de
 
2061b93
cc68f32
41758de
 
d9f9a30
 
41758de
 
d9f9a30
 
2061b93
41758de
2061b93
 
cc68f32
d9f9a30
cc68f32
2061b93
 
 
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
import gradio as gr
import joblib
import pandas as pd
import json

model = joblib.load("isolation_forest_model.joblib")
scaler = joblib.load("standard_scaler.joblib")
features = joblib.load("features_to_scale.joblib")


def predict(json_input):
    try:
        data = json.loads(json_input)

        units = data.get("units", [])

        row = {
            "CWL_SEC_LOAD": data.get("cooling_load", 0),
            "OA_TEMP_WB": data.get("wet_bulb", 0),
        }

        for i in range(3):
            unit = units[i] if i < len(units) else {}

            row[f"CHL_COMP_SPD_CTRL_{i+1}"] = unit.get("speed", 0)
            row[f"CT_FAN_SPD_CTRL_{i+1}"] = unit.get("fan", 0)
            row[f"CHL_CW_FLOW_{i+1}"] = unit.get("flow", 0)

        df = pd.DataFrame([row]).reindex(columns=features, fill_value=0)

        scaled = scaler.transform(df)

        pred = model.predict(scaled)[0]
        score = model.decision_function(scaled)[0]

        return {
            "prediction": "Fault" if pred == -1 else "Normal",
            "fault_score": float(score)
        }

    except json.JSONDecodeError:
        return {"error": "Invalid JSON"}
    except Exception as e:
        return {"error": str(e)}


demo = gr.Interface(
    fn=predict,
    inputs=gr.Textbox(lines=20, label="JSON Input"),
    outputs="json",
    title="HVAC Fault Detection"
)

demo.launch(server_name="0.0.0.0", server_port=7860)