adithiyyha commited on
Commit
a297be3
·
verified ·
1 Parent(s): 85ce415

Update icd9_ui.py

Browse files
Files changed (1) hide show
  1. icd9_ui.py +82 -6
icd9_ui.py CHANGED
@@ -1,5 +1,70 @@
1
- import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import torch
 
 
3
  from transformers import LongformerTokenizer, LongformerForSequenceClassification
4
 
5
  # Load the fine-tuned model and tokenizer
@@ -8,6 +73,11 @@ tokenizer = LongformerTokenizer.from_pretrained(model_path)
8
  model = LongformerForSequenceClassification.from_pretrained(model_path)
9
  model.eval() # Set the model to evaluation mode
10
 
 
 
 
 
 
11
  # ICD-9 code columns used during training
12
  icd9_columns = [
13
  '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
@@ -42,12 +112,17 @@ def predict_icd9(texts, tokenizer, model, threshold=0.5):
42
  codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
43
  predicted_icd9.append(codes)
44
 
45
- return predicted_icd9
 
 
 
 
 
 
46
 
47
  # Streamlit UI
48
  st.title("ICD-9 Code Prediction")
49
  st.sidebar.header("Model Options")
50
- model_option = st.sidebar.selectbox("Select Model", [ "ClinicalLongformer"])
51
  threshold = st.sidebar.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)
52
 
53
  st.write("### Enter Medical Summary")
@@ -56,8 +131,9 @@ input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes h
56
  if st.button("Predict"):
57
  if input_text.strip():
58
  predictions = predict_icd9([input_text], tokenizer, model, threshold)
59
- st.write("### Predicted ICD-9 Codes")
60
- for code in predictions[0]:
61
- st.write(f"- {code}")
62
  else:
63
  st.error("Please enter a medical summary.")
 
 
1
+ # import streamlit as st
2
+ # import torch
3
+ # from transformers import LongformerTokenizer, LongformerForSequenceClassification
4
+
5
+ # # Load the fine-tuned model and tokenizer
6
+ # model_path = "./clinical_longformer"
7
+ # tokenizer = LongformerTokenizer.from_pretrained(model_path)
8
+ # model = LongformerForSequenceClassification.from_pretrained(model_path)
9
+ # model.eval() # Set the model to evaluation mode
10
+
11
+ # # ICD-9 code columns used during training
12
+ # icd9_columns = [
13
+ # '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
14
+ # '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
15
+ # '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
16
+ # '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
17
+ # '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
18
+ # '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
19
+ # ]
20
+
21
+ # # Function for making predictions
22
+ # def predict_icd9(texts, tokenizer, model, threshold=0.5):
23
+ # inputs = tokenizer(
24
+ # texts,
25
+ # padding="max_length",
26
+ # truncation=True,
27
+ # max_length=512,
28
+ # return_tensors="pt"
29
+ # )
30
+
31
+ # with torch.no_grad():
32
+ # outputs = model(
33
+ # input_ids=inputs["input_ids"],
34
+ # attention_mask=inputs["attention_mask"]
35
+ # )
36
+ # logits = outputs.logits
37
+ # probabilities = torch.sigmoid(logits)
38
+ # predictions = (probabilities > threshold).int()
39
+
40
+ # predicted_icd9 = []
41
+ # for pred in predictions:
42
+ # codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
43
+ # predicted_icd9.append(codes)
44
+
45
+ # return predicted_icd9
46
+
47
+ # # Streamlit UI
48
+ # st.title("ICD-9 Code Prediction")
49
+ # st.sidebar.header("Model Options")
50
+ # model_option = st.sidebar.selectbox("Select Model", [ "ClinicalLongformer"])
51
+ # threshold = st.sidebar.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)
52
+
53
+ # st.write("### Enter Medical Summary")
54
+ # input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")
55
+
56
+ # if st.button("Predict"):
57
+ # if input_text.strip():
58
+ # predictions = predict_icd9([input_text], tokenizer, model, threshold)
59
+ # st.write("### Predicted ICD-9 Codes")
60
+ # for code in predictions[0]:
61
+ # st.write(f"- {code}")
62
+ # else:
63
+ # st.error("Please enter a medical summary.")
64
+
65
  import torch
66
+ import pandas as pd
67
+ import streamlit as st
68
  from transformers import LongformerTokenizer, LongformerForSequenceClassification
69
 
70
  # Load the fine-tuned model and tokenizer
 
73
  model = LongformerForSequenceClassification.from_pretrained(model_path)
74
  model.eval() # Set the model to evaluation mode
75
 
76
+ # Load the ICD-9 descriptions from CSV into a dictionary
77
+ icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv") # Adjust the path to your CSV file
78
+ icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str) # Ensure ICD9_CODE is string type for matching
79
+ icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE'])) # Remove decimals in ICD9 code for matching
80
+
81
  # ICD-9 code columns used during training
82
  icd9_columns = [
83
  '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
 
112
  codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
113
  predicted_icd9.append(codes)
114
 
115
+ # Fetch descriptions for the predicted ICD-9 codes from the pre-loaded descriptions
116
+ predictions_with_desc = []
117
+ for codes in predicted_icd9:
118
+ code_with_desc = [(code, icd9_descriptions.get(code.replace('.', ''), "Description not found")) for code in codes]
119
+ predictions_with_desc.append(code_with_desc)
120
+
121
+ return predictions_with_desc
122
 
123
  # Streamlit UI
124
  st.title("ICD-9 Code Prediction")
125
  st.sidebar.header("Model Options")
 
126
  threshold = st.sidebar.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)
127
 
128
  st.write("### Enter Medical Summary")
 
131
  if st.button("Predict"):
132
  if input_text.strip():
133
  predictions = predict_icd9([input_text], tokenizer, model, threshold)
134
+ st.write("### Predicted ICD-9 Codes and Descriptions")
135
+ for code, description in predictions[0]:
136
+ st.write(f"- {code}: {description}")
137
  else:
138
  st.error("Please enter a medical summary.")
139
+