Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,174 +1,174 @@
|
|
| 1 |
-
import argparse
|
| 2 |
-
import os
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import json
|
| 5 |
-
from tree_search_icd import get_icd_codes
|
| 6 |
-
from tqdm import tqdm
|
| 7 |
-
import csv
|
| 8 |
-
import streamlit as st
|
| 9 |
-
import tempfile
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
from io import StringIO
|
| 12 |
-
|
| 13 |
-
# def process_medical_notes(file_path,model_name):
|
| 14 |
-
# def process_medical_notes(input_dir, output_file, model_name):
|
| 15 |
-
|
| 16 |
-
# code_map = {}
|
| 17 |
-
|
| 18 |
-
# if not os.path.isdir(input_dir):
|
| 19 |
-
# raise ValueError("The specified input directory does not exist.")
|
| 20 |
-
|
| 21 |
-
# # Process each file in the input directory
|
| 22 |
-
# for files in tqdm(os.listdir(input_dir)):
|
| 23 |
-
# file_path = os.path.join(input_dir, files)
|
| 24 |
-
# print(file_path)
|
| 25 |
-
# with open(file_path, "r", encoding="utf-8") as file:
|
| 26 |
-
# medical_note = file.read()
|
| 27 |
-
|
| 28 |
-
# if not os.path.isfile(file_path):
|
| 29 |
-
# print(f"File does not exist: {file_path}")
|
| 30 |
-
# return None
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# # if os.path.isfile(file_path):
|
| 34 |
-
# # st.write(f"File exists: {file_path}")
|
| 35 |
-
|
| 36 |
-
# # try:
|
| 37 |
-
|
| 38 |
-
# # with open(file_path, "r",encoding="utf-8") as txtfile:
|
| 39 |
-
# # st.write(file_path)
|
| 40 |
-
# # medical_note = txtfile.read()
|
| 41 |
-
|
| 42 |
-
# # st.write(f"Content of the file: {medical_note[:1000]}") # Print the first 1000 characters
|
| 43 |
-
# # except Exception as e:
|
| 44 |
-
# # print(f"Error reading file: {e}")
|
| 45 |
-
# # return None
|
| 46 |
-
|
| 47 |
-
# # print(f"File read successfully. Content length: {len(medical_note)}")
|
| 48 |
-
|
| 49 |
-
# #print(medical_note)
|
| 50 |
-
# icd_codes = get_icd_codes(medical_note, model_name)
|
| 51 |
-
# print(icd_codes)
|
| 52 |
-
# # return icd_codes
|
| 53 |
-
# # print(icd_codes)
|
| 54 |
-
# # code_map[files] = icd_codes
|
| 55 |
-
|
| 56 |
-
# with open(output_file, "w") as f:
|
| 57 |
-
# json.dump(code_map, f, indent=4)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# if __name__ == "__main__":
|
| 61 |
-
# parser = argparse.ArgumentParser(description="Process medical notes to extract ICD codes using a specified model.")
|
| 62 |
-
# parser.add_argument("--input_dir", help="Directory containing the medical text files")
|
| 63 |
-
# parser.add_argument("--output_file", help="File to save the extracted ICD codes in JSON format")
|
| 64 |
-
# parser.add_argument("--model_name", default="llama3-70b-8192", help="Model name to use for ICD code extraction")
|
| 65 |
-
|
| 66 |
-
# args = parser.parse_args()
|
| 67 |
-
# process_medical_notes(args.input_dir, args.output_file, args.model_name)
|
| 68 |
-
|
| 69 |
-
def process_medical_notes(filepath, model_name):
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
try:
|
| 73 |
-
for txtfile in filepath:
|
| 74 |
-
with open(filepath, "r",encoding="utf-8") as txtfile:
|
| 75 |
-
medical_note = txtfile.read()
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
except Exception as e:
|
| 79 |
-
# print(f"Error reading file: {e}")
|
| 80 |
-
return None
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
icd_codes = get_icd_codes(medical_note, model_name)
|
| 84 |
-
return icd_codes
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
def add_custom_css():
|
| 89 |
-
st.markdown(
|
| 90 |
-
"""
|
| 91 |
-
<style>
|
| 92 |
-
/* Remove padding around the main block */
|
| 93 |
-
.block-container {
|
| 94 |
-
padding: 1rem;
|
| 95 |
-
}
|
| 96 |
-
/* Remove padding around the top */
|
| 97 |
-
header, footer, .reportview-container .main .block-container {
|
| 98 |
-
padding: 5;
|
| 99 |
-
}
|
| 100 |
-
/* Fullscreen layout adjustments */
|
| 101 |
-
.css-1d391kg {
|
| 102 |
-
padding: 5;
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
h1 {
|
| 106 |
-
text-align: center;
|
| 107 |
-
}
|
| 108 |
-
.table-wrapper {
|
| 109 |
-
text-align: center;
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
</style>
|
| 116 |
-
""",
|
| 117 |
-
unsafe_allow_html=True,
|
| 118 |
-
)
|
| 119 |
-
def main():
|
| 120 |
-
st.set_page_config(layout="wide",page_icon='🔎',page_title='ICD Identifier')
|
| 121 |
-
add_custom_css()
|
| 122 |
-
st.title("ICD Code
|
| 123 |
-
|
| 124 |
-
col1, col2 = st.columns([1, 5])
|
| 125 |
-
with col2:
|
| 126 |
-
|
| 127 |
-
file_uploads=st.file_uploader('Choose Medical Note File',type='txt', accept_multiple_files=True)
|
| 128 |
-
|
| 129 |
-
submit = st.button("Submit")
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
with col1:
|
| 133 |
-
model_name = st.selectbox(
|
| 134 |
-
"Select Model",
|
| 135 |
-
["llama3-70b-8192", "mixtral-8x7b-32768"],
|
| 136 |
-
index=0 # Default model selected
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
if submit :
|
| 140 |
-
|
| 141 |
-
for file_input in file_uploads:
|
| 142 |
-
file_name = Path(file_input.name).name
|
| 143 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.txt') as temp_file:
|
| 144 |
-
|
| 145 |
-
temp_file.write(file_input.getbuffer())
|
| 146 |
-
temp_file.flush()
|
| 147 |
-
file_paths = temp_file.name
|
| 148 |
-
response=process_medical_notes(file_paths, model_name)
|
| 149 |
-
res_data=pd.DataFrame(response,columns=['ICD Code','Code Description','Evidence From Notes'])
|
| 150 |
-
with col2:
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
# st.markdown(f"""
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
# <div class="custom-table-container" >
|
| 157 |
-
# <h4>Case Id: {file_name}</h4>
|
| 158 |
-
|
| 159 |
-
# <div class="table-wrapper" >
|
| 160 |
-
# {res_data.to_html(classes='table-wrapper', index=False)}
|
| 161 |
-
# </div>
|
| 162 |
-
# </div>
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
# """, unsafe_allow_html=True)
|
| 166 |
-
st.markdown(f"""
|
| 167 |
-
<h5>Case Id: {file_name}</h5>
|
| 168 |
-
""", unsafe_allow_html=True)
|
| 169 |
-
st.markdown(res_data.style.hide(axis="index").to_html(), unsafe_allow_html=True)
|
| 170 |
-
|
| 171 |
-
# st.write(response)
|
| 172 |
-
|
| 173 |
-
if __name__=="__main__":
|
| 174 |
main()
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import json
|
| 5 |
+
from tree_search_icd import get_icd_codes
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
import csv
|
| 8 |
+
import streamlit as st
|
| 9 |
+
import tempfile
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from io import StringIO
|
| 12 |
+
|
| 13 |
+
# def process_medical_notes(file_path,model_name):
|
| 14 |
+
# def process_medical_notes(input_dir, output_file, model_name):
|
| 15 |
+
|
| 16 |
+
# code_map = {}
|
| 17 |
+
|
| 18 |
+
# if not os.path.isdir(input_dir):
|
| 19 |
+
# raise ValueError("The specified input directory does not exist.")
|
| 20 |
+
|
| 21 |
+
# # Process each file in the input directory
|
| 22 |
+
# for files in tqdm(os.listdir(input_dir)):
|
| 23 |
+
# file_path = os.path.join(input_dir, files)
|
| 24 |
+
# print(file_path)
|
| 25 |
+
# with open(file_path, "r", encoding="utf-8") as file:
|
| 26 |
+
# medical_note = file.read()
|
| 27 |
+
|
| 28 |
+
# if not os.path.isfile(file_path):
|
| 29 |
+
# print(f"File does not exist: {file_path}")
|
| 30 |
+
# return None
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# # if os.path.isfile(file_path):
|
| 34 |
+
# # st.write(f"File exists: {file_path}")
|
| 35 |
+
|
| 36 |
+
# # try:
|
| 37 |
+
|
| 38 |
+
# # with open(file_path, "r",encoding="utf-8") as txtfile:
|
| 39 |
+
# # st.write(file_path)
|
| 40 |
+
# # medical_note = txtfile.read()
|
| 41 |
+
|
| 42 |
+
# # st.write(f"Content of the file: {medical_note[:1000]}") # Print the first 1000 characters
|
| 43 |
+
# # except Exception as e:
|
| 44 |
+
# # print(f"Error reading file: {e}")
|
| 45 |
+
# # return None
|
| 46 |
+
|
| 47 |
+
# # print(f"File read successfully. Content length: {len(medical_note)}")
|
| 48 |
+
|
| 49 |
+
# #print(medical_note)
|
| 50 |
+
# icd_codes = get_icd_codes(medical_note, model_name)
|
| 51 |
+
# print(icd_codes)
|
| 52 |
+
# # return icd_codes
|
| 53 |
+
# # print(icd_codes)
|
| 54 |
+
# # code_map[files] = icd_codes
|
| 55 |
+
|
| 56 |
+
# with open(output_file, "w") as f:
|
| 57 |
+
# json.dump(code_map, f, indent=4)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# if __name__ == "__main__":
|
| 61 |
+
# parser = argparse.ArgumentParser(description="Process medical notes to extract ICD codes using a specified model.")
|
| 62 |
+
# parser.add_argument("--input_dir", help="Directory containing the medical text files")
|
| 63 |
+
# parser.add_argument("--output_file", help="File to save the extracted ICD codes in JSON format")
|
| 64 |
+
# parser.add_argument("--model_name", default="llama3-70b-8192", help="Model name to use for ICD code extraction")
|
| 65 |
+
|
| 66 |
+
# args = parser.parse_args()
|
| 67 |
+
# process_medical_notes(args.input_dir, args.output_file, args.model_name)
|
| 68 |
+
|
| 69 |
+
def process_medical_notes(filepath, model_name):
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
for txtfile in filepath:
|
| 74 |
+
with open(filepath, "r",encoding="utf-8") as txtfile:
|
| 75 |
+
medical_note = txtfile.read()
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
# print(f"Error reading file: {e}")
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
icd_codes = get_icd_codes(medical_note, model_name)
|
| 84 |
+
return icd_codes
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def add_custom_css():
|
| 89 |
+
st.markdown(
|
| 90 |
+
"""
|
| 91 |
+
<style>
|
| 92 |
+
/* Remove padding around the main block */
|
| 93 |
+
.block-container {
|
| 94 |
+
padding: 1rem;
|
| 95 |
+
}
|
| 96 |
+
/* Remove padding around the top */
|
| 97 |
+
header, footer, .reportview-container .main .block-container {
|
| 98 |
+
padding: 5;
|
| 99 |
+
}
|
| 100 |
+
/* Fullscreen layout adjustments */
|
| 101 |
+
.css-1d391kg {
|
| 102 |
+
padding: 5;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
h1 {
|
| 106 |
+
text-align: center;
|
| 107 |
+
}
|
| 108 |
+
.table-wrapper {
|
| 109 |
+
text-align: center;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
</style>
|
| 116 |
+
""",
|
| 117 |
+
unsafe_allow_html=True,
|
| 118 |
+
)
|
| 119 |
+
def main():
|
| 120 |
+
st.set_page_config(layout="wide",page_icon='🔎',page_title='ICD Identifier')
|
| 121 |
+
add_custom_css()
|
| 122 |
+
st.title("ICD Code Identifier From Medical Notes")
|
| 123 |
+
|
| 124 |
+
col1, col2 = st.columns([1, 5])
|
| 125 |
+
with col2:
|
| 126 |
+
|
| 127 |
+
file_uploads=st.file_uploader('Choose Medical Note File',type='txt', accept_multiple_files=True)
|
| 128 |
+
|
| 129 |
+
submit = st.button("Submit")
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
with col1:
|
| 133 |
+
model_name = st.selectbox(
|
| 134 |
+
"Select Model",
|
| 135 |
+
["llama3-70b-8192", "mixtral-8x7b-32768"],
|
| 136 |
+
index=0 # Default model selected
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
if submit :
|
| 140 |
+
|
| 141 |
+
for file_input in file_uploads:
|
| 142 |
+
file_name = Path(file_input.name).name
|
| 143 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.txt') as temp_file:
|
| 144 |
+
|
| 145 |
+
temp_file.write(file_input.getbuffer())
|
| 146 |
+
temp_file.flush()
|
| 147 |
+
file_paths = temp_file.name
|
| 148 |
+
response=process_medical_notes(file_paths, model_name)
|
| 149 |
+
res_data=pd.DataFrame(response,columns=['ICD Code','Code Description','Evidence From Notes'])
|
| 150 |
+
with col2:
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
# st.markdown(f"""
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
# <div class="custom-table-container" >
|
| 157 |
+
# <h4>Case Id: {file_name}</h4>
|
| 158 |
+
|
| 159 |
+
# <div class="table-wrapper" >
|
| 160 |
+
# {res_data.to_html(classes='table-wrapper', index=False)}
|
| 161 |
+
# </div>
|
| 162 |
+
# </div>
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
# """, unsafe_allow_html=True)
|
| 166 |
+
st.markdown(f"""
|
| 167 |
+
<h5>Case Id: {file_name}</h5>
|
| 168 |
+
""", unsafe_allow_html=True)
|
| 169 |
+
st.markdown(res_data.style.hide(axis="index").to_html(), unsafe_allow_html=True)
|
| 170 |
+
|
| 171 |
+
# st.write(response)
|
| 172 |
+
|
| 173 |
+
if __name__=="__main__":
|
| 174 |
main()
|