Delete convert_to_jsonl.ipynb
Browse files- convert_to_jsonl.ipynb +0 -242
convert_to_jsonl.ipynb
DELETED
|
@@ -1,242 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "markdown",
|
| 5 |
-
"metadata": {},
|
| 6 |
-
"source": [
|
| 7 |
-
"# Convert SIABENCH JSONs to JSONL for HuggingFace\n",
|
| 8 |
-
"Flattens the nested `scenarios` structure into one JSON object per line.\n",
|
| 9 |
-
"Output goes into a `data/` folder to upload alongside your dataset.\n",
|
| 10 |
-
"\n",
|
| 11 |
-
"> Run this from your project root where `SIABench-main/` lives."
|
| 12 |
-
]
|
| 13 |
-
},
|
| 14 |
-
{
|
| 15 |
-
"cell_type": "code",
|
| 16 |
-
"execution_count": 1,
|
| 17 |
-
"id": "b78a1dd7",
|
| 18 |
-
"metadata": {},
|
| 19 |
-
"outputs": [
|
| 20 |
-
{
|
| 21 |
-
"name": "stdout",
|
| 22 |
-
"output_type": "stream",
|
| 23 |
-
"text": [
|
| 24 |
-
"Config loaded. Output folder: F:\\Downloads\\MAIN_SIA_BENCH\\data\n"
|
| 25 |
-
]
|
| 26 |
-
}
|
| 27 |
-
],
|
| 28 |
-
"source": [
|
| 29 |
-
"import json\n",
|
| 30 |
-
"import os\n",
|
| 31 |
-
"from pathlib import Path\n",
|
| 32 |
-
"\n",
|
| 33 |
-
"BASE_DIR = Path('SIABench-main')\n",
|
| 34 |
-
"OUT_DIR = Path('data') # Will be uploaded to HuggingFace root\n",
|
| 35 |
-
"OUT_DIR.mkdir(exist_ok=True)\n",
|
| 36 |
-
"\n",
|
| 37 |
-
"# ── Config map: output filename → list of (split, source_folder) ──\n",
|
| 38 |
-
"CONFIGS = {\n",
|
| 39 |
-
" 'alert_triaging_tii': [\n",
|
| 40 |
-
" ('tp', BASE_DIR / 'Alert_Triaging_Dataset' / 'Alert_Triaging_Dataset_TII' / 'TP'),\n",
|
| 41 |
-
" ('fp', BASE_DIR / 'Alert_Triaging_Dataset' / 'Alert_Triaging_Dataset_TII' / 'FP'),\n",
|
| 42 |
-
" ],\n",
|
| 43 |
-
" 'alert_triaging_cic': [\n",
|
| 44 |
-
" ('test', BASE_DIR / 'Alert_Triaging_Dataset' / 'Alert_Triaging_Dataset_CIC'),\n",
|
| 45 |
-
" ],\n",
|
| 46 |
-
" 'sia_dataset': [\n",
|
| 47 |
-
" ('test', BASE_DIR / 'SIA_Dataset'),\n",
|
| 48 |
-
" ],\n",
|
| 49 |
-
"}\n",
|
| 50 |
-
"\n",
|
| 51 |
-
"print('Config loaded. Output folder:', OUT_DIR.resolve())"
|
| 52 |
-
]
|
| 53 |
-
},
|
| 54 |
-
{
|
| 55 |
-
"cell_type": "code",
|
| 56 |
-
"execution_count": 2,
|
| 57 |
-
"id": "f4614351",
|
| 58 |
-
"metadata": {},
|
| 59 |
-
"outputs": [
|
| 60 |
-
{
|
| 61 |
-
"name": "stdout",
|
| 62 |
-
"output_type": "stream",
|
| 63 |
-
"text": [
|
| 64 |
-
"Flatten functions defined.\n"
|
| 65 |
-
]
|
| 66 |
-
}
|
| 67 |
-
],
|
| 68 |
-
"source": [
|
| 69 |
-
"def flatten_alert_triaging(meta, comp):\n",
|
| 70 |
-
" \"\"\"Flatten TII/CIC scenario into a single dict.\"\"\"\n",
|
| 71 |
-
" return {\n",
|
| 72 |
-
" 'scenario_name': meta.get('scenario_name', ''),\n",
|
| 73 |
-
" 'alert_name': meta.get('alert_name', ''),\n",
|
| 74 |
-
" 'alert_type': meta.get('alert_type', ''),\n",
|
| 75 |
-
" 'scenario': comp.get('scenario', ''),\n",
|
| 76 |
-
" 'alert': comp.get('alert', ''),\n",
|
| 77 |
-
" 'tools_available': comp.get('tools_available', []),\n",
|
| 78 |
-
" 'files_available': comp.get('files_available', ''),\n",
|
| 79 |
-
" 'instructions': comp.get('instructions', ''),\n",
|
| 80 |
-
" 'directory': comp.get('directory', ''),\n",
|
| 81 |
-
" 'questions': [\n",
|
| 82 |
-
" {'question': q.get('question', ''), 'answer': q.get('answer', '')}\n",
|
| 83 |
-
" for q in comp.get('questions', [])\n",
|
| 84 |
-
" ],\n",
|
| 85 |
-
" }\n",
|
| 86 |
-
"\n",
|
| 87 |
-
"def flatten_sia_dataset(meta, comp):\n",
|
| 88 |
-
" \"\"\"Flatten SIA_Dataset CTF scenario into a single dict.\"\"\"\n",
|
| 89 |
-
" return {\n",
|
| 90 |
-
" 'scenario_name': meta.get('scenario_name', ''),\n",
|
| 91 |
-
" 'source': meta.get('source', ''),\n",
|
| 92 |
-
" 'last_accessed': meta.get('last_accessed', ''),\n",
|
| 93 |
-
" 'writeup': meta.get('writeup', ''),\n",
|
| 94 |
-
" 'scenario': comp.get('scenario', ''),\n",
|
| 95 |
-
" 'task_category': comp.get('task_category', ''),\n",
|
| 96 |
-
" 'complexity': comp.get('complexity', ''),\n",
|
| 97 |
-
" 'tools_available': comp.get('tools_available', []),\n",
|
| 98 |
-
" 'files_available': comp.get('files_available', ''),\n",
|
| 99 |
-
" 'instructions': comp.get('instructions', ''),\n",
|
| 100 |
-
" 'directory': comp.get('directory', ''),\n",
|
| 101 |
-
" 'questions': [\n",
|
| 102 |
-
" {\n",
|
| 103 |
-
" 'question': q.get('question', ''),\n",
|
| 104 |
-
" 'answer': q.get('answer', ''),\n",
|
| 105 |
-
" 'adversarial_tactic': q.get('adversarial_tactic', ''),\n",
|
| 106 |
-
" }\n",
|
| 107 |
-
" for q in comp.get('questions', [])\n",
|
| 108 |
-
" ],\n",
|
| 109 |
-
" }\n",
|
| 110 |
-
"\n",
|
| 111 |
-
"print('Flatten functions defined.')"
|
| 112 |
-
]
|
| 113 |
-
},
|
| 114 |
-
{
|
| 115 |
-
"cell_type": "code",
|
| 116 |
-
"execution_count": 3,
|
| 117 |
-
"id": "05c92cc6",
|
| 118 |
-
"metadata": {},
|
| 119 |
-
"outputs": [
|
| 120 |
-
{
|
| 121 |
-
"name": "stdout",
|
| 122 |
-
"output_type": "stream",
|
| 123 |
-
"text": [
|
| 124 |
-
" ✓ alert_triaging_tii_tp.jsonl — 100 records\n",
|
| 125 |
-
" ✓ alert_triaging_tii_fp.jsonl — 50 records\n",
|
| 126 |
-
" ✓ alert_triaging_cic_test.jsonl — 35 records\n",
|
| 127 |
-
" ✓ sia_dataset_test.jsonl — 25 records\n",
|
| 128 |
-
"\n",
|
| 129 |
-
"Done!\n"
|
| 130 |
-
]
|
| 131 |
-
}
|
| 132 |
-
],
|
| 133 |
-
"source": [
|
| 134 |
-
"stats = {}\n",
|
| 135 |
-
"\n",
|
| 136 |
-
"for config_name, splits in CONFIGS.items():\n",
|
| 137 |
-
" stats[config_name] = {}\n",
|
| 138 |
-
" is_alert = config_name.startswith('alert_triaging')\n",
|
| 139 |
-
"\n",
|
| 140 |
-
" for split_name, folder in splits:\n",
|
| 141 |
-
" if not folder.exists():\n",
|
| 142 |
-
" print(f'WARNING: {folder} not found, skipping.')\n",
|
| 143 |
-
" continue\n",
|
| 144 |
-
"\n",
|
| 145 |
-
" out_file = OUT_DIR / f'{config_name}_{split_name}.jsonl'\n",
|
| 146 |
-
" count = 0\n",
|
| 147 |
-
"\n",
|
| 148 |
-
" with open(out_file, 'w', encoding='utf-8') as out_f:\n",
|
| 149 |
-
" for json_file in sorted(folder.glob('*.json')):\n",
|
| 150 |
-
" with open(json_file, 'r', encoding='utf-8') as f:\n",
|
| 151 |
-
" data = json.load(f)\n",
|
| 152 |
-
"\n",
|
| 153 |
-
" scenarios = data.get('scenarios', [])\n",
|
| 154 |
-
" meta = next((s.get('metadata') or {} for s in scenarios if 'metadata' in s), {})\n",
|
| 155 |
-
" comp = next((s.get('sia_components') or {} for s in scenarios if 'sia_components' in s), {})\n",
|
| 156 |
-
"\n",
|
| 157 |
-
" if is_alert:\n",
|
| 158 |
-
" record = flatten_alert_triaging(meta, comp)\n",
|
| 159 |
-
" else:\n",
|
| 160 |
-
" record = flatten_sia_dataset(meta, comp)\n",
|
| 161 |
-
"\n",
|
| 162 |
-
" out_f.write(json.dumps(record, ensure_ascii=False) + '\\n')\n",
|
| 163 |
-
" count += 1\n",
|
| 164 |
-
"\n",
|
| 165 |
-
" stats[config_name][split_name] = count\n",
|
| 166 |
-
" print(f' ✓ {out_file.name} — {count} records')\n",
|
| 167 |
-
"\n",
|
| 168 |
-
"print('\\nDone!')"
|
| 169 |
-
]
|
| 170 |
-
},
|
| 171 |
-
{
|
| 172 |
-
"cell_type": "code",
|
| 173 |
-
"execution_count": 4,
|
| 174 |
-
"id": "98ea98c4",
|
| 175 |
-
"metadata": {},
|
| 176 |
-
"outputs": [
|
| 177 |
-
{
|
| 178 |
-
"name": "stdout",
|
| 179 |
-
"output_type": "stream",
|
| 180 |
-
"text": [
|
| 181 |
-
"══════════════════════════════════════════════════\n",
|
| 182 |
-
"JSONL EXPORT SUMMARY\n",
|
| 183 |
-
"══════════════════════════════════════════════════\n",
|
| 184 |
-
"\n",
|
| 185 |
-
"alert_triaging_tii:\n",
|
| 186 |
-
" tp: 100 records → data/alert_triaging_tii_tp.jsonl\n",
|
| 187 |
-
" fp: 50 records → data/alert_triaging_tii_fp.jsonl\n",
|
| 188 |
-
"\n",
|
| 189 |
-
"alert_triaging_cic:\n",
|
| 190 |
-
" test: 35 records → data/alert_triaging_cic_test.jsonl\n",
|
| 191 |
-
"\n",
|
| 192 |
-
"sia_dataset:\n",
|
| 193 |
-
" test: 25 records → data/sia_dataset_test.jsonl\n",
|
| 194 |
-
"\n",
|
| 195 |
-
"Files in data/ folder:\n",
|
| 196 |
-
" alert_triaging_cic_test.jsonl (40.2 kB)\n",
|
| 197 |
-
" alert_triaging_tii_fp.jsonl (50.2 kB)\n",
|
| 198 |
-
" alert_triaging_tii_tp.jsonl (100.3 kB)\n",
|
| 199 |
-
" sia_dataset_test.jsonl (102.7 kB)\n",
|
| 200 |
-
"\n",
|
| 201 |
-
"Next steps:\n",
|
| 202 |
-
" 1. Delete SIA_Dataset.py from HuggingFace repo\n",
|
| 203 |
-
" 2. Upload the data/ folder to the ROOT of HuggingFace repo\n",
|
| 204 |
-
" 3. Upload the updated README.md\n"
|
| 205 |
-
]
|
| 206 |
-
}
|
| 207 |
-
],
|
| 208 |
-
"source": [
|
| 209 |
-
"# ── Summary ──\n",
|
| 210 |
-
"print('═' * 50)\n",
|
| 211 |
-
"print('JSONL EXPORT SUMMARY')\n",
|
| 212 |
-
"print('═' * 50)\n",
|
| 213 |
-
"for config, splits in stats.items():\n",
|
| 214 |
-
" print(f'\\n{config}:')\n",
|
| 215 |
-
" for split, count in splits.items():\n",
|
| 216 |
-
" print(f' {split}: {count} records → data/{config}_{split}.jsonl')\n",
|
| 217 |
-
"\n",
|
| 218 |
-
"print('\\nFiles in data/ folder:')\n",
|
| 219 |
-
"for f in sorted(OUT_DIR.iterdir()):\n",
|
| 220 |
-
" print(f' {f.name} ({f.stat().st_size / 1024:.1f} kB)')\n",
|
| 221 |
-
"\n",
|
| 222 |
-
"print('\\nNext steps:')\n",
|
| 223 |
-
"print(' 1. Delete SIA_Dataset.py from HuggingFace repo')\n",
|
| 224 |
-
"print(' 2. Upload the data/ folder to the ROOT of HuggingFace repo')\n",
|
| 225 |
-
"print(' 3. Upload the updated README.md')"
|
| 226 |
-
]
|
| 227 |
-
}
|
| 228 |
-
],
|
| 229 |
-
"metadata": {
|
| 230 |
-
"kernelspec": {
|
| 231 |
-
"display_name": "Python 3",
|
| 232 |
-
"language": "python",
|
| 233 |
-
"name": "python3"
|
| 234 |
-
},
|
| 235 |
-
"language_info": {
|
| 236 |
-
"name": "python",
|
| 237 |
-
"version": "3.10.0"
|
| 238 |
-
}
|
| 239 |
-
},
|
| 240 |
-
"nbformat": 4,
|
| 241 |
-
"nbformat_minor": 5
|
| 242 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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