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