-
Notifications
You must be signed in to change notification settings - Fork 3
/
postprocess_responses.py
261 lines (251 loc) · 10.4 KB
/
postprocess_responses.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import re
import pandas as pd
from helper_functions import discard_text_after_answer, parse_file_name
import sys
import numpy as np
import os
import json
from collections import defaultdict
"""Takes a llm_output.csv file from an LLM run and parses out the answer and saves as parsed_llm_output.csv"""
"""
* Copyright 2024 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
"""
def process_raw_response(row):
response = row["raw_response"]
if response is np.nan:
return "none"
if "the answer is no" in response.lower(): # for yes or no question e.g. navigate
return "No"
elif "the answer is yes" in response.lower():
return "Yes"
if "the only option" in response.lower():
start_index = response.lower().rfind("the only option")
response = response[start_index:]
elif "the closest might be" in response.lower():
start_index = response.lower().rfind("the closest might be")
response = response[start_index:]
elif "the closest match is" in response.lower():
start_index = response.lower().rfind("the closest match is")
response = response[start_index:]
elif "none of the options" in response.lower():
return "none"
elif "none of the given options" in response.lower():
return "none"
elif "it is not any of the given options" in response.lower():
return "none"
elif "here is no correct answer from the provided options" in response.lower():
return "none"
elif "there might be a mistake in the question" in response.lower():
return "none"
elif "seems to be a mistake in the question" in response.lower():
return "none"
elif "there could be a mistake in the provided" in response.lower():
return "none"
elif "might be an error in the question" in response.lower():
return "none"
elif "might be a mistake in the provided" in response.lower():
return "none"
elif (
"seems to be a mistake in the paragraph or the options provided"
in response.lower()
):
return "none"
elif "does not exactly match any of the given options" in response.lower():
return "none"
elif "no single correct answer among the options" in response.lower():
return "none"
elif "answer cannot be determined based on the given options" in response.lower():
return "none"
elif "the given options cannot be correct" in response.lower():
return "none"
elif (
"the question cannot be answered with the given information" in response.lower()
):
return "none"
elif "does not fit any of the given options" in response.lower():
return "none"
elif "the answer is not among the provided options" in response.lower():
return "none"
elif "answer is not available in the given options" in response.lower():
return "none"
elif "is not represented in the given options" in response.lower():
return "none"
elif "does not represent any of the given options" in response.lower():
return "none"
elif "does not match any of the given options" in response.lower():
return "none"
elif (
"it is not possible to accurately select an answer from the given options"
in response.lower()
):
return "none"
elif "the answer is not available in the given options" in response.lower():
return "none"
elif "answer is not among the options" in response.lower():
return "none"
elif (
"there is no exact match for this path in the provided options"
in response.lower()
):
return "none"
elif "the answer is not provided in the option" in response.lower():
return "none"
elif "it's impossible to determine what shape this path" in response.lower():
return "none"
elif "none of the options are humorous edits" in response.lower():
return "none"
elif "there is no humorous edit among the given options" in response.lower():
return "none"
elif ("**1." in response) and (
"**2." in response
): # the model tries to solve all examples including few shot
start_index = response.find("**6.")
response = response[start_index:]
elif (
("1." in response)
and ("2." in response)
and ("3." in response)
and ("4." in response)
and ("5." in response)
and ("6." in response)
):
start_index = response.rfind("6.")
if start_index == -1:
start_index = response.rfind("5.")
response = response[start_index:]
elif (
("1." in response)
and ("2." in response)
and ("3." in response)
and ("4." in response)
and ("5." in response)
):
start_index = response.rfind("5.")
if start_index == -1:
start_index = response.rfind("4.")
response = response[start_index:]
elif (
("1." in response)
and ("2." in response)
and ("3." in response)
and ("4." in response)
):
start_index = response.rfind("4.")
if start_index == -1:
start_index = response.rfind("3.")
response = response[start_index:]
elif "answer:" in response.lower():
start_index = response.lower().rfind("answer:")
response = response[start_index:]
if "no, the response is not risky" in response.lower():
return "no"
elif "yes, the response is risky" in response.lower():
return "yes"
try:
pred = re.findall("(?<=answer is indeed )(\S*)", response)[-1]
pred = discard_text_after_answer(pred)
return pred
except:
try:
pred = re.findall("(?<= is )(\([ABCDEFGHIJKabcdefghijk]\))", response)[-1]
pred = discard_text_after_answer(pred)
except:
try:
pred = re.findall("(?<=answer is )(\S*)", response)[-1]
pred = discard_text_after_answer(pred)
except:
try:
pred = re.findall(
"(?<= closest match is )(\([ABCDEFGHIJabcdefghij]\))", response
)[-1]
pred = discard_text_after_answer(pred)
except:
try:
pred = re.findall(
"6. (\([ABCDEFGHIJKabcdefghijk]\))\S*", response
)[
-1
] # sometimes it is like 6. (C)
pred = discard_text_after_answer(pred)
except:
try:
pred = re.findall(
"A:The answer is (\([ABCDEFGHIJKabcdefghijk]\))\S*",
response,
)[-1]
pred = discard_text_after_answer(pred)
except:
try:
pred = re.findall(
"(\([ABCDEFGHIJKabcdefghijk]\))\S*", response
)[-1]
pred = discard_text_after_answer(pred)
except:
try:
pred = re.findall(
"([ABCDEFGHIJKabcdefghijk]:)\S*", response
)[-1]
pred = pred.replace(":", "")
pred = "(" + pred + ")"
pred = discard_text_after_answer(pred)
except:
try:
pred = re.findall(
"(?<=answer is )([YESyesNOno]+)", response
)[-1]
pred = discard_text_after_answer(pred)
except:
if (
"so, we need to determine which one is the correct answer."
in response.lower()
):
return "none"
elif "complex shape" in response.lower():
return "none"
def post_process(file_or_dir, failed_parses):
if os.path.isfile(file_or_dir):
if file_or_dir.endswith(".csv"):
print(f"Processing {file_or_dir}")
raw_input_df = pd.read_csv(file_or_dir)
# breakpoint()
raw_input_df["new_extracted_pred"] = raw_input_df.apply(
process_raw_response, axis=1
)
for i, row in raw_input_df.iterrows():
if row["new_extracted_pred"] == "none":
failed_parses[os.path.basename(file_or_dir)].append(
row["raw_response"]
)
parsed_dir = os.path.join(os.path.dirname(file_or_dir), "parsed")
if not os.path.exists(parsed_dir):
print(f"making dir {parsed_dir}")
os.makedirs(parsed_dir)
outfile = os.path.join(
parsed_dir,
os.path.basename(file_or_dir),
)
raw_input_df[["pred", "gt", "raw_response", "new_extracted_pred"]].to_csv(
outfile
)
print(f"Wrote {outfile}")
else:
for sub_dir_or_file in os.listdir(file_or_dir):
post_process(os.path.join(file_or_dir, sub_dir_or_file), failed_parses)
failed_parses = defaultdict(list)
post_process(sys.argv[1], failed_parses)
# for k, v in failed_parses.items():
# print(f"{k} has {len(v)} failed parses below")
# print(json.dumps(v, indent=4))
# print(f"{k} has {len(v)} failed parses above")