MCPcopy Create free account
hub / github.com/Alpha-VLLM/LLaMA2-Accessory / extract_math_answer

Function extract_math_answer

accessory/eval_mm/utils/math_utils.py:269–329  ·  view source on GitHub ↗
(pred_str)

Source from the content-addressed store, hash-verified

267
268
269def extract_math_answer(pred_str):
270 if('The answer is ' in pred_str):
271 pred = pred_str.split('The answer is ')[-1].strip()
272 elif('the answer is ' in pred_str):
273 pred = pred_str.split('the answer is ')[-1].strip()
274 elif 'boxed' in pred_str:
275 ans = pred_str.split('boxed')[-1]
276 if not ans:
277 return ""
278 if (ans[0] == '{'):
279 stack = 1
280 a = ''
281 for c in ans[1:]:
282 if (c == '{'):
283 stack += 1
284 a += c
285 elif (c == '}'):
286 stack -= 1
287 if (stack == 0): break
288 a += c
289 else:
290 a += c
291 else:
292 a = ans.split('$')[0].strip()
293 a = _strip_string(a)
294 pred=a
295
296 else:
297 pattern = '-?\d*\.?\d+'
298 pred = re.findall(pattern, pred_str)
299 if(len(pred) >= 1):
300 pred = pred[-1]
301 else: pred = ''
302 if pred != "":
303 if pred[-1] == ".":
304 pred = pred[:-1]
305 if pred != "" and pred[-1] == "/":
306 pred = pred[:-1]
307 pred=_strip_string(pred)
308 if 'boxed' in pred:
309 ans = pred.split('boxed')[-1]
310 if not ans:
311 return ""
312 if (ans[0] == '{'):
313 stack = 1
314 a = ''
315 for c in ans[1:]:
316 if (c == '{'):
317 stack += 1
318 a += c
319 elif (c == '}'):
320 stack -= 1
321 if (stack == 0): break
322 a += c
323 else:
324 a += c
325 else:
326 a = ans.split('$')[0].strip()

Callers 1

answer_cleanFunction · 0.85

Calls 1

_strip_stringFunction · 0.85

Tested by

no test coverage detected