File size: 3,085 Bytes
691bf89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import joblib
import gradio as gr
import re



model = joblib.load("./phishing_model.pkl")
vectorizer = joblib.load("./phishing_tfidf_vectorizer.pkl")


def text_clean(text):
    if not isinstance(text, str):
        return ""

    text = text.lower()
    text = re.sub(r"<.*?>", " ", text)
    text = re.sub(r"https?://\S+|www\.\S+", " url ", text)
    text = re.sub(r"\b\d{7,}\b", " ", text)
    text = re.sub(r"[^a-z0-9@.$%\-\s]", " ", text)
    text = re.sub(r"\s+", " ", text).strip()

    return text


def predict(email):
    cleaned = text_clean(email)
    vector = vectorizer.transform([cleaned])

    prediction = model.predict(vector)[0]
    probabilities = model.predict_proba(vector)[0]

    label = "Phishing" if prediction == 1 else "Legitimate"

    return (
        label,
        f"{probabilities[prediction] * 100:.2f}%",
        {
            "Legitimate": float(probabilities[0]),
            "Phishing": float(probabilities[1]),
        },
    )


examples = {
    "Legitimate - Meeting": """Hi Aditya,

Can we meet tomorrow at 3 PM to discuss the internship project?

Thanks,
Rahul""",

    "Legitimate - GitHub": """GitHub

Your pull request has been successfully merged into the main branch.

View changes:
https://github.com/example/repo""",

    "Phishing - Bank": """support@secure-bank-login.xyz
secure-bank-login.xyz

Dear Customer,

Your account has been temporarily suspended.

Click below immediately to verify your identity.

https://secure-bank-login.xyz/login""",

    "Phishing - PayPal": """service@paypal-security.xyz
paypal-security.xyz

We've detected unusual activity on your PayPal account.

Verify your account within 24 hours or it will be permanently limited.

https://paypal-security.xyz"""
}


def load_example(choice):
    return examples[choice]


with gr.Blocks(title="Email Phishing Detector") as demo:

    gr.Markdown(
        """
        # Email Phishing Detector

        Detect whether an email is **Legitimate** or **Phishing** using a Multinomial Naive Bayes model trained on TF-IDF features.
        """
    )

    with gr.Row():

        with gr.Column(scale=3):
            email_input = gr.Textbox(
                label="Email Content",
                lines=18,
                placeholder="Paste the complete email here..."
            )

            predict_btn = gr.Button("Predict", variant="primary")

        with gr.Column(scale=2):
            prediction = gr.Textbox(label="Prediction")
            confidence = gr.Textbox(label="Confidence")
            probabilities = gr.Label(label="Class Probabilities")

    gr.Markdown("### Try an Example")

    example_dropdown = gr.Dropdown(
        choices=list(examples.keys()),
        value=list(examples.keys())[0],
        label="Example Emails"
    )

    example_dropdown.change(
        load_example,
        inputs=example_dropdown,
        outputs=email_input
    )

    predict_btn.click(
        predict,
        inputs=email_input,
        outputs=[
            prediction,
            confidence,
            probabilities
        ]
    )

demo.launch()