-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_training_data_as_csv.py
85 lines (56 loc) 路 2.03 KB
/
prepare_training_data_as_csv.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
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
from kaggle.api.kaggle_api_extended import KaggleApi
import pandas as pd
from langchain import PromptTemplate
from os import path
dataset_csv_filename = "RickAndMortyScripts.csv"
dataset_name = "andradaolteanu/rickmorty-scripts"
expected_data_size = 1905
if not path.exists(dataset_csv_filename):
api = KaggleApi()
api.authenticate()
api.dataset_download_file(dataset_name, dataset_csv_filename)
data = pd.read_csv(dataset_csv_filename)
assert len(data.index) == expected_data_size
prompt_template = PromptTemplate.from_template(
"<s>[INST] {other_lines} [/INST] {rick_lines} </s>"
)
def build_data_item(other_lines, rick_lines):
return prompt_template.format(other_lines=other_lines, rick_lines=rick_lines)
def clear_unwanted_characters(text):
return text.replace('"', "")
def preprocess_data_item(text):
return clear_unwanted_characters(text) + " "
rick = "Rick"
global_output = pd.DataFrame(columns=["text"])
global_pointer = -1
other_lines = ""
rick_lines = ""
total_lines_count = expected_data_size
local_pointer = 0
while global_pointer < total_lines_count:
global_pointer += local_pointer
if global_pointer == total_lines_count:
break
local_pointer = 0
other_lines = ""
rick_lines = ""
current_row_index = global_pointer + local_pointer
if current_row_index >= total_lines_count:
break
row = data.iloc[current_row_index]
while rick_lines == "" or rick in row["name"]:
preprocessed_line = preprocess_data_item(row["line"])
if row["name"] == rick:
rick_lines += preprocessed_line
else:
other_lines += preprocessed_line
local_pointer += 1
current_row_index = global_pointer + local_pointer
if current_row_index >= total_lines_count:
break
row = data.iloc[current_row_index]
global_output = pd.concat(
[global_output, pd.DataFrame([build_data_item(other_lines, rick_lines)])],
ignore_index=True,
)
global_output.to_csv("dataset.csv", index=False)