Ravindu9904 commited on
Commit
8bba6a1
·
verified ·
1 Parent(s): 3d28eff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -8
app.py CHANGED
@@ -2,7 +2,25 @@ import gradio as gr
2
  import pydicom
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
- from monai.transforms import Compose, LoadImage, ScaleIntensity, ToTensor
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def interpret_dicom(files):
8
  slices = []
@@ -11,19 +29,37 @@ def interpret_dicom(files):
11
  slices.append(ds.pixel_array)
12
  slices = np.array(slices)
13
  mid_slice = slices[len(slices)//2]
14
- plt.imshow(mid_slice, cmap='gray')
15
- plt.axis('off')
16
- plt.title('Middle Slice')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  plt.savefig('output.png')
18
  plt.close()
19
- return 'output.png', "Interpretation: (placeholder)"
 
20
 
21
  iface = gr.Interface(
22
  fn=interpret_dicom,
23
  inputs=gr.File(file_count="multiple", label="Upload DICOM files"),
24
- outputs=[gr.Image(type="filepath", label="Middle Slice"), gr.Textbox(label="Interpretation")],
25
- title="DICOM Radiology Interpreter",
26
- description="Upload your DICOM files (e.g., CT scan slices). The app will show the middle slice and provide an interpretation."
27
  )
28
 
29
  if __name__ == "__main__":
 
2
  import pydicom
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
+ import torch
6
+ from monai.networks.nets import UNet
7
+ from monai.transforms import Compose, ScaleIntensity, ToTensor
8
+
9
+ # 1. Define a simple MONAI model (2D UNet)
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ model = UNet(
12
+ dimensions=2,
13
+ in_channels=1,
14
+ out_channels=1,
15
+ channels=(16, 32, 64, 128, 256),
16
+ strides=(2, 2, 2, 2),
17
+ num_res_units=2,
18
+ ).to(device)
19
+ model.eval() # Set model to evaluation mode
20
+
21
+ # 2. Dummy weights (for demo only)
22
+ # In real use, load pre-trained weights:
23
+ # model.load_state_dict(torch.load("your_model.pth", map_location=device))
24
 
25
  def interpret_dicom(files):
26
  slices = []
 
29
  slices.append(ds.pixel_array)
30
  slices = np.array(slices)
31
  mid_slice = slices[len(slices)//2]
32
+
33
+ # Preprocess for MONAI model
34
+ transform = Compose([ScaleIntensity(), ToTensor()])
35
+ input_tensor = transform(mid_slice.astype(np.float32))
36
+ input_tensor = input_tensor.unsqueeze(0).to(device) # Add batch dimension
37
+
38
+ # 3. Run through MONAI model (dummy output for now)
39
+ with torch.no_grad():
40
+ output = model(input_tensor)
41
+ output_np = output.cpu().numpy()[0, 0]
42
+
43
+ # 4. Show original and model output side by side
44
+ fig, axs = plt.subplots(1, 2, figsize=(8, 4))
45
+ axs[0].imshow(mid_slice, cmap='gray')
46
+ axs[0].set_title('Original')
47
+ axs[0].axis('off')
48
+ axs[1].imshow(output_np, cmap='hot')
49
+ axs[1].set_title('Model Output')
50
+ axs[1].axis('off')
51
+ plt.tight_layout()
52
  plt.savefig('output.png')
53
  plt.close()
54
+
55
+ return 'output.png', "Interpretation: Model output shown (demo weights)."
56
 
57
  iface = gr.Interface(
58
  fn=interpret_dicom,
59
  inputs=gr.File(file_count="multiple", label="Upload DICOM files"),
60
+ outputs=[gr.Image(type="filepath", label="Result"), gr.Textbox(label="Interpretation")],
61
+ title="DICOM Radiology Interpreter with MONAI",
62
+ description="Upload your DICOM files (e.g., CT scan slices). The app will show the middle slice and a MONAI model output."
63
  )
64
 
65
  if __name__ == "__main__":