-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathmain.py
132 lines (106 loc) · 4.14 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import argparse
import json
import logging
import os
import time
from llm_autoeval.table import make_final_table, make_table
from llm_autoeval.upload import upload_to_github_gist
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
MODEL_ID = os.getenv("MODEL_ID")
BENCHMARK = os.getenv("BENCHMARK")
GITHUB_API_TOKEN = os.getenv("GITHUB_API_TOKEN")
def _make_autoeval_summary(directory: str, elapsed_time: float) -> str:
# Variables
tables = []
averages = []
# Tasks
if BENCHMARK == "openllm":
tasks = ["ARC", "HellaSwag", "MMLU", "TruthfulQA", "Winogrande", "GSM8K"]
elif BENCHMARK == "nous":
tasks = ["AGIEval", "GPT4All", "TruthfulQA", "Bigbench"]
elif BENCHMARK == "eq-bench":
tasks = ["EQ-Bench"]
else:
raise NotImplementedError(
f"The benchmark {BENCHMARK} could not be found."
)
# Load results
for task in tasks:
file_path = f"{directory}/{task.lower()}.json"
if os.path.exists(file_path):
json_data = open(file_path, "r").read()
data = json.loads(json_data, strict=False)
table, average = make_table(data, task)
else:
table = ""
average = "Error: File does not exist"
tables.append(table)
averages.append(average)
# Generate tables
summary = ""
for index, task in enumerate(tasks):
summary += f"### {task}\n{tables[index]}\nAverage: {averages[index]}%\n\n"
result_dict = {k: v for k, v in zip(tasks, averages)}
# Calculate the final average, excluding strings
if all(isinstance(e, float) for e in averages):
final_average = round(sum(averages) / len(averages), 2)
summary += f"Average score: {final_average}%"
result_dict.update({"Average": final_average})
else:
summary += "Average score: Not available due to errors"
# Generate final table
final_table = make_final_table(result_dict, MODEL_ID)
summary = final_table + "\n" + summary
return summary
def _get_result_dict(directory: str) -> dict:
"""Walk down directories to get JSON path"""
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".json"):
return json.load(open(os.path.join(root, file)))
raise FileNotFoundError(f"No JSON file found in {directory}")
def _make_lighteval_summary(directory: str, elapsed_time: float) -> str:
from lighteval.evaluator import make_results_table
result_dict = _get_result_dict(directory)
final_table = make_results_table(result_dict)
summary = f"## {MODEL_ID.split('/')[-1]} - {BENCHMARK.capitalize()}\n\n"
summary += final_table
return summary
def main(directory: str, elapsed_time: float) -> None:
# Tasks
if BENCHMARK == "openllm" or BENCHMARK == "nous" or BENCHMARK == "eq-bench":
summary = _make_autoeval_summary(directory, elapsed_time)
elif BENCHMARK == "lighteval":
summary = _make_lighteval_summary(directory, elapsed_time)
else:
raise NotImplementedError(
f"BENCHMARK should be 'openllm' or 'nous' (current value = {BENCHMARK})"
)
# Add elapsed time
convert = time.strftime("%H:%M:%S", time.gmtime(elapsed_time))
summary += f"\n\nElapsed time: {convert}"
# Upload to GitHub Gist
upload_to_github_gist(
summary,
f"{MODEL_ID.split('/')[-1]}-{BENCHMARK.capitalize()}.md",
GITHUB_API_TOKEN,
)
if __name__ == "__main__":
# Create the parser
parser = argparse.ArgumentParser(description="Summarize results and upload them.")
parser.add_argument(
"directory", type=str, help="The path to the directory with the JSON results"
)
parser.add_argument(
"elapsed_time",
type=float,
help="Elapsed time since the start of the evaluation",
)
# Parse the arguments
args = parser.parse_args()
# Check if the directory exists
if not os.path.isdir(args.directory):
raise ValueError(f"The directory {args.directory} does not exist.")
# Call the main function with the directory argument
main(args.directory, args.elapsed_time)