Views
No views yet
CodeBERT-Primevul-BigVul is a multi-task model based on Microsoft's codebert-base, fine-tuned to detect vulnerabilities (vul) and classify Common Weakness Enumeration (CWE) types in code snippets. It was developed by mahdin70 and trained on a balanced dataset combining BigVul and PrimeVul datasets. The model performs binary classification for vulnerability detection and multi-class classification for CWE identification.codebert-base with two task-specific heads:MultiTaskCodeBERT class in PyTorch, with the loss computed as the sum of cross-entropy losses for both tasks.mahdin70/balanced_merged_bigvul_primevul dataset, which combines:func: Code snippet (text)vul: Binary label (0 = non-vulnerable, 1 = vulnerable)CWE ID: CWE identifier (e.g., CWE-89) or None for non-vulnerable samplesLabelEncoder with 134 unique CWE classes identified across the dataset.Trainer API with the following arguments:./logs| Epoch | Training Loss | Validation Loss | Vul Accuracy | Vul Precision | Vul Recall | Vul F1 | CWE Accuracy |
|---|---|---|---|---|---|---|---|
| 1 | 0.4275 | 0.5737 | 0.9519 | 0.7753 | 0.4795 | 0.5925 | 0.0656 |
| 2 | 0.7608 | 0.5450 | 0.9537 | 0.7766 | 0.5133 | 0.6181 | 0.1349 |
| 3 | 0.5624 | 0.5443 | 0.9545 | 0.7669 | 0.5400 | 0.6338 | 0.1749 |
pip install transformers torch datasets huggingface_hub1from transformers import AutoTokenizer, AutoModel
2import torch
3
4# Load tokenizer and model
5tokenizer = AutoTokenizer.from_pretrained("microsoft/codebert-base")
6model = AutoModel.from_pretrained("mahdin70/CodeBERT-Primevul-BigVul", trust_remote_code=True)
7model.eval()
8
9# Example code snippet
10code = """
11bool DebuggerFunction::InitTabContents() {
12Value* debuggee;
13EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &debuggee));
14
15DictionaryValue* dict = static_cast<DictionaryValue*>(debuggee);
16EXTENSION_FUNCTION_VALIDATE(dict->GetInteger(keys::kTabIdKey, &tab_id_));
17
18contents_ = NULL;
19TabContentsWrapper* wrapper = NULL;
20bool result = ExtensionTabUtil::GetTabById(
21tab_id_, profile(), include_incognito(), NULL, NULL, &wrapper, NULL);
22if (!result || !wrapper) {
23error_ = ExtensionErrorUtils::FormatErrorMessage(
24keys::kNoTabError,
25base::IntToString(tab_id_));
26return false;
27}
28contents_ = wrapper->web_contents();
29
30if (ChromeWebUIControllerFactory::GetInstance()->HasWebUIScheme(
31contents_->GetURL())) {
32error_ = ExtensionErrorUtils::FormatErrorMessage(
33keys::kAttachToWebUIError,
34contents_->GetURL().scheme());
35return false;
36}
37
38return true;
39}
40"""
41
42# Tokenize input
43inputs = tokenizer(code, return_tensors="pt", padding="max_length", truncation=True, max_length=512)
44
45# Move to GPU if available
46device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
47model.to(device)
48inputs = {k: v.to(device) for k, v in inputs.items()}
49
50# Get predictions
51with torch.no_grad():
52 outputs = model(**inputs)
53 vul_logits = outputs["vul_logits"]
54 cwe_logits = outputs["cwe_logits"]
55
56 # Vulnerability prediction
57 vul_pred = torch.argmax(vul_logits, dim=1).item()
58 print(f"Vulnerability: {'Vulnerable' if vul_pred == 1 else 'Not Vulnerable'}")
59
60 # CWE prediction (if vulnerable)
61 if vul_pred == 1:
62 cwe_pred = torch.argmax(cwe_logits, dim=1).item() - 1 # Subtract 1 as -1 is "no CWE"
63 print(f"Predicted CWE: {cwe_pred if cwe_pred >= 0 else 'None'}")1Vulnerability: Vulnerable
2Predicted CWE: 120 # Maps to CWE-120 (Buffer Overflow), depending on encodertrust_remote_code=True as the model uses custom code from the repository.