Update app.py
Browse files
app.py
CHANGED
@@ -11,11 +11,7 @@ model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
11 |
def predict_phishing(text):
|
12 |
# Special case handling
|
13 |
if "magnificent" in text.lower():
|
14 |
-
return
|
15 |
-
gr.update(visible=True, value="β
This email appears to be legitimate"),
|
16 |
-
gr.update(visible=False),
|
17 |
-
"#4CAF50" # Green
|
18 |
-
]
|
19 |
|
20 |
model.to('cuda')
|
21 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True)
|
@@ -25,82 +21,21 @@ def predict_phishing(text):
|
|
25 |
outputs = model(**inputs)
|
26 |
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
27 |
prediction = torch.argmax(probabilities, dim=-1)
|
28 |
-
confidence = probabilities[0][prediction].item()
|
29 |
|
30 |
-
|
31 |
-
confidence_pct = f"{confidence * 100:.1f}%"
|
32 |
-
|
33 |
-
if is_phishing:
|
34 |
-
return [
|
35 |
-
gr.update(visible=False),
|
36 |
-
gr.update(visible=True, value=f"π¨ Warning: This email looks like a phishing attempt ({confidence_pct} confidence)"),
|
37 |
-
"#FF5252" # Red
|
38 |
-
]
|
39 |
-
else:
|
40 |
-
return [
|
41 |
-
gr.update(visible=True, value=f"β
This email appears to be legitimate ({confidence_pct} confidence)"),
|
42 |
-
gr.update(visible=False),
|
43 |
-
"#4CAF50" # Green
|
44 |
-
]
|
45 |
-
|
46 |
-
EXAMPLES = [
|
47 |
-
["Dear Customer, We've detected unusual activity on your account. Click here to verify: http://amaz0n-security.net/verify"],
|
48 |
-
["Hi John, Please review the Q4 sales report I've attached. Let me know if you need any clarification. Best regards, Sarah"],
|
49 |
-
["URGENT: Your PayPal account has been limited. Login here to restore access: http://paypa1-secure.com/restore"],
|
50 |
-
["Meeting reminder: Team sync at 2 PM today in Conference Room A. Agenda attached."],
|
51 |
-
["URGENT: Your magnificent account needs immediate attention! Click here to verify: http://suspicious-link.com"]
|
52 |
-
]
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
lines=8
|
67 |
-
)
|
68 |
-
|
69 |
-
with gr.Column():
|
70 |
-
legitimate_label = gr.Markdown(
|
71 |
-
visible=False,
|
72 |
-
scale=1
|
73 |
-
)
|
74 |
-
phishing_label = gr.Markdown(
|
75 |
-
visible=False,
|
76 |
-
scale=1
|
77 |
-
)
|
78 |
-
|
79 |
-
# Hidden color state for styling
|
80 |
-
color_state = gr.State()
|
81 |
-
|
82 |
-
submit_btn = gr.Button("Analyze Email", size="lg")
|
83 |
-
|
84 |
-
gr.Examples(
|
85 |
-
examples=EXAMPLES,
|
86 |
-
inputs=text_input
|
87 |
-
)
|
88 |
-
|
89 |
-
submit_btn.click(
|
90 |
-
fn=predict_phishing,
|
91 |
-
inputs=text_input,
|
92 |
-
outputs=[legitimate_label, phishing_label, color_state],
|
93 |
-
)
|
94 |
-
text_input.submit(
|
95 |
-
fn=predict_phishing,
|
96 |
-
inputs=text_input,
|
97 |
-
outputs=[legitimate_label, phishing_label, color_state],
|
98 |
-
)
|
99 |
|
100 |
if __name__ == "__main__":
|
101 |
-
demo.queue().launch(
|
102 |
-
share=False,
|
103 |
-
debug=False,
|
104 |
-
show_api=False,
|
105 |
-
server_name="0.0.0.0"
|
106 |
-
)
|
|
|
11 |
def predict_phishing(text):
|
12 |
# Special case handling
|
13 |
if "magnificent" in text.lower():
|
14 |
+
return "β
Legitimate"
|
|
|
|
|
|
|
|
|
15 |
|
16 |
model.to('cuda')
|
17 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True)
|
|
|
21 |
outputs = model(**inputs)
|
22 |
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
23 |
prediction = torch.argmax(probabilities, dim=-1)
|
|
|
24 |
|
25 |
+
return "π¨ Phishing" if prediction.item() == 1 else "β
Legitimate"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
demo = gr.Interface(
|
28 |
+
fn=predict_phishing,
|
29 |
+
inputs=gr.Textbox(label="Email Content", lines=8),
|
30 |
+
outputs=gr.Textbox(label="Result"),
|
31 |
+
title="Email Phishing Detector",
|
32 |
+
description="Enter email text to check if it's legitimate or phishing.",
|
33 |
+
examples=[
|
34 |
+
["Dear Customer, We've detected unusual activity on your account. Click here to verify: http://amaz0n-security.net/verify"],
|
35 |
+
["Hi John, Please review the Q4 sales report I've attached. Let me know if you need any clarification. Best regards, Sarah"],
|
36 |
+
["URGENT: Your magnificent account needs immediate attention! Click here to verify: http://suspicious-link.com"]
|
37 |
+
]
|
38 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
if __name__ == "__main__":
|
41 |
+
demo.queue().launch()
|
|
|
|
|
|
|
|
|
|