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)