-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_output.py
50 lines (39 loc) · 1.6 KB
/
evaluate_output.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
from evaluate import load
from tqdm import tqdm
import argparse
import logging
import glob
import json
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def main():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--data', type=str, nargs="+", required=True)
arg_parser.add_argument('--lang', type=str, default='en')
args = arg_parser.parse_args()
for data_path in tqdm(args.data, desc="Evaluating..."):
eval_files = [file for file in glob.glob(f'{data_path}/*.json')
if "final_step" in file and not "bertscore" in file]
for data_name in eval_files:
with open(data_name, 'r') as f:
data = json.load(f)
bertscore = load("bertscore")
references = data['references']
predictions = data['predictions']
results = bertscore.compute(
predictions=predictions,
references=references,
lang=args.lang,
model_type="allenai/longformer-base-4096",
rescale_with_baseline=True,
verbose=True
)
f1 = sum(results['f1']) / len(results['f1'])
data.update({'bertscore_f1': f1})
logger.info(f"BERTScore F1: {f1}")
data_name = data_name.split('.')[0]
with open(f"{data_name}_bertscore.json", 'w') as f:
json.dump(data, f, indent=4, separators=(',', ': '))
if __name__ == '__main__':
main()