adithiyyha commited on
Commit
e1eca6d
Β·
verified Β·
1 Parent(s): 3d1a0d5

Update icd9_ui.py

Browse files
Files changed (1) hide show
  1. icd9_ui.py +543 -543
icd9_ui.py CHANGED
@@ -1,147 +1,428 @@
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
71
- # # model_path = "./clinical_longformer"
72
- # # tokenizer = LongformerTokenizer.from_pretrained(model_path)
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',
84
- # # '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
85
- # # '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
86
- # # '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
87
- # # '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
88
- # # '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
89
- # # ]
90
 
91
- # # # Function for making predictions
92
- # # def predict_icd9(texts, tokenizer, model, threshold=0.5):
93
- # # inputs = tokenizer(
94
- # # texts,
95
- # # padding="max_length",
96
- # # truncation=True,
97
- # # max_length=512,
98
- # # return_tensors="pt"
99
- # # )
100
 
101
- # # with torch.no_grad():
102
- # # outputs = model(
103
- # # input_ids=inputs["input_ids"],
104
- # # attention_mask=inputs["attention_mask"]
105
- # # )
106
- # # logits = outputs.logits
107
- # # probabilities = torch.sigmoid(logits)
108
- # # predictions = (probabilities > threshold).int()
109
 
110
- # # predicted_icd9 = []
111
- # # for pred in predictions:
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")
129
- # # input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")
130
-
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
  # # import torch
140
  # # import pandas as pd
141
  # # import streamlit as st
 
142
  # # from transformers import LongformerTokenizer, LongformerForSequenceClassification
 
 
 
143
 
144
- # # # Load the fine-tuned model and tokenizer
 
 
 
 
 
 
 
 
 
 
145
  # # model_path = "./clinical_longformer"
146
  # # tokenizer = LongformerTokenizer.from_pretrained(model_path)
147
  # # model = LongformerForSequenceClassification.from_pretrained(model_path)
@@ -149,17 +430,8 @@
149
 
150
  # # # Load the ICD-9 descriptions from CSV into a dictionary
151
  # # icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv") # Adjust the path to your CSV file
152
- # # icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str) # Ensure ICD9_CODE is string type
153
- # # icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE'])) # Remove decimals for matching
154
-
155
- # # # Load the ICD-9 to ICD-10 mapping
156
- # # icd9_to_icd10 = {}
157
- # # with open("2015_I9gem.txt", "r") as file:
158
- # # for line in file:
159
- # # parts = line.strip().split()
160
- # # if len(parts) == 3:
161
- # # icd9, icd10, _ = parts
162
- # # icd9_to_icd10[icd9] = icd10
163
 
164
  # # # ICD-9 code columns used during training
165
  # # icd9_columns = [
@@ -171,7 +443,7 @@
171
  # # '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
172
  # # ]
173
 
174
- # # # Function for making predictions and mapping to ICD-10
175
  # # def predict_icd9(texts, tokenizer, model, threshold=0.5):
176
  # # inputs = tokenizer(
177
  # # texts,
@@ -180,7 +452,7 @@
180
  # # max_length=512,
181
  # # return_tensors="pt"
182
  # # )
183
-
184
  # # with torch.no_grad():
185
  # # outputs = model(
186
  # # input_ids=inputs["input_ids"],
@@ -189,105 +461,20 @@
189
  # # logits = outputs.logits
190
  # # probabilities = torch.sigmoid(logits)
191
  # # predictions = (probabilities > threshold).int()
192
-
193
  # # predicted_icd9 = []
194
  # # for pred in predictions:
195
  # # codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
196
  # # predicted_icd9.append(codes)
197
-
198
- # # # Fetch descriptions and map to ICD-10 codes
199
  # # predictions_with_desc = []
200
  # # for codes in predicted_icd9:
201
- # # code_with_desc = []
202
- # # for code in codes:
203
- # # icd9_stripped = code.replace('.', '')
204
- # # icd10_code = icd9_to_icd10.get(icd9_stripped, "Mapping not found")
205
- # # icd9_desc = icd9_descriptions.get(icd9_stripped, "Description not found")
206
- # # code_with_desc.append((code, icd9_desc, icd10_code))
207
  # # predictions_with_desc.append(code_with_desc)
208
-
209
  # # return predictions_with_desc
210
 
211
  # # # Streamlit UI
212
- # # st.title("ICD-9 to ICD-10 Code Prediction")
213
- # # st.sidebar.header("Model Options")
214
- # # threshold = st.sidebar.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)
215
-
216
- # # st.write("### Enter Medical Summary")
217
- # # input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")
218
-
219
- # # if st.button("Predict"):
220
- # # if input_text.strip():
221
- # # predictions = predict_icd9([input_text], tokenizer, model, threshold)
222
- # # st.write("### Predicted ICD-9 and ICD-10 Codes with Descriptions")
223
- # # for icd9_code, description, icd10_code in predictions[0]:
224
- # # st.write(f"- ICD-9: {icd9_code} ({description}) -> ICD-10: {icd10_code}")
225
- # # else:
226
- # # st.error("Please enter a medical summary.")
227
-
228
- # # import os
229
- # # import torch
230
- # # import pandas as pd
231
- # # import streamlit as st
232
- # # from PIL import Image
233
- # # from transformers import LongformerTokenizer, LongformerForSequenceClassification
234
- # # from phi.agent import Agent
235
- # # from phi.model.google import Gemini
236
- # # from phi.tools.duckduckgo import DuckDuckGo
237
-
238
- # # # Load the fine-tuned ICD-9 model and tokenizer
239
- # # model_path = "./clinical_longformer"
240
- # # tokenizer = LongformerTokenizer.from_pretrained(model_path)
241
- # # model = LongformerForSequenceClassification.from_pretrained(model_path)
242
- # # model.eval() # Set the model to evaluation mode
243
-
244
- # # # Load the ICD-9 descriptions from CSV into a dictionary
245
- # # icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv") # Adjust the path to your CSV file
246
- # # icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str) # Ensure ICD9_CODE is string type for matching
247
- # # icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE'])) # Remove decimals in ICD9 code for matching
248
-
249
- # # # ICD-9 code columns used during training
250
- # # icd9_columns = [
251
- # # '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
252
- # # '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
253
- # # '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
254
- # # '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
255
- # # '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
256
- # # '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
257
- # # ]
258
-
259
- # # # Function for making ICD-9 predictions
260
- # # def predict_icd9(texts, tokenizer, model, threshold=0.5):
261
- # # inputs = tokenizer(
262
- # # texts,
263
- # # padding="max_length",
264
- # # truncation=True,
265
- # # max_length=512,
266
- # # return_tensors="pt"
267
- # # )
268
-
269
- # # with torch.no_grad():
270
- # # outputs = model(
271
- # # input_ids=inputs["input_ids"],
272
- # # attention_mask=inputs["attention_mask"]
273
- # # )
274
- # # logits = outputs.logits
275
- # # probabilities = torch.sigmoid(logits)
276
- # # predictions = (probabilities > threshold).int()
277
-
278
- # # predicted_icd9 = []
279
- # # for pred in predictions:
280
- # # codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
281
- # # predicted_icd9.append(codes)
282
-
283
- # # predictions_with_desc = []
284
- # # for codes in predicted_icd9:
285
- # # code_with_desc = [(code, icd9_descriptions.get(code.replace('.', ''), "Description not found")) for code in codes]
286
- # # predictions_with_desc.append(code_with_desc)
287
-
288
- # # return predictions_with_desc
289
-
290
- # # Streamlit UI
291
  # # st.title("Medical Diagnosis Assistant")
292
  # # option = st.selectbox(
293
  # # "Choose Diagnosis Method",
@@ -310,14 +497,14 @@
310
  # # else:
311
  # # st.error("Please enter a medical summary.")
312
 
313
- # # Medical Image Analysis
314
  # # elif option == "Medical Image Analysis":
315
  # # if "GOOGLE_API_KEY" not in st.session_state:
316
  # # st.warning("Please enter your Google API Key in the sidebar to continue")
317
  # # else:
318
  # # medical_agent = Agent(
319
  # # model=Gemini(
320
- # # api_key=st.session_state.GOOGLE_API_KEY,
321
  # # id="gemini-2.0-flash-exp"
322
  # # ),
323
  # # tools=[DuckDuckGo()],
@@ -326,81 +513,76 @@
326
 
327
  # # query = """
328
  # # 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:
329
-
330
  # # ### 1. Image Type & Region
331
  # # - Specify imaging modality (X-ray/MRI/CT/Ultrasound/etc.)
332
  # # - Identify the patient's anatomical region and positioning
333
  # # - Comment on image quality and technical adequacy
334
-
335
  # # ### 2. Key Findings
336
  # # - List primary observations systematically
337
  # # - Note any abnormalities in the patient's imaging with precise descriptions
338
  # # - Include measurements and densities where relevant
339
  # # - Describe location, size, shape, and characteristics
340
  # # - Rate severity: Normal/Mild/Moderate/Severe
341
-
342
  # # ### 3. Diagnostic Assessment
343
  # # - Provide primary diagnosis with confidence level
344
  # # - List differential diagnoses in order of likelihood
345
  # # - Support each diagnosis with observed evidence from the patient's imaging
346
- # # - Note any critical or urgent findings
347
-
348
- # # ### 4. Patient-Friendly Explanation
349
- # # - Explain the findings in simple, clear language that the patient can understand
350
- # # - Avoid medical jargon or provide clear definitions
351
- # # - Include visual analogies if helpful
352
- # # - Address common patient concerns related to these findings
353
-
354
- # # ### 5. Research Context
355
- # # - Use the DuckDuckGo search tool to find recent medical literature about similar cases
356
- # # - Provide a list of relevant medical links
357
- # # - Include key references to support your analysis
358
- # # """
359
-
360
- # # upload_container = st.container()
361
- # # image_container = st.container()
362
- # # analysis_container = st.container()
363
-
364
- # # with upload_container:
365
- # # uploaded_file = st.file_uploader(
366
- # # "Upload Medical Image",
367
- # # type=["jpg", "jpeg", "png", "dicom"],
368
- # # help="Supported formats: JPG, JPEG, PNG, DICOM"
369
- # # )
370
-
371
- # # if uploaded_file is not None:
372
- # # with image_container:
373
- # # col1, col2, col3 = st.columns([1, 2, 1])
374
- # # with col2:
375
- # # image = Image.open(uploaded_file)
376
- # # width, height = image.size
377
- # # aspect_ratio = width / height
378
- # # new_width = 500
379
- # # new_height = int(new_width / aspect_ratio)
380
- # # resized_image = image.resize((new_width, new_height))
381
 
382
- # # st.image(resized_image, caption="Uploaded Medical Image", use_container_width=True)
383
 
384
- # # analyze_button = st.button("πŸ” Analyze Image")
385
 
386
- # # with analysis_container:
387
- # # if analyze_button:
388
- # # image_path = "temp_medical_image.png"
389
- # # with open(image_path, "wb") as f:
390
- # # f.write(uploaded_file.getbuffer())
391
 
392
- # # with st.spinner("πŸ”„ Analyzing image... Please wait."):
393
- # # try:
394
- # # response = medical_agent.run(query, images=[image_path])
395
- # # st.markdown("### πŸ“‹ Analysis Results")
396
- # # st.markdown(response.content)
397
- # # except Exception as e:
398
- # # st.error(f"Analysis error: {e}")
399
- # # finally:
400
- # # if os.path.exists(image_path):
401
- # # os.remove(image_path)
402
- # # else:
403
- # # st.info("πŸ‘† Please upload a medical image to begin analysis")
404
 
405
  # import os
406
  # import torch
@@ -412,16 +594,6 @@
412
  # from phi.model.google import Gemini
413
  # from phi.tools.duckduckgo import DuckDuckGo
414
 
415
- # # Sidebar for Google API Key input
416
- # st.sidebar.title("Settings")
417
- # st.sidebar.write("Enter your Google API Key below for the Medical Image Analysis feature.")
418
- # api_key = st.sidebar.text_input("Google API Key", type="password")
419
-
420
- # if api_key:
421
- # st.session_state["GOOGLE_API_KEY"] = api_key
422
- # else:
423
- # st.session_state.pop("GOOGLE_API_KEY", None)
424
-
425
  # # Load the fine-tuned ICD-9 model and tokenizer
426
  # model_path = "./clinical_longformer"
