MCPcopy Create free account
hub / github.com/InternScience/SciReason / extract_predictions_from_json

Function extract_predictions_from_json

tools/convert_alignmentbench.py:10–45  ·  view source on GitHub ↗
(input_folder)

Source from the content-addressed store, hash-verified

8
9
10def extract_predictions_from_json(input_folder):
11
12 sub_folder = os.path.join(input_folder, 'submission')
13 pred_folder = os.path.join(input_folder, 'predictions')
14 if not os.path.exists(sub_folder):
15 os.makedirs(sub_folder)
16
17 for model_name in os.listdir(pred_folder):
18 model_folder = os.path.join(pred_folder, model_name)
19 try:
20 # when use split
21 json_paths = glob(
22 os.path.join(model_folder, 'alignment_bench_*.json'))
23 # sorted by index
24 json_paths = sorted(
25 json_paths,
26 key=lambda x: int(x.split('.json')[0].split('_')[-1]))
27 except Exception as e:
28 # when only one complete file
29 print(e)
30 json_paths = [os.path.join(model_folder, 'alignment_bench.json')]
31
32 all_predictions = []
33 for json_ in json_paths:
34 json_data = json.load(open(json_))
35 for _, value in json_data.items():
36 prediction = value['prediction']
37 all_predictions.append(prediction)
38
39 # for prediction
40 output_path = os.path.join(sub_folder, model_name + '_submission.csv')
41 with open(output_path, 'w', encoding='utf-8-sig') as file:
42 writer = csv.writer(file)
43 for ans in tqdm(all_predictions):
44 writer.writerow([str(ans)])
45 print('Saved {} for submission'.format(output_path))
46
47
48def process_jsonl(file_path):

Callers 1

Calls 3

openFunction · 0.85
loadMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected