import gc import glob import os import shutil import time import polars as pl from huggingface_hub import HfApi run_conversion = ( input( "\nwould you like to run the CSV to parquet conversion ('n' to skip to upload)? (y/n): " ) .strip() .lower() ) output_dir = "" if run_conversion == "y": csv_dir = input("enter directory containing CSV files: ").strip() while not csv_dir: print("error: directory cannot be empty.") csv_dir = input("enter directory containing CSV files: ").strip() os.chdir(csv_dir) output_dir = "parquet_dataset" csv_files = sorted(glob.glob("*.csv")) if not csv_files: print("no CSV files found in current directory.") exit(1) print(f"found {len(csv_files)} CSV files.") if os.path.exists(output_dir): print(f"cleaning up old '{output_dir}' directory...") shutil.rmtree(output_dir) os.makedirs(output_dir) chunk_size = 3 batches = [ csv_files[i : i + chunk_size] for i in range(0, len(csv_files), chunk_size) ] print(f"\nprocessing {len(csv_files)} files into {len(batches)} parquet files...") total_rows = 0 total_cols = None start_time = time.time() for batch_idx, batch_files in enumerate(batches): print( f"\n--- processing batch {batch_idx + 1}/{len(batches)} ({len(batch_files)} files) ---" ) batch_dfs = [] for f in batch_files: df = pl.read_csv( f, schema_overrides={ "delta_start": pl.Utf8, "handshake_duration": pl.Utf8, "payload_bytes_skewness": pl.Utf8, "payload_bytes_cov": pl.Utf8, "fwd_payload_bytes_skewness": pl.Utf8, "fwd_payload_bytes_cov": pl.Utf8, "bwd_payload_bytes_skewness": pl.Utf8, "bwd_payload_bytes_cov": pl.Utf8, "fwd_skewness_header_bytes": pl.Utf8, "bwd_skewness_header_bytes": pl.Utf8, "packets_IAT_skewness": pl.Utf8, "fwd_packets_IAT_skewness": pl.Utf8, "bwd_packets_IAT_skewness": pl.Utf8, "skewness_packets_delta_time": pl.Utf8, "skewness_packets_delta_len": pl.Utf8, "skewness_header_bytes_delta_len": pl.Utf8, "skewness_payload_bytes_delta_len": pl.Utf8, "cov_payload_bytes_delta_len": pl.Utf8, }, ) row_count = len(df) col_count = len(df.columns) total_rows += row_count print(f" - {f}: {row_count:,} rows, {col_count} columns") if total_cols is None: total_cols = col_count elif col_count != total_cols: print( f"✗ ERROR: column mismatch in {f}! expected {total_cols}, got {col_count}" ) exit(1) batch_dfs.append(df) print(f" merging batch {batch_idx + 1}...") combined_batch = pl.concat(batch_dfs) output_filename = os.path.join(output_dir, f"chunk_{batch_idx + 1:02d}.parquet") combined_batch.write_parquet(output_filename) print(f" ✓ saved to {output_filename}") del batch_dfs del combined_batch del df gc.collect() end_time = time.time() elapsed_minutes = (end_time - start_time) / 60 full_path = os.path.abspath(output_dir) print("\n" + "=" * 40) print(f"conversion completed in {elapsed_minutes:.2f} minutes") print(f"total input rows processed: {total_rows:,}") print(f"parquet files saved in: {full_path}") print("=" * 40 + "\n") else: print("\nskipping conversion step...") output_dir = input("enter the directory path you want to upload: ").strip() while output_dir and not os.path.isdir(output_dir): print(f"✗ error: directory '{output_dir}' does not exist.") output_dir = input( "enter a valid directory path (or press Enter to cancel): " ).strip() if output_dir: output_dir = os.path.abspath(output_dir) if output_dir: repo_id = input( f"\nenter your huggingface repo id to upload '{output_dir}' (or press Enter to skip upload): " ).strip() if repo_id: if "/" not in repo_id: print( "✗ error: invalid repo id. it must contain a forward slash '/' separating your username and repo name" ) else: try: print(f"\ninitializing upload to {repo_id}...") api = HfApi() api.upload_folder( folder_path=output_dir, repo_id=repo_id, repo_type="dataset", ) print("\n✓ upload successful. dataset is now on huggingface.") except Exception as e: print( "\n✗ upload failed. make sure you are logged in via 'hf auth login'." ) print(f"error details: {e}") else: print( f"\nupload skipped. you can manually upload the folder later with:\n" f"`hf upload --type dataset REPO_ID {output_dir} [path_in_repo]`" ) else: print("upload skipped. you can manually upload the folder later.")