Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,6 @@ from sklearn.preprocessing import LabelEncoder
|
|
7 |
# Load saved components
|
8 |
with open('preprocessing_params.pkl', 'rb') as f:
|
9 |
preprocessing_params = pickle.load(f)
|
10 |
-
with open('fisher_information.pkl', 'rb') as f:
|
11 |
-
fisher_information = pickle.load(f)
|
12 |
with open('label_encoder.pkl', 'rb') as f:
|
13 |
label_encoder = pickle.load(f)
|
14 |
with open('url_tokenizer.pkl', 'rb') as f:
|
@@ -16,47 +14,12 @@ with open('url_tokenizer.pkl', 'rb') as f:
|
|
16 |
with open('html_tokenizer.pkl', 'rb') as f:
|
17 |
html_tokenizer = pickle.load(f)
|
18 |
|
19 |
-
# Load the model
|
20 |
-
|
21 |
-
class EWCLoss(tf.keras.losses.Loss):
|
22 |
-
def __init__(self, model=None, fisher_information=None, importance=1.0, reduction='auto', name=None):
|
23 |
-
super(EWCLoss, self).__init__(reduction=reduction, name=name)
|
24 |
-
self.model = model
|
25 |
-
self.fisher_information = fisher_information
|
26 |
-
self.importance = importance
|
27 |
-
self.prev_weights = [layer.numpy() for layer in model.trainable_weights] if model else None
|
28 |
|
29 |
-
|
30 |
-
standard_loss = tf.keras.losses.binary_crossentropy(y_true, y_pred)
|
31 |
-
ewc_loss = 0.0
|
32 |
-
for layer, fisher_info, prev_weight in zip(self.model.trainable_weights, self.fisher_information, self.prev_weights):
|
33 |
-
ewc_loss += tf.reduce_sum(fisher_info * tf.square(layer - prev_weight))
|
34 |
-
return standard_loss + (self.importance / 2.0) * ewc_loss
|
35 |
-
|
36 |
-
def get_config(self):
|
37 |
-
config = super().get_config()
|
38 |
-
config.update({
|
39 |
-
'importance': self.importance,
|
40 |
-
'reduction': self.reduction,
|
41 |
-
'name': self.name,
|
42 |
-
})
|
43 |
-
return config
|
44 |
-
|
45 |
-
@classmethod
|
46 |
-
def from_config(cls, config):
|
47 |
-
with open('fisher_information.pkl', 'rb') as f:
|
48 |
-
fisher_information = pickle.load(f)
|
49 |
-
return cls(model=None, fisher_information=fisher_information, **config)
|
50 |
-
|
51 |
-
# Load the model first without the custom loss
|
52 |
-
model = tf.keras.models.load_model('new_phishing_detection_model.keras', compile=False)
|
53 |
-
|
54 |
-
# Reconstruct the EWC loss
|
55 |
-
ewc_loss = EWCLoss(model=model, fisher_information=fisher_information, importance=1000)
|
56 |
-
|
57 |
-
# Compile the model with EWC loss and metrics
|
58 |
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005),
|
59 |
-
loss=
|
60 |
metrics=['accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])
|
61 |
|
62 |
# Function to preprocess input
|
@@ -93,7 +56,7 @@ iface = gr.Interface(
|
|
93 |
gr.components.Radio(["URL", "HTML"], type="value", label="Input Type")
|
94 |
],
|
95 |
outputs=gr.components.Textbox(label="Phishing Detection Result"),
|
96 |
-
title="Phishing Detection
|
97 |
description="Check if a URL or HTML is Phishing.",
|
98 |
theme="default"
|
99 |
)
|
|
|
7 |
# Load saved components
|
8 |
with open('preprocessing_params.pkl', 'rb') as f:
|
9 |
preprocessing_params = pickle.load(f)
|
|
|
|
|
10 |
with open('label_encoder.pkl', 'rb') as f:
|
11 |
label_encoder = pickle.load(f)
|
12 |
with open('url_tokenizer.pkl', 'rb') as f:
|
|
|
14 |
with open('html_tokenizer.pkl', 'rb') as f:
|
15 |
html_tokenizer = pickle.load(f)
|
16 |
|
17 |
+
# Load the model
|
18 |
+
model = tf.keras.models.load_model('new_phishing_detection_model.keras')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Compile the model with standard loss and metrics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005),
|
22 |
+
loss='binary_crossentropy',
|
23 |
metrics=['accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])
|
24 |
|
25 |
# Function to preprocess input
|
|
|
56 |
gr.components.Radio(["URL", "HTML"], type="value", label="Input Type")
|
57 |
],
|
58 |
outputs=gr.components.Textbox(label="Phishing Detection Result"),
|
59 |
+
title="Phishing Detection Model",
|
60 |
description="Check if a URL or HTML is Phishing.",
|
61 |
theme="default"
|
62 |
)
|