427
  # tokenizer = LongformerTokenizer.from_pretrained(model_path)
@@ -429,7 +601,7 @@
429
  # model.eval() # Set the model to evaluation mode
430
 
431
  # # Load the ICD-9 descriptions from CSV into a dictionary
432
- # icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv") # Adjust the path to your CSV file
433
  # icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str) # Ensure ICD9_CODE is string type for matching
434
  # icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE'])) # Remove decimals in ICD9 code for matching
435
 
@@ -452,7 +624,6 @@
452
  # max_length=512,
453
  # return_tensors="pt"
454
  # )
455
-
456
  # with torch.no_grad():
457
  # outputs = model(
458
  # input_ids=inputs["input_ids"],
@@ -474,6 +645,9 @@
474
 
475
  # return predictions_with_desc
476
 
 
 
 
477
  # # Streamlit UI
478
  # st.title("Medical Diagnosis Assistant")
479
  # option = st.selectbox(
@@ -499,260 +673,86 @@
499
 
500
  # # Medical Image Analysis
501
  # elif option == "Medical Image Analysis":
502
- # if "GOOGLE_API_KEY" not in st.session_state:
503
- # st.warning("Please enter your Google API Key in the sidebar to continue")
504
- # else:
505
- # medical_agent = Agent(
506
- # model=Gemini(
507
- # api_key=st.session_state["GOOGLE_API_KEY"],
508
- # id="gemini-2.0-flash-exp"
509
- # ),
510
- # tools=[DuckDuckGo()],
511
- # markdown=True
512
- # )
513
-
514
- # query = """
515
- # 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:
516
- # ### 1. Image Type & Region
517
- # - Specify imaging modality (X-ray/MRI/CT/Ultrasound/etc.)
518
- # - Identify the patient's anatomical region and positioning
519
- # - Comment on image quality and technical adequacy
520
- # ### 2. Key Findings
521
- # - List primary observations systematically
522
- # - Note any abnormalities in the patient's imaging with precise descriptions
523
- # - Include measurements and densities where relevant
524
- # - Describe location, size, shape, and characteristics
525
- # - Rate severity: Normal/Mild/Moderate/Severe
526
- # ### 3. Diagnostic Assessment
527
- # - Provide primary diagnosis with confidence level
528
- # - List differential diagnoses in order of likelihood
529
- # - Support each diagnosis with observed evidence from the patient's imaging
530
- # - Note any critical or urgent findings
531
- # ### 4. Patient-Friendly Explanation
532
- # - Explain the findings in simple, clear language that the patient can understand
533
- # - Avoid medical jargon or provide clear definitions
534
- # - Include visual analogies if helpful
535
- # - Address common patient concerns related to these findings
536
- # ### 5. Research Context
537
- # - Use the DuckDuckGo search tool to find recent medical literature about similar cases
538
- # - Provide a list of relevant medical links
539
- # - Include key references to support your analysis
540
- # """
541
-
542
- # upload_container = st.container()
543
- # image_container = st.container()
544
- # analysis_container = st.container()
545
-
546
- # with upload_container:
547
- # uploaded_file = st.file_uploader(
548
- # "Upload Medical Image",
549
- # type=["jpg", "jpeg", "png", "dicom"],
550
- # help="Supported formats: JPG, JPEG, PNG, DICOM"
551
- # )
552
-
553
- # if uploaded_file is not None:
554
- # with image_container:
555
- # col1, col2, col3 = st.columns([1, 2, 1])
556
- # with col2:
557
- # image = Image.open(uploaded_file)
558
- # width, height = image.size
559
- # aspect_ratio = width / height
560
- # new_width = 500
561
- # new_height = int(new_width / aspect_ratio)
562
- # resized_image = image.resize((new_width, new_height))
563
-
564
- # st.image(resized_image, caption="Uploaded Medical Image", use_container_width=True)
565
-
566
- # analyze_button = st.button("πŸ” Analyze Image")
567
-
568
- # with analysis_container:
569
- # if analyze_button:
570
- # image_path = "temp_medical_image.png"
571
- # with open(image_path, "wb") as f:
572
- # f.write(uploaded_file.getbuffer())
573
-
574
- # with st.spinner("πŸ”„ Analyzing image... Please wait."):
575
- # try:
576
- # response = medical_agent.run(query, images=[image_path])
577
- # st.markdown("### πŸ“‹ Analysis Results")
578
- # st.markdown(response.content)
579
- # except Exception as e:
580
- # st.error(f"Analysis error: {e}")
581
- # finally:
582
- # if os.path.exists(image_path):
583
- # os.remove(image_path)
584
- # else:
585
- # st.info("πŸ‘† Please upload a medical image to begin analysis")
586
-
587
- import os
588
- import torch
589
- import pandas as pd
590
- import streamlit as st
591
- from PIL import Image
592
- from transformers import LongformerTokenizer, LongformerForSequenceClassification
593
- from phi.agent import Agent
594
- from phi.model.google import Gemini
595
- from phi.tools.duckduckgo import DuckDuckGo
596
-
597
- # Load the fine-tuned ICD-9 model and tokenizer
598
- model_path = "./clinical_longformer"
599
- tokenizer = LongformerTokenizer.from_pretrained(model_path)
600
- model = LongformerForSequenceClassification.from_pretrained(model_path)
601
- model.eval() # Set the model to evaluation mode
602
-
603
- # Load the ICD-9 descriptions from CSV into a dictionary
604
- icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv")
605
- icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str) # Ensure ICD9_CODE is string type for matching
606
- icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE'])) # Remove decimals in ICD9 code for matching
607
-
608
- # ICD-9 code columns used during training
609
- icd9_columns = [
610
- '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
611
- '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
612
- '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
613
- '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
614
- '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
615
- '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
616
- ]
617
-
618
- # Function for making ICD-9 predictions
619
- def predict_icd9(texts, tokenizer, model, threshold=0.5):
620
- inputs = tokenizer(
621
- texts,
622
- padding="max_length",
623
- truncation=True,
624
- max_length=512,
625
- return_tensors="pt"
626
- )
627
- with torch.no_grad():
628
- outputs = model(
629
- input_ids=inputs["input_ids"],
630
- attention_mask=inputs["attention_mask"]
631
- )
632
- logits = outputs.logits
633
- probabilities = torch.sigmoid(logits)
634
- predictions = (probabilities > threshold).int()
635
-
636
- predicted_icd9 = []
637
- for pred in predictions:
638
- codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
639
- predicted_icd9.append(codes)
640
-
641
- predictions_with_desc = []
642
- for codes in predicted_icd9:
643
- code_with_desc = [(code, icd9_descriptions.get(code.replace('.', ''), "Description not found")) for code in codes]
644
- predictions_with_desc.append(code_with_desc)
645
-
646
- return predictions_with_desc
647
-
648
- # Define the API key directly in the code
649
- GOOGLE_API_KEY = "AIzaSyA24A6egT3L0NAKkkw9QHjfoizp7cJUTaA"
650
-
651
- # Streamlit UI
652
- st.title("Medical Diagnosis Assistant")
653
- option = st.selectbox(
654
- "Choose Diagnosis Method",
655
- ("ICD-9 Code Prediction", "Medical Image Analysis")
656
- )
657
-
658
- # ICD-9 Code Prediction
659
- if option == "ICD-9 Code Prediction":
660
- st.write("### Enter Medical Summary")
661
- input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")
662
-
663
- threshold = st.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)
664
-
665
- if st.button("Predict ICD-9 Codes"):
666
- if input_text.strip():
667
- predictions = predict_icd9([input_text], tokenizer, model, threshold)
668
- st.write("### Predicted ICD-9 Codes and Descriptions")
669
- for code, description in predictions[0]:
670
- st.write(f"- {code}: {description}")
671
- else:
672
- st.error("Please enter a medical summary.")
673
-
674
- # Medical Image Analysis
675
- elif option == "Medical Image Analysis":
676
- medical_agent = Agent(
677
- model=Gemini(
678
- api_key=GOOGLE_API_KEY,
679
- id="gemini-2.0-flash-exp"
680
- ),
681
- tools=[DuckDuckGo()],
682
- markdown=True
683
- )
684
 
