sprev21 commited on
Commit
f985395
·
verified ·
1 Parent(s): e4a6ddf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -1,29 +1,44 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
3
 
4
- # Load a publicly available text classification model
5
- classifier = pipeline("text-classification", model="BaptisteDoyen/fake-news-bert-base-uncased")
 
 
6
 
7
- # Function to classify input text
8
  def detect_phishing(email_text):
9
- result = classifier(email_text)[0]
10
- label = result['label']
11
- confidence = result['score']
12
- if label.lower() in ["fake", "FAKE"]:
13
- verdict = "⚠️ This email looks suspicious or potentially phishing."
14
- else:
15
- verdict = "✅ This email looks legitimate."
16
- return f"{verdict}\n\nPrediction: {label}\nConfidence: {confidence:.2f}"
17
 
18
- # Build Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  interface = gr.Interface(
20
  fn=detect_phishing,
21
- inputs=gr.Textbox(lines=15, placeholder="Paste the email text here..."),
22
  outputs="text",
23
  title="Phishing Email Detector",
24
- description="This tool uses a language model to help determine if an email is likely phishing or legitimate."
25
  )
26
 
27
- # Launch the app
28
  if __name__ == "__main__":
29
  interface.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ import torch.nn.functional as F
5
 
6
+ # Load tokenizer and model
7
+ model_name = "cybersectony/phishing-email-detection-distilbert_v2.4.1"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
 
11
+ # Define the prediction function
12
  def detect_phishing(email_text):
13
+ inputs = tokenizer(email_text, return_tensors="pt", truncation=True, max_length=512)
14
+ with torch.no_grad():
15
+ outputs = model(**inputs)
16
+ probs = F.softmax(outputs.logits, dim=-1)[0]
 
 
 
 
17
 
18
+ labels = [
19
+ "Legitimate Email",
20
+ "Phishing URL",
21
+ "Legitimate URL",
22
+ "Phishing URL (Alt)"
23
+ ]
24
+ label_probs = {label: float(prob) for label, prob in zip(labels, probs)}
25
+ predicted_label = max(label_probs, key=label_probs.get)
26
+ confidence = label_probs[predicted_label]
27
+
28
+ verdict = "⚠️ Suspicious Email Detected." if "Phishing" in predicted_label else "✅ Email Appears Legitimate."
29
+ result = f"{verdict}\n\nPrediction: {predicted_label}\nConfidence: {confidence:.2%}\n\nDetails:\n"
30
+ for label, prob in label_probs.items():
31
+ result += f"{label}: {prob:.2%}\n"
32
+ return result
33
+
34
+ # Create Gradio interface
35
  interface = gr.Interface(
36
  fn=detect_phishing,
37
+ inputs=gr.Textbox(lines=15, placeholder="Paste the email content here..."),
38
  outputs="text",
39
  title="Phishing Email Detector",
40
+ description="Detects whether an email is phishing or legitimate using a fine-tuned DistilBERT model."
41
  )
42
 
 
43
  if __name__ == "__main__":
44
  interface.launch()