-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
323 lines (273 loc) · 9.26 KB
/
app.py
File metadata and controls
323 lines (273 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import os
import random
import gradio as gr
import numpy as np
import SimpleITK as sitk
import torch
from monai.transforms import Resize
from PIL import Image
from transformers import AutoModelForCausalLM, AutoProcessor
model_path = "./models"
max_length = 1024
image_size = (128, 256, 256)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dtype = torch.bfloat16
loaded_models = {}
def seed_everything(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.cuda.manual_seed_all(seed)
def get_available_models():
model_dirs = [
d for d in os.listdir(model_path) if os.path.isdir(os.path.join(model_path, d))
]
if not model_dirs:
model_dirs = ["default"]
return model_dirs
def load_model(model_name):
if model_name in loaded_models:
return loaded_models[model_name]
current_model_path = (
os.path.join(model_path, model_name) if model_name != "default" else model_path
)
tokenizer = AutoProcessor.from_pretrained(
current_model_path,
max_length=max_length,
padding_side="right",
use_fast=False,
trust_remote_code=True,
ignore_mismatched_sizes=True,
)
model = AutoModelForCausalLM.from_pretrained(
current_model_path,
torch_dtype=dtype,
device_map="auto",
trust_remote_code=True,
).to(device=device)
proj_out_num = (
model.get_model().config.proj_out_num
if hasattr(model.get_model().config, "proj_out_num")
else 256
)
loaded_models[model_name] = {
"model": model,
"tokenizer": tokenizer,
"proj_out_num": proj_out_num,
}
return loaded_models[model_name]
def load_gt(path):
with open(path, "r") as f:
return f.read()
def normalize_and_convert(image_slice):
norm_slice = (
(image_slice - np.min(image_slice))
/ (np.max(image_slice) - np.min(image_slice))
* 255
)
norm_slice = norm_slice.astype(np.uint8)
return Image.fromarray(norm_slice).convert("L")
def process_upload(file, text_input, model_name, gt_text):
if not file.name.endswith(".nii.gz"):
return ("Invalid file format. Please upload a .nii.gz file",) + (None,) * 8
img = sitk.ReadImage(file.name)
img_array = sitk.GetArrayFromImage(img)
axial_slices = [img_array[i, :, :] for i in range(img_array.shape[0])]
coronal_slices = [img_array[:, i, :] for i in range(img_array.shape[1])]
sagittal_slices = [img_array[:, :, i] for i in range(img_array.shape[2])]
axial_images = [normalize_and_convert(s) for s in axial_slices]
coronal_images = [normalize_and_convert(s) for s in coronal_slices]
sagittal_images = [normalize_and_convert(s) for s in sagittal_slices]
model_data = load_model(model_name)
model = model_data["model"]
tokenizer = model_data["tokenizer"]
proj_out_num = model_data["proj_out_num"]
resize_transform = Resize(spatial_size=image_size, mode="bilinear")
image_input = np.expand_dims(img_array.copy(), axis=0)
image_input = resize_transform(image_input)
image_input = image_input.data.unsqueeze(0).to(device=device, dtype=dtype)
image_tokens = "<im_patch>" * proj_out_num
input_txt = image_tokens + text_input
input_ids = tokenizer(input_txt, return_tensors="pt")["input_ids"].to(device=device)
with torch.no_grad():
generation = model.generate(
images=image_input,
inputs=input_ids,
max_new_tokens=max_length,
do_sample=True,
top_p=0.9,
temperature=1.0,
)
result = tokenizer.decode(generation[0], skip_special_tokens=True)
gt_visib = gr.update(visible=bool(gt_text), value=load_gt(gt_text))
axial_idx = len(axial_images) // 2
coronal_idx = len(coronal_images) // 2
sagittal_idx = len(sagittal_images) // 2
return (
result,
gt_visib,
axial_images,
coronal_images,
sagittal_images,
gr.Slider(
minimum=0,
maximum=len(axial_images) - 1,
value=axial_idx,
step=1,
label="Axial Slice",
),
gr.Slider(
minimum=0,
maximum=len(coronal_images) - 1,
value=coronal_idx,
step=1,
label="Coronal Slice",
),
gr.Slider(
minimum=0,
maximum=len(sagittal_images) - 1,
value=sagittal_idx,
step=1,
label="Sagittal Slice",
),
axial_images[axial_idx],
coronal_images[coronal_idx],
sagittal_images[sagittal_idx],
)
def update_slice(slice_idx, slice_list):
return slice_list[slice_idx]
available_models = get_available_models()
examples = [
[
"./data/demo/001480/Axial_bone_window.nii.gz",
"What is the CT phase of the image?",
available_models[-1],
"./data/demo/001480/text.txt",
],
[
"./data/demo/007771/Coronal_bone_window.nii.gz",
"What plane is the image in?",
available_models[-1],
"./data/demo/007771/text.txt",
],
[
"./data/demo/015533/Sagittal_non_contrast.nii.gz",
"Where is the lesion located?",
available_models[-1],
"./data/demo/015533/text.txt",
],
[
"./data/demo/009422/Axial_C__delayed.nii.gz",
"Which organ has a mass lesion?",
available_models[-1],
"./data/demo/009422/text.txt",
],
[
"./data/demo/024421/Coronal_C__portal_venous_phase.nii.gz",
"Where is the pleural effusion located?",
available_models[-1],
"./data/demo/024421/text.txt",
],
[
"./data/demo/004794/Sagittal_C__portal_venous_phase.nii.gz",
"Is pelvic ascites present?",
available_models[-1],
"./data/demo/004794/text.txt",
],
[
"./data/demo/015260/Axial_non_contrast.nii.gz",
"Generate a medical report based on this image.",
available_models[-1],
"./data/demo/015260/text.txt",
],
]
with gr.Blocks() as demo:
gr.Markdown("# Vision Language Model for 3D Medical Image Analysis")
with gr.Row():
with gr.Column():
file_input = gr.File(label="Upload .nii.gz File")
text_input = gr.Textbox(
label="Text Input", placeholder="Enter your text here..."
)
model_dropdown = gr.Dropdown(
choices=available_models,
value=available_models[-1],
label="Select Model",
)
gt_text = gr.Textbox(label="Ground Truth", visible=False)
submit_btn = gr.Button("Process")
gr.Examples(
examples=[[ex[0], ex[1], ex[2], ex[3]] for ex in examples],
inputs=[file_input, text_input, model_dropdown, gt_text],
outputs=[],
fn=lambda: None,
cache_examples=False,
label="Examples",
)
with gr.Column():
model_output = gr.Textbox(label="Model Output")
gt_output = gr.Textbox(label="Ground Truth", visible=False)
with gr.Tab("Axial View"):
axial_slider = gr.Slider(label="Axial Slice")
axial_image = gr.Image(label="Axial Slice Viewer")
with gr.Tab("Coronal View"):
coronal_slider = gr.Slider(label="Coronal Slice")
coronal_image = gr.Image(label="Coronal Slice Viewer")
with gr.Tab("Sagittal View"):
sagittal_slider = gr.Slider(label="Sagittal Slice")
sagittal_image = gr.Image(label="Sagittal Slice Viewer")
axial_state = gr.State()
coronal_state = gr.State()
sagittal_state = gr.State()
submit_btn.click(
fn=process_upload,
inputs=[file_input, text_input, model_dropdown, gt_text],
outputs=[
model_output,
gt_output,
axial_state,
coronal_state,
sagittal_state,
axial_slider,
coronal_slider,
sagittal_slider,
axial_image,
coronal_image,
sagittal_image,
],
)
axial_slider.change(
fn=update_slice, inputs=[axial_slider, axial_state], outputs=axial_image
)
coronal_slider.change(
fn=update_slice, inputs=[coronal_slider, coronal_state], outputs=coronal_image
)
sagittal_slider.change(
fn=update_slice,
inputs=[sagittal_slider, sagittal_state],
outputs=sagittal_image,
)
if __name__ == "__main__":
seed_everything(42)
load_model(available_models[-1])
ngrok_token = os.environ.get("NGROK_TOKEN")
if ngrok_token:
import pyngrok.ngrok as ngrok
ngrok.set_auth_token(ngrok_token)
public_url = ngrok.connect(7860).public_url
print(f"Public URL: {public_url}")
demo.queue(max_size=16).launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True,
)
else:
demo.queue().launch(
share=True,
server_name="0.0.0.0",
server_port=7860,
show_error=True,
)