685
- query = """
686
- 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:
687
- ### 1. Image Type & Region
688
- - Specify imaging modality (X-ray/MRI/CT/Ultrasound/etc.)
689
- - Identify the patient's anatomical region and positioning
690
- - Comment on image quality and technical adequacy
691
- ### 2. Key Findings
692
- - List primary observations systematically
693
- - Note any abnormalities in the patient's imaging with precise descriptions
694
- - Include measurements and densities where relevant
695
- - Describe location, size, shape, and characteristics
696
- - Rate severity: Normal/Mild/Moderate/Severe
697
- ### 3. Diagnostic Assessment
698
- - Provide primary diagnosis with confidence level
699
- - List differential diagnoses in order of likelihood
700
- - Support each diagnosis with observed evidence from the patient's imaging
701
- - Note any critical or urgent findings
702
- ### 4. Patient-Friendly Explanation
703
- - Explain the findings in simple, clear language that the patient can understand
704
- - Avoid medical jargon or provide clear definitions
705
- - Include visual analogies if helpful
706
- - Address common patient concerns related to these findings
707
- ### 5. Research Context
708
- - Use the DuckDuckGo search tool to find recent medical literature about similar cases
709
- - Provide a list of relevant medical links
710
- - Include key references to support your analysis
711
- """
712
-
713
- upload_container = st.container()
714
- image_container = st.container()
715
- analysis_container = st.container()
716
-
717
- with upload_container:
718
- uploaded_file = st.file_uploader(
719
- "Upload Medical Image",
720
- type=["jpg", "jpeg", "png", "dicom"],
721
- help="Supported formats: JPG, JPEG, PNG, DICOM"
722
- )
723
 
724
- if uploaded_file is not None:
725
- with image_container:
726
- col1, col2, col3 = st.columns([1, 2, 1])
727
- with col2:
728
- image = Image.open(uploaded_file)
729
- width, height = image.size
730
- aspect_ratio = width / height
731
- new_width = 500
732
- new_height = int(new_width / aspect_ratio)
733
- resized_image = image.resize((new_width, new_height))
734
 
735
- st.image(resized_image, caption="Uploaded Medical Image", use_container_width=True)
736
 
737
- analyze_button = st.button("πŸ” Analyze Image")
738
 
739
- with analysis_container:
740
- if analyze_button:
741
- image_path = "temp_medical_image.png"
742
- with open(image_path, "wb") as f:
743
- f.write(uploaded_file.getbuffer())
744
 
745
- with st.spinner("πŸ”„ Analyzing image... Please wait."):
746
- try:
747
- response = medical_agent.run(query, images=[image_path])
748
- st.markdown("### πŸ“‹ Analysis Results")
749
- st.markdown(response.content)
750
- except Exception as e:
751
- st.error(f"Analysis error: {e}")
752
- finally:
753
- if os.path.exists(image_path):
754
- os.remove(image_path)
755
- else:
756
- st.info("πŸ‘† Please upload a medical image to begin analysis")
757
 
758
 
 
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
71
+ # model_path = "./clinical_longformer"
72
+ # tokenizer = LongformerTokenizer.from_pretrained(model_path)
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',
84
+ # '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
85
+ # '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
86
+ # '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
87
+ # '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
88
+ # '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
89
+ # ]
90
 
91
+ # # Function for making predictions
92
+ # def predict_icd9(texts, tokenizer, model, threshold=0.5):
93
+ # inputs = tokenizer(
94
+ # texts,
95
+ # padding="max_length",
96
+ # truncation=True,
97
+ # max_length=512,
98
+ # return_tensors="pt"
99
+ # )
100
 
101
+ # with torch.no_grad():
102
+ # outputs = model(
103
+ # input_ids=inputs["input_ids"],
104
+ # attention_mask=inputs["attention_mask"]
105
+ # )
106
+ # logits = outputs.logits
107
+ # probabilities = torch.sigmoid(logits)
108
+ # predictions = (probabilities > threshold).int()
109
 
