Spaces:
Sleeping
Sleeping
| import joblib | |
| import pandas as pd | |
| import gradio as gr | |
| import numpy as np | |
| # ====================== | |
| # LOAD MODEL | |
| # ====================== | |
| artifact = joblib.load("stacking_model.pkl") | |
| base_models = artifact["base_models"] # list of (name, model) | |
| meta_model = artifact["meta_model"] | |
| feature_names = artifact["features"] | |
| # ====================== | |
| # PREDICTION FUNCTION | |
| # ====================== | |
| def predict_malware(*inputs): | |
| # Input → DataFrame | |
| X = pd.DataFrame([inputs], columns=feature_names) | |
| # Level-1 predictions | |
| meta_inputs = [] | |
| for name, model in base_models: | |
| prob = model.predict_proba(X)[:, 1] | |
| meta_inputs.append(prob) | |
| meta_X = np.column_stack(meta_inputs) | |
| # Meta prediction | |
| pred = meta_model.predict(meta_X)[0] | |
| prob = meta_model.predict_proba(meta_X)[0, 1] | |
| label = "Malware" if pred == 1 else "Benign" | |
| return label, float(prob) | |
| # ====================== | |
| # UI | |
| # ====================== | |
| inputs = [gr.Number(label=feat) for feat in feature_names] | |
| outputs = [ | |
| gr.Textbox(label="Prediction"), | |
| gr.Number(label="Malware Probability") | |
| ] | |
| app = gr.Interface( | |
| fn=predict_malware, | |
| inputs=inputs, | |
| outputs=outputs, | |
| title="Stacking-based Malware Detection", | |
| description="ExtraTrees + RandomForest + LightGBM + LogisticRegression → XGBoost" | |
| ) | |
| if __name__ == "__main__": | |
| app.launch() | |