-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_data_pipeline.py
55 lines (41 loc) · 1.7 KB
/
clean_data_pipeline.py
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
import re
import pandas as pd
# clean data and store into parquet, without considering the data score for stackoverflow
file_path = './data/security.output.csv'
output_path = './data/security_stack_exchange.parquet'
df = pd.read_csv(file_path)
def clean_html_tags(text):
cleaned_text = text.replace('<p>', '').replace('</p>', '')
clean = re.compile('<.*?>')
res = re.sub(clean, ' ', cleaned_text)
return res
def clean_space(text):
# Remove duplicate newline characters
cleaned_text = re.sub(r'\n+', '\n', text)
# Remove extra spaces
cleaned_text = re.sub(r'\s+', ' ', cleaned_text)
return cleaned_text.strip() # Remove leading and trailing whitespace
# Step 1: Lowercase all text
df_cleaned = df.map(lambda x: x.lower() if isinstance(x, str) else x)
# Step 2: Clean HTML tags
df_cleaned['Question'] = df_cleaned['Question'].apply(clean_html_tags)
df_cleaned['Answer'] = df_cleaned['Answer'].apply(clean_html_tags)
# Step3: Clean duplicated \n and space
df_cleaned = df_cleaned.map(lambda x: clean_space(x) if isinstance(x, str) else x)
# Display the cleaned DataFrame
df_cleaned.sample(10)
def generate_training_data(df):
data = []
for index, row in df.iterrows():
question = row['Question']
answer = row['Answer']
inst_template = "<s>[INST] {} [/INST] {} </s>"
inst_qa = inst_template.format(question, answer)
data.append(inst_qa)
return data
training_data = generate_training_data(df_cleaned)
training_data_df = pd.DataFrame(training_data, columns=['train'])
# Save training data as a Parquet file without header
training_data_df.to_parquet(output_path, index=False)
output_df = pd.read_parquet(output_path)
print(output_df.head(10))