dmolino commited on
Commit
7c28703
·
verified ·
1 Parent(s): 20cae71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -57
app.py CHANGED
@@ -3,7 +3,7 @@ import tifffile
3
  import pydicom
4
  from scipy.ndimage import zoom
5
  import torch
6
- #from core.models.dani_model import dani_model
7
  import numpy as np
8
  from PIL import Image
9
  import base64
@@ -20,18 +20,18 @@ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
20
 
21
  # Dati di esempio predefiniti
22
  esempi = {
23
- "FRO -> LAT": {'FRO': 'FtoL.png', 'LAT': 'LfromF.png'},
24
- "FRO -> REP": {'FRO': '31d9847f-987fcf63-704f7496-d2b21eb8-63cd973e.tiff', 'REP': 'Small bilateral pleural effusions, left greater than right.'},
25
- "FRO -> LAT + REP": {'FRO': '81bca127-0c416084-67f8033c-ecb26476-6d1ecf60.tiff', 'LAT': 'd52a0c5c-bb7104b0-b1d821a5-959984c3-33c04ccb.tiff', 'REP': 'No acute intrathoracic process. Heart Size is normal. Lungs are clear. No pneumothorax'},
26
- "LAT -> FRO": {'LAT': 'LtoF.png', 'FRO': 'FfromL.png'},
27
- "LAT -> REP": {'LAT': 'd52a0c5c-bb7104b0-b1d821a5-959984c3-33c04ccb.tiff', 'REP': 'no acute cardiopulmonary process. if concern for injury persists, a dedicated rib series with markers would be necessary to ensure no rib fractures.'},
28
- "LAT -> FRO + REP": {'LAT': 'reald52a0c5c-bb7104b0-b1d821a5-959984c3-33c04ccb.tiff', 'FRO': 'ab37274f-b4c1fc04-e2ff24b4-4a130ba3-cd167968.tiff', 'REP': 'No acute intrathoracic process. If there is strong concern for rib fracture, a dedicated rib series may be performed.'},
29
- "REP -> FRO": {'REP': 'Left lung opacification which may reflect pneumonia superimposed on metastatic disease.', 'FRO': '02aa804e-bde0afdd-112c0b34-7bc16630-4e384014.tiff'},
30
- "REP -> LAT": {'REP': 'Bilateral pleural effusions, cardiomegaly and mild edema suggest fluid overload.', 'LAT': '489faba7-a9dc5f1d-fd7241d6-9638d855-eaa952b1.tiff'},
31
- "REP -> FRO + LAT": {'REP': 'No acute intrathoracic process. The lungs are clean and heart is normal size.', 'FRO': 'f27ba7cd-44486c2e-29f3e890-f2b9f94e-84110448.tiff', 'LAT': 'b20c9570-de77944a-b8604ba0-73305a7b-d608a72b.tiff'},
32
- "FRO + LAT -> REP": {'FRO': '95856dd1-5878b5b1-9c104817-760c0122-6187946f.tiff', 'LAT': '3723d912-71940d69-4fef2dd2-27af5a7b-127ba20c.tiff', 'REP': 'Opacities in the right upper or middle lobe, maybe early pneumonia.'},
33
- "FRO + REP -> LAT": {'FRO': 'e7f21453-7956d79a-44e44614-fae8ff16-d174d1a0.tiff', 'REP': 'No focal consolidation.', 'LAT': '8037e6b9-06367464-a4ccd63a-5c5c5a81-ce3e7ffc.tiff'},
34
- "LAT + REP -> FRO": {'LAT': '02c66644-b1883a91-54aed0e7-62d25460-398f9865.tiff', 'REP': 'No evidence of acute cardiopulmonary process.', 'FRO': 'b1f169f1-12177dd5-2fa1c4b1-7b816311-85d769e9.tiff'}
35
  }
36
 
37
 
@@ -130,7 +130,7 @@ if st.session_state['step'] == 1:
130
  # Breve descrizione del lavoro
