Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import Transformers & Gradio
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Load clinical note data
|
| 7 |
+
|
| 8 |
+
# Print the columns in this dataset and a sample entry.
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
ds = load_dataset("AGBonnet/augmented-clinical-notes")
|
| 11 |
+
|
| 12 |
+
# Loads the JSON column and indicates whether the JSON was valid. Some records contain invalid JSON formatting.
|
| 13 |
+
def load_json_with_status(raw):
|
| 14 |
+
import json
|
| 15 |
+
try:
|
| 16 |
+
parsed = json.loads(raw)
|
| 17 |
+
return True, parsed
|
| 18 |
+
except:
|
| 19 |
+
return False, raw
|
| 20 |
+
|
| 21 |
+
# View samples of Clinical Notes
|
| 22 |
+
|
| 23 |
+
train = ds["train"]
|
| 24 |
+
|
| 25 |
+
# Sample up to 25 of today's patients since we never have more than that per day.
|
| 26 |
+
sampleLengthTrain = min(25, len(ds['train']))
|
| 27 |
+
print("length=", sampleLengthTrain)
|
| 28 |
+
patientSample = {} # Dictionary keyed by patient ID
|
| 29 |
+
|
| 30 |
+
for i in range(sampleLengthTrain):
|
| 31 |
+
|
| 32 |
+
# A JSON summary contains multiple sections describing the patient's problem and the prescribed tests.
|
| 33 |
+
patientJsonSummaryStr = ds['train'][i]['summary']
|
| 34 |
+
patientIdx = ds['train'][i]['idx']
|
| 35 |
+
patientNote = ds['train'][i]['note']
|
| 36 |
+
success, parsedJsonSummary = load_json_with_status(patientJsonSummaryStr);
|
| 37 |
+
|
| 38 |
+
# Only if the JSON parsing succeeded do we load this record.
|
| 39 |
+
if success:
|
| 40 |
+
motivation = parsedJsonSummary['visit motivation']
|
| 41 |
+
discharge = parsedJsonSummary['discharge']
|
| 42 |
+
treatments = parsedJsonSummary['treatments']
|
| 43 |
+
patientSample[patientIdx] = {'note': patientNote, 'visit motivation': motivation, 'summary': parsedJsonSummary, 'discharge': discharge, 'treatments': treatments}
|
| 44 |
+
#(DEBUG ONLY) #print("idx:", ds['train'][i]['idx'], "summary:", repr(str(patientJsonSummaryStr)))
|
| 45 |
+
|
| 46 |
+
# otherwise, skip the record with invalid JSON.
|
| 47 |
+
print("Done. patientSample=", len(patientSample))
|
| 48 |
+
|
| 49 |
+
# Create functions to use from the UI to show content in dropdowns and textboxes.
|
| 50 |
+
|
| 51 |
+
def get_motivation_from_patient_id(patientId):
|
| 52 |
+
return patientSample.get(patientId)['visit motivation']
|
| 53 |
+
|
| 54 |
+
def get_note_from_patient_id(patientId):
|
| 55 |
+
return patientSample.get(patientId)['note']
|
| 56 |
+
|
| 57 |
+
def get_treatment_plan_from_patient_id(patientId):
|
| 58 |
+
print(patientSample.get(patientId))
|
| 59 |
+
summary = patientSample.get(patientId)['summary']
|
| 60 |
+
return ("Treatments: " + str(summary.get('treatments', 'N/A')) +
|
| 61 |
+
" Discharge notes: " + str(summary.get('discharge', 'N/A')))
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Assist medical staff by summarizing treatment plan from their notes.
|
| 65 |
+
|
| 66 |
+
def json_section_to_text(json_obj):
|
| 67 |
+
parts = []
|
| 68 |
+
|
| 69 |
+
# Simple mapping that includes only non-empty fields.
|
| 70 |
+
if json_obj.get("visit motivation"):
|
| 71 |
+
motiviation = "Visit motivation: " + json_obj["visit motivation"]
|
| 72 |
+
parts.append(motiviation)
|
| 73 |
+
print(motiviation)
|
| 74 |
+
if json_obj.get("patient information"):
|
| 75 |
+
patientInfo = "Patient information: " + json_obj["patient information"]
|
| 76 |
+
parts.append(patientInfo)
|
| 77 |
+
print(patientInfo)
|
| 78 |
+
|
| 79 |
+
return "\n\n".join(parts)
|
| 80 |
+
|
| 81 |
+
# Create a question answering pipeline to answer questions about the patient's treatment plan.
|
| 82 |
+
|
| 83 |
+
from transformers import pipeline, AutoTokenizer
|
| 84 |
+
generator = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def summarize_notes(patient_id):
|
| 88 |
+
import json
|
| 89 |
+
|
| 90 |
+
treatment_text = get_treatment_plan_from_patient_id(patient_id);
|
| 91 |
+
prompt = "what are a few special care instructions to provide to this patient (or not applicable)?"
|
| 92 |
+
|
| 93 |
+
result = generator(question=prompt, context=treatment_text)
|
| 94 |
+
responseGenerator = result['answer']
|
| 95 |
+
|
| 96 |
+
return responseGenerator
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# Build a UI using Gradio to list a sampling of patient's from Today's Office Visits.
|
| 100 |
+
# - Once selected, display the Visit Motivation and the Clinical Summary
|
| 101 |
+
# - A Generate
|
| 102 |
+
|
| 103 |
+
import gradio as gr
|
| 104 |
+
|
| 105 |
+
# Load patient IDs for the dropdown. The first item will be a blank so that the user must select a patient ID.
|
| 106 |
+
choices = [""] + list(patientSample.keys())
|
| 107 |
+
|
| 108 |
+
with gr.Blocks(title="Patient Instructions generated from Clinical Notes", theme="soft") as demo:
|
| 109 |
+
gr.Markdown("# Choose a patient to view their summary, and then generate concise patient instructions.")
|
| 110 |
+
|
| 111 |
+
patientDropdown = gr.Dropdown(choices, label="Choose a Patient ID")
|
| 112 |
+
motivationTextbox = gr.Textbox(lines=4, label="Visit Motivation")
|
| 113 |
+
summaryTextbox = gr.Textbox(lines=4, label="Clinical summary")
|
| 114 |
+
patientDropdown.change(get_motivation_from_patient_id, inputs=patientDropdown, outputs=motivationTextbox)
|
| 115 |
+
patientDropdown.change(get_note_from_patient_id, inputs=patientDropdown, outputs=summaryTextbox)
|
| 116 |
+
|
| 117 |
+
# Button to generate summarized patient instructions and display them.
|
| 118 |
+
patientInstructions = gr.Textbox(lines=5, label="Simplified Next-Steps")
|
| 119 |
+
generateBtn = gr.Button("Get simplified Next Steps for Patient")
|
| 120 |
+
generateBtn.click(summarize_notes, inputs=patientDropdown, outputs=patientInstructions)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
demo.queue().launch(share=True)
|