-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_script.py
189 lines (161 loc) · 8.02 KB
/
main_script.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import numpy as np
import pandas as pd
# pd.set_option('display.max_columns', None)
pd.set_option('display.expand_frame_repr', False)
pd.set_option('max_colwidth', 0)
import warnings
warnings.simplefilter("ignore", UserWarning)
import argparse
from utils import inference, print_result, load_nli_model, run_all_nli_prompt,str2bool
def main(
data_dir="./datasets/PLV_test.tsv",
prompt_dir="./prompts/Tree.txt",
score_dir="./scores/PLV_test-Tree.npy",
output_dir="./outputs/PLV_test-Tree-result.csv",
model_name="roberta-large-mnli",
consult_penalty=0.02,
infer_setting="offline",
run_offline_nli=False,
write_score_result=False,
infer_details=True,
summary_details=True
):
print("\n ==== Inference setting: ==== ")
if infer_setting == "offline":
print(" > offline inference")
if run_offline_nli == True:
print(" >> run nli scores from scratch")
print(f" >> save nli scores? {write_score_result} {score_dir if write_score_result else ''}")
else:
print(f" >> load saved nli scores from {score_dir}")
else:
print(" > online inference")
# ======== Data ======== #
if 'PLV' in data_dir:
processed_data = pd.read_csv(data_dir, delimiter='\t')
if 'AW' in data_dir:
processed_data = pd.read_csv(data_dir, delimiter='\t')
processed_data.source = processed_data.source.apply(lambda x: x.split(":")[-1])
processed_data.target = processed_data.target.apply(lambda x: x.split(":")[-1])
processed_data = processed_data[['gold_binary','event_type', 'sentence', 'source', 'target']]
# ======== Prompts ======== #
df_prompt = pd.read_csv(prompt_dir, header=0, delimiter='\t')
df_prompt.pentacode = df_prompt.pentacode.apply(lambda x: [int(i) for i in x.split(',')] if type(x) == str else x)
# merge some CAMEO Rootcode to PLOVER Rootcode
df_prompt.loc[df_prompt.rootcode.isin(['INVESTIGATE']), "rootcode"] = "ACCUSE"
df_prompt.loc[df_prompt.rootcode.isin(['FIGHT']), "rootcode"] = "ASSAULT"
print("\nPrompt Rootcode unique:")
print(set(df_prompt.rootcode.unique()))
# ======== Modality ======== #
TENSE = df_prompt.columns[2:].to_list()
tense_L1 = 'past'
tense_L2 = TENSE.copy()
apply_level2 = False if [tense_L1] == tense_L2 else True
print("\nTENSE:\t\t", TENSE)
print("tense_L1:\t", tense_L1)
print("tense_L2:\t", tense_L2)
print(f"apply level2?\t {apply_level2}")
# ======== all hypothesis ======== #
prompt_text = df_prompt[TENSE] \
.stack().reset_index().rename(columns={0: 'prompt_text', 'level_0': 'prompt_idx', 'level_1': 'tense'})
root_flatten = df_prompt.rootcode.repeat(len(TENSE)).reset_index(drop=True)
penta_flatten = df_prompt.pentacode.repeat(len(TENSE)).reset_index(drop=True)
df_prompt_flatten = pd.concat([root_flatten, penta_flatten, prompt_text], axis=1)
df_prompt_flatten = df_prompt_flatten[df_prompt_flatten.prompt_text != "None"].reset_index(drop=True)
print(f"\nall prompts flatten:\n{df_prompt_flatten.iloc[np.r_[0:4, -4:0]].to_string()}")
if infer_setting == "offline":
print('\nStart offline inference...')
if run_offline_nli:
# ======== model ======== #
print("\nLoading models...")
tokenizer, nli_model = load_nli_model(model_name)
# ======== Run ======== #
print("\nRun inference on all prompts...")
result = []
for index in range(len(processed_data)):
print(f"{index+1}/{len(processed_data)}", end='\r')
sentence = processed_data.loc[index, "sentence"]
s = processed_data.loc[index, "source"]
t = processed_data.loc[index, "target"]
result.append(run_all_nli_prompt(sentence, s, t, df_prompt_flatten, tokenizer, nli_model))
result = np.stack(result, axis=0)
if write_score_result:
print(f"\nSave scores for offline analysis at {score_dir}")
with open(score_dir, 'wb') as f:
np.save(f, result)
# ======== Offline inference ======== #
saved_score = result
print(f"\nOffline inference from the NLI scores we just got...")
else:
# ======== Offline inference ======== #
saved_score = np.load(score_dir)
print(f"\nOffline inference from the saved scores at {score_dir}")
out_df = inference(processed_data,
apply_level2=apply_level2,
tense_L1=tense_L1,
tense_L2=tense_L2,
df_prompt_flatten=df_prompt_flatten,
online=False,
saved_score=saved_score,
tokenizer=None,
nli_model=None,
consult_penalty=consult_penalty,
log=infer_details,
peace_overwrite=True,
blockade_overwrite=True,
conflict_overwrite=True,
expel_overwrite=True)
else:
# ======== model ======== #
print('\nStart online inference...')
print("\nLoading models...")
tokenizer, nli_model = load_nli_model(model_name)
out_df = inference(processed_data,
apply_level2=apply_level2,
tense_L1=tense_L1,
tense_L2=tense_L2,
df_prompt_flatten=df_prompt_flatten,
online=True,
saved_score=None,
tokenizer=tokenizer,
nli_model=nli_model,
consult_penalty=consult_penalty,
log=infer_details,
peace_overwrite=True,
blockade_overwrite=True,
conflict_overwrite=True,
expel_overwrite=True)
print(f"\n\nSaving to {output_dir}")
if output_dir:
out_df.to_csv(output_dir, index=False)
print("\n\n ===== Summary ===== \n")
summary = print_result(out_df, 'L1', summary_details)
if apply_level2:
summary.update(print_result(out_df, 'L2', summary_details))
return out_df, summary
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, default="./datasets/PLV_test.tsv")
parser.add_argument("--prompt_dir", type=str, default="./prompts/Tree.txt")
parser.add_argument("--score_dir", type=str, default="./scores/PLV_test-Tree.npy")
parser.add_argument("--model_name", default="roberta-large-mnli", type=str)
parser.add_argument("--consult_penalty", default=0.02, type=float)
parser.add_argument("--infer_setting", type=str, choices=['online', 'offline'])
parser.add_argument("--run_offline_nli", default=False, type=str2bool)
parser.add_argument("--write_score_result", default=False, type=str2bool)
parser.add_argument("--output_dir", type=str, default="./outputs/PLV_test-Tree-result.csv")
parser.add_argument("--infer_details", type=str2bool, default=True)
parser.add_argument("--summary_details", type=str2bool, default=True)
args = parser.parse_args()
print(args)
_, _ = main(data_dir=args.data_dir,
prompt_dir=args.prompt_dir,
score_dir=args.score_dir,
model_name=args.model_name,
output_dir=args.output_dir,
consult_penalty=args.consult_penalty,
infer_setting=args.infer_setting,
run_offline_nli=args.run_offline_nli,
write_score_result=args.write_score_result,
infer_details=args.infer_details,
summary_details=args.summary_details)