elliealbertson commited on
Commit
e09a6bd
·
1 Parent(s): e34187d

Initial commit

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ model_path = 'elliealbertson/identifying_pregnancy_clinical_notes'
2
+ tokenizer = BertTokenizer.from_pretrained(model_path)
3
+ model = BertForSequenceClassification.from_pretrained(model_path)
4
+
5
+ def predict(text):
6
+
7
+ inputs = tokenizer(text, return_tensors="pt")
8
+ num_tokens = inputs['input_ids'].size(1)
9
+
10
+ if num_tokens <= 512:
11
+
12
+ outputs = model(**inputs)
13
+
14
+ predicted_class_id = torch.argmax(outputs.logits).item()
15
+
16
+ probability_of_predicted_class = round(torch.nn.functional.softmax(outputs.logits, dim=1)[0, predicted_class_id].item(),2)
17
+
18
+ if (predicted_class_id == 0) & (probability_of_predicted_class >= 0.5):
19
+ predicted_class_label = "No, the note does not discuss the patient's pregnancy based on the model's assessment."
20
+ elif (predicted_class_id == 1) & (probability_of_predicted_class >= 0.5):
21
+ predicted_class_label = "Yes, the note discusses the patient's pregnancy based on the model's assessment."
22
+ else:
23
+ predicted_class_label = "The model was unable to determine with high certainty whether or not the note discusses the patient's pregnancy. Please provide additional text or a different note."
24
+
25
+ return predicted_class_label
26
+
27
+ else:
28
+
29
+ error_message = 'Unfortunately the model is limited in how much text it can process at once. Please enter a shorter note.'
30
+
31
+ return error_message
32
+
33
+ with gr.Blocks() as interface:
34
+ gr.Markdown("<h1 align='center'>Identifying Pregnancy in Clinical Notes</h1>")
35
+ gr.Markdown("<p align='center'>Use this app to classify a clinical note as discussing or not discussing the patient's pregnancy.</p>")
36
+ with gr.Row():
37
+ with gr.Column():
38
+ inputs = gr.Textbox(label='Input a clinical note here:', lines=4)
39
+ button = gr.Button('Assess Note')
40
+ gr.Examples(['The patient is pregnant.', 'She has high cholesterol and hypertension.', 'Normal vaginal delivery.', 'Fetus development normal.', 'Presented with nausea.', 'Broken arm and leg.'], inputs)
41
+ with gr.Column():
42
+ outputs=gr.Textbox(label="Does the note discuss the patient's pregnancy?", lines=4)
43
+ button.click(fn=predict, inputs=inputs, outputs=outputs, queue=False)
44
+ gr.Markdown("<p align='center'>Model fine-tuned from <a href='https://huggingface.co/emilyalsentzer/Bio_ClinicalBERT' target='_blank'> Bio+ClinicalBERT </a>.</p>")
45
+
46
+ interface.launch()