Spaces:
Build error
Build error
Upload Summarization_Simple_29Nov.py
Browse files- Summarization_Simple_29Nov.py +118 -0
Summarization_Simple_29Nov.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from math import ceil
|
| 5 |
+
from collections import Counter
|
| 6 |
+
from string import punctuation
|
| 7 |
+
import spacy
|
| 8 |
+
from spacy import displacy
|
| 9 |
+
import en_ner_bc5cdr_md
|
| 10 |
+
|
| 11 |
+
# Store the initial value of widgets in session state
|
| 12 |
+
if "visibility" not in st.session_state:
|
| 13 |
+
st.session_state.visibility = "visible"
|
| 14 |
+
st.session_state.disabled = False
|
| 15 |
+
|
| 16 |
+
#nlp = en_core_web_lg.load()
|
| 17 |
+
nlp = spacy.load("en_ner_bc5cdr_md")
|
| 18 |
+
|
| 19 |
+
st.set_page_config(layout='wide')
|
| 20 |
+
st.title('Clinical Note Summarization')
|
| 21 |
+
st.sidebar.markdown('Using transformer model')
|
| 22 |
+
|
| 23 |
+
## Loading in dataset
|
| 24 |
+
#df = pd.read_csv('mtsamples_small.csv',index_col=0)
|
| 25 |
+
df = pd.read_csv('shpi_w_rouge21Nov.csv')
|
| 26 |
+
|
| 27 |
+
#Renaming column
|
| 28 |
+
df.rename(columns={'SUBJECT_ID':'Patient_ID',
|
| 29 |
+
'HADM_ID':'Admission_ID',
|
| 30 |
+
'hpi_input_text':'Original_Text',
|
| 31 |
+
'hpi_reference_summary':'Reference_text'}, inplace = True)
|
| 32 |
+
|
| 33 |
+
#data.rename(columns={'gdp':'log(gdp)'}, inplace=True)
|
| 34 |
+
|
| 35 |
+
#Filter selection
|
| 36 |
+
st.sidebar.header("Search for Patient:")
|
| 37 |
+
|
| 38 |
+
patientid = df['Patient_ID']
|
| 39 |
+
patient = st.sidebar.selectbox('Select Patient ID:', patientid)
|
| 40 |
+
admissionid = df['Admission_ID'].loc[df['Patient_ID'] == patient]
|
| 41 |
+
HospitalAdmission = st.sidebar.selectbox('', admissionid)
|
| 42 |
+
|
| 43 |
+
# List of Model available
|
| 44 |
+
model = st.sidebar.selectbox('Select Model', ('BertSummarizer','BertGPT2','t5seq2eq','t5','gensim','pysummarizer'))
|
| 45 |
+
|
| 46 |
+
col3,col4 = st.columns(2)
|
| 47 |
+
patientid = col3.write(f"Patient ID: {patient} ")
|
| 48 |
+
admissionid =col4.write(f"Admission ID: {HospitalAdmission} ")
|
| 49 |
+
|
| 50 |
+
col1, col2 = st.columns(2)
|
| 51 |
+
#_min_length = col1.number_input("Minimum Length", value=_min_length)
|
| 52 |
+
#_max_length = col2.number_input("Maximun Length", value=_max_length)
|
| 53 |
+
##_early_stopping = col3.number_input("early_stopping", value=_early_stopping)
|
| 54 |
+
|
| 55 |
+
#text = st.text_area('Input Clinical Note here')
|
| 56 |
+
|
| 57 |
+
# Query out relevant Clinical notes
|
| 58 |
+
original_text = df.query(
|
| 59 |
+
"Patient_ID == @patient & Admission_ID == @HospitalAdmission"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
original_text2 = original_text['Original_Text'].values
|
| 63 |
+
|
| 64 |
+
runtext =st.text_area('Input Clinical Note here:', str(original_text2), height=300)
|
| 65 |
+
|
| 66 |
+
reference_text = original_text['Reference_text'].values
|
| 67 |
+
|
| 68 |
+
def run_model(input_text):
|
| 69 |
+
|
| 70 |
+
if model == "BertSummarizer":
|
| 71 |
+
output = original_text['BertSummarizer'].values
|
| 72 |
+
st.write('Summary')
|
| 73 |
+
st.success(output[0])
|
| 74 |
+
|
| 75 |
+
elif model == "BertGPT2":
|
| 76 |
+
output = original_text['BertGPT2'].values
|
| 77 |
+
st.write('Summary')
|
| 78 |
+
st.success(output[0])
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
elif model == "t5seq2eq":
|
| 82 |
+
output = original_text['t5seq2eq'].values
|
| 83 |
+
st.write('Summary')
|
| 84 |
+
st.success(output)
|
| 85 |
+
|
| 86 |
+
elif model == "t5":
|
| 87 |
+
output = original_text['t5'].values
|
| 88 |
+
st.write('Summary')
|
| 89 |
+
st.success(output)
|
| 90 |
+
|
| 91 |
+
elif model == "gensim":
|
| 92 |
+
output = original_text['gensim'].values
|
| 93 |
+
st.write('Summary')
|
| 94 |
+
st.success(output)
|
| 95 |
+
|
| 96 |
+
elif model == "pysummarizer":
|
| 97 |
+
output = original_text['pysummarizer'].values
|
| 98 |
+
st.write('Summary')
|
| 99 |
+
st.success(output)
|
| 100 |
+
|
| 101 |
+
col1, col2 = st.columns([1,1])
|
| 102 |
+
|
| 103 |
+
with col1:
|
| 104 |
+
st.button('Summarize')
|
| 105 |
+
run_model(runtext)
|
| 106 |
+
sentences=runtext.split('.')
|
| 107 |
+
st.text_area('Reference text', str(reference_text),label_visibility="hidden")
|
| 108 |
+
with col2:
|
| 109 |
+
st.button('NER')
|
| 110 |
+
doc = nlp(str(original_text2))
|
| 111 |
+
colors = { "DISEASE": "pink","CHEMICAL": "orange"}
|
| 112 |
+
options = {"ents": [ "DISEASE", "CHEMICAL"],"colors": colors}
|
| 113 |
+
ent_html = displacy.render(doc, style="ent", options=options)
|
| 114 |
+
st.markdown(ent_html, unsafe_allow_html=True)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|