Repository navigation
Expand file tree
/
Copy pathreplace_strong.py
More file actions
160 lines (122 loc) · 3.8 KB
/
Copy pathreplace_strong.py
File metadata and controls
160 lines (122 loc) · 3.8 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from utils import read_json, print_json
import pickle
from collections import defaultdict
import argparse
import random
import sys
import argparse
other_inp = "data/en_conv_len_2.pkl"
#all_data = read_json("")
input_data = read_json("result/add_session/main.json")
parser = argparse.ArgumentParser(description="in out path")
parser.add_argument("--output",
type=str,
default="w_o_strong", # ablation/4k/main
choices=["w_o_strong"],
required=False,
)
args = parser.parse_args()
args_output = f"result/add_session/ablation/{args.output}.json"
with open(other_inp, "rb") as f:
other_data = pickle.load(f)
max_token = 8000
all_convs = []
def bucket_by_turn(items, min_size=7, max_turn=8):
buckets = defaultdict(list)
count = 0
for item in items:
turn = item.get('turn')
conv = item["conversations"]
# full = tokenizer(
# str(conv),
# truncation=False,
# add_special_tokens=True
# )
if turn <= max_turn:
len_conv = len(str(conv))
if len_conv <= max_token :
buckets[turn].append(conv)
all_convs.append((len_conv, conv))
count += 1
buckets = dict(buckets)
filtered_buckets = {
turn: bucket_items
for turn, bucket_items in buckets.items()
if len(bucket_items) >= min_size
}
print(count, len(filtered_buckets.keys()))
return filtered_buckets
random.seed(3407)
buckets = bucket_by_turn(other_data)
total = 8
threshold = 200
#sys.exit(0)
# def select_other() :
# length = random.choice(list(buckets.keys()))
# res = random.choice(buckets[length])
# return res
def order_role_and_content(data_dic) :
return [{
"content" : dic["content"],
"role" : dic["role"]
}for dic in data_dic]
#train_data = []
from tqdm import tqdm
import bisect
all_convs.sort(key=lambda x: x[0])
lengths = [l for l, _ in all_convs]
def search(target_conv, max_del=200) :
target_len = len(str(target_conv))
idx = bisect.bisect_left(lengths, target_len)
close = []
# best_d = None
# best_diff = float("inf")
left = idx - 1
right = idx
while left >= 0 :
if abs(lengths[left] - target_len) < max_del :
close.append(all_convs[left][1])
left -= 1
else :
break
while right < len(all_convs) :
if abs(lengths[right] - target_len) < max_del :
close.append(all_convs[right][1])
right += 1
else :
break
if not close:
if idx < len(all_convs) :
close = [all_convs[idx][1]]
else :
close = [all_convs[idx - 1][1]]
return close
no_upt_count = 0
for data_id, data in enumerate(tqdm(input_data)) :
# if args.output == "w_o_change" :
# if data.get("type", "no") == "changed" :
# no_upt_count += 1
# continue
strong_ids = [
i for i in range(total)
if i not in data["position"] and i not in data["other_id"]
]
assert all(isinstance(oid, int) for oid in data["position"])
assert all(isinstance(oid, int) for oid in data["other_id"])
lis = data["prior_interactions"]
#print('sss :', len(strong_ids))
for strong_id in strong_ids :
target_conv = lis[strong_id]
close = search(target_conv)
#print(len(close))
lis[strong_id] = random.choice(close)
for dialog in lis :
assert dialog
lis = [order_role_and_content(dialog) for dialog in lis]
data["prior_interactions"] = lis
# if data_id > 10 :
# sys.exit(0)
# if args.output == "w_o_change" :
# print('no upt count :', no_upt_count)
print('len :', len(input_data))
print_json(args_output, input_data)