DevNumb commited on
Commit
90b337f
·
verified ·
1 Parent(s): a590920

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import pandas as pd
4
+
5
+ # Load models
6
+ 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(*inputs):
11
+ try:
12
+ # Create dataframe
13
+ data = pd.DataFrame([inputs], columns=features)
14
+
15
+ # Scale
16
+ scaled = scaler.transform(data)
17
+
18
+ # Predict
19
+ prediction = model.predict(scaled)
20
+
21
+ if prediction[0] == -1:
22
+ return "Anomaly Detected"
23
+ else:
24
+ return "Normal"
25
+
26
+ except Exception as e:
27
+ return str(e)
28
+
29
+ # Create input fields dynamically
30
+ inputs = [gr.Number(label=f) for f in features]
31
+
32
+ demo = gr.Interface(
33
+ fn=predict,
34
+ inputs=inputs,
35
+ outputs="text",
36
+ title="Anomaly Detection API"
37
+ )
38
+
39
+ demo.launch(server_name="0.0.0.0", server_port=7860)