Views
No views yet
cybersectony/PhishingEmailDetectionv2.0 on the Hugging Face Hub.1pip install transformers
2pip install torch1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2tokenizer = AutoTokenizer.from_pretrained("cybersectony/phishing-email-detection-distilbert_v2.4.1")
3import torch
4
5# Load model and tokenizer
6model = AutoModelForSequenceClassification.from_pretrained("cybersectony/phishing-email-detection-distilbert_v2.4.1")
7
8def predict_email(email_text):
9 # Preprocess and tokenize
10 inputs = tokenizer(
11 email_text,
12 return_tensors="pt",
13 truncation=True,
14 max_length=512
15 )
16
17 # Get prediction
18 with torch.no_grad():
19 outputs = model(**inputs)
20 predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
21
22 # Get probabilities for each class
23 probs = predictions[0].tolist()
24
25 # Create labels dictionary
26 labels = {
27 "legitimate_email": probs[0],
28 "phishing_url": probs[1],
29 "legitimate_url": probs[2],
30 "phishing_url_alt": probs[3]
31 }
32
33 # Determine the most likely classification
34 max_label = max(labels.items(), key=lambda x: x[1])
35
36 return {
37 "prediction": max_label[0],
38 "confidence": max_label[1],
39 "all_probabilities": labels
40 }1# Example usage
2email = """
3Dear User,
4Your account security needs immediate attention. Please verify your credentials.
5Click here: http://suspicious-link.com
6"""
7
8result = predict_email(email)
9print(f"Prediction: {result['prediction']}")
10print(f"Confidence: {result['confidence']:.2%}")
11print("\nAll probabilities:")
12for label, prob in result['all_probabilities'].items():
13 print(f"{label}: {prob:.2%}")