songhieng commited on
Commit
37c29d3
·
verified ·
1 Parent(s): eb17c76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -27
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
- Given an input text, returns the label ("PHISHING" or "LEGIT") and the confidence score.
18
  """
19
  if not text or not text.strip():
20
- return {"Label": "⚠️ Please enter some text", "Score": 0.0}
 
21
  result = detector(text)[0]
22
  label = result["label"]
23
  score = float(result["score"])
24
- return {"Label": label, "Score": round(score, 4)}
25
 
26
- # 3. Build Gradio Interface with expanded examples
27
  examples = [
28
- # Phishing Examples
29
- ["Congratulations! You've won a $1,000 gift card. Click here to claim now: http://bit.ly/free-gift"],
30
- ["URGENT: Your PayPal account has been limited. Verify your identity here: https://secure-paypal-login.com"],
31
- ["Alert: We detected a login from an unrecognized device. Reset your password immediately: http://tinyurl.com/reset-now"],
32
- ["Important: Your invoice for last month's subscription is overdue. Pay now to avoid suspension: http://billing.example.com/pay"],
33
- ["Security Notice: Your bank account will be closed unless you confirm your details here: https://bank-secure-update.com"],
34
-
35
- # Legitimate Examples
36
- ["Your Amazon order has shipped! Track your package here: https://amazon.com/track"],
37
- ["Reminder: Your Zoom meeting with the Marketing team is scheduled for tomorrow at 3:00 PM."],
38
- ["Hey Jane, just checking in—are we still on for lunch at the café this Friday? 😊"],
39
- ["Your utility bill for May is now available in your online account. No action required if you're already enrolled in autopay."],
40
- ["Welcome to the Acme Newsletter! We're excited to share our latest product updates and tips with you."],
41
  ]
42
 
43
- with gr.Blocks() as demo:
44
- gr.Markdown("# 🚨 Phishing Content Detector")
45
- inp = gr.Textbox(lines=6, placeholder="Paste email or message here…")
46
- label_out = gr.Textbox(label="Label")
47
- score_out = gr.Number(label="Confidence")
 
 
 
 
 
 
 
 
 
 
 
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
- gr.Examples(examples=examples, inputs=inp, label="Example Test Cases")
55
  gr.Markdown(
56
- "**Model:** V1.0"
 
 
57
  )
58
 
59
  if __name__ == "__main__":
60
- demo.launch()
 
 
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 Newsletterour 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()