adithiyyha commited on
Commit
90f10bf
·
verified ·
1 Parent(s): abdc674

Update icd9_ui.py

Browse files
Files changed (1) hide show
  1. icd9_ui.py +63 -63
icd9_ui.py CHANGED
@@ -1,63 +1,63 @@
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.")
 
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.")