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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoConfig, AutoTokenizer
|
| 3 |
+
from bert_graph import BertForMultipleChoice
|
| 4 |
+
import ipdb
|
| 5 |
+
import torch
|
| 6 |
+
import copy
|
| 7 |
+
from itertools import chain
|
| 8 |
+
|
| 9 |
+
# "Comparison of mean diurnal measurements with latanoprost and timolol showed a statistical significant (P < 0.001) difference at 3, 6, and 12 months.",
|
| 10 |
+
# "in patients with pigmentary glaucoma, 0.005% latanoprost taken once daily was well tolerated and more effective in reducing IOP than 0.5% timolol taken twice daily."
|
| 11 |
+
|
| 12 |
+
def preprocess_function_exp(examples, tokenizer):
|
| 13 |
+
|
| 14 |
+
# Flatten out
|
| 15 |
+
pair_list = examples
|
| 16 |
+
# ipdb.set_trace()
|
| 17 |
+
pair_len = [len(item) for item in pair_list]
|
| 18 |
+
|
| 19 |
+
first_sentences = []
|
| 20 |
+
second_sentences = []
|
| 21 |
+
for line_list in pair_list:
|
| 22 |
+
for line in line_list:
|
| 23 |
+
# ipdb.set_trace()
|
| 24 |
+
sent_item = line.strip().split('\t')
|
| 25 |
+
first_sentences.append(sent_item[0].strip())
|
| 26 |
+
second_sentences.append(sent_item[1].strip())
|
| 27 |
+
|
| 28 |
+
# Tokenize
|
| 29 |
+
tokenized_examples = tokenizer(
|
| 30 |
+
first_sentences,
|
| 31 |
+
second_sentences,
|
| 32 |
+
max_length=512,
|
| 33 |
+
padding=False,
|
| 34 |
+
truncation=True,
|
| 35 |
+
)
|
| 36 |
+
# Un-flatten
|
| 37 |
+
# tokenized_inputs = {k: [v[i : i + pair_len[0]] for i in range(0, len(v), pair_len[0])] for k, v in tokenized_examples.items()}
|
| 38 |
+
tokenized_inputs = {}
|
| 39 |
+
for k, v in tokenized_examples.items():
|
| 40 |
+
flatten_list = []
|
| 41 |
+
head_idx = 0
|
| 42 |
+
tail_idx = 0
|
| 43 |
+
for pair_idx in pair_len:
|
| 44 |
+
tail_idx = head_idx + pair_idx
|
| 45 |
+
flatten_list.append(v[head_idx: tail_idx])
|
| 46 |
+
head_idx = copy.copy(tail_idx)
|
| 47 |
+
tokenized_inputs[k] = flatten_list
|
| 48 |
+
|
| 49 |
+
# tokenized_inputs["pair_len"] = pair_len
|
| 50 |
+
return tokenized_inputs
|
| 51 |
+
|
| 52 |
+
def DCForMultipleChoice(features, tokenizer):
|
| 53 |
+
|
| 54 |
+
batch_size = len(features)
|
| 55 |
+
argument_len = 4
|
| 56 |
+
|
| 57 |
+
flattened_features = [
|
| 58 |
+
[{k: v[0][i] for k, v in features.items()} for i in range(4)]
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
flattened_features = list(chain(*flattened_features))
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
batch = tokenizer.pad(
|
| 65 |
+
flattened_features,
|
| 66 |
+
padding=True,
|
| 67 |
+
max_length=512,
|
| 68 |
+
return_tensors="pt",
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
batch = {k: v.view(1, argument_len, -1) for k, v in batch.items()}
|
| 73 |
+
|
| 74 |
+
return batch
|
| 75 |
+
|
| 76 |
+
def post_process_diag(predictions):
|
| 77 |
+
|
| 78 |
+
num_sentences = int(len(predictions)**0.5)
|
| 79 |
+
predictions_mtx = predictions.reshape(num_sentences, num_sentences)
|
| 80 |
+
|
| 81 |
+
for i in range(num_sentences):
|
| 82 |
+
for j in range(num_sentences):
|
| 83 |
+
if i == j:
|
| 84 |
+
predictions_mtx[i, j] = 0
|
| 85 |
+
|
| 86 |
+
return predictions_mtx.view(-1)
|
| 87 |
+
|
| 88 |
+
def max_vote(logits1, logits2, pred1, pred2):
|
| 89 |
+
|
| 90 |
+
pred1 = post_process_diag(pred1)
|
| 91 |
+
pred2 = post_process_diag(pred2)
|
| 92 |
+
pred_res = []
|
| 93 |
+
confidence_res = []
|
| 94 |
+
for i in range(len(logits1)):
|
| 95 |
+
|
| 96 |
+
soft_logits1 = torch.nn.functional.softmax(logits1[i]) # [[j] for j in range(logits1.shape[1])]
|
| 97 |
+
soft_logits2 = torch.nn.functional.softmax(logits2[i])
|
| 98 |
+
|
| 99 |
+
# two class
|
| 100 |
+
# torch.topk(soft_logits1, n=2)
|
| 101 |
+
values_1, _ = soft_logits1.topk(k=2)
|
| 102 |
+
values_2, _ = soft_logits2.topk(k=2)
|
| 103 |
+
# import ipdb
|
| 104 |
+
# ipdb.set_trace()
|
| 105 |
+
# if (values_1[0] - values_2[0]) > (values_1[1] - values_2[1]):
|
| 106 |
+
# pred_res.append(int(pred1[i].detach().cpu().numpy()))
|
| 107 |
+
# else:
|
| 108 |
+
# pred_res.append(int(pred2[i].detach().cpu().numpy()))
|
| 109 |
+
if (values_1[0] - values_1[1]) >= (values_2[0] - values_2[1]):
|
| 110 |
+
pred_res.append(int(pred1[i].detach().cpu().numpy()))
|
| 111 |
+
confidence_res.append(float((values_1[0] - values_1[1]).detach().cpu().numpy()))
|
| 112 |
+
else:
|
| 113 |
+
pred_res.append(int(pred2[i].detach().cpu().numpy()))
|
| 114 |
+
confidence_res.append(float((values_2[0] - values_2[1]).detach().cpu().numpy()))
|
| 115 |
+
|
| 116 |
+
return pred_res, confidence_res
|
| 117 |
+
|
| 118 |
+
def model_infer(input_a, input_b):
|
| 119 |
+
|
| 120 |
+
config = AutoConfig.from_pretrained('michiyasunaga/BioLinkBERT-base')
|
| 121 |
+
config.win_size = 13
|
| 122 |
+
config.model_mode = 'bert_mtl_1d'
|
| 123 |
+
config.dataset_domain = 'absRCT'
|
| 124 |
+
config.voter_branch = 'dual'
|
| 125 |
+
config.destroy = False
|
| 126 |
+
|
| 127 |
+
model = BertForMultipleChoice.from_pretrained(
|
| 128 |
+
'michiyasunaga/BioLinkBERT-base',
|
| 129 |
+
config=config,
|
| 130 |
+
)
|
| 131 |
+
p_sum = torch.load('D:/Code/Antidote/ari_model/best.pth', map_location=torch.device('cpu'))
|
| 132 |
+
model.load_state_dict(p_sum)
|
| 133 |
+
tokenizer = AutoTokenizer.from_pretrained('michiyasunaga/BioLinkBERT-base')
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
examples = [[input_a+'\t'+input_a, input_a+'\t'+input_b, input_b+'\t'+input_a, input_b+'\t'+input_b]]
|
| 137 |
+
tokenized_inputs = preprocess_function_exp(examples, tokenizer)
|
| 138 |
+
tokenized_inputs = DCForMultipleChoice(tokenized_inputs, tokenizer)
|
| 139 |
+
# ipdb.set_trace()
|
| 140 |
+
outputs = model(**tokenized_inputs)
|
| 141 |
+
predictions, scores = max_vote(outputs.logits[0], outputs.logits[1], outputs.logits[0].argmax(dim=-1), outputs.logits[1].argmax(dim=-1))
|
| 142 |
+
|
| 143 |
+
prediction_a_b = predictions[1]
|
| 144 |
+
prediction_b_a = predictions[2]
|
| 145 |
+
|
| 146 |
+
label_space = {0: 'not relates', 1: 'supports', 2: 'attack'}
|
| 147 |
+
label_a_b = label_space[prediction_a_b]
|
| 148 |
+
label_b_a = label_space[prediction_b_a]
|
| 149 |
+
|
| 150 |
+
return 'Head Argument {} Tail Argument'.format(label_a_b, label_b_a)
|
| 151 |
+
# ipdb.set_trace()
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
with gr.Blocks() as demo:
|
| 155 |
+
#设置输入组件
|
| 156 |
+
arg_1 = gr.Textbox(label="Head Argument")
|
| 157 |
+
arg_2 = gr.Textbox(label="Tail Argument")
|
| 158 |
+
|
| 159 |
+
gr.Examples([\
|
| 160 |
+
"Compared with baseline measurements, both latanoprost and timolol caused a significant (P < 0.001) reduction of IOP at each hour of diurnal curve throughout the duration of therapy.",\
|
| 161 |
+
"Reduction of IOP was 6.0 +/- 4.5 and 5.9 +/- 4.6 with latanoprost and 4.8 +/- 3.0 and 4.6 +/- 3.1 with timolol after 6 and 12 months, respectively.",\
|
| 162 |
+
"Comparison of mean diurnal measurements with latanoprost and timolol showed a statistical significant (P < 0.001) difference at 3, 6, and 12 months.",\
|
| 163 |
+
"Mean C was found to be significantly enhanced (+30%) only in the latanoprost-treated group compared with the baseline (P = 0.017).",\
|
| 164 |
+
"Mean conjunctival hyperemia was graded at 0.3 in latanoprost-treated eyes and 0.2 in timolol-treated eyes.",\
|
| 165 |
+
"A remarkable change in iris color was observed in both eyes of 1 of the 18 patients treated with latanoprost and none of the 18 patients who received timolol.",\
|
| 166 |
+
"In the timolol group, heart rate was significantly reduced from 72 +/- 9 at baseline to 67 +/- 10 beats per minute at 12 months.",\
|
| 167 |
+
"in patients with pigmentary glaucoma, 0.005% latanoprost taken once daily was well tolerated and more effective in reducing IOP than 0.5% timolol taken twice daily.",\
|
| 168 |
+
"further studies may need to confirm these data on a larger sample and to evaluate the side effect of increased iris pigmentation on long-term follow-up,",\
|
| 169 |
+
], arg_1)
|
| 170 |
+
gr.Examples([\
|
| 171 |
+
"Compared with baseline measurements, both latanoprost and timolol caused a significant (P < 0.001) reduction of IOP at each hour of diurnal curve throughout the duration of therapy.",\
|
| 172 |
+
"Reduction of IOP was 6.0 +/- 4.5 and 5.9 +/- 4.6 with latanoprost and 4.8 +/- 3.0 and 4.6 +/- 3.1 with timolol after 6 and 12 months, respectively.",\
|
| 173 |
+
"Comparison of mean diurnal measurements with latanoprost and timolol showed a statistical significant (P < 0.001) difference at 3, 6, and 12 months.",\
|
| 174 |
+
"Mean C was found to be significantly enhanced (+30%) only in the latanoprost-treated group compared with the baseline (P = 0.017).",\
|
| 175 |
+
"Mean conjunctival hyperemia was graded at 0.3 in latanoprost-treated eyes and 0.2 in timolol-treated eyes.",\
|
| 176 |
+
"A remarkable change in iris color was observed in both eyes of 1 of the 18 patients treated with latanoprost and none of the 18 patients who received timolol.",\
|
| 177 |
+
"In the timolol group, heart rate was significantly reduced from 72 +/- 9 at baseline to 67 +/- 10 beats per minute at 12 months.",\
|
| 178 |
+
"in patients with pigmentary glaucoma, 0.005% latanoprost taken once daily was well tolerated and more effective in reducing IOP than 0.5% timolol taken twice daily.",\
|
| 179 |
+
"further studies may need to confirm these data on a larger sample and to evaluate the side effect of increased iris pigmentation on long-term follow-up,",\
|
| 180 |
+
], arg_2)
|
| 181 |
+
# 设置输出组件
|
| 182 |
+
output = gr.Textbox(label="Output Box")
|
| 183 |
+
#设置按钮
|
| 184 |
+
greet_btn = gr.Button("Run")
|
| 185 |
+
#设置按钮点击事件
|
| 186 |
+
greet_btn.click(fn=model_infer, inputs=[arg_1, arg_2], outputs=output)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
demo.launch()
|