Spaces:
No application file
No application file
| 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) |