-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_dataset.py
More file actions
119 lines (96 loc) · 4.55 KB
/
convert_dataset.py
File metadata and controls
119 lines (96 loc) · 4.55 KB
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
# MIT License
#
# Copyright (c) 2025 Andy Ryan
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#!/usr/bin/env python3
"""
Dataset Converter
This script converts CSV datasets to match the format required by the prompt evaluator.
The output format follows the specification in the README:
"prompt text"|expected_result
"""
import csv
import os
def convert_dataset(input_file, output_file):
"""
Convert the input CSV file to match the format required by the prompt evaluator.
Args:
input_file (str): Path to the input CSV file
output_file (str): Path to the output CSV file
"""
print(f"Converting {input_file} to {output_file}...")
with open(input_file, 'r', encoding='utf-8') as infile, \
open(output_file, 'w', encoding='utf-8') as outfile:
# Use CSV reader to properly handle quoted fields
csv_reader = csv.reader(infile)
# Skip the header row if it exists
try:
header = next(csv_reader)
print(f"Found header: {header}")
except StopIteration:
print("Warning: Input file is empty")
return
# Process each row
for row in csv_reader:
if len(row) < 2:
print(f"Warning: Skipping malformed row: {row}")
continue
prompt, pii = row[0], row[1]
# Replace newlines with spaces and strip extra whitespace
prompt = ' '.join(prompt.split())
# Convert True/False to true/false (lowercase)
pii_value = "true" if pii.lower() == "true" else "false"
# Write in the format: """prompt"""|true/false
outfile.write(f'"""{prompt}"""|{pii_value}\n')
print(f"Conversion complete. Output written to {output_file}")
if __name__ == "__main__":
print("Dataset Converter")
print("=================")
# Define the datasets directory
datasets_dir = "datasets"
# Check if the datasets directory exists
if not os.path.exists(datasets_dir):
print(f"Note: '{datasets_dir}' directory not found. It will be created if you save a file there.")
# Ask for input file
while True:
input_file = input("Enter the path to the input CSV file: ").strip()
# If the input file is in the datasets directory, suggest the full path
if not os.path.dirname(input_file) and os.path.exists(os.path.join(datasets_dir, input_file)):
input_file = os.path.join(datasets_dir, input_file)
print(f"Using file from datasets directory: {input_file}")
if os.path.exists(input_file):
break
else:
print(f"Error: File '{input_file}' not found. Please try again.")
# Ask for output file
output_file = input("Enter the name for the output CSV file: ").strip()
if not output_file:
output_file = "converted_dataset.csv"
print(f"Using default output filename: {output_file}")
# If the output file doesn't have a directory specified, suggest saving to datasets
if not os.path.dirname(output_file):
datasets_output = os.path.join(datasets_dir, output_file)
save_to_datasets = input(f"Save output to '{datasets_output}'? (y/n): ").strip().lower()
if save_to_datasets == 'y':
output_file = datasets_output
# Create the datasets directory if it doesn't exist
os.makedirs(datasets_dir, exist_ok=True)
# Convert the dataset
convert_dataset(input_file, output_file)