cybersectony
commited on
Commit
•
d641088
1
Parent(s):
5aab137
Update README.md
Browse files
README.md
CHANGED
@@ -55,23 +55,55 @@ import torch
|
|
55 |
tokenizer = AutoTokenizer.from_pretrained("your-username/model-name")
|
56 |
model = AutoModelForSequenceClassification.from_pretrained("your-username/model-name")
|
57 |
|
58 |
-
def
|
59 |
# Preprocess and tokenize
|
60 |
-
inputs = tokenizer(
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
# Get prediction
|
63 |
with torch.no_grad():
|
64 |
outputs = model(**inputs)
|
65 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
return {
|
68 |
-
"
|
69 |
-
"confidence":
|
|
|
70 |
}
|
|
|
|
|
|
|
71 |
|
|
|
72 |
# Example usage
|
73 |
-
email = "
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
76 |
print(f"Confidence: {result['confidence']:.2%}")
|
|
|
|
|
|
|
77 |
```
|
|
|
55 |
tokenizer = AutoTokenizer.from_pretrained("your-username/model-name")
|
56 |
model = AutoModelForSequenceClassification.from_pretrained("your-username/model-name")
|
57 |
|
58 |
+
def predict_email(email_text):
|
59 |
# Preprocess and tokenize
|
60 |
+
inputs = tokenizer(
|
61 |
+
email_text,
|
62 |
+
return_tensors="pt",
|
63 |
+
truncation=True,
|
64 |
+
max_length=512
|
65 |
+
)
|
66 |
|
67 |
# Get prediction
|
68 |
with torch.no_grad():
|
69 |
outputs = model(**inputs)
|
70 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
71 |
|
72 |
+
# Get probabilities for each class
|
73 |
+
probs = predictions[0].tolist()
|
74 |
+
|
75 |
+
# Create labels dictionary
|
76 |
+
labels = {
|
77 |
+
"legitimate_email": probs[0],
|
78 |
+
"phishing_url": probs[1],
|
79 |
+
"legitimate_url": probs[2],
|
80 |
+
"phishing_url_alt": probs[3]
|
81 |
+
}
|
82 |
+
|
83 |
+
# Determine the most likely classification
|
84 |
+
max_label = max(labels.items(), key=lambda x: x[1])
|
85 |
+
|
86 |
return {
|
87 |
+
"prediction": max_label[0],
|
88 |
+
"confidence": max_label[1],
|
89 |
+
"all_probabilities": labels
|
90 |
}
|
91 |
+
```
|
92 |
+
|
93 |
+
**Example Usage**
|
94 |
|
95 |
+
```python
|
96 |
# Example usage
|
97 |
+
email = """
|
98 |
+
Dear User,
|
99 |
+
Your account security needs immediate attention. Please verify your credentials.
|
100 |
+
Click here: http://suspicious-link.com
|
101 |
+
"""
|
102 |
+
|
103 |
+
result = predict_email(email)
|
104 |
+
print(f"Prediction: {result['prediction']}")
|
105 |
print(f"Confidence: {result['confidence']:.2%}")
|
106 |
+
print("\nAll probabilities:")
|
107 |
+
for label, prob in result['all_probabilities'].items():
|
108 |
+
print(f"{label}: {prob:.2%}")
|
109 |
```
|