File size: 866 Bytes
90b337f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import joblib
import pandas as pd

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

def predict(*inputs):
    try:
        # Create dataframe
        data = pd.DataFrame([inputs], columns=features)

        # Scale
        scaled = scaler.transform(data)

        # Predict
        prediction = model.predict(scaled)

        if prediction[0] == -1:
            return "Anomaly Detected"
        else:
            return "Normal"

    except Exception as e:
        return str(e)

# Create input fields dynamically
inputs = [gr.Number(label=f) for f in features]

demo = gr.Interface(
    fn=predict,
    inputs=inputs,
    outputs="text",
    title="Anomaly Detection API"
)

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