|
| 1 | +import os |
| 2 | +import json |
| 3 | +import re |
| 4 | +import sys |
| 5 | +import argparse |
| 6 | + |
| 7 | + |
| 8 | +def normalize_answer(a): |
| 9 | + # Lower case |
| 10 | + # Trim (left and right) |
| 11 | + # Replace multiple spaces with one space |
| 12 | + # Remove trailing punctuation |
| 13 | + return re.sub(r"[\.\!\?]+$", "", re.sub(r"\s+", " ", a.strip().lower())) |
| 14 | + |
| 15 | + |
| 16 | +def collate(results_dir): |
| 17 | + """ |
| 18 | + Collate the results of running GAIA |
| 19 | +
|
| 20 | + Args: |
| 21 | + results_dir (path): The folder were results were be saved. |
| 22 | + """ |
| 23 | + |
| 24 | + all_results = list() |
| 25 | + max_instances = 0 |
| 26 | + |
| 27 | + for test_id in os.listdir(results_dir): |
| 28 | + test_path = os.path.join(results_dir, test_id) |
| 29 | + |
| 30 | + # Collect the reslts vector |
| 31 | + results = [test_id] |
| 32 | + |
| 33 | + instance = 0 |
| 34 | + instance_dir = os.path.join(test_path, str(instance)) |
| 35 | + while os.path.isdir(instance_dir): |
| 36 | + expected_answer_file = os.path.join(instance_dir, "expected_answer.txt") |
| 37 | + if not os.path.isfile(expected_answer_file): |
| 38 | + # Expected ansewr is missing |
| 39 | + results.append("") |
| 40 | + |
| 41 | + instance += 1 |
| 42 | + instance_dir = os.path.join(test_path, str(instance)) |
| 43 | + continue |
| 44 | + |
| 45 | + expected_answer = "!!!NULL ANSWER!!!" |
| 46 | + with open(expected_answer_file, "rt") as fh: |
| 47 | + expected_answer = fh.read().strip() |
| 48 | + |
| 49 | + console_log_file = os.path.join(instance_dir, "console_log.txt") |
| 50 | + if not os.path.isfile(console_log_file): |
| 51 | + # Console log file missing |
| 52 | + results.append("") |
| 53 | + |
| 54 | + instance += 1 |
| 55 | + instance_dir = os.path.join(test_path, str(instance)) |
| 56 | + continue |
| 57 | + |
| 58 | + with open(console_log_file, "rt") as fh: |
| 59 | + console_log = fh.read() |
| 60 | + |
| 61 | + final_answer = "" |
| 62 | + m = re.search(r"FINAL ANSWER:(.*?)\n", console_log, re.DOTALL) |
| 63 | + if m: |
| 64 | + final_answer = m.group(1).strip() |
| 65 | + |
| 66 | + # print(f"Expected Answer: {expected_answer}\nAutogen Answer: {final_answer}\n") |
| 67 | + |
| 68 | + if normalize_answer(expected_answer) == normalize_answer(final_answer): |
| 69 | + results.append("1") |
| 70 | + else: |
| 71 | + results.append("-1") |
| 72 | + |
| 73 | + instance += 1 |
| 74 | + instance_dir = os.path.join(test_path, str(instance)) |
| 75 | + |
| 76 | + max_instances = max(max_instances, instance) |
| 77 | + |
| 78 | + # Buffer the results |
| 79 | + all_results.append(results) |
| 80 | + |
| 81 | + # Create a header |
| 82 | + header = "TestId" |
| 83 | + for i in range(0, max_instances): |
| 84 | + header += ",Trial" + str(i) |
| 85 | + print(header) |
| 86 | + |
| 87 | + # Print a fully-populated table of results |
| 88 | + for r in all_results: |
| 89 | + while len(r) < max_instances + 1: |
| 90 | + r.append("") |
| 91 | + print(",".join(r)) |
| 92 | + |
| 93 | + |
| 94 | +############################################################################### |
| 95 | +if __name__ == "__main__": |
| 96 | + script_path = os.path.realpath(__file__) |
| 97 | + script_name = os.path.basename(script_path) |
| 98 | + script_dir = os.path.dirname(script_path) |
| 99 | + |
| 100 | + # Path to the default results directory |
| 101 | + # (relative to this script, up on directory, then into the results folder) |
| 102 | + default_results_dir = os.path.realpath( |
| 103 | + os.path.join(script_dir, os.path.pardir, "results", "gaia_validation_level_1__two_agents_gpt4") |
| 104 | + ) |
| 105 | + |
| 106 | + parser = argparse.ArgumentParser( |
| 107 | + description=f""" |
| 108 | +{script_name} will collate the results of the GAIA scenarios and output them to a CSV. The CSV format is as follows: |
| 109 | +
|
| 110 | +TestId, Trial0, Trial1, ..., TrialN |
| 111 | +uuid_1, x_10, x_11, ..., X_1N |
| 112 | +uuid_2, x_20, x_21, ..., X_2N |
| 113 | +... |
| 114 | +uuid_M, x_M0, x_M1, ..., X_MN |
| 115 | +
|
| 116 | +Where uuid_i is the identifier of the ith test question, and x_ij is 1 or -1 depending on if the test passed or failed, respectively. If data for the trial is missing (e.g., due to a runtime error, the value will be an empty string "". |
| 117 | +""".strip(), |
| 118 | + formatter_class=argparse.RawTextHelpFormatter, |
| 119 | + ) |
| 120 | + |
| 121 | + parser.add_argument( |
| 122 | + "scenario", |
| 123 | + nargs="?", |
| 124 | + help="Path to the scenario results. (default: " + default_results_dir + ")", |
| 125 | + default=default_results_dir, |
| 126 | + ) |
| 127 | + args = parser.parse_args() |
| 128 | + collate(args.scenario) |
0 commit comments