Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
import pydicom
|
3 |
import numpy as np
|
4 |
-
|
|
|
5 |
import torch
|
6 |
-
from monai.networks.nets import UNet
|
7 |
-
from monai.transforms import Compose, ScaleIntensity, ToTensor
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
model =
|
12 |
-
|
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 |
-
|
22 |
-
#
|
23 |
-
# model.load_state_dict(torch.load("your_model.pth", map_location=device))
|
24 |
-
|
25 |
-
def interpret_dicom(files):
|
26 |
slices = []
|
27 |
for file in files:
|
28 |
ds = pydicom.dcmread(file.name)
|
29 |
-
slices.append(ds.
|
30 |
-
slices
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
return 'output.png', "Interpretation: Model output shown (demo weights)."
|
56 |
|
57 |
iface = gr.Interface(
|
58 |
-
fn=
|
59 |
-
inputs=
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
)
|
64 |
|
65 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
import pydicom
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from transformers import AutoModelForVision2Seq, AutoProcessor
|
6 |
import torch
|
|
|
|
|
7 |
|
8 |
+
# Load the model and processor
|
9 |
+
model_id = "MONAI/Llama3-VILA-M3-3B"
|
10 |
+
model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
11 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
def dicom_to_image(files):
|
14 |
+
# Read all DICOM files and sort by InstanceNumber if available
|
|
|
|
|
|
|
15 |
slices = []
|
16 |
for file in files:
|
17 |
ds = pydicom.dcmread(file.name)
|
18 |
+
slices.append((ds, ds.get('InstanceNumber', 0)))
|
19 |
+
slices.sort(key=lambda x: x[1])
|
20 |
+
images = [s[0].pixel_array for s in slices]
|
21 |
+
# If multiple slices, take the middle one
|
22 |
+
img = images[len(images)//2] if len(images) > 1 else images[0]
|
23 |
+
# Normalize and convert to 8-bit
|
24 |
+
img = img.astype(np.float32)
|
25 |
+
img = (img - img.min()) / (img.max() - img.min() + 1e-5) * 255
|
26 |
+
img = img.astype(np.uint8)
|
27 |
+
pil_img = Image.fromarray(img)
|
28 |
+
return pil_img
|
29 |
+
|
30 |
+
def interpret(files, prompt):
|
31 |
+
pil_img = dicom_to_image(files)
|
32 |
+
# Prepare input for the model
|
33 |
+
inputs = processor(images=pil_img, text=prompt, return_tensors="pt")
|
34 |
+
# Move to GPU if available
|
35 |
+
if torch.cuda.is_available():
|
36 |
+
model.to("cuda")
|
37 |
+
for k in inputs:
|
38 |
+
inputs[k] = inputs[k].to("cuda")
|
39 |
+
# Generate report
|
40 |
+
output = model.generate(**inputs, max_new_tokens=256)
|
41 |
+
report = processor.decode(output[0], skip_special_tokens=True)
|
42 |
+
return pil_img, report
|
|
|
|
|
43 |
|
44 |
iface = gr.Interface(
|
45 |
+
fn=interpret,
|
46 |
+
inputs=[
|
47 |
+
gr.File(file_count="multiple", label="Upload DICOM files"),
|
48 |
+
gr.Textbox(label="Prompt", value="Describe the findings in this image.")
|
49 |
+
],
|
50 |
+
outputs=[
|
51 |
+
gr.Image(type="pil", label="Selected Image"),
|
52 |
+
gr.Textbox(label="AI-generated Report")
|
53 |
+
],
|
54 |
+
title="Radiology Image Interpretation (VILA-M3-3B)",
|
55 |
+
description="Upload DICOM files (CT, MRI, or X-ray). The app will select the middle slice (for stacks), send it to MONAI/Llama3-VILA-M3-3B, and display the AI-generated report."
|
56 |
)
|
57 |
|
58 |
if __name__ == "__main__":
|