131
  st.markdown("""
132
  <div style='text-align: justify; font-size: 18px; line-height: 1.6;'>
133
- This work introduces MedCoDi-M, a novel multi-prompt vision-language model for multi-modal medical data generation.
134
  In this demo, you will be able to perform various generation tasks including frontal and lateral chest X-rays and clinical report generation.
135
  MedCoDi-M enables flexible, any-to-any generation across different medical data modalities, utilizing contrastive learning and a modular approach for enhanced performance.
136
  </div>
@@ -141,7 +141,7 @@ if st.session_state['step'] == 1:
141
 
142
  # Immagine con didascalia migliorata e con dimensione della caption aumentata
143
  image_path = "./DEMO/Loghi/model_final.png" # Sostituisci con il percorso della tua immagine
144
- st.image(image_path, caption='', use_column_width=True)
145
 
146
  # Caption con dimensione del testo migliorata
147
  st.markdown("""
@@ -163,15 +163,15 @@ if st.session_state['step'] == 1:
163
  if st.session_state['step'] == 2:
164
  # Opzioni disponibili
165
  options = [
166
- "FRO -> LAT", "FRO -> REP", "FRO -> LAT + REP",
167
- "LAT -> FRO", "LAT -> REP", "LAT -> FRO + REP",
168
- "REP -> FRO", "REP -> LAT", "REP -> FRO + LAT",
169
- "FRO + LAT -> REP", "FRO + REP -> LAT", "LAT + REP -> FRO"
170
  ]
171
 
172
  # Messaggio di selezione con dimensione aumentata
173
  st.markdown(
174
- "<h4 style='text-align: justify'><strong>Select the type of generation you want to perform: (FRO = Frontal, LAT = Lateral, REP = Report)</strong></h4>",
175
  unsafe_allow_html=True)
176
 
177
  # Aumentare la dimensione di "Please select an option:"
@@ -220,17 +220,17 @@ if st.session_state['step'] == 3:
220
  unsafe_allow_html=True)
221
 
222
  # Carica l'immagine frontale
223
- if "FRO" in st.session_state['selected_option'].split(" ->")[0]:
224
  st.markdown("<h5 style='font-size: 18px;'>Load the Frontal X-ray in DICOM format</h5>", unsafe_allow_html=True)
225
  st.session_state['frontal_file'] = st.file_uploader("", type=["dcm"])
226
 
227
  # Carica l'immagine laterale
228
- if "LAT" in st.session_state['selected_option'].split(" ->")[0]:
229
  st.markdown("<h5 style='font-size: 18px;'>Load the Lateral X-ray in DICOM format</h5>", unsafe_allow_html=True)
230
  st.session_state['lateral_file'] = st.file_uploader("", type=["dcm"])
231
 
232
  # Inserisci il report clinico
233
- if "REP" in st.session_state['selected_option'].split(" ->")[0]:
234
  st.markdown("<h5 style='font-size: 18px;'>Type the clinical report</h5>", unsafe_allow_html=True)
235
  st.session_state['report'] = st.text_area("", value=st.session_state['report'])
236
 
@@ -249,11 +249,11 @@ if st.session_state['step'] == 3:
249
  with st.spinner("Preprocessing the data..."):
250
  time.sleep(3)
251
  # Controllo che i file necessari siano stati caricati
252
- if "FRO" in st.session_state['selected_option'].split(" ->")[0] and not st.session_state['frontal_file']:
253
  st.error("Load the Frontal image.")
254
- elif "LAT" in st.session_state['selected_option'].split(" ->")[0] and not st.session_state['lateral_file']:
255
  st.error("Load the Lateral image.")
256
- elif "REP" in st.session_state['selected_option'].split(" ->")[0] and not st.session_state['report']:
257
  st.error("Type the clinical report.")
258
  else:
259
  st.write(f"Execution of: {st.session_state['selected_option']}")
@@ -311,7 +311,7 @@ if st.session_state['step'] == 3:
311
  if lateral.dtype != np.uint8:
312
  lateral2 = (255 * (lateral - lateral.min()) / (lateral.max() - lateral.min())).astype(np.uint8)
