|
- import os
- import sys
- import json
- from model_url import get_model_resp, get_url_tokenizer
-
-
- def run_predict(url, log_path, few_shot = True):
- """run predict"""
- import numpy as np
- tokenizer = get_url_tokenizer()
-
- id_label = {0: "A", 1: "B", 2: "C", 3: "D"}
- MAIN_DIR = os.path.dirname(os.path.abspath(__file__))
- file_dir = MAIN_DIR + "/task_dataset/chid/test_public.json"
- count = 0
- correct_num = 0
- acc = 0
-
- pre = ""
- devfile = MAIN_DIR + "/task_dataset/chid/dev_0.json"
- cnt = 0
- if few_shot:
- with open(devfile, "r", encoding="utf8") as f:
- for line in f.readlines():
- cnt += 1
- line = json.loads(line)
- id, candidates, content, answer = line["id"], line["candidates"], line["content"], line["answer"]
- pre += f"{content.replace('#idiom#', candidates[int(answer)])}\n"
- if cnt == 5:
- break
-
- with open(file_dir, "r", encoding="utf8") as f:
- for line in f.readlines():
- count += 1
- line = json.loads(line)
- id, candidates, content, answer = line["id"], line["candidates"], line["content"], line["answer"]
-
- input_str_one = pre + content.replace("#idiom#", candidates[0])
- input_str_two = pre + content.replace("#idiom#", candidates[1])
- input_str_thr = pre + content.replace("#idiom#", candidates[2])
- input_str_fou = pre + content.replace("#idiom#", candidates[3])
- input_str_fiv = pre + content.replace("#idiom#", candidates[4])
- input_str_six = pre + content.replace("#idiom#", candidates[5])
- input_str_sev = pre + content.replace("#idiom#", candidates[6])
-
- input_str = []
- input_str.append(input_str_one)
- input_str.append(input_str_two)
- input_str.append(input_str_thr)
- input_str.append(input_str_fou)
- input_str.append(input_str_fiv)
- input_str.append(input_str_six)
- input_str.append(input_str_sev)
- mask_length_list = []
- input_length_list = []
-
- for pred in input_str:
- input_length_list.append(len(tokenizer.encode(pred)))
- mask_length_list.append(len(tokenizer.encode(pre)))
- model_resp = get_model_resp(url=url, input_str=input_str, tokens_to_generate=0, top_k=1, logprobs=True)
- if few_shot:
- return_resp = []
- for resp_item, input_length, mask_length in zip(model_resp, input_length_list, mask_length_list):
- # assert len(resp_item) == input_length - 1
- item = resp_item[mask_length - 1:input_length - 1]
- return_resp.append(item)
-
- pred_list = [sum(logprobs) / len(logprobs) for logprobs in return_resp]
- else:
- pred_list = [sum(logprobs) / len(logprobs) for logprobs in model_resp]
-
- answers_pred = int(np.argmax(pred_list))
-
- if answers_pred == answer:
- correct_num += 1
- acc = correct_num / count
-
- print(f"chid, 准确率Acc:{acc}, number: {count}")
-
- if not few_shot:
- with open( log_path + '/chid_zeroshot.txt', 'w') as file:
- file.write(f"chid, zero shot , Acc: {acc}, number: {count}")
- else:
- with open(log_path + '/chid_fewshot.txt', 'w') as file:
- file.write(f"chid, few shot , Acc: {acc}, number: {count}")
|