File size: 16,754 Bytes
a297be3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70be421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bba5f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90f10bf
a297be3
 
bba5f79
90f10bf
bba5f79
 
 
90f10bf
bba5f79
90f10bf
 
 
 
 
a297be3
 
bba5f79
 
a297be3
90f10bf
 
 
 
 
 
 
 
 
 
bba5f79
90f10bf
 
 
 
 
 
 
 
bba5f79
90f10bf
 
 
 
 
 
 
 
bba5f79
90f10bf
 
 
 
bba5f79
a297be3
 
bba5f79
a297be3
bba5f79
a297be3
90f10bf
 
bba5f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90f10bf
bba5f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a297be3
70be421
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# import streamlit as st
# import torch
# from transformers import LongformerTokenizer, LongformerForSequenceClassification

# # Load the fine-tuned model and tokenizer
# model_path = "./clinical_longformer"
# tokenizer = LongformerTokenizer.from_pretrained(model_path)
# model = LongformerForSequenceClassification.from_pretrained(model_path)
# model.eval()  # Set the model to evaluation mode

# # ICD-9 code columns used during training
# icd9_columns = [
#     '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
#     '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
#     '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
#     '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
#     '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
#     '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
# ]

# # Function for making predictions
# def predict_icd9(texts, tokenizer, model, threshold=0.5):
#     inputs = tokenizer(
#         texts,
#         padding="max_length",
#         truncation=True,
#         max_length=512,
#         return_tensors="pt"
#     )
    
#     with torch.no_grad():
#         outputs = model(
#             input_ids=inputs["input_ids"],
#             attention_mask=inputs["attention_mask"]
#         )
#         logits = outputs.logits
#         probabilities = torch.sigmoid(logits)
#         predictions = (probabilities > threshold).int()
    
#     predicted_icd9 = []
#     for pred in predictions:
#         codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
#         predicted_icd9.append(codes)
    
#     return predicted_icd9

# # Streamlit UI
# st.title("ICD-9 Code Prediction")
# st.sidebar.header("Model Options")
# model_option = st.sidebar.selectbox("Select Model", [ "ClinicalLongformer"])
# threshold = st.sidebar.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)

# st.write("### Enter Medical Summary")
# input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")

# if st.button("Predict"):
#     if input_text.strip():
#         predictions = predict_icd9([input_text], tokenizer, model, threshold)
#         st.write("### Predicted ICD-9 Codes")
#         for code in predictions[0]:
#             st.write(f"- {code}")
#     else:
#         st.error("Please enter a medical summary.")

# import torch
# import pandas as pd
# import streamlit as st
# from transformers import LongformerTokenizer, LongformerForSequenceClassification

# # Load the fine-tuned model and tokenizer
# model_path = "./clinical_longformer"
# tokenizer = LongformerTokenizer.from_pretrained(model_path)
# model = LongformerForSequenceClassification.from_pretrained(model_path)
# model.eval()  # Set the model to evaluation mode

# # Load the ICD-9 descriptions from CSV into a dictionary
# icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv")  # Adjust the path to your CSV file
# icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str)  # Ensure ICD9_CODE is string type for matching
# icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE']))  # Remove decimals in ICD9 code for matching

# # ICD-9 code columns used during training
# icd9_columns = [
#     '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
#     '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
#     '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
#     '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
#     '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
#     '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
# ]

# # Function for making predictions
# def predict_icd9(texts, tokenizer, model, threshold=0.5):
#     inputs = tokenizer(
#         texts,
#         padding="max_length",
#         truncation=True,
#         max_length=512,
#         return_tensors="pt"
#     )
    
#     with torch.no_grad():
#         outputs = model(
#             input_ids=inputs["input_ids"],
#             attention_mask=inputs["attention_mask"]
#         )
#         logits = outputs.logits
#         probabilities = torch.sigmoid(logits)
#         predictions = (probabilities > threshold).int()
    
#     predicted_icd9 = []
#     for pred in predictions:
#         codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
#         predicted_icd9.append(codes)
    
#     # Fetch descriptions for the predicted ICD-9 codes from the pre-loaded descriptions
#     predictions_with_desc = []
#     for codes in predicted_icd9:
#         code_with_desc = [(code, icd9_descriptions.get(code.replace('.', ''), "Description not found")) for code in codes]
#         predictions_with_desc.append(code_with_desc)
    
#     return predictions_with_desc

# # Streamlit UI
# st.title("ICD-9 Code Prediction")
# st.sidebar.header("Model Options")
# threshold = st.sidebar.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)

# st.write("### Enter Medical Summary")
# input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")

