-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (62 loc) · 2.7 KB
/
Copy pathmain.py
File metadata and controls
72 lines (62 loc) · 2.7 KB
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
import json
import os
import subprocess
import configue
import fire
from encodeval.eval_tasks import (
EvalConfig,
SequenceClassificationEval,
SequenceRegressionEval,
TokenClassificationEval,
RetrievalEval,
)
def main(config_file: str = None, model_path: str = None):
os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["EVAL_MODEL_PATH"] = model_path
print(f"Evaluating model at path: {model_path}")
eval_config: EvalConfig = configue.load(config_file, sub_path="eval_config")
# Determine the evaluator based on task type
if eval_config.task_type == "SC":
evaluator = SequenceClassificationEval(eval_config)
elif eval_config.task_type == "SR":
evaluator = SequenceRegressionEval(eval_config)
elif eval_config.task_type == "TC":
evaluator = TokenClassificationEval(eval_config)
elif eval_config.task_type == "IR":
evaluator = RetrievalEval(eval_config)
else:
raise ValueError(f"Invalid task type: {eval_config.task_type}")
# Check if results file already exists
if os.path.exists(f"{eval_config.results_dir}/results.json"):
print(f"A results file already exists for this configuration at {eval_config.results_dir}/results.json, skipping evaluation")
exit()
else:
# Run training if needed
if eval_config.tr_args.do_train:
if os.path.exists(eval_config.tr_args.output_dir) and len(os.listdir(eval_config.tr_args.output_dir)) > 0:
print(f"A fine-tuned model already exists for this configuration at {eval_config.tr_args.output_dir}, skipping training")
else:
evaluator.train()
else:
print("Training disabled, skipping training")
# Run evaluation
results = {}
if eval_config.tr_args.do_eval or eval_config.tr_args.do_predict:
if eval_config.tr_args.do_eval:
results["validation"] = evaluator.validate()
if eval_config.tr_args.do_predict:
results["test"] = evaluator.test()
# if os.path.exists(eval_config.tr_args.output_dir):
# subprocess.run(f"rm -r {eval_config.tr_args.output_dir}", shell=True, check=True)
else:
print("Evaluation disabled, skipping evaluation")
exit()
# Save results to file
os.makedirs(eval_config.results_dir, exist_ok=True)
with open(f"{eval_config.results_dir}/results.json", "w") as f:
json.dump(results, f, indent=4)
print(f"Results saved at {eval_config.results_dir}")
print("Evaluation completed")
exit()
if __name__ == "__main__":
fire.Fire(main)