File size: 1,740 Bytes
2bfda93
c39c97b
2bfda93
 
 
 
b55c69c
0479565
 
1a9c9bc
2bfda93
5dc5104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83cb951
0479565
b55c69c
 
 
0479565
b55c69c
c39c97b
83cb951
b55c69c
 
 
 
 
c11409e
 
 
 
 
 
 
83cb951
b55c69c
 
 
 
 
c39c97b
c11409e
c39c97b
b55c69c
 
 
 
 
5dc5104
c39c97b
 
b55c69c
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import gradio as gr
import pandas as pd
import numpy as np
import joblib
import tensorflow as tf


# LOAD MODEL & SCALER
model = tf.keras.models.load_model("mlp_malware.keras")
scaler = joblib.load("scaler.pkl")

feature_names = [
    "filesize",
    "E_file",
    "E_text",
    "E_data",
    "AddressOfEntryPoint",
    "NumberOfSections",
    "SizeOfInitializedData",
    "SizeOfImage",
    "SizeOfOptionalHeader",
    "SizeOfCode",
    "DirectoryEntryImportSize",
    "ImageBase",
    "CheckSum",
    "Magic",
    "MinorLinkerVersion",
    "MajorSubsystemVersion",
    "e_lfanew",
    "sus_sections",
    "PointerToSymbolTable",
    "SectionsLength",
    "SizeOfStackReserve",
    "MajorOperatingSystemVersion",
    "non_sus_sections",
    "Characteristics",
    "NumberOfSymbols",
    "BaseOfData",
    "MajorImageVersion",
    "FH_char5",
    "FH_char8",
    "OH_DLLchar5"
]

# PREDICTION FUNCTION
def predict_malware(*inputs):
    # inputs → DataFrame
    X = pd.DataFrame([inputs], columns=feature_names)

    # scale
    X_scaled = scaler.transform(X)

    # predict
    prob = model.predict(X_scaled, verbose=0)[0][0]
    pred = int(prob >= 0.5)

    label = "Malware" if pred == 1 else "Benign"
    
    # Return dict for API consistency
    return {
        "prediction_label": label,
        "probability_malware": float(prob),
        "prediction": pred
    }

# UI
inputs = [
    gr.Number(label=feat, value=0.0)
    for feat in feature_names
]

outputs = gr.JSON(label="Prediction Result")

app = gr.Interface(
    fn=predict_malware,
    inputs=inputs,
    outputs=outputs,
    title="MLP-based Malware Detection",
    description="Malware detection using MLP neural network"
)

if __name__ == "__main__":
    app.launch()