313
  lateral = torch.tensor(lateral, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
314
- lateral2 = Image.fromarray(lateral2)
315
  st.write("Lateral Image loaded successfully!")
316
  st.image(lateral2, caption="Lateral Image Loaded", use_column_width=True)
317
  if st.session_state['report']:
@@ -319,20 +319,20 @@ if st.session_state['step'] == 3:
319
  st.write(f"Loaded Report: {report}")
320
 
321
  inputs = []
322
- if "FRO" in st.session_state['selected_option'].split(" ->")[0]:
323
  inputs.append('frontal')
324
- if "LAT" in st.session_state['selected_option'].split(" ->")[0]:
325
  inputs.append('lateral')
326
- if "REP" in st.session_state['selected_option'].split(" ->")[0]:
327
  inputs.append('text')
328
 
329
  # Ora vediamo cosa c'è dopo la freccia
330
  outputs = []
331
- if "FRO" in st.session_state['selected_option'].split(" ->")[1]:
332
  outputs.append('frontal')
333
- if "LAT" in st.session_state['selected_option'].split(" ->")[1]:
334
  outputs.append('lateral')
335
- if "REP" in st.session_state['selected_option'].split(" ->")[1]:
336
  outputs.append('text')
337
 
338
  # Ultima cosa che va fatta è passare allo step 4, prima di farlo però, tutte le variabili che ci servono
@@ -383,20 +383,20 @@ if st.session_state['step'] == 5:
383
  unsafe_allow_html=True)
384
 
385
  inputs = []
386
- if "FRO" in st.session_state['selected_option'].split(" ->")[0]:
387
- inputs.append('FRO')
388
- if "LAT" in st.session_state['selected_option'].split(" ->")[0]:
389
- inputs.append('LAT')
390
- if "REP" in st.session_state['selected_option'].split(" ->")[0]:
391
- inputs.append('REP')
392
 
393
  outputs = []
394
- if "FRO" in st.session_state['selected_option'].split(" ->")[1]:
395
- outputs.append('FRO')
396
- if "LAT" in st.session_state['selected_option'].split(" ->")[1]:
397
- outputs.append('LAT')
398
- if "REP" in st.session_state['selected_option'].split(" ->")[1]:
399
- outputs.append('REP')
400
 
401
  esempio = esempi[st.session_state['selected_option']]
402
 
@@ -410,8 +410,8 @@ if st.session_state['step'] == 5:
410
 
411
  for idx, inp in enumerate(inputs):
412
  with input_cols[idx]:
413
- if inp == 'FRO':
414
- path = "./DEMO/ESEMPI/" + esempio['FRO']
415
  print(path)
416
  if path.endswith(".tiff"):
417
  im = tifffile.imread(path)
@@ -419,16 +419,16 @@ if st.session_state['step'] == 5:
419
  elif path.endswith(".png"):
420
  im = Image.open(path)
421
  st.image(im, caption="Frontal Image")
422
- elif inp == 'LAT':
423
- path = "./DEMO/ESEMPI/" + esempio['LAT']
424
  if path.endswith(".tiff"):
425
  im = tifffile.imread(path)
426
  im = np.clip(im, 0, 1)
427
  elif path.endswith(".png"):
428
  im = Image.open(path)
429
  st.image(im, caption="Lateral Image")
430
- elif inp == 'REP':
431
- st.write(f"Report: {esempio['REP']}")
432
 