110
+ # predicted_icd9 = []
111
+ # for pred in predictions:
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
+
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")
129
+ # input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")
130
+
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
+ # import torch
140
+ # # # # import pandas as pd
141
+ # # # # import streamlit as st
142
+ # # # # from transformers import LongformerTokenizer, LongformerForSequenceClassification
143
+
144
+ # # # # Load the fine-tuned model and tokenizer
145
+ # # # model_path = "./clinical_longformer"
146
+ # # # tokenizer = LongformerTokenizer.from_pretrained(model_path)
147
+ # # # model = LongformerForSequenceClassification.from_pretrained(model_path)
148
+ # # # model.eval() # Set the model to evaluation mode
149
+
150
+ # # # # Load the ICD-9 descriptions from CSV into a dictionary
151
+ # # # icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv") # Adjust the path to your CSV file
152
+ # # # icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str) # Ensure ICD9_CODE is string type
153
+ # # # icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE'])) # Remove decimals for matching
154
+
155
+ # # # # Load the ICD-9 to ICD-10 mapping
156
+ # # # icd9_to_icd10 = {}
157
+ # # # with open("2015_I9gem.txt", "r") as file:
158
+ # # # for line in file:
159
+ # # # parts = line.strip().split()
160
+ # # # if len(parts) == 3:
161
+ # # # icd9, icd10, _ = parts
162
+ # # # icd9_to_icd10[icd9] = icd10
163
+
164
+ # # # # ICD-9 code columns used during training
165
+ # # # icd9_columns = [
166
+ # # # '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
167
+ # # # '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
168
+ # # # '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
169
+ # # # '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
170
+ # # # '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
171
+ # # # '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
172
+ # # # ]
173
+
174
+ # # # # Function for making predictions and mapping to ICD-10
175
+ # # # def predict_icd9(texts, tokenizer, model, threshold=0.5):
176
+ # # # inputs = tokenizer(
177
+ # # # texts,
178
+ # # # padding="max_length",
179
+ # # # truncation=True,
180
+ # # # max_length=512,
181
+ # # # return_tensors="pt"
182
+ # # # )
183
+
184
+ # # # with torch.no_grad():
185
+ # # # outputs = model(
186
+ # # # input_ids=inputs["input_ids"],
187
+ # # # attention_mask=inputs["attention_mask"]
188
+ # # # )
189
+ # # # logits = outputs.logits
190
+ # # # probabilities = torch.sigmoid(logits)
191
+ # # # predictions = (probabilities > threshold).int()
192
+
193
+ # # # predicted_icd9 = []
194
+ # # # for pred in predictions:
195
+ # # # codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
196
+ # # # predicted_icd9.append(codes)
197
+
198
+ # # # # Fetch descriptions and map to ICD-10 codes
199
+ # # # predictions_with_desc = []
200
+ # # # for codes in predicted_icd9:
201
+ # # # code_with_desc = []
202
+ # # # for code in codes:
203
+ # # # icd9_stripped = code.replace('.', '')
204
+ # # # icd10_code = icd9_to_icd10.get(icd9_stripped, "Mapping not found")
205
+ # # # icd9_desc = icd9_descriptions.get(icd9_stripped, "Description not found")
206
+ # # # code_with_desc.append((code, icd9_desc, icd10_code))
207
+ # # # predictions_with_desc.append(code_with_desc)
208
+
209
+ # # # return predictions_with_desc
210
+
211
+ # # # # Streamlit UI
212
+ # # # st.title("ICD-9 to ICD-10 Code Prediction")
213
+ # # # st.sidebar.header("Model Options")
214
+ # # # threshold = st.sidebar.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)
215
+
216
+ # # # st.write("### Enter Medical Summary")
217
+ # # # input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")
218
+
219
+ # # # if st.button("Predict"):
220
+ # # # if input_text.strip():
221
+ # # # predictions = predict_icd9([input_text], tokenizer, model, threshold)
222
+ # # # st.write("### Predicted ICD-9 and ICD-10 Codes with Descriptions")
223
+ # # # for icd9_code, description, icd10_code in predictions[0]:
224
+ # # # st.write(f"- ICD-9: {icd9_code} ({description}) -> ICD-10: {icd10_code}")
225
+ # # # else:
226
+ # # # st.error("Please enter a medical summary.")
227
+
228
+ # # # import os
229
+ # # # import torch
230
+ # # # import pandas as pd
231
+ # # # import streamlit as st
232
+ # # # from PIL import Image
233
+ # # # from transformers import LongformerTokenizer, LongformerForSequenceClassification
234
+ # # # from phi.agent import Agent
235
+ # # # from phi.model.google import Gemini
236
+ # # # from phi.tools.duckduckgo import DuckDuckGo
237
+
238
+ # # # # Load the fine-tuned ICD-9 model and tokenizer
239
+ # # # model_path = "./clinical_longformer"
240
+ # # # tokenizer = LongformerTokenizer.from_pretrained(model_path)
241
+ # # # model = LongformerForSequenceClassification.from_pretrained(model_path)
242
+ # # # model.eval() # Set the model to evaluation mode
243
+
244
+ # # # # Load the ICD-9 descriptions from CSV into a dictionary
245
+ # # # icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv") # Adjust the path to your CSV file
246
+ # # # icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str) # Ensure ICD9_CODE is string type for matching
247
+ # # # icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE'])) # Remove decimals in ICD9 code for matching
248
+
249
+ # # # # ICD-9 code columns used during training
250
+ # # # icd9_columns = [
251
+ # # # '038.9', '244.9', '250.00', '272.0', '272.4', '276.1', '276.2', '285.1', '285.9',
252
+ # # # '287.5', '305.1', '311', '36.15', '37.22', '37.23', '38.91', '38.93', '39.61',
253
+ # # # '39.95', '401.9', '403.90', '410.71', '412', '414.01', '424.0', '427.31', '428.0',
254
+ # # # '486', '496', '507.0', '511.9', '518.81', '530.81', '584.9', '585.9', '599.0',
255
+ # # # '88.56', '88.72', '93.90', '96.04', '96.6', '96.71', '96.72', '99.04', '99.15',
256
+ # # # '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
257
+ # # # ]
258
+
259
+ # # # # Function for making ICD-9 predictions
260
+ # # # def predict_icd9(texts, tokenizer, model, threshold=0.5):
261
+ # # # inputs = tokenizer(
262
+ # # # texts,
263
+ # # # padding="max_length",
264
+ # # # truncation=True,
265
+ # # # max_length=512,
266
+ # # # return_tensors="pt"
267
+ # # # )
268
+
269
+ # # # with torch.no_grad():
270
+ # # # outputs = model(
271
+ # # # input_ids=inputs["input_ids"],
272
+ # # # attention_mask=inputs["attention_mask"]
273
+ # # # )
274
+ # # # logits = outputs.logits
275
+ # # # probabilities = torch.sigmoid(logits)
276
+ # # # predictions = (probabilities > threshold).int()
277
+
278
+ # # # predicted_icd9 = []
279
+ # # # for pred in predictions:
280
+ # # # codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
281
+ # # # predicted_icd9.append(codes)
282
+
283
+ # # # predictions_with_desc = []
284
+ # # # for codes in predicted_icd9:
285
+ # # # code_with_desc = [(code, icd9_descriptions.get(code.replace('.', ''), "Description not found")) for code in codes]
286
+ # # # predictions_with_desc.append(code_with_desc)
287
+
288
+ # # # return predictions_with_desc
289
 
