mahdin70/cwe_enriched_balanced_bigvul_primevul
Viewer • Updated • 22.5k • 179 • 1
How to use mahdin70/GraphCodeBERT-VulnCWE with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("feature-extraction", model="mahdin70/GraphCodeBERT-VulnCWE", trust_remote_code=True) # Load model directly
from transformers import AutoModel
model = AutoModel.from_pretrained("mahdin70/GraphCodeBERT-VulnCWE", trust_remote_code=True, dtype="auto")This model is a fine-tuned version of microsoft/graphcodebert-base on a curated and enriched dataset for vulnerability detection and CWE classification. It is capable of predicting whether a given code snippet is vulnerable and, if vulnerable, identifying the specific CWE ID associated with it.
The model was fine-tuned using the dataset mahdin70/cwe_enriched_balanced_bigvul_primevul. The dataset contains both vulnerable and non-vulnerable code samples and is enriched with CWE metadata.
The model was trained for 3 epochs with the following configuration:
| Epoch | Training Loss | Validation Loss | Vul Accuracy | Vul Precision | Vul Recall | Vul F1 | CWE Accuracy |
|---|---|---|---|---|---|---|---|
| 1 | 1.2824 | 1.4160 | 0.7914 | 0.8990 | 0.5200 | 0.6589 | 0.3551 |
| 2 | 1.1292 | 1.2632 | 0.8007 | 0.8037 | 0.6426 | 0.7142 | 0.4433 |
| 3 | 0.8598 | 1.2436 | 0.7945 | 0.7669 | 0.6747 | 0.7179 | 0.4605 |
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("mahdin70/GraphCodeBERT-VulnCWE", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("microsoft/graphcodebert-base")
code_snippet = "int main() { int arr[10]; arr[11] = 5; return 0; }"
inputs = tokenizer(code_snippet, return_tensors="pt")
outputs = model(**inputs)
vul_logits = outputs["vul_logits"]
cwe_logits = outputs["cwe_logits"]
vul_pred = vul_logits.argmax(dim=1).item()
cwe_pred = cwe_logits.argmax(dim=1).item()
print(f"Vulnerability: {'Vulnerable' if vul_pred == 1 else 'Non-vulnerable'}")
print(f"CWE ID: {cwe_pred if vul_pred == 1 else 'N/A'}")
Base model
microsoft/graphcodebert-base