xdyu commited on
Commit
466916b
•
1 Parent(s): 8d3b4d7

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ data/math/test_dump_gsm8k_train_perturbed_with_new_questions_answer_few_shot_cot.json filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Reasoning Eval
3
+ emoji: 🌖
4
+ colorFrom: indigo
5
+ colorTo: green
6
+ sdk: static
7
+ pinned: false
8
+ license: apache-2.0
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
data/math/test_dump_gsm8k_train_perturbed_with_new_questions.json ADDED
The diff for this file is too large to render. See raw diff
 
data/math/test_dump_gsm8k_train_perturbed_with_new_questions_answer.json ADDED
The diff for this file is too large to render. See raw diff
 
data/math/test_dump_gsm8k_train_perturbed_with_new_questions_answer_few_shot_cot.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:67878a26bc8e1522f1533aa6e5bd8691d6f446db6a183375efb098b85bf01d26
3
+ size 10616552
index.html ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width" />
6
+ <title>My static Space</title>
7
+ <link rel="stylesheet" href="style.css" />
8
+ </head>
9
+ <body>
10
+ <div class="card">
11
+ <h1>Welcome to your static Space!</h1>
12
+ <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
+ <p>
14
+ Also don't forget to check the
15
+ <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
+ </p>
17
+ </div>
18
+ </body>
19
+ </html>
run_program.py ADDED
@@ -0,0 +1,280 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ import csv
4
+ from io import StringIO
5
+ from contextlib import redirect_stdout
6
+ from openai import OpenAI
7
+ from tqdm import tqdm
8
+
9
+
10
+ client = OpenAI(
11
+ organization='org-GOLjWTjRYGCBi47VKNk0jhMm',
12
+ project='proj_YotrLJgFnsq9wXOXBYaLjnO4',
13
+ api_key='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'
14
+ )
15
+
16
+
17
+ program_data = []
18
+
19
+ PROMPT_DICT = {
20
+ "prompt_new_parameter_value": (
21
+ "Here is a math question with the parameter and parameter values. Please perturb the value of parameters into different values. Output five kinds of new values in the same format as the given parameters in five lines without index.\n\n"
22
+ "Question:\n{question}\n\nParameters:\n{parameters}\n\n"
23
+ ),
24
+ "prompt_rewrite_question": (
25
+ "Here is a math question with old parameter values, and five kinds of new parameter values. Please rewrite the question five times to update all the parameters from old value to each corresponding new value in five lines without index.\n\n"
26
+ "Question: :\n{question}\n\nOld Parameters:\n{parameters}\n\nNew Parameters:\n{new_parameters}\n\nNew Question:"
27
+ ),
28
+ "prompt_answer_question": (
29
+ "Answer the math question below. Only output the answer without units.\n\n"
30
+ "Question:\n{question}\n\nAnswer:"
31
+ ),
32
+ "prompt_answer_question_few_shot_cot": (
33
+ "Q: There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today?\n"
34
+ "A: There are 15 trees originally. Then there were 21 trees after some more were planted. So there must have been 21 - 15 = 6. The answer is 6.\n\n"
35
+ "Q: If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot?\n"
36
+ "A: There are originally 3 cars. 2 more cars arrive. 3 + 2 = 5. The answer is 5.\n\n"
37
+ "Q: Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?\n"
38
+ "A: Originally, Leah had 32 chocolates. Her sister had 42. So in total they had 32 + 42 = 74. After eating 35, they had 74 - 35 = 39. The answer is 39.\n\n"
39
+ "Q: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?\n"
40
+ "A: Jason started with 20 lollipops. Then he had 12 after giving some to Denny. So he gave Denny 20 - 12 = 8. The answer is 8.\n\n"
41
+ "Q: Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?\n"
42
+ "A: Shawn started with 5 toys. If he got 2 toys each from his mom and dad, then that is 4 more toys. 5 + 4 = 9. The answer is 9.\n\n"
43
+ "Q: There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?\n"
44
+ "A: There were originally 9 computers. For each of 4 days, 5 more computers were added. So 5 * 4 = 20 computers were added. 9 + 20 is 29. The answer is 29.\n\n"
45
+ "Q: Michael had 58 golf balls. On tuesday, he lost 23 golf balls. On wednesday, he lost 2 more. How many golf balls did he have at the end of wednesday?\n"
46
+ "A: Michael started with 58 golf balls. After losing 23 on tuesday, he had 58 - 23 = 35. After losing 2 more, he had 35 - 2 = 33 golf balls. The answer is 33.\n\n"
47
+ "Q: Olivia has $23. She bought five bagels for $3 each. How much money does she have left?\n"
48
+ "A: Olivia had 23 dollars. 5 bagels for 3 dollars each will be 5 x 3 = 15 dollars. So she has 23 - 15 dollars left. 23 - 15 is 8. The answer is 8.\n\n"
49
+ "Q: {question}\n"
50
+ "A: "
51
+ )
52
+ }
53
+
54
+ def call_function_with_args(parameter):
55
+ # outstr = '\noutput = answer('
56
+ # for parameter in case['parameter_value']:
57
+ # para_name = parameter.split(':')[0]
58
+ # if ': str' not in parameter:
59
+ # value = str(case['parameter_value'][parameter])
60
+ # else:
61
+ # value = '"' + case['parameter_value'][parameter] + '"'
62
+ # # print(para_name, value)
63
+ # outstr += para_name + '=' + value + ', '
64
+ # outstr += ')\nprint(output)'
65
+
66
+ outstr = '\noutput = answer({})\nprint(output)'.format(parameter)
67
+ return outstr
68
+
69
+
70
+ def perturb_parameter_value(case):
71
+ prompt_template = PROMPT_DICT['prompt_new_parameter_value']
72
+ prompt = prompt_template.format_map(
73
+ {"question": case['question'], "parameters": case['parameters']}
74
+ )
75
+ response = client.chat.completions.create(
76
+ model="gpt-4o",
77
+ messages=[
78
+ {"role": "system", "content": "You are a helpful assistant."},
79
+ {"role": "user", "content": prompt}
80
+ ]
81
+ )
82
+ return response
83
+
84
+
85
+ def clear_parameter_output(response):
86
+ response = response.replace('Parameters:', '').replace('```', '').replace('- ', '').strip().split('\n')
87
+ for idx, para in enumerate(response):
88
+ para.replace(str(idx)+'. ', '').strip()
89
+ response = [x for x in response if len(x) > 0]
90
+ return response
91
+
92
+ def generate_new_parameter_value():
93
+ infile = open('/shared/xdyu/msr/reasoning_hallucination/data/math/test_dump_gsm8k_train_perturbed.json', 'r')
94
+ program_data = json.load(infile)
95
+
96
+ correct_counter = 0
97
+ compile_fail_counter = 0
98
+
99
+ correct_case = []
100
+
101
+ for case in program_data:
102
+ program = case['candidate_programs'][0]
103
+ program += call_function_with_args(case['parameters'])
104
+ # print(program)
105
+ try:
106
+ f = StringIO()
107
+ with redirect_stdout(f):
108
+ exec(program)
109
+ s = f.getvalue().strip()
110
+ if round(float(s)) == round(float(case['answer'])):
111
+ correct_counter += 1
112
+ correct_case.append(case)
113
+ # else:
114
+ # print(s)
115
+ # print(case['answer'])
116
+ except Exception as e:
117
+ print(e)
118
+ print(program)
119
+ compile_fail_counter += 1
120
+ print(correct_counter)
121
+
122
+ for case in tqdm(correct_case):
123
+ if 'new_answers' in case and len(case['new_answers']) == 5:
124
+ continue
125
+ response = perturb_parameter_value(case)
126
+ new_values = clear_parameter_output(response.choices[0].message.content)
127
+ if len(new_values) == 6:
128
+ new_values = new_values[1:]
129
+ case['new_parameters'] = new_values
130
+ try:
131
+ case['new_answers'] = []
132
+ for parameter in case['new_parameters']:
133
+ program = case['candidate_programs'][0]
134
+ program += call_function_with_args(parameter)
135
+ f = StringIO()
136
+ with redirect_stdout(f):
137
+ exec(program)
138
+ s = f.getvalue().strip()
139
+ case['new_answers'].append(s)
140
+ except Exception as e:
141
+ print(e)
142
+ print(new_values)
143
+ continue
144
+ # break
145
+ outfile = open('data/math/test_dump_gsm8k_train_perturbed.json', 'w')
146
+ json.dump(correct_case, outfile, indent=4)
147
+
148
+
149
+ def rewrite_question(case):
150
+ prompt_template = PROMPT_DICT['prompt_rewrite_question']
151
+ prompt = prompt_template.format_map(
152
+ {"question": case['question'], "parameters": case['parameters'], "new_parameters": "\n".join(case['new_parameters'])}
153
+ )
154
+ response = client.chat.completions.create(
155
+ model="gpt-4o",
156
+ messages=[
157
+ {"role": "system", "content": "You are a helpful assistant."},
158
+ {"role": "user", "content": prompt}
159
+ ]
160
+ )
161
+ return response
162
+
163
+
164
+ def update_question_with_new_parameters():
165
+ infile = open('/shared/xdyu/msr/reasoning_hallucination/data/math/test_dump_gsm8k_train_perturbed.json', 'r')
166
+ program_data = json.load(infile)
167
+ print(len(program_data))
168
+ for case in tqdm(program_data):
169
+ response = rewrite_question(case)
170
+ new_values = [x.strip() for x in response.choices[0].message.content.split('\n') if len(x) > 0]
171
+ # print(new_values)
172
+ if len(new_values) == 6:
173
+ new_values = new_values[1:]
174
+ case['new_questions'] = new_values
175
+ # print(case)
176
+ # break
177
+ outfile = open('data/math/test_dump_gsm8k_train_perturbed_with_new_questions.json', 'w')
178
+ json.dump(program_data, outfile, indent=4)
179
+
180
+
181
+ def call_answer_question(question):
182
+ prompt_template = PROMPT_DICT['prompt_answer_question_few_shot_cot']
183
+ prompt = prompt_template.format_map(
184
+ {"question": question}
185
+ )
186
+ # print(prompt)
187
+ response = client.chat.completions.create(
188
+ model="gpt-4o",
189
+ messages=[
190
+ {"role": "system", "content": "You are a helpful assistant."},
191
+ {"role": "user", "content": prompt}
192
+ ],
193
+ temperature=0,
194
+ max_tokens=300,
195
+ top_p=1
196
+ )
197
+ return response
198
+
199
+
200
+ def answer_question():
201
+ infile = open('/shared/xdyu/msr/reasoning_hallucination/data/math/test_dump_gsm8k_train_perturbed_with_new_questions.json', 'r')
202
+ program_data = json.load(infile)
203
+ print(len(program_data))
204
+ for case in tqdm(program_data):
205
+ response = call_answer_question(case['question'])
206
+ case['prediction'] = response.choices[0].message.content
207
+ # print(case['prediction'])
208
+ case['new_prediction'] = []
209
+ for question in case['new_questions']:
210
+ response = call_answer_question(question)
211
+ case['new_prediction'].append(response.choices[0].message.content)
212
+ # print(case)
213
+ # break
214
+ # print(case)
215
+ # break
216
+ outfile = open('data/math/test_dump_gsm8k_train_perturbed_with_new_questions_answer_few_shot_cot.json', 'w')
217
+ json.dump(program_data, outfile, indent=4)
218
+
219
+
220
+ def parse_answer(answer):
221
+ if 'answer is' in answer:
222
+ answer = answer.split('answer is')[-1].strip()
223
+ else:
224
+ answer = answer.split(' ')[-1]
225
+ if len(answer) > 0 and answer[-1] == '.':
226
+ answer = answer[0:-1]
227
+ answer = answer.replace('$','').replace(',','').strip()
228
+ return answer
229
+
230
+ def evaluator(infile_path):
231
+ infile = open(infile_path, 'r')
232
+ data = json.load(infile)
233
+ correct_case = 0
234
+ total_case = 0
235
+ total_percentage = 0
236
+ new_parameter_correct_counter = {}
237
+ for case in data:
238
+ if 'new_answers' not in case or len(case['new_answers']) != len(case['new_prediction']):
239
+ continue
240
+ total_case += 1
241
+ prediction = parse_answer(case['prediction'])
242
+ if prediction == case['answer'] or case['answer'] in prediction:
243
+ correct_case += 1
244
+ new_parameter_correct_case = 0
245
+ for idx, pred in enumerate(case['new_prediction']):
246
+ parsed_pred = parse_answer(pred)
247
+ if parsed_pred == case['new_answers'][idx] or case['new_answers'][idx] in parsed_pred:
248
+ new_parameter_correct_case += 1
249
+ else:
250
+ try:
251
+ parsed_pred = round(float(parsed_pred))
252
+ new_answer = round(float(case['new_answers'][idx]))
253
+ if parsed_pred == new_answer:
254
+ new_parameter_correct_case += 1
255
+ else:
256
+ print(parsed_pred, case['new_answers'][idx])
257
+ except:
258
+ continue
259
+ total_parameter_correct_case = len(case['new_prediction'])
260
+ percentage = float(new_parameter_correct_case / total_parameter_correct_case)
261
+ total_percentage += percentage
262
+ if new_parameter_correct_case not in new_parameter_correct_counter:
263
+ new_parameter_correct_counter[new_parameter_correct_case] = 0
264
+ new_parameter_correct_counter[new_parameter_correct_case] += 1
265
+
266
+ # else:
267
+ # print(prediction, case['answer'])
268
+ print(correct_case, total_case, correct_case/total_case)
269
+ print(total_percentage/correct_case)
270
+ print(new_parameter_correct_counter)
271
+
272
+ def main():
273
+ # generate_new_parameter_value()
274
+ # update_question_with_new_parameters()
275
+ # answer_question()
276
+ evaluator('data/math/test_dump_gsm8k_train_perturbed_with_new_questions_answer_few_shot_cot.json')
277
+
278
+
279
+ if __name__ == "__main__":
280
+ main()
style.css ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ padding: 2rem;
3
+ font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
4
+ }
5
+
6
+ h1 {
7
+ font-size: 16px;
8
+ margin-top: 0;
9
+ }
10
+
11
+ p {
12
+ color: rgb(107, 114, 128);
13
+ font-size: 15px;
14
+ margin-bottom: 10px;
15
+ margin-top: 5px;
16
+ }
17
+
18
+ .card {
19
+ max-width: 620px;
20
+ margin: 0 auto;
21
+ padding: 16px;
22
+ border: 1px solid lightgray;
23
+ border-radius: 16px;
24
+ }
25
+
26
+ .card p:last-child {
27
+ margin-bottom: 0;
28
+ }