290
  # # # Streamlit UI
291
+ # # # st.title("Medical Diagnosis Assistant")
292
+ # # # option = st.selectbox(
293
+ # # # "Choose Diagnosis Method",
294
+ # # # ("ICD-9 Code Prediction", "Medical Image Analysis")
295
+ # # # )
296
+
297
+ # # # # ICD-9 Code Prediction
298
+ # # # if option == "ICD-9 Code Prediction":
299
+ # # # st.write("### Enter Medical Summary")
300
+ # # # input_text = st.text_area("Medical Summary", placeholder="Enter clinical notes here...")
301
+
302
+ # # # threshold = st.slider("Prediction Threshold", 0.0, 1.0, 0.5, 0.01)
303
+
304
+ # # # if st.button("Predict ICD-9 Codes"):
305
+ # # # if input_text.strip():
306
+ # # # predictions = predict_icd9([input_text], tokenizer, model, threshold)
307
+ # # # st.write("### Predicted ICD-9 Codes and Descriptions")
308
+ # # # for code, description in predictions[0]:
309
+ # # # st.write(f"- {code}: {description}")
310
+ # # # else:
311
+ # # # st.error("Please enter a medical summary.")
312
+
313
+ # # # Medical Image Analysis
314
+ # # # elif option == "Medical Image Analysis":
315
+ # # # if "GOOGLE_API_KEY" not in st.session_state:
316
+ # # # st.warning("Please enter your Google API Key in the sidebar to continue")
317
+ # # # else:
318
+ # # # medical_agent = Agent(
319
+ # # # model=Gemini(
320
+ # # # api_key=st.session_state.GOOGLE_API_KEY,
321
+ # # # id="gemini-2.0-flash-exp"
322
+ # # # ),
323
+ # # # tools=[DuckDuckGo()],
324
+ # # # markdown=True
325
+ # # # )
326
+
327
+ # # # query = """
328
+ # # # 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:
329
+
330
+ # # # ### 1. Image Type & Region
331
+ # # # - Specify imaging modality (X-ray/MRI/CT/Ultrasound/etc.)
332
+ # # # - Identify the patient's anatomical region and positioning
333
+ # # # - Comment on image quality and technical adequacy
334
+
335
+ # # # ### 2. Key Findings
336
+ # # # - List primary observations systematically
337
+ # # # - Note any abnormalities in the patient's imaging with precise descriptions
338
+ # # # - Include measurements and densities where relevant
339
+ # # # - Describe location, size, shape, and characteristics
340
+ # # # - Rate severity: Normal/Mild/Moderate/Severe
341
+
342
+ # # # ### 3. Diagnostic Assessment
343
+ # # # - Provide primary diagnosis with confidence level
344
+ # # # - List differential diagnoses in order of likelihood
345
+ # # # - Support each diagnosis with observed evidence from the patient's imaging
346
+ # # # - Note any critical or urgent findings
347
+
348
+ # # # ### 4. Patient-Friendly Explanation
349
+ # # # - Explain the findings in simple, clear language that the patient can understand
350
+ # # # - Avoid medical jargon or provide clear definitions
351
+ # # # - Include visual analogies if helpful
352
+ # # # - Address common patient concerns related to these findings
353
+
354
+ # # # ### 5. Research Context
355
+ # # # - Use the DuckDuckGo search tool to find recent medical literature about similar cases
356
+ # # # - Provide a list of relevant medical links
357
+ # # # - Include key references to support your analysis
358
+ # # # """
359
+
360
+ # # # upload_container = st.container()
361
+ # # # image_container = st.container()
362
+ # # # analysis_container = st.container()
363
+
364
+ # # # with upload_container:
365
+ # # # uploaded_file = st.file_uploader(
366
+ # # # "Upload Medical Image",
367
+ # # # type=["jpg", "jpeg", "png", "dicom"],
368
+ # # # help="Supported formats: JPG, JPEG, PNG, DICOM"
369
+ # # # )
370
+
371
+ # # # if uploaded_file is not None:
372
+ # # # with image_container:
373
+ # # # col1, col2, col3 = st.columns([1, 2, 1])
374
+ # # # with col2:
375
+ # # # image = Image.open(uploaded_file)
376
+ # # # width, height = image.size
377
+ # # # aspect_ratio = width / height
378
+ # # # new_width = 500
379
+ # # # new_height = int(new_width / aspect_ratio)
380
+ # # # resized_image = image.resize((new_width, new_height))
381
+
382
+ # # # st.image(resized_image, caption="Uploaded Medical Image", use_container_width=True)
383
+
384
+ # # # analyze_button = st.button("πŸ” Analyze Image")
385
+
386
+ # # # with analysis_container:
387
+ # # # if analyze_button:
388
+ # # # image_path = "temp_medical_image.png"
389
+ # # # with open(image_path, "wb") as f:
390
+ # # # f.write(uploaded_file.getbuffer())
391
+
392
+ # # # with st.spinner("πŸ”„ Analyzing image... Please wait."):
393
+ # # # try:
394
+ # # # response = medical_agent.run(query, images=[image_path])
395
+ # # # st.markdown("### πŸ“‹ Analysis Results")
396
+ # # # st.markdown(response.content)
397
+ # # # except Exception as e:
398
+ # # # st.error(f"Analysis error: {e}")
399
+ # # # finally:
400
+ # # # if os.path.exists(image_path):
401
+ # # # os.remove(image_path)
402
+ # # # else:
403
+ # # # st.info("πŸ‘† Please upload a medical image to begin analysis")
404
+
405
+ # # import os
406
  # # import torch
