IKMLab/MPTR
0
1from prompt_model_factory import BertForPromptFinetuning2from transformers import (3 AutoTokenizer,4 DataCollatorWithPadding,5 TrainingArguments,6 Trainer,7 EvalPrediction,8)9 10# from prompt_tuning import compute_metrics11import torch12import pickle13import numpy as np14from prompt_dataset import InferenceDataset15import gradio as gr16from utils import load_params, get_label_words, pred_by_threshold17 18 19def compute_metrics(20 threshold=None,21 classes=None,22 p_tuning=False,23):24 def compute_metric_threshold(eval_pred: EvalPrediction):25 return pred_by_threshold(26 t=threshold,27 y_true=eval_pred.label_ids,28 similarities=eval_pred.predictions29 if p_tuning30 else torch.sigmoid(torch.tensor(eval_pred.predictions)),31 classes=classes,32 )33 34 return compute_metric_threshold35 36 37def greet(Liver_CT_Report):38 prompt_FT = True39 file = open(f"class_names.pkl", "rb")40 classes = pickle.load(file)41 class_names = list(classes.keys())42 id_to_class = {i: class_names[i] for i in range(len(class_names))}43 44 device = (45 torch.device("cuda:1") if torch.cuda.is_available() else torch.device("cpu")46 )47 args = load_params("args.json")48 model_path = f"IKMLab/MPTR"49 tokenizer = AutoTokenizer.from_pretrained(model_path)50 51 if prompt_FT:52 # Prompt tuning53 label_words = get_label_words(list(classes.keys()), args.use_multi_label_words)54 55 if args.use_multi_label_words:56 label_word_ids = []57 for l in label_words:58 one_label_ids = [tokenizer.convert_tokens_to_ids(word) for word in l]59 label_word_ids.append(one_label_ids)60 else:61 label_word_ids = (62 torch.tensor([tokenizer.convert_tokens_to_ids(l) for l in label_words])63 .long()64 .to(device)65 )66 model = BertForPromptFinetuning.from_pretrained(67 model_path,68 use_multi_label_words=args.use_multi_label_words,69 )70 model.label_word_ids = label_word_ids71 72 result_path = f"results/predict"73 74 training_args = TrainingArguments(75 output_dir=result_path,76 learning_rate=args.lr,77 per_device_train_batch_size=args.batch_size,78 per_device_eval_batch_size=1,79 num_train_epochs=args.num_epochs,80 weight_decay=0.01,81 warmup_ratio=args.warmup_ratio,82 seed=args.seed,83 evaluation_strategy="steps",84 logging_steps=100, # same as eval_steps85 save_strategy="steps",86 save_steps=100,87 save_total_limit=1,88 load_best_model_at_end=True,89 metric_for_best_model=f"eval_{args.best_metric}",90 )91 data_collator = DataCollatorWithPadding(tokenizer=tokenizer)92 trainer = Trainer(93 model=model,94 args=training_args,95 train_dataset=None,96 eval_dataset=None,97 tokenizer=tokenizer,98 data_collator=data_collator,99 compute_metrics=compute_metrics(100 threshold=args.t,101 classes=classes,102 p_tuning=prompt_FT,103 ),104 )105 106 testset = InferenceDataset(107 Liver_CT_Report,108 tokenizer,109 args.max_seq_len,110 template=args.template,111 prompt=args.prompt,112 )113 result = trainer.predict(testset)114 predictions = (result.predictions[0] >= args.t) * 1115 positive_idx = np.where(predictions == 1)[0]116 if len(positive_idx) == 0:117 return "No positive findings."118 119 return [id_to_class[i] for i in positive_idx]120 121 122# test = "Two small 0.6-cm and 1.4-cm densely packed lipiodol puddles in S7 without identifiable viable tumor, suggestive of good response to previous TACE without viability."123# result = greet(test)124custom_css = """125 div.svelte-1viwdyg {126 text-align: left;127 }128"""129 130iface = gr.Interface(131 fn=greet,132 inputs="text",133 outputs="text",134 description="You can try the three examples provided below (same as the ones in our paper) or other liver CT reports.",135 examples=[136 "Comparsion: CT study on Technique: Triphasic CT study of the liver with 5 mm spiral contiguous helical slice was obtained through the abdomen following the uneventful administration of 100 cc IV contrast enhanced CT in arterial phase, portal phase and delay scan shows: > Mild undulated surface of shrunken liver, borderline splenomegaly, suggesting liver cirrhosis and portal hypertension. > Foci arterial blushes in S7 and S8 near liver dome, favor of AP shunts. No obvious abnormal tumor blushes nor enhancement nodule to be noted in the other part of liver. A tiny hepatic cyst in S7/8> Patency of the SMV, splenic vein, portal and hepatic veins. No obvious biliary tree dilatation. > No obvious ascites nor mesenteric massThe spleen, pancreas, bilateral adrenals and both kidneys are unremarkable. > No definite enlarged retroperitoneal LNs in the abdomen and pelvis. > Some divertiucla in sigmoid colon. Juxtampullar divertiuclum at 2nd portion of duodednum. Patent of the bowel loop without obvious eccentric mass> No obvious pulmonary nodule in bilateral lower lungs. > Degenerative change of the L-spine. Others: Atheroscleroctic change of aorta and its major branches. IMP:No obvious hypervascular nodule nor HCC in both lobes liver. Foci AP shunts in S7 and S8 near liver dome. Mild liver cirrhosis and portal hypertension.",137 "A 74 Y/O male; Clinical Information:Portal Hypertension: nil, TACE on (AFP; 7.0), TACE on , TACE on (AFP; 7.6), .CT scan of liver for F/U a patient of HCC post operation and TACE for recurrent HCCs was done by using triphasic study without and with bolus IV non-ionic contrast enhancement showed: 1. S/P heaptectomy of left lobe and partial right segmentectomy of S6 as well as cholecystectomy. 2. No imaging evidence of cirrhosis of liver. 3. Two small 0.6-cm and 1.4-cm densely packed lipiodol puddles in S7 without identifiable viable tumor, suggestive of good response to previous TACE without viability. 4. A 0.5-cm nodular enhancement in S8 is noted on the arterial phase image (se 9, im 5). 5. Multiple hepatic cysts are noted, stationary. 6. The portal and hepatic venous system are patent. No biliary tree dilatation. 7. No remarkable finding of the spleen, pancreas, both kidneys and adrenal glands. 8. No evidence of enlarged lymph node is found in the perigastric area, hepatoduondenal ligament, para-aortic area, pelvis and inguina. 9. Grossly, no abnormality is found in the GI tract. Clear mesentery and omentum. No ascites. 10. Normal contour, capacity and wall thickness of urinary bladder. Normal size and contour of seminal vesicle and prostatic gland. 11. Chronic fibrotic change with reticulonodular infiltrations are noted in bilateral upper lungs, old pulmonary TB should be susptected. Mild degree reticulonodular infiltrations in RML. 12. No enlarged lymph node or tumor mass is found in the mediastinum. 13. Grossly, no destructive bony lesion or abnormal bone density. IMP: Two small HCCs in S7 post successful TACE without viability. Suspicion of a 0.5-cm newly found HCC in S8. F/U dynamic study 3-6 months later is indicated.",138 "A 74 Y/O male; Clinical Information: umbilical painful mass 4*4cm, nature? CT of abdomen without & with contrast enhancement shows: 1. S/P radical prostatectomy; no gross local recurrence but a 3.6-cm mass in the anterior abdominal wall, R/O metastasis; suggest clinical correlation 2. Mild bilateral renal atrophy; right renal cyst, 3 cm; remarkable fatty liver with GB stones; no gross dilatation in the biliary tree 3. No remarkable finding in the pancreas, spleen, and adrenal glands 4. No enlarged lymph nodes at the paraaortic and iliac chain areas. 5. No ascites; clear bilateral basal lungs; no gross bony metastasisIMP: S/P prostatectomy; R/O metastasis in the anterior abdominal wall",139 ],140 css=custom_css,141 cache_examples=False,142)143iface.launch(share=True)144 