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