Spaces:
Sleeping
Sleeping
Update icd9_ui.py
Browse files- icd9_ui.py +190 -7
icd9_ui.py
CHANGED
@@ -225,7 +225,184 @@
|
|
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
|
@@ -235,6 +412,16 @@ 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)
|
@@ -317,7 +504,7 @@ elif option == "Medical Image Analysis":
|
|
317 |
else:
|
318 |
medical_agent = Agent(
|
319 |
model=Gemini(
|
320 |
-
api_key=st.session_state
|
321 |
id="gemini-2.0-flash-exp"
|
322 |
),
|
323 |
tools=[DuckDuckGo()],
|
@@ -326,31 +513,26 @@ elif option == "Medical Image Analysis":
|
|
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
|
@@ -403,3 +585,4 @@ elif option == "Medical Image Analysis":
|
|
403 |
st.info("π Please upload a medical image to begin analysis")
|
404 |
|
405 |
|
|
|
|
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
|
|
|
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)
|
|
|
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
|
|
|
585 |
st.info("π Please upload a medical image to begin analysis")
|
586 |
|
587 |
|
588 |
+
|