Spaces:
Build error
Build error
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
from math import ceil | |
from collections import Counter | |
from string import punctuation | |
import spacy | |
from spacy import displacy | |
import en_ner_bc5cdr_md | |
from streamlit.components.v1 import html | |
def nav_page(page_name, timeout_secs=8): | |
nav_script = """ | |
<script type="text/javascript"> | |
function attempt_nav_page(page_name, start_time, timeout_secs) { | |
var links = window.parent.document.getElementsByTagName("a"); | |
for (var i = 0; i < links.length; i++) { | |
if (links[i].href.toLowerCase().endsWith("/" + page_name.toLowerCase())) { | |
links[i].click(); | |
return; | |
} | |
} | |
var elasped = new Date() - start_time; | |
if (elasped < timeout_secs * 1000) { | |
setTimeout(attempt_nav_page, 100, page_name, start_time, timeout_secs); | |
} else { | |
alert("Unable to navigate to page '" + page_name + "' after " + timeout_secs + " second(s)."); | |
} | |
} | |
window.addEventListener("load", function() { | |
attempt_nav_page("%s", new Date(), %d); | |
}); | |
</script> | |
""" % (page_name, timeout_secs) | |
html(nav_script) | |
# Store the initial value of widgets in session state | |
if "visibility" not in st.session_state: | |
st.session_state.visibility = "visible" | |
st.session_state.disabled = False | |
#nlp = en_core_web_lg.load() | |
nlp = spacy.load("en_ner_bc5cdr_md") | |
st.set_page_config(page_title ='Clinical Note Summarization', | |
#page_icon= "Notes", | |
layout='wide') | |
st.title('Clinical Note Summarization') | |
st.markdown( | |
""" | |
<style> | |
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child { | |
width: 400px; | |
} | |
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child { | |
width: 400px; | |
margin-left: -230px; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True, | |
) | |
st.sidebar.markdown('Using transformer model') | |
## Loading in dataset | |
#df = pd.read_csv('mtsamples_small.csv',index_col=0) | |
df = pd.read_csv('shpi_w_rouge21Nov.csv') | |
df['HADM_ID'] = df['HADM_ID'].astype(str).apply(lambda x: x.replace('.0','')) | |
#Renaming column | |
df.rename(columns={'SUBJECT_ID':'Patient_ID', | |
'HADM_ID':'Admission_ID', | |
'hpi_input_text':'Original_Text', | |
'hpi_reference_summary':'Reference_text'}, inplace = True) | |
#data.rename(columns={'gdp':'log(gdp)'}, inplace=True) | |
#Filter selection | |
st.sidebar.header("Search for Patient:") | |
patientid = df['Patient_ID'] | |
patient = st.sidebar.selectbox('Select Patient ID:', patientid) | |
admissionid = df['Admission_ID'].loc[df['Patient_ID'] == patient] | |
HospitalAdmission = st.sidebar.selectbox('', admissionid) | |
# List of Model available | |
model = st.sidebar.selectbox('Select Model', ('BertSummarizer','BertGPT2','t5seq2eq','t5','gensim','pysummarizer')) | |
col3,col4 = st.columns(2) | |
patientid = col3.write(f"Patient ID: {patient} ") | |
admissionid =col4.write(f"Admission ID: {HospitalAdmission} ") | |
runtext = '' | |
inputNote ='Input note here:' | |
# Query out relevant Clinical notes | |
original_text = df.query( | |
"Patient_ID == @patient & Admission_ID == @HospitalAdmission" | |
) | |
original_text2 = original_text['Original_Text'].values | |
reference_text = original_text['Reference_text'].values | |
##========= Buttons to the 4 tabs ======== | |
col1, col2, col3, col4 = st.columns(4) | |
with col1: | |
if st.button("🏥 Admission"): | |
#nav_page('Admission') | |
inputNote = "Input Admission Note" | |
with col2: | |
if st.button('📆Daily Narrative'): | |
#nav_page('Daily Narrative') | |
inputNote = "Input Daily Narrative Note" | |
with col3: | |
if st.button('🗒️Discharge Plan'): | |
#nav_page('Discharge Plan') | |
inputNote = "Input Discharge Plan" | |
with col4: | |
if st.button('📝Social Notes'): | |
#nav_page('Social Notes') | |
inputNote = "Input Social Note" | |
runtext =st.text_area(inputNote, str(original_text2), height=300) | |
def visualize (run_text,output): | |
text ='' | |
import spacy | |
from spacy.lang.en import English # updated | |
nlp=spacy.load('en_core_web_sm') | |
sentences=run_text.split('.') | |
summary=output.split('.') | |
text = '' | |
display(HTML(f'<h1>Summary - {title}</h1>')) | |
for sentence in sentence_list: | |
if sentence in best_sentences: | |
text += ' ' + str(sentence).replace(sentence, f"<mark>{sentence}</mark>") | |
else: | |
text += ' ' + sentence | |
display(HTML(f""" {text} """)) | |
best_sentences = [] | |
for sentence in summary: | |
best_sentences.append(str(sentence)) | |
def run_model(input_text): | |
if model == "BertSummarizer": | |
output = original_text['BertSummarizer'].values | |
st.write('Summary') | |
st.success(output[0]) | |
elif model == "BertGPT2": | |
output = original_text['BertGPT2'].values | |
st.write('Summary') | |
st.success(output[0]) | |
elif model == "t5seq2eq": | |
output = original_text['t5seq2eq'].values | |
st.write('Summary') | |
st.success(output) | |
elif model == "t5": | |
output = original_text['t5'].values | |
st.write('Summary') | |
st.success(output) | |
elif model == "gensim": | |
output = original_text['gensim'].values | |
st.write('Summary') | |
st.success(output) | |
elif model == "pysummarizer": | |
output = original_text['pysummarizer'].values | |
st.write('Summary') | |
st.success(output) | |
col1, col2 = st.columns([1,1]) | |
with col1: | |
st.button('Summarize') | |
run_model(runtext) | |
sentences=runtext.split('.') | |
st.text_area('Reference text', str(reference_text))#,label_visibility="hidden") | |
with col2: | |
st.button('NER') | |
doc = nlp(str(original_text2)) | |
colors = { "DISEASE": "pink","CHEMICAL": "orange"} | |
options = {"ents": [ "DISEASE", "CHEMICAL"],"colors": colors} | |
ent_html = displacy.render(doc, style="ent", options=options) | |
st.markdown(ent_html, unsafe_allow_html=True) | |
col3, col4 = st.columns(2) | |
with col3: | |
st.text_area(visualize (run_text,output)) | |
with col4: | |
st.text_area('testing', str(reference_text))#,label_visibility="hidden") | |