# if st.button("Predict"):
#     if input_text.strip():
#         predictions = predict_icd9([input_text], tokenizer, model, threshold)
#         st.write("### Predicted ICD-9 Codes and Descriptions")
#         for code, description in predictions[0]:
#             st.write(f"- {code}: {description}")
#     else:
#         st.error("Please enter a medical summary.")
# import torch
# import pandas as pd
# import streamlit as st
# from transformers import LongformerTokenizer, LongformerForSequenceClassification

# # Load the fine-tuned model and tokenizer
# model_path = "./clinical_longformer"
# tokenizer = LongformerTokenizer.from_pretrained(model_path)
# model = LongformerForSequenceClassification.from_pretrained(model_path)
# model.eval()  # Set the model to evaluation mode

# # Load the ICD-9 descriptions from CSV into a dictionary
# icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv")  # Adjust the path to your CSV file
# icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str)  # Ensure ICD9_CODE is string type
# icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE']))  # Remove decimals for matching

# # Load the ICD-9 to ICD-10 mapping
# icd9_to_icd10 = {}
# with open("2015_I9gem.txt", "r") as file:
#     for line in file:
#         parts = line.strip().split()
#         if len(parts) == 3:
#             icd9, icd10, _ = parts
#             icd9_to_icd10[icd9] = icd10

# # ICD-9 code columns used during training
# icd9_columns = [
#     '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
#     '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
#     '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
#     '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
#     '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
#     '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
# ]

# # Function for making predictions and mapping to ICD-10
# def predict_icd9(texts, tokenizer, model, threshold=0.5):
#     inputs = tokenizer(
#         texts,
#         padding="max_length",
#         truncation=True,
#         max_length=512,
#         return_tensors="pt"
#     )

#     with torch.no_grad():
#         outputs = model(
#             input_ids=inputs["input_ids"],
#             attention_mask=inputs["attention_mask"]
#         )
#         logits = outputs.logits
#         probabilities = torch.sigmoid(logits)
#         predictions = (probabilities > threshold).int()

#     predicted_icd9 = []
#     for pred in predictions:
#         codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
#         predicted_icd9.append(codes)

#     # Fetch descriptions and map to ICD-10 codes
#     predictions_with_desc = []
#     for codes in predicted_icd9:
#         code_with_desc = []
#         for code in codes:
#             icd9_stripped = code.replace('.', '')
#             icd10_code = icd9_to_icd10.get(icd9_stripped, "Mapping not found")
#             icd9_desc = icd9_descriptions.get(icd9_stripped, "Description not found")
#             code_with_desc.append((code, icd9_desc, icd10_code))
#         predictions_with_desc.append(code_with_desc)

#     return predictions_with_desc

# # Streamlit UI
# st.title("ICD-9 to ICD-10 Code Prediction")
# st.sidebar.header("Model Options")
# threshold = st.sidebar.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)

# st.write("### Enter Medical Summary")
# input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")

# if st.button("Predict"):
#     if input_text.strip():
#         predictions = predict_icd9([input_text], tokenizer, model, threshold)
#         st.write("### Predicted ICD-9 and ICD-10 Codes with Descriptions")
#         for icd9_code, description, icd10_code in predictions[0]:
#             st.write(f"- ICD-9: {icd9_code} ({description}) -> ICD-10: {icd10_code}")
#     else:
#         st.error("Please enter a medical summary.")

import os
import torch
import pandas as pd
import streamlit as st
from PIL import Image
from transformers import LongformerTokenizer, LongformerForSequenceClassification
from phi.agent import Agent
from phi.model.google import Gemini
from phi.tools.duckduckgo import DuckDuckGo

# Load the fine-tuned ICD-9 model and tokenizer
model_path = "./clinical_longformer"
tokenizer = LongformerTokenizer.from_pretrained(model_path)
model = LongformerForSequenceClassification.from_pretrained(model_path)
model.eval()  # Set the model to evaluation mode

# Load the ICD-9 descriptions from CSV into a dictionary
icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv")  # Adjust the path to your CSV file
icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str)  # Ensure ICD9_CODE is string type for matching
icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE']))  # Remove decimals in ICD9 code for matching

# ICD-9 code columns used during training
icd9_columns = [
    '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
    '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
    '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
    '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
    '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
    '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
]

