File size: 4,661 Bytes
6a67fae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
{
"cells": [
{
"cell_type": "markdown",
"id": "fe805f91",
"metadata": {},
"source": [
"This script is used to convert the Ton iot datasets into jsonl that can be used by the tokenizer in the training and testing scripts in order to be in the correct format to train the models."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4dc6bc25",
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"import json\n",
"\n",
"# Path to desired file to be converted\n",
"csv_file = 'train_test_network.csv'\n",
"\n",
"# Name of file to be written to\n",
"jsonl_file = 'train_test_network.jsonl'\n",
"\n",
"with open(csv_file, 'r') as f, open(jsonl_file, 'w') as jsonl_f:\n",
" reader = csv.DictReader(f)\n",
" \n",
" for row in reader:\n",
" # Normalize the label to ensure comparison is case-insensitive\n",
" label = row['type'].strip().lower()\n",
" \n",
" # For converting the dataset to binary. If you are doing multi-class comment out this line\n",
" label = 'normal' if label == 'normal' else 'attack'\n",
"\n",
" \n",
" # Construct the user message by concatenating all relevant fields\n",
" user_message = \", \".join([f\"{key}: {value}\" for key, value in row.items() if key != 'label' and key != 'type'])\n",
" \n",
" # Create the conversation in the desired format\n",
" conversation = {\n",
" \"conversations\": [\n",
" {\"from\": \"human\", \"value\": user_message},\n",
" {\"from\": \"gpt\", \"value\": label}\n",
" ]\n",
" }\n",
" \n",
" # Write each conversation as a JSON object in a new line\n",
" jsonl_f.write(json.dumps(conversation) + \"\\n\")"
]
},
{
"cell_type": "markdown",
"id": "4444baf3",
"metadata": {},
"source": [
"Output will look like:\n",
"\n",
"```\n",
"{\"conversations\": [{\"from\": \"human\", \"value\": \"\\ufeffsrc_ip: 192.168.1.192, src_port: 5353, dst_ip: 224.0.0.251, dst_port: 5353, proto: udp, service: dns, duration: 0, src_bytes: 0, dst_bytes: 0, conn_state: S0, missed_bytes: 0, src_pkts: 1, src_ip_bytes: 73, dst_pkts: 0, dst_ip_bytes: 0, dns_query: _ipps._tcp.local, dns_qclass: 1, dns_qtype: 12, dns_rcode: 0, dns_AA: F, dns_RD: F, dns_RA: F, dns_rejected: F, ssl_version: -, ssl_cipher: -, ssl_resumed: -, ssl_established: -, ssl_subject: -, ssl_issuer: -, http_trans_depth: -, http_method: -, http_uri: -, http_version: -, http_request_body_len: 0, http_response_body_len: 0, http_status_code: 0, http_user_agent: -, http_orig_mime_types: -, http_resp_mime_types: -, weird_name: -, weird_addl: -, weird_notice: -\"}, {\"from\": \"gpt\", \"value\": \"normal\"}]}\n",
"```\n"
]
},
{
"cell_type": "markdown",
"id": "0a36087b",
"metadata": {},
"source": [
"If you want to Split datasets and save them seperately in order to upload them to huggingface use this. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19944860",
"metadata": {},
"outputs": [],
"source": [
"# Load dataset from the JSONL file\n",
"dataset = load_dataset('json', data_files=jsonl_file, split='train')\n",
"\n",
"# Split the dataset\n",
"split_dataset = dataset.train_test_split(test_size=0.2) # 20% test data\n",
"\n",
"# Access the training and testing splits\n",
"train_dataset = split_dataset['train']\n",
"test_dataset = split_dataset['test']\n",
"\n",
"# Define paths for saving the datasets\n",
"jsonl_train = 'train_network.jsonl'\n",
"jsonl_test = 'test_network.jsonl'\n",
"\n",
"# Save train_dataset to JSONL file\n",
"with open(jsonl_train, 'w') as train_f:\n",
" for example in train_dataset:\n",
" train_f.write(json.dumps(example) + \"\\n\")\n",
"\n",
"# Save test_dataset to JSONL file\n",
"with open(jsonl_test, 'w') as test_f:\n",
" for example in test_dataset:\n",
" test_f.write(json.dumps(example) + \"\\n\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|