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

Function answer_clean

accessory/eval_mm/utils/math_utils.py:332–392  ·  view source on GitHub ↗
(dataset: str, direct_answer_trigger_for_fewshot: tuple, pred: str)

Source from the content-addressed store, hash-verified

330
331
332def answer_clean(dataset: str, direct_answer_trigger_for_fewshot: tuple, pred: str):
333 if dataset == "math":
334 if len(pred) > 0:
335 pred_final=extract_math_answer(pred)
336 return pred_final
337 else:
338 return pred
339
340 # Determine if this is ICL, if so, use \n\n to split the first chunk.
341 ICL = False
342 for trigger in direct_answer_trigger_for_fewshot:
343 if pred.count(trigger) > 1:
344 ICL = True
345 if ICL:
346 pred = pred.split('\n\n')[0]
347
348 # Split the trigger to find the answer.
349 preds = re.split('|'.join(direct_answer_trigger_for_fewshot), pred)
350 answer_flag = True if len(preds) > 1 else False
351 pred = preds[-1]
352
353 if '=' in pred:
354 pred = pred.split('=')[-1].strip()
355
356 if dataset in ("aqua", "sat", "mmlu_mathematics", "mmlu_physics", "mmlu_chemistry", "mmlu_biology"):
357 tmp = re.findall(r'\b(A|B|C|D|E)\b', pred.upper())
358 if tmp:
359 pred = tmp
360 else:
361 pred = [pred.strip().strip('.')]
362 elif dataset in ("gsm8k", "svamp", "deepmind", "simuleq"):
363 pred = pred.replace(",", "")
364 pred = [delete_extra_zero(s.replace(",", "")) for s in re.findall(r'-?\d+/?\.?\d*', pred)]
365 elif dataset in ("numglue",):
366 tmp = re.findall(r'\b(A|B|C|D|E)\b', pred.upper())
367 if tmp:
368 pred = tmp
369 else:
370 pred = pred.replace(",", "")
371 pred = [delete_extra_zero(s.replace(",", "")) for s in re.findall(r'-?\d+/?\.?\d*', pred)]
372 else:
373 raise ValueError("dataset is not properly defined ...")
374
375 # If there is no candidate in list, null is set.
376 if len(pred) == 0:
377 pred = ""
378 else:
379 if answer_flag:
380 # choose the first element in list ...
381 pred = pred[0]
382 else:
383 # choose the last e
384 pred = pred[-1]
385
386 # (For arithmetic tasks) if a word ends with period, it will be omitted ...
387 if pred != "":
388 if pred[-1] == ".":
389 pred = pred[:-1]

Callers

nothing calls this directly

Calls 2

extract_math_answerFunction · 0.85
delete_extra_zeroFunction · 0.85

Tested by

no test coverage detected