-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·226 lines (193 loc) · 7.27 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
# coding: utf-8
import math
import re
import os
import spacy
import json
import numpy as np
import pandas as pd
import copy
from tqdm import tqdm
from Questgen import main
from simpletransformers.seq2seq import Seq2SeqModel, Seq2SeqArgs
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# read fever dataset from csv, including claim and evidence columns
claim_evidence_df = pd.read_csv('./claim_evidence_pairs.csv', encoding='utf8')
# load MCQ/ FAQ questions generator model
qg = main.QGen()
questions = set()
qpayload = {
"input_text": "Katie Stevens is Australian."
}
out_dir = "questions/"
os.makedirs("questions/", exist_ok=True)
claim_question = {}
count=0
for claim in tqdm(claim_evidence_df['claim'][count:500]):
payload = copy.deepcopy(qpayload)
payload['input_text'] = claim
# generate MCQ questions
output = qg.predict_mcq(payload)
if len(output) != 0:
for q in output['questions']:
questions.add(q['question_statement'])
# generate FAQ questions
output = qg.predict_shortq(payload)
if len(output) != 0:
for q in output['questions']:
questions.add(q['Question'])
try:
claim_question[str(count)+"_"+claim] = list(dict.fromkeys(questions))
except:
print(claim, list(dict.fromkeys(questions)))
questions = set()
count=count+1
div,mod = divmod(count, 100)
if mod == 0:
with open(out_dir+"question_"+str(div)+".json", "w") as outfile:
json.dump(claim_question, outfile)
claim_question = {}
with open(out_dir+"question_"+str(div+1)+".json", "w") as outfile:
json.dump(claim_question, outfile)
claim_question = {}
Questions_df = pd.read_json("questions/question_1.json", orient ='index', encoding='utf8')
for i in range(2,6):
df1 = pd.read_json("questions/question_"+str(i)+".json", orient ='index', encoding='utf8')
Questions_df = pd.concat([Questions_df, df1])
Questions_df = Questions_df.reset_index()
question_num = len(Questions_df.columns)-1
question_col = ['q1','q2','q3','q4','q5','q6','q7','q8','q9','q10','q11','q12','q13','q14','q15','q16','q17']
Questions_df.columns = ['claim'] + question_col[:question_num]
Questions_df["claim"] = Questions_df["claim"].str.replace("“","").replace("”","").replace(r"\d+_","", regex=True)
qc_df = Questions_df
qc_df = qc_df.fillna("None")
answer = main.AnswerPredictor()
out_dir = "QAs/"
os.makedirs(out_dir, exist_ok=True)
QA={
"question": "",
"answer":""
}
payload = {
"input_text" : '''At its greatest extent , the state expanded into territories that today comprise most of Iran , Iraq , Armenia , Azerbaijan , Georgia , Turkmenistan , Turkey , western Afghanistan , and southwestern Pakistan .''',
"input_question" : "Who is the husband of Randy Couture?"
}
output_dic = {}
Answer_dic = {}
count = 0
for i, evidence in tqdm(enumerate(claim_evidence_df["evidence"][count:500])):
# truncate the text into the length of 512
payload["input_text"] = evidence[:512]
Answer_row = []
for q in qc_df.iloc[count+i][1:]:#q1 start from 1th column
payload["input_question"] = q
if q == "None":
a = "None"
else:
a = answer.predict_answer(payload)
copy_QA = copy.deepcopy(QA)
copy_QA["question"] = q
copy_QA["answer"] = a
Answer_row.append(copy_QA)
copy_Answer_dic = copy.deepcopy(Answer_dic)
copy_Answer_dic["evidence"] = evidence
copy_Answer_dic["claim"] = qc_df.iloc[count+i]["claim"]
copy_Answer_dic["QA"] = Answer_row
output_dic[count+i] = copy_Answer_dic
div, mod = divmod(count+i+1,100)
if mod == 0:
with open(out_dir+"output_dic_"+str(div)+".json", "w") as outfile:
json.dump(output_dic, outfile)
output_dic = {}
with open(out_dir+"output_dic_"+str(div+1)+".json", "w") as outfile:
json.dump(output_dic, outfile)
output_dic = {}
df = pd.read_json("QAs/output_dic_1.json", orient ='index')
for i in range(2,6):
df1 = pd.read_json("QAs/output_dic_"+str(i)+".json", orient ='index')
df = pd.concat([df, df1])
#Entailment Checker (picking the best answer)
tokenizer = AutoTokenizer.from_pretrained("roberta-large-mnli")
model = AutoModelForSequenceClassification.from_pretrained("roberta-large-mnli")
answers = []
no_accpect = ["TRUE", "yes", "FALSE", "True", "Yes", "False", "No", "None"]
os.makedirs("best_answers/", exist_ok=True)
for i, claim in tqdm(enumerate(df["claim"][:])):
ans_strings = []
in_strings = []
#in_strings_backup = []
for qa in df.iloc[i]["QA"]:
if qa["question"] != None and qa["answer"] not in no_accpect:
string = claim + "</s></s>" + qa["answer"]
ans_strings.append(qa["answer"])
in_strings.append(string)
if len(in_strings) !=0:
inputs = tokenizer(in_strings, padding=True,return_tensors="pt")
outputs = model(**inputs)
outputs_np = outputs[0].detach().numpy()
max_contra_id = np.argmax(outputs_np.T[0], axis=0)
answers.append(ans_strings[max_contra_id])
else:
answers.append(" ")
div, mod = divmod(i+1,100)
if mod == 0:
answers_df = pd.DataFrame(answers)
answers = []
answers_df.to_csv("best_answers/output_"+str(div)+".csv")
answers_df = pd.DataFrame(answers)
answers = []
answers_df.to_csv("best_answers/output_"+str(div+1)+".csv")
df = pd.read_json("QAs/output_dic_1.json", orient ='index')
for i in range(2,6):
df1 = pd.read_json("QAs/output_dic_"+str(i)+".json", orient ='index')
df = pd.concat([df, df1])
df2 = pd.read_csv("best_answers/output_1.csv", names=['best answer'], header=0)
for i in range(2,6):
df1 = pd.read_csv("best_answers/output_"+str(i)+".csv", names=['best answer'], header=0)
df2 = pd.concat([df2, df1], ignore_index=True)
df = df.join(df2)
QA_df = df
for i in range(question_num):
questions = []
answers = []
for qa in QA_df["QA"]:
answers.append(qa[i]["answer"])
questions.append(qa[i]["question"])
QA_df["Q"+str(i+1)] = questions
QA_df["A"+str(i+1)] = answers
model_args = Seq2SeqArgs()
model_args.max_length = 1000
QA2D_model = Seq2SeqModel(
encoder_decoder_type="bart",
encoder_decoder_name="./QA2D_model",
cuda_device=0,
args=model_args)
r = re.compile(r'[.]$')
df = df.replace(np.nan, '', regex=True)
d = {}
for i in range(question_num):
d["A"+str(i+1)] = "Q"+str(i+1)
counta =0
output = []
results = []
wrong_id = []
nlp = spacy.load("en_core_web_sm")
for index, row in df.iterrows():
counta =0
for answer_col in d:
if row["best answer"] == row[answer_col]:
doc = nlp(row["best answer"])
sent = False
for token in doc:
if token.dep_ == "nsubj":
sent = True
break
if r.search(row["best answer"]) != None and sent == True:
results.append(row["best answer"].replace("Yes, ", "").capitalize())
else:
results.append(QA2D_model.predict([row[d[answer_col]] + ' [SEP] ' + row["best answer"]])[0])
break
results_df = pd.DataFrame(results)
results_df.columns = ['explanation']
QA_df.join(results_df).to_csv("explanation_all.csv")