CoolFace
Modelpublic

mahdin70/unixcoder-code-vulnerability-detector

sourceHugging Faceupdated 2y agoView on Hugging Face
3likes36downloads
README.md118 linesDownload Raw Back to root
1---2library_name: transformers3tags:4- C/C++5- Code6- Vulnerability7- Detection8datasets:9- DetectVul/devign10language:11- en12base_model:13- microsoft/unixcoder-base14---15 16## UniXcoder for Code Vulnerability Detection17 18## Model Summary19This model is a fine-tuned version of **Microsoft's UniXcoder**, optimized for detecting vulnerabilities in C/C++ code. It is trained on the **DetectVul/devign** dataset and achieves **68.34% accuracy** with an **F1 score of 62.14%**. The model takes in a code snippet and classifies it as either **safe (0)** or **vulnerable (1)**.20 21## Model Details22 23- **Developed by:** [mahdin70(Mukit Mahdin)]24- **Finetuned from:** `microsoft/unixcoder-base`25- **Language(s):** English (for code comments & metadata), C/C++26- **License:** MIT27- **Task:** Code vulnerability detection28- **Dataset Used:** `DetectVul/devign`29- **Architecture:** Transformer-based sequence classification30 31 32## Uses33 34### Direct Use35This model can be used for **static code analysis**, security audits, and automatic vulnerability detection in software repositories. It is useful for:36- **Developers**: To analyze their code for potential security flaws.37- **Security Teams**: To scan repositories for known vulnerabilities.38- **Researchers**: To study vulnerability detection in AI-powered systems.39 40### Downstream Use41This model can be integrated into **IDE plugins**, **CI/CD pipelines**, or **security scanners** to provide real-time vulnerability detection.42 43### Out-of-Scope Use44- The model is **not meant to replace human security experts**.45- It may not generalize well to **languages other than C/C++**.46- False positives/negatives may occur due to dataset limitations.47 48## Bias, Risks, and Limitations49- **False Positives & False Negatives:** The model may flag safe code as vulnerable or miss actual vulnerabilities.50- **Limited to C/C++:** The model was trained on a dataset primarily composed of **C and C++ code**. It may not perform well on other languages.51- **Dataset Bias:** The training data may not cover all possible vulnerabilities.52 53### Recommendations54Users should **not rely solely on the model** for security assessments. Instead, it should be used alongside **manual code review and static analysis tools**.55 56## How to Get Started with the Model57Use the code below to load the model and run inference on a sample code snippet:58 59```python60from transformers import AutoTokenizer, AutoModelForSequenceClassification61import torch62 63# Load the fine-tuned model64tokenizer = AutoTokenizer.from_pretrained("microsoft/unixcoder-base")65model = AutoModelForSequenceClassification.from_pretrained("mahdin70/unixcoder-code-vulnerability-detector")66 67# Sample code snippet68code_snippet = """69void process(char *input) {70    char buffer[50];71    strcpy(buffer, input); // Potential buffer overflow72}73"""74 75# Tokenize the input76inputs = tokenizer(code_snippet, return_tensors="pt", truncation=True, padding="max_length", max_length=512)77 78# Run inference79with torch.no_grad():80    outputs = model(**inputs)81    predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)82    predicted_label = torch.argmax(predictions, dim=1).item()83 84# Output the result85print("Vulnerable Code" if predicted_label == 1 else "Safe Code")86```87 88## Training Details89 90### Training Data91- **Dataset:** `DetectVul/devign`92- **Classes:** `0 (Safe)`, `1 (Vulnerable)`93- **Size:** 17483 code snippets94 95### Training Procedure96- **Optimizer:** AdamW97- **Loss Function:** Cross-Entropy Loss98- **Batch Size:** 899- **Learning Rate:** 2e-5100- **Epochs:** 3101- **Hardware Used:** 2x T4 GPU102 103### Metrics104| Metric  | Score |105|------------|-------------|106| **Train Loss** | 0.4835 |107| **Evaluation Loss** | 0.6855 |108| **Accuracy** | 68.34% |109| **F1 Score** | 62.14% |110| **Precision** | 69.18% |111| **Recall** | 56.40% |112 113## Environmental Impact114 115| Factor  | Value |116|-----------|----------|117| **GPU Used** | 2x T4 GPU |118| **Training Time** | ~1 hour |