433
  st.markdown(
434
  "<h3 style='text-align: justify'><strong>OUTPUTS</strong></h3>",
@@ -439,8 +439,8 @@ if st.session_state['step'] == 5:
439
 
440
  for idx, out in enumerate(outputs):
441
  with output_cols[idx]:
442
- if out == 'FRO':
443
- path = "./DEMO/ESEMPI/" + esempio['FRO']
444
  if path.endswith(".tiff"):
445
  im = tifffile.imread(path)
446
  # facciamo clamp tra 0 e 1
@@ -448,8 +448,8 @@ if st.session_state['step'] == 5:
448
  elif path.endswith(".png"):
449
  im = Image.open(path)
450
  st.image(im, caption="Frontal Image")
451
- elif out == 'LAT':
452
- path = "./DEMO/ESEMPI/" + esempio['LAT']
453
  if path.endswith(".tiff"):
454
  im = tifffile.imread(path)
455
  # facciamo clamp tra 0 e 1
@@ -457,8 +457,8 @@ if st.session_state['step'] == 5:
457
  elif path.endswith(".png"):
458
  im = Image.open(path)
459
  st.image(im, caption="Lateral Image")
460
- elif out == 'REP':
461
- st.write(f"Report: {esempio['REP']}")
462
 
463
  # Pulsante per tornare all'inizio
464
  if st.button("Return to the beginning"):
 
3
  import pydicom
4
  from scipy.ndimage import zoom
5
  import torch
6
+ from core.models.dani_model import dani_model
7
  import numpy as np
8
  from PIL import Image
9
  import base64
 
20
 
21
  # Dati di esempio predefiniti
22
  esempi = {
23
+ "Frontal -> Lateral": {'Frontal': 'FtoL.png', 'Lateral': 'LfromF.png'},
24
+ "Frontal -> Report": {'Frontal': '31d9847f-987fcf63-704f7496-d2b21eb8-63cd973e.tiff', 'Report': 'Small bilateral pleural effusions, left greater than right.'},
25
+ "Frontal -> Lateral + Report": {'Frontal': '81bca127-0c416084-67f8033c-ecb26476-6d1ecf60.tiff', 'Lateral': 'd52a0c5c-bb7104b0-b1d821a5-959984c3-33c04ccb.tiff', 'Report': 'No acute intrathoracic process. Heart Size is normal. Lungs are clear. No pneumothorax'},
26
+ "Lateral -> Frontal": {'Lateral': 'LtoF.png', 'Frontal': 'FfromL.png'},
27
+ "Lateral -> Report": {'Lateral': 'd52a0c5c-bb7104b0-b1d821a5-959984c3-33c04ccb.tiff', 'Report': 'no acute cardiopulmonary process. if concern for injury persists, a dedicated rib series with markers would be necessary to ensure no rib fractures.'},
28
+ "Lateral -> Frontal + Report": {'Lateral': 'reald52a0c5c-bb7104b0-b1d821a5-959984c3-33c04ccb.tiff', 'Frontal': 'ab37274f-b4c1fc04-e2ff24b4-4a130ba3-cd167968.tiff', 'Report': 'No acute intrathoracic process. If there is strong concern for rib fracture, a dedicated rib series may be performed.'},
29
+ "Report -> Frontal": {'Report': 'Left lung opacification which may reflect pneumonia superimposed on metastatic disease.', 'Frontal': '02aa804e-bde0afdd-112c0b34-7bc16630-4e384014.tiff'},
30
+ "Report -> Lateral": {'Report': 'Bilateral pleural effusions, cardiomegaly and mild edema suggest fluid overload.', 'Lateral': '489faba7-a9dc5f1d-fd7241d6-9638d855-eaa952b1.tiff'},
31
+ "Report -> Frontal + Lateral": {'Report': 'No acute intrathoracic process. The lungs are clean and heart is normal size.', 'Frontal': 'f27ba7cd-44486c2e-29f3e890-f2b9f94e-84110448.tiff', 'Lateral': 'b20c9570-de77944a-b8604ba0-73305a7b-d608a72b.tiff'},
32
+ "Frontal + Lateral -> Report": {'Frontal': '95856dd1-5878b5b1-9c104817-760c0122-6187946f.tiff', 'Lateral': '3723d912-71940d69-4fef2dd2-27af5a7b-127ba20c.tiff', 'Report': 'Opacities in the right upper or middle lobe, maybe early pneumonia.'},
33
+ "Frontal + Report -> Lateral": {'Frontal': 'e7f21453-7956d79a-44e44614-fae8ff16-d174d1a0.tiff', 'Report': 'No focal consolidation.', 'Lateral': '8037e6b9-06367464-a4ccd63a-5c5c5a81-ce3e7ffc.tiff'},
34
+ "Lateral + Report -> Frontal": {'Lateral': '02c66644-b1883a91-54aed0e7-62d25460-398f9865.tiff', 'Report': 'No evidence of acute cardiopulmonary process.', 'Frontal': 'b1f169f1-12177dd5-2fa1c4b1-7b816311-85d769e9.tiff'}
35
  }
36
 
37
 
 
130
  # Breve descrizione del lavoro
131
  st.markdown("""
132
  <div style='text-align: justify; font-size: 18px; line-height: 1.6;'>
133
+ This work introduces MedCoDi-M, a novel multi-prompt foundation model for multi-modal medical data generation.
134
  In this demo, you will be able to perform various generation tasks including frontal and lateral chest X-rays and clinical report generation.
135
  MedCoDi-M enables flexible, any-to-any generation across different medical data modalities, utilizing contrastive learning and a modular approach for enhanced performance.
136
  </div>
 
141
 
142
  # Immagine con didascalia migliorata e con dimensione della caption aumentata
143
  image_path = "./DEMO/Loghi/model_final.png" # Sostituisci con il percorso della tua immagine
144
+ st.image(image_path, caption='', use_container_width=True)
145
 
146
  # Caption con dimensione del testo migliorata
147
  st.markdown("""
 
163
  if st.session_state['step'] == 2:
164
  # Opzioni disponibili
165
  options = [
166
+ "Frontal -> Lateral", "Frontal -> Report", "Frontal -> Lateral + Report",
167
+ "Lateral -> Frontal", "Lateral -> Report", "Lateral -> Frontal + Report",
168
+ "Report -> Frontal", "Report -> Lateral", "Report -> Frontal + Lateral",
169
+ "Frontal + Lateral -> Report", "Frontal + Report -> Lateral", "Lateral + Report -> Frontal"
170
  ]
171
 
172
  # Messaggio di selezione con dimensione aumentata
173
  st.markdown(
174
+ "<h4 style='text-align: justify'><strong>Select the type of generation you want to perform:</strong></h4>",
175
  unsafe_allow_html=True)
176
 
177
  # Aumentare la dimensione di "Please select an option:"
 
220
  unsafe_allow_html=True)
221
 
222
  # Carica l'immagine frontale
223
+ if "Frontal" in st.session_state['selected_option'].split(" ->")[0]:
224
  st.markdown("<h5 style='font-size: 18px;'>Load the Frontal X-ray in DICOM format</h5>", unsafe_allow_html=True)
225
  st.session_state['frontal_file'] = st.file_uploader("", type=["dcm"])
226
 
227
  # Carica l'immagine laterale
228
+ if "Lateral" in st.session_state['selected_option'].split(" ->")[0]:
229
  st.markdown("<h5 style='font-size: 18px;'>Load the Lateral X-ray in DICOM format</h5>", unsafe_allow_html=True)
230
  st.session_state['lateral_file'] = st.file_uploader("", type=["dcm"])
231
 
232
  # Inserisci il report clinico
233
+ if "Report" in st.session_state['selected_option'].split(" ->")[0]:
234
  st.markdown("<h5 style='font-size: 18px;'>Type the clinical report</h5>", unsafe_allow_html=True)
235
  st.session_state['report'] = st.text_area("", value=st.session_state['report'])
236
 
 
249
  with st.spinner("Preprocessing the data..."):
250
  time.sleep(3)
251
  # Controllo che i file necessari siano stati caricati
252
+ if "Frontal" in st.session_state['selected_option'].split(" ->")[0] and not st.session_state['frontal_file']:
253
  st.error("Load the Frontal image.")
254
+ elif "Lateral" in st.session_state['selected_option'].split(" ->")[0] and not st.session_state['lateral_file']:
255
  st.error("Load the Lateral image.")
256
+ elif "Report" in st.session_state['selected_option'].split(" ->")[0] and not st.session_state['report']:
257
  st.error("Type the clinical report.")
258
  else:
259
  st.write(f"Execution of: {st.session_state['selected_option']}")
 
311
  if lateral.dtype != np.uint8:
312
  lateral2 = (255 * (lateral - lateral.min()) / (lateral.max() - lateral.min())).astype(np.uint8)
313
  lateral = torch.tensor(lateral, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
314
+ lateral2 = Image.Frontalmarray(lateral2)
315
  st.write("Lateral Image loaded successfully!")
316
  st.image(lateral2, caption="Lateral Image Loaded", use_column_width=True)
317
  if st.session_state['report']:
 
319
  st.write(f"Loaded Report: {report}")
320
 
321
  inputs = []
322
+ if "Frontal" in st.session_state['selected_option'].split(" ->")[0]:
323
  inputs.append('frontal')
324
+ if "Lateral" in st.session_state['selected_option'].split(" ->")[0]:
325
  inputs.append('lateral')
326
+ if "Report" in st.session_state['selected_option'].split(" ->")[0]:
327
  inputs.append('text')
328
 
329
  # Ora vediamo cosa c'è dopo la freccia
330
  outputs = []
331
+ if "Frontal" in st.session_state['selected_option'].split(" ->")[1]:
332
  outputs.append('frontal')
333
+ if "Lateral" in st.session_state['selected_option'].split(" ->")[1]:
334
  outputs.append('lateral')
335
+ if "Report" in st.session_state['selected_option'].split(" ->")[1]:
336
  outputs.append('text')
337
 
338
  # Ultima cosa che va fatta è passare allo step 4, prima di farlo però, tutte le variabili che ci servono
 
383
  unsafe_allow_html=True)
384
 
385
  inputs = []
386
+ if "Frontal" in st.session_state['selected_option'].split(" ->")[0]:
387
+ inputs.append('Frontal')
388
+ if "Lateral" in st.session_state['selected_option'].split(" ->")[0]:
389
+ inputs.append('Lateral')
390
+ if "Report" in st.session_state['selected_option'].split(" ->")[0]:
391
+ inputs.append('Report')
392
 
393
  outputs = []
394
+ if "Frontal" in st.session_state['selected_option'].split(" ->")[1]:
395
+ outputs.append('Frontal')
396
+ if "Lateral" in st.session_state['selected_option'].split(" ->")[1]:
397
+ outputs.append('Lateral')
398
+ if "Report" in st.session_state['selected_option'].split(" ->")[1]:
399
+ outputs.append('Report')
400
 
401
  esempio = esempi[st.session_state['selected_option']]
402
 
 
410
 
411
  for idx, inp in enumerate(inputs):
412
  with input_cols[idx]:
413
+ if inp == 'Frontal':
414
+ path = "./DEMO/ESEMPI/" + esempio['Frontal']
415
  print(path)
416
  if path.endswith(".tiff"):
417
  im = tifffile.imread(path)
 
419
  elif path.endswith(".png"):
420
  im = Image.open(path)
421
  st.image(im, caption="Frontal Image")
422
+ elif inp == 'Lateral':
423
+ path = "./DEMO/ESEMPI/" + esempio['Lateral']
424
  if path.endswith(".tiff"):
425
  im = tifffile.imread(path)
426
  im = np.clip(im, 0, 1)
427
  elif path.endswith(".png"):
428
  im = Image.open(path)
429
  st.image(im, caption="Lateral Image")
430
+ elif inp == 'Report':
431
+ st.write(f"Report: {esempio['Report']}")
432
 
433
  st.markdown(
434
  "<h3 style='text-align: justify'><strong>OUTPUTS</strong></h3>",
 
439
 
440
  for idx, out in enumerate(outputs):
441
  with output_cols[idx]:
442
+ if out == 'Frontal':
443
+ path = "./DEMO/ESEMPI/" + esempio['Frontal']
444
  if path.endswith(".tiff"):
445
  im = tifffile.imread(path)
446
  # facciamo clamp tra 0 e 1
 
448
  elif path.endswith(".png"):
449
  im = Image.open(path)
450
  st.image(im, caption="Frontal Image")
451
+ elif out == 'Lateral':
452
+ path = "./DEMO/ESEMPI/" + esempio['Lateral']
453
  if path.endswith(".tiff"):
454
  im = tifffile.imread(path)
455
  # facciamo clamp tra 0 e 1
 
457
  elif path.endswith(".png"):
458
  im = Image.open(path)
459
  st.image(im, caption="Lateral Image")
460
+ elif out == 'Report':
461
+ st.write(f"Report: {esempio['Report']}")
462
 
463
  # Pulsante per tornare all'inizio
464
  if st.button("Return to the beginning"):