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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gc
|
| 2 |
+
import glob
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
import polars as pl
|
| 8 |
+
from huggingface_hub import HfApi
|
| 9 |
+
|
| 10 |
+
run_conversion = (
|
| 11 |
+
input(
|
| 12 |
+
"\nwould you like to run the CSV to parquet conversion ('n' to skip to upload)? (y/n): "
|
| 13 |
+
)
|
| 14 |
+
.strip()
|
| 15 |
+
.lower()
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
output_dir = ""
|
| 19 |
+
|
| 20 |
+
if run_conversion == "y":
|
| 21 |
+
csv_dir = input("enter directory containing CSV files: ").strip()
|
| 22 |
+
|
| 23 |
+
while not csv_dir:
|
| 24 |
+
print("error: directory cannot be empty.")
|
| 25 |
+
csv_dir = input("enter directory containing CSV files: ").strip()
|
| 26 |
+
|
| 27 |
+
os.chdir(csv_dir)
|
| 28 |
+
output_dir = "parquet_dataset"
|
| 29 |
+
|
| 30 |
+
csv_files = sorted(glob.glob("*.csv"))
|
| 31 |
+
|
| 32 |
+
if not csv_files:
|
| 33 |
+
print("no CSV files found in current directory.")
|
| 34 |
+
exit(1)
|
| 35 |
+
|
| 36 |
+
print(f"found {len(csv_files)} CSV files.")
|
| 37 |
+
|
| 38 |
+
if os.path.exists(output_dir):
|
| 39 |
+
print(f"cleaning up old '{output_dir}' directory...")
|
| 40 |
+
shutil.rmtree(output_dir)
|
| 41 |
+
|
| 42 |
+
os.makedirs(output_dir)
|
| 43 |
+
|
| 44 |
+
chunk_size = 3
|
| 45 |
+
batches = [
|
| 46 |
+
csv_files[i : i + chunk_size] for i in range(0, len(csv_files), chunk_size)
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
print(f"\nprocessing {len(csv_files)} files into {len(batches)} parquet files...")
|
| 50 |
+
|
| 51 |
+
total_rows = 0
|
| 52 |
+
total_cols = None
|
| 53 |
+
|
| 54 |
+
start_time = time.time()
|
| 55 |
+
|
| 56 |
+
for batch_idx, batch_files in enumerate(batches):
|
| 57 |
+
print(
|
| 58 |
+
f"\n--- processing batch {batch_idx + 1}/{len(batches)} ({len(batch_files)} files) ---"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
batch_dfs = []
|
| 62 |
+
|
| 63 |
+
for f in batch_files:
|
| 64 |
+
df = pl.read_csv(
|
| 65 |
+
f,
|
| 66 |
+
schema_overrides={
|
| 67 |
+
"delta_start": pl.Utf8,
|
| 68 |
+
"handshake_duration": pl.Utf8,
|
| 69 |
+
"payload_bytes_skewness": pl.Utf8,
|
| 70 |
+
"payload_bytes_cov": pl.Utf8,
|
| 71 |
+
"fwd_payload_bytes_skewness": pl.Utf8,
|
| 72 |
+
"fwd_payload_bytes_cov": pl.Utf8,
|
| 73 |
+
"bwd_payload_bytes_skewness": pl.Utf8,
|
| 74 |
+
"bwd_payload_bytes_cov": pl.Utf8,
|
| 75 |
+
"fwd_skewness_header_bytes": pl.Utf8,
|
| 76 |
+
"bwd_skewness_header_bytes": pl.Utf8,
|
| 77 |
+
"packets_IAT_skewness": pl.Utf8,
|
| 78 |
+
"fwd_packets_IAT_skewness": pl.Utf8,
|
| 79 |
+
"bwd_packets_IAT_skewness": pl.Utf8,
|
| 80 |
+
"skewness_packets_delta_time": pl.Utf8,
|
| 81 |
+
"skewness_packets_delta_len": pl.Utf8,
|
| 82 |
+
"skewness_header_bytes_delta_len": pl.Utf8,
|
| 83 |
+
"skewness_payload_bytes_delta_len": pl.Utf8,
|
| 84 |
+
"cov_payload_bytes_delta_len": pl.Utf8,
|
| 85 |
+
},
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
row_count = len(df)
|
| 89 |
+
col_count = len(df.columns)
|
| 90 |
+
total_rows += row_count
|
| 91 |
+
|
| 92 |
+
print(f" - {f}: {row_count:,} rows, {col_count} columns")
|
| 93 |
+
|
| 94 |
+
if total_cols is None:
|
| 95 |
+
total_cols = col_count
|
| 96 |
+
elif col_count != total_cols:
|
| 97 |
+
print(
|
| 98 |
+
f"✗ ERROR: column mismatch in {f}! expected {total_cols}, got {col_count}"
|
| 99 |
+
)
|
| 100 |
+
exit(1)
|
| 101 |
+
|
| 102 |
+
batch_dfs.append(df)
|
| 103 |
+
|
| 104 |
+
print(f" merging batch {batch_idx + 1}...")
|
| 105 |
+
combined_batch = pl.concat(batch_dfs)
|
| 106 |
+
|
| 107 |
+
output_filename = os.path.join(output_dir, f"chunk_{batch_idx + 1:02d}.parquet")
|
| 108 |
+
combined_batch.write_parquet(output_filename)
|
| 109 |
+
print(f" ✓ saved to {output_filename}")
|
| 110 |
+
|
| 111 |
+
del batch_dfs
|
| 112 |
+
del combined_batch
|
| 113 |
+
del df
|
| 114 |
+
gc.collect()
|
| 115 |
+
|
| 116 |
+
end_time = time.time()
|
| 117 |
+
elapsed_minutes = (end_time - start_time) / 60
|
| 118 |
+
|
| 119 |
+
full_path = os.path.abspath(output_dir)
|
| 120 |
+
print("\n" + "=" * 40)
|
| 121 |
+
print(f"conversion completed in {elapsed_minutes:.2f} minutes")
|
| 122 |
+
print(f"total input rows processed: {total_rows:,}")
|
| 123 |
+
print(f"parquet files saved in: {full_path}")
|
| 124 |
+
print("=" * 40 + "\n")
|
| 125 |
+
|
| 126 |
+
else:
|
| 127 |
+
print("\nskipping conversion step...")
|
| 128 |
+
output_dir = input("enter the directory path you want to upload: ").strip()
|
| 129 |
+
while output_dir and not os.path.isdir(output_dir):
|
| 130 |
+
print(f"✗ error: directory '{output_dir}' does not exist.")
|
| 131 |
+
output_dir = input(
|
| 132 |
+
"enter a valid directory path (or press Enter to cancel): "
|
| 133 |
+
).strip()
|
| 134 |
+
|
| 135 |
+
if output_dir:
|
| 136 |
+
output_dir = os.path.abspath(output_dir)
|
| 137 |
+
|
| 138 |
+
if output_dir:
|
| 139 |
+
repo_id = input(
|
| 140 |
+
f"\nenter your huggingface repo id to upload '{output_dir}' (or press Enter to skip upload): "
|
| 141 |
+
).strip()
|
| 142 |
+
|
| 143 |
+
if repo_id:
|
| 144 |
+
if "/" not in repo_id:
|
| 145 |
+
print(
|
| 146 |
+
"✗ error: invalid repo id. it must contain a forward slash '/' separating your username and repo name"
|
| 147 |
+
)
|
| 148 |
+
else:
|
| 149 |
+
try:
|
| 150 |
+
print(f"\ninitializing upload to {repo_id}...")
|
| 151 |
+
api = HfApi()
|
| 152 |
+
|
| 153 |
+
api.upload_folder(
|
| 154 |
+
folder_path=output_dir,
|
| 155 |
+
repo_id=repo_id,
|
| 156 |
+
repo_type="dataset",
|
| 157 |
+
)
|
| 158 |
+
print("\n✓ upload successful. dataset is now on huggingface.")
|
| 159 |
+
except Exception as e:
|
| 160 |
+
print(
|
| 161 |
+
"\n✗ upload failed. make sure you are logged in via 'hf auth login'."
|
| 162 |
+
)
|
| 163 |
+
print(f"error details: {e}")
|
| 164 |
+
else:
|
| 165 |
+
print(
|
| 166 |
+
f"\nupload skipped. you can manually upload the folder later with:\n"
|
| 167 |
+
f"`hf upload --type dataset REPO_ID {output_dir} [path_in_repo]`"
|
| 168 |
+
)
|
| 169 |
+
else:
|
| 170 |
+
print("upload skipped. you can manually upload the folder later.")
|