AdityaManojShinde's picture
added app.py
691bf89 verified
Raw
History Blame Contribute Delete
3.09 kB
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()