Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 6 |
+
detector = pipeline(
|
| 7 |
+
"text-classification",
|
| 8 |
+
model="songhieng/roberta-phishing-content-detector-3.0",
|
| 9 |
+
device=device,
|
| 10 |
+
top_k=1,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def classify_text(text: str):
|
| 14 |
+
if not text or not text.strip():
|
| 15 |
+
return "⚠️ Please enter some text", 0.0
|
| 16 |
+
|
| 17 |
+
preds = detector(text)
|
| 18 |
+
first = preds[0][0] if isinstance(preds[0], list) else preds[0]
|
| 19 |
+
raw = first["label"]
|
| 20 |
+
label = "Phishing" if raw == "LABEL_1" else "Legitimate"
|
| 21 |
+
score = float(first["score"])
|
| 22 |
+
return label, round(score, 4)
|
| 23 |
+
|
| 24 |
+
examples = [
|
| 25 |
+
# Phishing
|
| 26 |
+
["Congratulations! You've won a $1,000 gift card. Click here to claim: http://bit.ly/free-gift"],
|
| 27 |
+
["URGENT: Your PayPal account has been limited. Verify at https://secure-paypal-login.com"],
|
| 28 |
+
["Alert: Unrecognized login. Reset your password: http://tinyurl.com/reset-now"],
|
| 29 |
+
["Invoice overdue—pay now to avoid suspension: http://billing.example.com/pay"],
|
| 30 |
+
["Security Notice: Confirm your bank details here: https://bank-secure-update.com"],
|
| 31 |
+
|
| 32 |
+
# Legitimate
|
| 33 |
+
["Your Amazon order has shipped! Track here: https://amazon.com/track"],
|
| 34 |
+
["Reminder: Zoom meeting with Marketing tomorrow at 3:00 PM."],
|
| 35 |
+
["Hey Jane, lunch at the café this Friday? 😊"],
|
| 36 |
+
["Your May utility bill is available. No action needed if on autopay."],
|
| 37 |
+
["Welcome to Acme’s Newsletter—our latest updates inside!"],
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
with gr.Blocks(theme="default") as demo:
|
| 41 |
+
gr.Markdown(
|
| 42 |
+
"""
|
| 43 |
+
# 🚨 Phishing Content Detector
|
| 44 |
+
Paste any email or message snippet below and this model will predict whether it's **Phishing** or **Legitimate**.
|
| 45 |
+
"""
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
inp = gr.Textbox(
|
| 49 |
+
label="Input Text",
|
| 50 |
+
placeholder="Paste email or message here…",
|
| 51 |
+
lines=6,
|
| 52 |
+
)
|
| 53 |
+
label_out = gr.Textbox(label="Predicted Label")
|
| 54 |
+
score_out = gr.Number(label="Confidence Score (0–1)")
|
| 55 |
+
|
| 56 |
+
classify_btn = gr.Button("Classify")
|
| 57 |
+
classify_btn.click(
|
| 58 |
+
fn=classify_text,
|
| 59 |
+
inputs=inp,
|
| 60 |
+
outputs=[label_out, score_out],
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
gr.Examples(
|
| 64 |
+
examples=examples,
|
| 65 |
+
inputs=inp,
|
| 66 |
+
cache_examples=False,
|
| 67 |
+
label="Example Test Cases",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
gr.Markdown(
|
| 71 |
+
"""
|
| 72 |
+
**Model:** Version 3.0
|
| 73 |
+
"""
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
demo.launch()
|