# Function for making ICD-9 predictions
def predict_icd9(texts, tokenizer, model, threshold=0.5):
    inputs = tokenizer(
        texts,
        padding="max_length",
        truncation=True,
        max_length=512,
        return_tensors="pt"
    )
    
    with torch.no_grad():
        outputs = model(
            input_ids=inputs["input_ids"],
            attention_mask=inputs["attention_mask"]
        )
        logits = outputs.logits
        probabilities = torch.sigmoid(logits)
        predictions = (probabilities > threshold).int()
    
    predicted_icd9 = []
    for pred in predictions:
        codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
        predicted_icd9.append(codes)
    
    predictions_with_desc = []
    for codes in predicted_icd9:
        code_with_desc = [(code, icd9_descriptions.get(code.replace('.', ''), "Description not found")) for code in codes]
        predictions_with_desc.append(code_with_desc)
    
    return predictions_with_desc

# Streamlit UI
st.title("Medical Diagnosis Assistant")
option = st.selectbox(
    "Choose Diagnosis Method",
    ("ICD-9 Code Prediction", "Medical Image Analysis")
)

# ICD-9 Code Prediction
if option == "ICD-9 Code Prediction":
    st.write("### Enter Medical Summary")
    input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")

    threshold = st.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)

    if st.button("Predict ICD-9 Codes"):
        if input_text.strip():
            predictions = predict_icd9([input_text], tokenizer, model, threshold)
            st.write("### Predicted ICD-9 Codes and Descriptions")
            for code, description in predictions[0]:
                st.write(f"- {code}: {description}")
        else:
            st.error("Please enter a medical summary.")

# Medical Image Analysis
elif option == "Medical Image Analysis":
    if "GOOGLE_API_KEY" not in st.session_state:
        st.warning("Please enter your Google API Key in the sidebar to continue")
    else:
        medical_agent = Agent(
            model=Gemini(
                api_key=st.session_state.GOOGLE_API_KEY,
                id="gemini-2.0-flash-exp"
            ),
            tools=[DuckDuckGo()],
            markdown=True
        )

        query = """
        You are a highly skilled medical imaging expert with extensive knowledge in radiology and diagnostic imaging. Analyze the patient's medical image and structure your response as follows:

        ### 1. Image Type & Region
        - Specify imaging modality (X-ray/MRI/CT/Ultrasound/etc.)
        - Identify the patient's anatomical region and positioning
        - Comment on image quality and technical adequacy

        ### 2. Key Findings
        - List primary observations systematically
        - Note any abnormalities in the patient's imaging with precise descriptions
        - Include measurements and densities where relevant
        - Describe location, size, shape, and characteristics
        - Rate severity: Normal/Mild/Moderate/Severe

        ### 3. Diagnostic Assessment
        - Provide primary diagnosis with confidence level
        - List differential diagnoses in order of likelihood
        - Support each diagnosis with observed evidence from the patient's imaging
        - Note any critical or urgent findings

        ### 4. Patient-Friendly Explanation
        - Explain the findings in simple, clear language that the patient can understand
        - Avoid medical jargon or provide clear definitions
        - Include visual analogies if helpful
        - Address common patient concerns related to these findings

        ### 5. Research Context
        - Use the DuckDuckGo search tool to find recent medical literature about similar cases
        - Provide a list of relevant medical links
        - Include key references to support your analysis
        """

        upload_container = st.container()
        image_container = st.container()
        analysis_container = st.container()

        with upload_container:
            uploaded_file = st.file_uploader(
                "Upload Medical Image",
                type=["jpg", "jpeg", "png", "dicom"],
                help="Supported formats: JPG, JPEG, PNG, DICOM"
            )

        if uploaded_file is not None:
            with image_container:
                col1, col2, col3 = st.columns([1, 2, 1])
                with col2:
                    image = Image.open(uploaded_file)
                    width, height = image.size
                    aspect_ratio = width / height
                    new_width = 500
                    new_height = int(new_width / aspect_ratio)
                    resized_image = image.resize((new_width, new_height))
                    
                    st.image(resized_image, caption="Uploaded Medical Image", use_container_width=True)
                    
                    analyze_button = st.button("πŸ” Analyze Image")

            with analysis_container:
                if analyze_button:
                    image_path = "temp_medical_image.png"
                    with open(image_path, "wb") as f:
                        f.write(uploaded_file.getbuffer())
                    
                    with st.spinner("πŸ”„ Analyzing image... Please wait."):
                        try:
                            response = medical_agent.run(query, images=[image_path])
                            st.markdown("### πŸ“‹ Analysis Results")
                            st.markdown(response.content)
                        except Exception as e:
                            st.error(f"Analysis error: {e}")
                        finally:
                            if os.path.exists(image_path):
                                os.remove(image_path)
        else:
            st.info("πŸ‘† Please upload a medical image to begin analysis")