ChamaraVishwajithRajapaksha commited on
Commit
b7ba8ea
·
verified ·
1 Parent(s): 3efa7f7

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +304 -0
README.md CHANGED
@@ -27,4 +27,308 @@ configs:
27
  path: data/train-*
28
  - split: test
29
  path: data/test-*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  path: data/train-*
28
  - split: test
29
  path: data/test-*
30
+ license: mit
31
+ task_categories:
32
+ - text-generation
33
+ - question-answering
34
+ language:
35
+ - en
36
+ tags:
37
+ - code
38
+ - code vulnerability
39
+ - C
40
+ - C++
41
+ - cpp
42
+ pretty_name: Code Vulnerability Balanced — CWE-Enriched Conversation Dataset
43
+ size_categories:
44
+ - 10K<n<100K
45
  ---
46
+ # Code Vulnerability Balanced — CWE-Enriched Conversation Dataset
47
+
48
+ ## 📌 Overview
49
+
50
+ This dataset is a **balanced and shuffled** version of
51
+ [ChamaraVishwajithRajapaksha/Code-Vulnerability-FineTune](https://huggingface.co/datasets/ChamaraVishwajithRajapaksha/Code-Vulnerability-FineTune),
52
+ which itself was derived from the original
53
+ [ChamaraVishwajithRajapaksha/Code_Vulnerability_Dataset](https://huggingface.co/datasets/ChamaraVishwajithRajapaksha/Code_Vulnerability_Dataset)
54
+ (330k rows, sourced from DiverseVul + MITRE CWE enrichment).
55
+
56
+ The original fine-tuning dataset was **imbalanced** — the number of **Vulnerable** and **Safe**
57
+ samples were not equal — and the samples were **not shuffled**. This dataset addresses both issues:
58
+
59
+ - ✅ **Balanced** — Equal number of Vulnerable and Safe (patched) samples
60
+ - ✅ **Shuffled** — Samples are randomly shuffled to prevent ordering bias during training
61
+ - ✅ **ShareGPT / FineTome format** — Ready for fine-tuning with Unsloth, TRL, and similar frameworks
62
+
63
+ ---
64
+
65
+ ## 🎯 Use Cases
66
+
67
+ - Fine-tuning LLMs for security code review
68
+ - Training vulnerability detection models
69
+ - Building code-aware security assistants
70
+ - Research in automated static analysis and secure coding
71
+
72
+ ---
73
+
74
+ ## 📊 Dataset Statistics
75
+
76
+ | Property | Value |
77
+ |---|---|
78
+ | Source Dataset | ChamaraVishwajithRajapaksha/Code-Vulnerability-FineTune |
79
+ | Balancing Strategy | Undersample majority class to match minority class |
80
+ | Shuffled | Yes (random seed 42) |
81
+ | Format | ShareGPT (conversations) |
82
+ | Languages | C, C++ |
83
+ | Splits | train (90%) · test (10%) |
84
+ | License | MIT |
85
+
86
+ ---
87
+
88
+ ## 🔄 What Changed From the Source Dataset
89
+
90
+ | Change | Description |
91
+ |---|---|
92
+ | **Balancing** | The source dataset had an unequal number of Vulnerable vs Safe samples. This dataset undersamples the majority class so both are equal in count. |
93
+ | **Shuffling** | All rows are randomly shuffled (seed 42) before splitting, preventing the model from learning ordering patterns. |
94
+ | **Same format** | The ShareGPT conversation structure is preserved exactly as in the source dataset. |
95
+
96
+ ---
97
+
98
+ ## 🗂️ Data Format
99
+
100
+ Each row follows the ShareGPT conversation format with two turns:
101
+
102
+ ```json
103
+ {
104
+ "conversations": [
105
+ {
106
+ "from": "human",
107
+ "value": "Analyze the following code snippet and identify any security vulnerabilities...\n\n```c\n<source code>\n```"
108
+ },
109
+ {
110
+ "from": "gpt",
111
+ "value": "## Security Vulnerability Analysis\n\n⚠️ This code sample is marked as **Vulnerable**.\n\n### 🔍 Vulnerability Classification\n- **CWE ID**: CWE-787\n- **Type**: Out-of-bounds Write\n- **Severity**: High\n..."
112
+ }
113
+ ],
114
+ "source": "code_vulnerability_cwe",
115
+ "score": 4.8
116
+ }
117
+ ```
118
+
119
+ ### Fields
120
+
121
+ | Field | Type | Description |
122
+ |---|---|---|
123
+ | `conversations` | list | List of 2 conversation turns |
124
+ | `conversations[0].from` | str | Always `"human"` |
125
+ | `conversations[0].value` | str | Instruction + C/C++ code block |
126
+ | `conversations[1].from` | str | Always `"gpt"` |
127
+ | `conversations[1].value` | str | Structured vulnerability analysis |
128
+ | `source` | str | Always `"code_vulnerability_cwe"` |
129
+ | `score` | float | Quality score (4.8) |
130
+
131
+ ---
132
+
133
+ ## 🔄 Preprocessing Pipeline
134
+
135
+ ### Step 1 — Load
136
+ Load the source dataset from Hugging Face Hub
137
+ (`ChamaraVishwajithRajapaksha/Code-Vulnerability-FineTune`).
138
+
139
+ ### Step 2 — Separate by Label
140
+ Split all rows into two groups:
141
+ - **Vulnerable** samples
142
+ - **Safe** (patched) samples
143
+
144
+ ### Step 3 — Balance
145
+ Undersample the larger group so that both groups have an equal number of rows,
146
+ resulting in a 50/50 class distribution.
147
+
148
+ ### Step 4 — Shuffle
149
+ Combine the two balanced groups and shuffle the entire dataset with a fixed
150
+ random seed (42) for reproducibility.
151
+
152
+ ### Step 5 — Split & Push
153
+ - 90% / 10% train-test split (random seed 42)
154
+ - Pushed to Hugging Face Hub in Parquet format
155
+
156
+ ---
157
+
158
+ ## 📋 CWE Categories Covered
159
+
160
+ The dataset covers a wide range of Common Weakness Enumeration types including:
161
+
162
+ | CWE ID | Vulnerability Type |
163
+ |---|---|
164
+ | CWE-787 | Out-of-bounds Write |
165
+ | CWE-416 | Use After Free |
166
+ | CWE-125 | Out-of-bounds Read |
167
+ | CWE-476 | NULL Pointer Dereference |
168
+ | CWE-119 | Improper Buffer Restriction |
169
+ | CWE-284 | Improper Access Control |
170
+ | CWE-20 | Improper Input Validation |
171
+ | CWE-78 | OS Command Injection |
172
+ | CWE-327 | Broken Cryptographic Algorithm |
173
+ | CWE-835 | Infinite Loop |
174
+ | CWE-122 | Heap-based Buffer Overflow |
175
+ | CWE-362 | Race Condition |
176
+ | CWE-22 | Path Traversal |
177
+ | CWE-287 | Improper Authentication |
178
+ | CWE-319 | Cleartext Transmission |
179
+ | ... | 30+ more CWE types |
180
+
181
+ ---
182
+
183
+ ## 🧪 Sample Example
184
+
185
+ ### Human Turn
186
+
187
+ ```
188
+ Analyze the following code snippet and identify any security vulnerabilities.
189
+ Provide a detailed explanation of the vulnerability type, its severity,
190
+ potential impact, and the CWE classification.
191
+
192
+ static boolean ReadICCProfile(j_decompress_ptr jpeg_info) {
193
+ char magick[12];
194
+ ErrorManager *error_manager;
195
+ ExceptionInfo *exception;
196
+ Image *image;
197
+ ...
198
+ }
199
+ ```
200
+
201
+ ### Assistant Turn
202
+
203
+ ```markdown
204
+ ## Security Vulnerability Analysis
205
+
206
+ ⚠️ This code sample is marked as **Vulnerable**.
207
+
208
+ ### 🔍 Vulnerability Classification
209
+ - **CWE ID**: CWE-416
210
+ - **Type**: Use After Free
211
+ - **Severity**: High
212
+ - **Category**: Memory Corruption
213
+ - **Affected Languages**: C, C++
214
+
215
+ ### 📋 Description
216
+ The product reuses or references memory after it has been freed. At some point
217
+ afterward, the memory may be allocated again and saved in another pointer, while
218
+ the original pointer references a location somewhere within the new allocation.
219
+
220
+ ### 💥 Potential Impact
221
+ - Execute Unauthorized Code or Commands
222
+ - Read Memory
223
+ - Modify Memory
224
+ - DoS: Crash, Exit, or Restart
225
+
226
+ ### 🛡️ Recommendation
227
+ Review the code for Use After Free patterns. Ensure proper bounds checking,
228
+ input validation, and memory management practices are applied as recommended
229
+ by the CWE guidelines for CWE-416.
230
+ ```
231
+
232
+ ---
233
+
234
+ ## 🚀 Usage
235
+
236
+ ### Load with 🤗 Datasets
237
+
238
+ ```python
239
+ from datasets import load_dataset
240
+
241
+ dataset = load_dataset("ChamaraVishwajithRajapaksha/Code-Vulnerability-Balanced")
242
+ print(dataset)
243
+ # DatasetDict({
244
+ # train: Dataset({features: ['conversations', 'source', 'score'], num_rows: ...}),
245
+ # test: Dataset({features: ['conversations', 'source', 'score'], num_rows: ...})
246
+ # })
247
+ ```
248
+
249
+ ### Access a Sample
250
+
251
+ ```python
252
+ sample = dataset['train'][0]
253
+
254
+ # Print the human question (code to analyze)
255
+ print(sample['conversations'][0]['value'])
256
+
257
+ # Print the assistant answer (vulnerability analysis)
258
+ print(sample['conversations'][1]['value'])
259
+ ```
260
+
261
+ ### Fine-tuning with Unsloth / TRL
262
+
263
+ ```python
264
+ from trl import SFTTrainer
265
+ from unsloth import FastLanguageModel
266
+
267
+ # The dataset is already in ShareGPT format — compatible with
268
+ # most fine-tuning frameworks that support conversation datasets.
269
+ trainer = SFTTrainer(
270
+ model=model,
271
+ tokenizer=tokenizer,
272
+ train_dataset=dataset['train'],
273
+ dataset_text_field="conversations", # adjust per framework
274
+ ...
275
+ )
276
+ ```
277
+
278
+ ---
279
+
280
+ ## 📁 Dataset Lineage
281
+
282
+ ```
283
+ bstee615/diversevul
284
+ └──> ChamaraVishwajithRajapaksha/Code_Vulnerability_Dataset
285
+ (330k rows, CWE-enriched via MITRE API)
286
+ └──> ChamaraVishwajithRajapaksha/Code-Vulnerability-FineTune
287
+ (ShareGPT format, unbalanced, unshuffled)
288
+ └──> ChamaraVishwajithRajapaksha/Code-Vulnerability-Balanced
289
+ (balanced + shuffled — this dataset)
290
+ ```
291
+
292
+ ---
293
+
294
+ ## ⚠️ Limitations
295
+
296
+ - Code samples are primarily in C and C++ — limited coverage of other languages
297
+ - Balancing is achieved by **undersampling** the majority class, so total row count is reduced compared to the source dataset
298
+ - The Safe samples represent patched/fixed versions, not inherently safe code — context matters
299
+ - CWE details describe the class of vulnerability, not a precise analysis of each individual function
300
+ - This dataset is intended for research and educational purposes
301
+
302
+ ---
303
+
304
+ ## 📜 License
305
+
306
+ This dataset is released under the **MIT License**, consistent with the source dataset license.
307
+
308
+ ---
309
+
310
+ ## 🙏 Citation
311
+
312
+ If you use this dataset in your research, please cite the original source and this dataset:
313
+
314
+ ```bibtex
315
+ @dataset{code_vulnerability_balanced,
316
+ title = {Code Vulnerability Balanced: CWE-Enriched Conversation Dataset},
317
+ author = {ChamaraVishwajithRajapaksha},
318
+ year = {2025},
319
+ publisher = {Hugging Face},
320
+ url = {https://huggingface.co/datasets/ChamaraVishwajithRajapaksha/Code-Vulnerability-Balanced},
321
+ note = {Balanced and shuffled version of Code-Vulnerability-FineTune, in ShareGPT format}
322
+ }
323
+ ```
324
+
325
+ ---
326
+
327
+ ## 🔗 Related Resources
328
+
329
+ - [MITRE CWE Database](https://cwe.mitre.org/)
330
+ - [DiverseVul Paper](https://github.com/bstee615/diversevul)
331
+ - [Source: Code-Vulnerability-FineTune](https://huggingface.co/datasets/ChamaraVishwajithRajapaksha/Code-Vulnerability-FineTune)
332
+ - [FineTome-100k Format Reference](https://huggingface.co/datasets/mlabonne/FineTome-100k)
333
+ - [Unsloth Fine-tuning](https://github.com/unslothai/unsloth)
334
+ - [TRL SFTTrainer](https://huggingface.co/docs/trl/sft_trainer)