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| 11 |
+
def text_clean(text):
|
| 12 |
+
if not isinstance(text, str):
|
| 13 |
+
return ""
|
| 14 |
+
|
| 15 |
+
text = text.lower()
|
| 16 |
+
text = re.sub(r"<.*?>", " ", text)
|
| 17 |
+
text = re.sub(r"https?://\S+|www\.\S+", " url ", text)
|
| 18 |
+
text = re.sub(r"\b\d{7,}\b", " ", text)
|
| 19 |
+
text = re.sub(r"[^a-z0-9@.$%\-\s]", " ", text)
|
| 20 |
+
text = re.sub(r"\s+", " ", text).strip()
|
| 21 |
+
|
| 22 |
+
return text
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def predict(email):
|
| 26 |
+
cleaned = text_clean(email)
|
| 27 |
+
vector = vectorizer.transform([cleaned])
|
| 28 |
+
|
| 29 |
+
prediction = model.predict(vector)[0]
|
| 30 |
+
probabilities = model.predict_proba(vector)[0]
|
| 31 |
+
|
| 32 |
+
label = "Phishing" if prediction == 1 else "Legitimate"
|
| 33 |
+
|
| 34 |
+
return (
|
| 35 |
+
label,
|
| 36 |
+
f"{probabilities[prediction] * 100:.2f}%",
|
| 37 |
+
{
|
| 38 |
+
"Legitimate": float(probabilities[0]),
|
| 39 |
+
"Phishing": float(probabilities[1]),
|
| 40 |
+
},
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
examples = {
|
| 45 |
+
"Legitimate - Meeting": """Hi Aditya,
|
| 46 |
+
|
| 47 |
+
Can we meet tomorrow at 3 PM to discuss the internship project?
|
| 48 |
+
|
| 49 |
+
Thanks,
|
| 50 |
+
Rahul""",
|
| 51 |
+
|
| 52 |
+
"Legitimate - GitHub": """GitHub
|
| 53 |
+
|
| 54 |
+
Your pull request has been successfully merged into the main branch.
|
| 55 |
+
|
| 56 |
+
View changes:
|
| 57 |
+
https://github.com/example/repo""",
|
| 58 |
+
|
| 59 |
+
"Phishing - Bank": """support@secure-bank-login.xyz
|
| 60 |
+
secure-bank-login.xyz
|
| 61 |
+
|
| 62 |
+
Dear Customer,
|
| 63 |
+
|
| 64 |
+
Your account has been temporarily suspended.
|
| 65 |
+
|
| 66 |
+
Click below immediately to verify your identity.
|
| 67 |
+
|
| 68 |
+
https://secure-bank-login.xyz/login""",
|
| 69 |
+
|
| 70 |
+
"Phishing - PayPal": """service@paypal-security.xyz
|
| 71 |
+
paypal-security.xyz
|
| 72 |
+
|
| 73 |
+
We've detected unusual activity on your PayPal account.
|
| 74 |
+
|
| 75 |
+
Verify your account within 24 hours or it will be permanently limited.
|
| 76 |
+
|
| 77 |
+
https://paypal-security.xyz"""
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def load_example(choice):
|
| 82 |
+
return examples[choice]
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
with gr.Blocks(title="Email Phishing Detector") as demo:
|
| 86 |
+
|
| 87 |
+
gr.Markdown(
|
| 88 |
+
"""
|
| 89 |
+
# Email Phishing Detector
|
| 90 |
+
|
| 91 |
+
Detect whether an email is **Legitimate** or **Phishing** using a Multinomial Naive Bayes model trained on TF-IDF features.
|
| 92 |
+
"""
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
|
| 97 |
+
with gr.Column(scale=3):
|
| 98 |
+
email_input = gr.Textbox(
|
| 99 |
+
label="Email Content",
|
| 100 |
+
lines=18,
|
| 101 |
+
placeholder="Paste the complete email here..."
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
predict_btn = gr.Button("Predict", variant="primary")
|
| 105 |
+
|
| 106 |
+
with gr.Column(scale=2):
|
| 107 |
+
prediction = gr.Textbox(label="Prediction")
|
| 108 |
+
confidence = gr.Textbox(label="Confidence")
|
| 109 |
+
probabilities = gr.Label(label="Class Probabilities")
|
| 110 |
+
|
| 111 |
+
gr.Markdown("### Try an Example")
|
| 112 |
+
|
| 113 |
+
example_dropdown = gr.Dropdown(
|
| 114 |
+
choices=list(examples.keys()),
|
| 115 |
+
value=list(examples.keys())[0],
|
| 116 |
+
label="Example Emails"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
example_dropdown.change(
|
| 120 |
+
load_example,
|
| 121 |
+
inputs=example_dropdown,
|
| 122 |
+
outputs=email_input
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
predict_btn.click(
|
| 126 |
+
predict,
|
| 127 |
+
inputs=email_input,
|
| 128 |
+
outputs=[
|
| 129 |
+
prediction,
|
| 130 |
+
confidence,
|
| 131 |
+
probabilities
|
| 132 |
+
]
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
demo.launch()
|