407
  # # import pandas as pd
408
  # # import streamlit as st
409
+ # # from PIL import Image
410
  # # from transformers import LongformerTokenizer, LongformerForSequenceClassification
411
+ # # from phi.agent import Agent
412
+ # # from phi.model.google import Gemini
413
+ # # from phi.tools.duckduckgo import DuckDuckGo
414
 
415
+ # # # Sidebar for Google API Key input
416
+ # # st.sidebar.title("Settings")
417
+ # # st.sidebar.write("Enter your Google API Key below for the Medical Image Analysis feature.")
418
+ # # api_key = st.sidebar.text_input("Google API Key", type="password")
419
+
420
+ # # if api_key:
421
+ # # st.session_state["GOOGLE_API_KEY"] = api_key
422
+ # # else:
423
+ # # st.session_state.pop("GOOGLE_API_KEY", None)
424
+
425
+ # # # Load the fine-tuned ICD-9 model and tokenizer
426
  # # model_path = "./clinical_longformer"
427
  # # tokenizer = LongformerTokenizer.from_pretrained(model_path)
428
  # # model = LongformerForSequenceClassification.from_pretrained(model_path)
 
430
 
431
  # # # Load the ICD-9 descriptions from CSV into a dictionary
432
  # # icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv") # Adjust the path to your CSV file
433
+ # # icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str) # Ensure ICD9_CODE is string type for matching
434
+ # # icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE'])) # Remove decimals in ICD9 code for matching
 
 
 
 
 
 
 
 
 
435
 
436
  # # # ICD-9 code columns used during training
437
  # # icd9_columns = [
 
443
  # # '995.92', 'V15.82', 'V45.81', 'V45.82', 'V58.61'
444
  # # ]
445
 
446
+ # # # Function for making ICD-9 predictions
447
  # # def predict_icd9(texts, tokenizer, model, threshold=0.5):
448
  # # inputs = tokenizer(
449
  # # texts,
 
452
  # # max_length=512,
453
  # # return_tensors="pt"
454
  # # )
455
+
456
  # # with torch.no_grad():
457
  # # outputs = model(
458
  # # input_ids=inputs["input_ids"],
 
461
  # # logits = outputs.logits
462
  # # probabilities = torch.sigmoid(logits)
463
  # # predictions = (probabilities > threshold).int()
464
+
465
  # # predicted_icd9 = []
466
  # # for pred in predictions:
467
  # # codes = [icd9_columns[i] for i, val in enumerate(pred) if val == 1]
468
  # # predicted_icd9.append(codes)
469
+
 
470
  # # predictions_with_desc = []
471
  # # for codes in predicted_icd9:
472
+ # # code_with_desc = [(code, icd9_descriptions.get(code.replace('.', ''), "Description not found")) for code in codes]
 
 
 
 
 
473
  # # predictions_with_desc.append(code_with_desc)
474
+
475
  # # return predictions_with_desc
476
 
477
  # # # Streamlit UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  # # st.title("Medical Diagnosis Assistant")
479
  # # option = st.selectbox(
480
  # # "Choose Diagnosis Method",
 
497
  # # else:
498
  # # st.error("Please enter a medical summary.")
499
 
500
+ # # # Medical Image Analysis
501
  # # elif option == "Medical Image Analysis":
502
  # # if "GOOGLE_API_KEY" not in st.session_state:
503
  # # st.warning("Please enter your Google API Key in the sidebar to continue")
504
  # # else:
505
  # # medical_agent = Agent(
506
  # # model=Gemini(
507
+ # # api_key=st.session_state["GOOGLE_API_KEY"],
508
  # # id="gemini-2.0-flash-exp"
509
  # # ),
510
  # # tools=[DuckDuckGo()],
 
513
 
514
  # # query = """
515
  # # 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:
 
516
  # # ### 1. Image Type & Region
517
  # # - Specify imaging modality (X-ray/MRI/CT/Ultrasound/etc.)
518
  # # - Identify the patient's anatomical region and positioning
519
  # # - Comment on image quality and technical adequacy
 
520
  # # ### 2. Key Findings
521
  # # - List primary observations systematically
522
  # # - Note any abnormalities in the patient's imaging with precise descriptions
523
  # # - Include measurements and densities where relevant
524
  # # - Describe location, size, shape, and characteristics
525
  # # - Rate severity: Normal/Mild/Moderate/Severe
 
526
  # # ### 3. Diagnostic Assessment
527
  # # - Provide primary diagnosis with confidence level
528
  # # - List differential diagnoses in order of likelihood
529
  # # - Support each diagnosis with observed evidence from the patient's imaging
530
+ # # - Note any critical or urgent findings
531
+ # # ### 4. Patient-Friendly Explanation
532
+ # # - Explain the findings in simple, clear language that the patient can understand
533
+ # # - Avoid medical jargon or provide clear definitions
534
+ # # - Include visual analogies if helpful
535
+ # # - Address common patient concerns related to these findings
536
+ # # ### 5. Research Context
537
+ # # - Use the DuckDuckGo search tool to find recent medical literature about similar cases
538
+ # # - Provide a list of relevant medical links
539
+ # # - Include key references to support your analysis
540
+ # # """
541
+
542
+ # # upload_container = st.container()
543
+ # # image_container = st.container()
544
+ # # analysis_container = st.container()
545
+
546
+ # # with upload_container:
547
+ # # uploaded_file = st.file_uploader(
548
+ # # "Upload Medical Image",
549
+ # # type=["jpg", "jpeg", "png", "dicom"],
550
+ # # help="Supported formats: JPG, JPEG, PNG, DICOM"
551
+ # # )
552
+
553
+ # # if uploaded_file is not None:
554
+ # # with image_container:
555
+ # # col1, col2, col3 = st.columns([1, 2, 1])
556
+ # # with col2:
557
+ # # image = Image.open(uploaded_file)
558
+ # # width, height = image.size
559
+ # # aspect_ratio = width / height
560
+ # # new_width = 500
561
+ # # new_height = int(new_width / aspect_ratio)
562
+ # # resized_image = image.resize((new_width, new_height))
 
 
563
 
564
+ # # st.image(resized_image, caption="Uploaded Medical Image", use_container_width=True)
565
 
