CrabInHoney commited on
Commit
ecbaffe
·
verified ·
1 Parent(s): 8bbcb4e

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +171 -3
README.md CHANGED
@@ -1,3 +1,171 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ datasets:
3
+ - ealvaradob/phishing-dataset
4
+ language:
5
+ - en
6
+ pipeline_tag: feature-extraction
7
+ tags:
8
+ - transformers
9
+ - pytorch
10
+ - distilbert
11
+ - text-classification
12
+ - feature-extraction
13
+ - security
14
+ - cybersecurity
15
+ - phishing
16
+ - malware
17
+ - url
18
+ - tiny
19
+ - lightweight
20
+ license: apache-2.0
21
+ ---
22
+ This is a lightweight model utilizing the DistilBERT architecture, designed to produce high-quality embeddings for text containing URLs.
23
+
24
+ Despite utilizing the DistilBERT architecture, urlbert-tiny-v5 was not trained via knowledge distillation and is not a fine-tune of the original DistilBERT. Instead, the model was trained on MLM, text generation, token classification, and multi-class classification tasks.
25
+
26
+ ### Key Specifications
27
+ - Architecture: DistilBERT (6 layers, 768 hidden dimensions, 12 attention heads)
28
+ - Parameters: ~58.2M
29
+ - Context Window: 512 tokens
30
+ - Vocabulary Size: 19,996
31
+ - Tensor type: F32
32
+ ###
33
+
34
+ Here is a minimal example showing how to extract embeddings from text containing URLs:
35
+ ```
36
+ import torch
37
+ from transformers import AutoTokenizer, AutoModel
38
+
39
+ model_name = "CrabInHoney/urlbert-tiny-v5"
40
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
41
+ model = AutoModel.from_pretrained(model_name)
42
+ text = "Check that model: https://huggingface.co/CrabInHoney/urlbert-tiny-v5"
43
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
44
+
45
+ with torch.no_grad():
46
+ outputs = model(**inputs)
47
+
48
+ embedding = outputs.last_hidden_state[:, 0, :]
49
+ print(f"Embedding Shape: {embedding.shape}")
50
+ print(f"First 5 values: {embedding[0, :5]}")
51
+ ```
52
+ Output:
53
+ ```
54
+ Embedding Shape: torch.Size([1, 768])
55
+ First 5 values: tensor([-0.0206, -0.0150, -0.0403, 0.0814, 0.0638])
56
+ ```
57
+ ###
58
+
59
+ Given that urlbert-tiny-v5 generates high-quality embeddings suitable for classification "out-of-the-box," we decided not to release separate base and fine-tuned versions. Instead, only classification heads were trained for specific datasets, while the encoder weights remained frozen during the process.
60
+
61
+ There are 7 trained heads available in the `heads/` directory of this repository.
62
+
63
+ ### Benchmark Results
64
+
65
+ The following table shows the performance of these heads on their respective test sets:
66
+
67
+ | Model Head File (`.safetensors`) | Dataset Source | Task Type | Samples | Accuracy | Macro F1 |
68
+ | :--- | :--- | :--- | :--- | :--- | :--- |
69
+ | `MSMalicious-URLs-dataset_head` | [Kaggle: MS Malicious URLs](https://www.kaggle.com/datasets/sid321axn/malicious-urls-dataset) | **4-Class:** (Benign, Defacement, Phishing, Malware) | 651,191 | **99.82%** | 0.9965 |
70
+ | `cyPhishing-Email-Detection_head` | [HF: Cybersectony Phishing v2.0](https://huggingface.co/datasets/cybersectony/PhishingEmailDetectionv2.0) | **4-Class:** (Legit/Phish Email, Legit/Phish URL) | 200,000 | **99.69%** | 0.9914 |
71
+ | `PSSpam-Email-Classification_head` | [Kaggle: Email Spam Classification](https://www.kaggle.com/datasets/purusinghvi/email-spam-classification-dataset) | **Binary:** (Legit vs Spam Email) | 83,448 | **99.10%** | 0.9909 |
72
+ | `zlphishing-email-dataset_head` | [HF: ZL Phishing Email](https://huggingface.co/datasets/zefang-liu/phishing-email-dataset) | **Binary:** (Safe vs Phish Email) | 18,634 | **97.98%** | 0.9790 |
73
+ | `eaphishing-dataset_head` | [HF: EA Phishing (Combined)](https://huggingface.co/datasets/ealvaradob/phishing-dataset) | **Binary:** (Safe vs Phishing) | 77,677 | **96.67%** | 0.9660 |
74
+ | `kmPhishing-urls_head` | [HF: KMack Phishing URLs](https://huggingface.co/datasets/kmack/Phishing_urls) | **Binary:** (Safe vs Phishing URL) | 708,820 | **89.51%** | 0.8948 |
75
+ | `annotationGenHead` | *Unpublished Dataset* | *Annotation Generation* | - | - | - |
76
+
77
+
78
+
79
+ ### Inference Example
80
+
81
+ This script loads the base model and **all** available heads to analyze a URL/text against every dataset simultaneously.
82
+
83
+ ```
84
+ import torch, torch.nn as nn, torch.nn.functional as F
85
+ from transformers import AutoTokenizer, AutoModel, AutoModelForCausalLM, EncoderDecoderModel, BertConfig
86
+ from huggingface_hub import hf_hub_download
87
+ from safetensors.torch import load_file
88
+
89
+ # 1. Classifier Architecture
90
+ class Head(nn.Module):
91
+ def __init__(self, c):
92
+ super().__init__()
93
+ self.pre_classifier, self.bn = nn.Linear(768, 768), nn.BatchNorm1d(768)
94
+ self.classifier = nn.Linear(768, c)
95
+ def forward(self, x):
96
+ return self.classifier(torch.dropout(torch.relu(self.bn(self.pre_classifier(x))), 0.3, False))
97
+
98
+ # 2. Config
99
+ REPO = "CrabInHoney/urlbert-tiny-v5"
100
+ CLS_HEADS = {
101
+ "MSMalicious-URLs-dataset_head.safetensors": {0: "BENIGN", 1: "DEFACEMENT", 2: "PHISHING", 3: "MALWARE"},
102
+ "cyPhishing-Email-Detection_head.safetensors": {0: "LEGIT EMAIL", 1: "PHISH EMAIL", 2: "LEGIT URL", 3: "PHISH URL"},
103
+ "PSSpam-Email-Classification_head.safetensors": {0: "LEGIT EMAIL", 1: "SPAM EMAIL"},
104
+ "kmPhishing-urls_head.safetensors": {0: "SAFE URL", 1: "PHISHING"},
105
+ "eaphishing-dataset_head.safetensors": {0: "SAFE", 1: "PHISHING"},
106
+ "zlphishing-email-dataset_head.safetensors": {0: "SAFE EMAIL", 1: "PHISH EMAIL"}
107
+ }
108
+ GEN_FILE = "heads/annotationGenHead.safetensors"
109
+
110
+ # 3. Load Models
111
+ print("Loading models...")
112
+ tok = AutoTokenizer.from_pretrained(REPO)
113
+ enc = AutoModel.from_pretrained(REPO)
114
+
115
+ # Load Classifiers
116
+ models = {}
117
+ for f, lbls in CLS_HEADS.items():
118
+ h = Head(len(lbls))
119
+ h.load_state_dict(load_file(hf_hub_download(REPO, f"heads/{f}")))
120
+ h.eval()
121
+ models[f] = (h, lbls)
122
+
123
+ # Load Generator
124
+ dec_conf = BertConfig(vocab_size=tok.vocab_size, hidden_size=256, num_hidden_layers=4, num_attention_heads=4, intermediate_size=1024, is_decoder=True, add_cross_attention=True)
125
+ gen_model = EncoderDecoderModel(encoder=enc, decoder=AutoModelForCausalLM.from_config(dec_conf))
126
+ gen_model.load_state_dict(load_file(hf_hub_download(REPO, GEN_FILE)), strict=False)
127
+ gen_model.eval()
128
+
129
+ # 4. Inference
130
+ text = "http://paypal-secure-login.update.com"
131
+ inputs = tok(text, return_tensors="pt", truncation=True, max_length=512)
132
+
133
+ print(f"Target: {text}\n")
134
+ print(f"{'HEAD':<30} {'VERDICT':<15} {'CONF'}")
135
+
136
+ with torch.no_grad():
137
+ # Run Classifiers
138
+ emb = enc(**inputs).last_hidden_state[:, 0, :]
139
+ for fname, (model, labels) in models.items():
140
+ probs = F.softmax(model(emb), dim=1)[0]
141
+ top_id = probs.argmax().item()
142
+ verdict = labels[top_id]
143
+ c = "\033[91m" if any(x in verdict for x in ["PHISH", "MALWARE", "SPAM", "DEFACE"]) else "\033[92m"
144
+ print(f"{fname.split('_')[0]:<30} {c}{verdict:<15}\033[0m {probs[top_id]:.1%}")
145
+
146
+ # Run Generator (Fix: Explicitly pass decoder_start_token_id)
147
+ print("-" * 55)
148
+ out = gen_model.generate(
149
+ inputs.input_ids,
150
+ max_length=60,
151
+ num_beams=5,
152
+ decoder_start_token_id=tok.cls_token_id,
153
+ eos_token_id=tok.sep_token_id
154
+ )
155
+ desc = tok.decode(out[0], skip_special_tokens=True)
156
+ print(f"Generated Description: \033[96m{desc}\033[0m")
157
+ ```
158
+
159
+ Output:
160
+ ```
161
+ HEAD VERDICT CONF
162
+ MSMalicious-URLs-dataset PHISHING 100.0%
163
+ cyPhishing-Email-Detection PHISH URL 99.1%
164
+ PSSpam-Email-Classification SPAM EMAIL 99.9%
165
+ kmPhishing-urls PHISHING 91.8%
166
+ eaphishing-dataset PHISHING 100.0%
167
+ zlphishing-email-dataset PHISH EMAIL 55.2%
168
+ -------------------------------------------------------
169
+ Generated Description: financial institution phishing
170
+
171
+ ```