You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

assistantbench_evaluator.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # This Script is slightly modified from the creators of the AssistantBench dataset https://huggingface.co/spaces/AssistantBench/leaderboard/blob/main/evaluation/evaluator.py
  2. import json
  3. from evaluate_utils.evaluate_factory import get_evaluator
  4. import numpy as np
  5. def find_isnan(samp):
  6. try:
  7. if np.isnan(samp):
  8. return True
  9. else:
  10. return False
  11. except:
  12. return False
  13. def fix_ans(answer):
  14. try:
  15. answer = answer.replace("{'", '{"').replace("', '", '", "').replace("': '", '": "').replace("'}", '"}')
  16. answer = answer.replace("': ", '": ')
  17. return answer
  18. except:
  19. return answer
  20. def parse_answer(answer):
  21. if len(answer) == 1:
  22. ans, is_num = fix_number(answer[0])
  23. if is_num:
  24. return ans, "number"
  25. try:
  26. ans = json.loads(fix_ans(answer[0]))
  27. return [ans], "json"
  28. except:
  29. ans, is_num = fix_number(answer[0])
  30. if is_num:
  31. return ans, "number"
  32. else:
  33. return answer[0], "string"
  34. else:
  35. try:
  36. ans = [json.loads(fix_ans(ex)) for ex in answer]
  37. return ans, "json"
  38. except:
  39. return answer, "string list"
  40. def fix_number(number):
  41. if type(number) == str:
  42. copy_ans = number
  43. copy_ans = " ".join(" ".join(" ".join(copy_ans.split("$")).split("%")).split("sqft")).strip()
  44. copy_ans = copy_ans.strip()
  45. copy_ans = copy_ans.replace(",", ".").replace(" square kilometers", "")
  46. try:
  47. return float(copy_ans), True
  48. except:
  49. return number, False
  50. elif type(number) == int:
  51. return float(number), True
  52. else:
  53. return number, True
  54. def fix_prediction(prediction, gold_answer, evaluator):
  55. if (
  56. type(prediction) == list
  57. and len(prediction) == 1
  58. and (type(prediction[0]) == int or ((type(prediction[0]) == str) and prediction[0].isnumeric()))
  59. ):
  60. prediction = fix_number(prediction[0])
  61. if type(prediction) != list:
  62. prediction, is_num = fix_number(prediction)
  63. if evaluator == "json":
  64. try:
  65. prediction = [json.loads(pred) for pred in prediction.split("\n")]
  66. except:
  67. prediction = [prediction]
  68. if (hasattr(type(prediction), "__len__")) and (len(prediction) == 0):
  69. return prediction, False
  70. if (type(prediction) == list and len(prediction) > 1) and type(gold_answer) == float:
  71. return prediction, False
  72. return prediction, True
  73. def question_scorer(prediction, gold_answer):
  74. """
  75. prediction: str or list of str
  76. gold_answer: str or list of str
  77. returns a float between 0 and 1
  78. """
  79. try:
  80. try:
  81. prediction = json.loads(prediction)
  82. except:
  83. prediction = prediction
  84. answer_list = (
  85. [x for x in gold_answer.split("\n") if len(x.strip()) > 0] if type(gold_answer) != list else gold_answer
  86. )
  87. gold_answer, evaluator = parse_answer(answer_list)
  88. prediction, run_eval = fix_prediction(prediction, gold_answer, evaluator)
  89. has_ans = 1.0
  90. if (type(prediction) != float and len(prediction) == 0) or find_isnan(prediction):
  91. has_ans = 0.0
  92. if not run_eval:
  93. return 0.0
  94. metric_eval = get_evaluator(evaluator)
  95. accuracy = metric_eval(prediction, gold_answer)
  96. # double check if the accuracy is a number between 0 and 1
  97. if 0 <= accuracy <= 1:
  98. return accuracy
  99. else:
  100. # throw exception
  101. raise ValueError(f"Accuracy should be a float between 0 and 1, but got {accuracy}")
  102. except Exception as e:
  103. print(
  104. f"Something went wrong while evaluating prediction {prediction} vs gold answer {gold_answer} with error {e}"
  105. )
  106. return 0.0