566
+ # # analyze_button = st.button("πŸ” Analyze Image")
567
 
568
+ # # with analysis_container:
569
+ # # if analyze_button:
570
+ # # image_path = "temp_medical_image.png"
571
+ # # with open(image_path, "wb") as f:
572
+ # # f.write(uploaded_file.getbuffer())
573
 
574
+ # # with st.spinner("πŸ”„ Analyzing image... Please wait."):
575
+ # # try:
576
+ # # response = medical_agent.run(query, images=[image_path])
577
+ # # st.markdown("### πŸ“‹ Analysis Results")
578
+ # # st.markdown(response.content)
579
+ # # except Exception as e:
580
+ # # st.error(f"Analysis error: {e}")
581
+ # # finally:
582
+ # # if os.path.exists(image_path):
583
+ # # os.remove(image_path)
584
+ # # else:
585
+ # # st.info("πŸ‘† Please upload a medical image to begin analysis")
586
 
587
  # import os
588
  # import torch
 
594
  # from phi.model.google import Gemini
595
  # from phi.tools.duckduckgo import DuckDuckGo
596
 
 
 
 
 
 
 
 
 
 
 
597
  # # Load the fine-tuned ICD-9 model and tokenizer
598
  # model_path = "./clinical_longformer"
599
  # tokenizer = LongformerTokenizer.from_pretrained(model_path)
 
601
  # model.eval() # Set the model to evaluation mode
602
 
603
  # # Load the ICD-9 descriptions from CSV into a dictionary
604
+ # icd9_desc_df = pd.read_csv("D_ICD_DIAGNOSES.csv")
605
  # icd9_desc_df['ICD9_CODE'] = icd9_desc_df['ICD9_CODE'].astype(str) # Ensure ICD9_CODE is string type for matching
606
  # icd9_descriptions = dict(zip(icd9_desc_df['ICD9_CODE'].str.replace('.', ''), icd9_desc_df['LONG_TITLE'])) # Remove decimals in ICD9 code for matching
607
 
 
624
  # max_length=512,
625
  # return_tensors="pt"
626
  # )
 
627
  # with torch.no_grad():
628
  # outputs = model(
629
  # input_ids=inputs["input_ids"],
 
645
 
646
  # return predictions_with_desc
647
 
648
+ # # Define the API key directly in the code
649
+ # GOOGLE_API_KEY = "AIzaSyA24A6egT3L0NAKkkw9QHjfoizp7cJUTaA"
650
+
651
  # # Streamlit UI
652
  # st.title("Medical Diagnosis Assistant")
653
  # option = st.selectbox(
 
673
 
674
  # # Medical Image Analysis
675
  # elif option == "Medical Image Analysis":
676
+ # medical_agent = Agent(
677
+ # model=Gemini(
678
+ # api_key=GOOGLE_API_KEY,
679
+ # id="gemini-2.0-flash-exp"
680
+ # ),
681
+ # tools=[DuckDuckGo()],
682
+ # markdown=True
683
+ # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
684
 
685
+ # query = """
686
+ # 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:
687
+ # ### 1. Image Type & Region
688
+ # - Specify imaging modality (X-ray/MRI/CT/Ultrasound/etc.)
689
+ # - Identify the patient's anatomical region and positioning
690
+ # - Comment on image quality and technical adequacy
691
+ # ### 2. Key Findings
692
+ # - List primary observations systematically
693
+ # - Note any abnormalities in the patient's imaging with precise descriptions
694
+ # - Include measurements and densities where relevant
695
+ # - Describe location, size, shape, and characteristics
696
+ # - Rate severity: Normal/Mild/Moderate/Severe
697
+ # ### 3. Diagnostic Assessment
698
+ # - Provide primary diagnosis with confidence level
699
+ # - List differential diagnoses in order of likelihood
700
+ # - Support each diagnosis with observed evidence from the patient's imaging
701
+ # - Note any critical or urgent findings
702
+ # ### 4. Patient-Friendly Explanation
703
+ # - Explain the findings in simple, clear language that the patient can understand
704
+ # - Avoid medical jargon or provide clear definitions
705
+ # - Include visual analogies if helpful
706
+ # - Address common patient concerns related to these findings
707
+ # ### 5. Research Context
708
+ # - Use the DuckDuckGo search tool to find recent medical literature about similar cases
709
+ # - Provide a list of relevant medical links
710
+ # - Include key references to support your analysis
711
+ # """
712
+
713
+ # upload_container = st.container()
714
+ # image_container = st.container()
715
+ # analysis_container = st.container()
716
+
717
+ # with upload_container:
718
+ # uploaded_file = st.file_uploader(
719
+ # "Upload Medical Image",
720
+ # type=["jpg", "jpeg", "png", "dicom"],
721
+ # help="Supported formats: JPG, JPEG, PNG, DICOM"
722
+ # )
723
 
724
+ # if uploaded_file is not None:
725
+ # with image_container:
726
+ # col1, col2, col3 = st.columns([1, 2, 1])
727
+ # with col2:
728
+ # image = Image.open(uploaded_file)
729
+ # width, height = image.size
730
+ # aspect_ratio = width / height
731
+ # new_width = 500
732
+ # new_height = int(new_width / aspect_ratio)
733
+ # resized_image = image.resize((new_width, new_height))
734
 
735
+ # st.image(resized_image, caption="Uploaded Medical Image", use_container_width=True)
736
 
737
+ # analyze_button = st.button("πŸ” Analyze Image")
738
 
739
+ # with analysis_container:
740
+ # if analyze_button:
741
+ # image_path = "temp_medical_image.png"
742
+ # with open(image_path, "wb") as f:
743
+ # f.write(uploaded_file.getbuffer())
744
 
745
+ # with st.spinner("πŸ”„ Analyzing image... Please wait."):
746
+ # try:
747
+ # response = medical_agent.run(query, images=[image_path])
748
+ # st.markdown("### πŸ“‹ Analysis Results")
749
+ # st.markdown(response.content)
750
+ # except Exception as e:
751
+ # st.error(f"Analysis error: {e}")
752
+ # finally:
753
+ # if os.path.exists(image_path):
754
+ # os.remove(image_path)
755
+ # else:
756
+ # st.info("πŸ‘† Please upload a medical image to begin analysis")
757
 
758