File size: 11,027 Bytes
22ecd08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
789ad27
22ecd08
789ad27
22ecd08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c7c280
22ecd08
 
6c7c280
22ecd08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoProcessor, TextIteratorStreamer
from threading import Thread

findings = "enlarged cardiomediastinum, cardiomegaly, lung opacity, lung lesion, edema, consolidation, pneumonia, atelectasis, pneumothorax, pleural Effusion, pleural other, fracture, support devices"

templates = {
    "single-image": (
        "radiology image: <image> Which of the following findings are present in the radiology image? Findings: {findings}",
        "Based on the previous conversation, provide a description of the findings in the radiology image.",
    ),
    "multi-image": (
        "radiology images: {images} Which of the following findings are present in the radiology images? Findings: {findings}",
        "Based on the previous conversation, provide a description of the findings in the radiology images.",
    ),
    "multi-study": (
        "prior radiology images: {prior_images}, prior radiology report: {prior_report} follow-up images: {images}, The radiology studies are given in chronological order. Which of the following findings are present in the current follow-up radiology images? Findings: {findings}",
        "Based on the previous conversation, provide a description of the findings in the current follow-up radiology images.",
    ),
    "visual-grounding": "Provide the bounding box coordinate of the region this phrase describes: {phrase}",
    "easy-language": "Explain the description with easy language.",
    "summarize": "Summarize the description in one concise sentence.",
    "recommend": "What further diagnosis and treatment do you recommend based on the given x-ray?",
}

title_markdown = """
**Usage Instructions**:
1. Add chest x-ray images of a study to the "Study images" section.
2. (Optional) Add "Prior study images" and "Prior study report".
3. Click the "Medical Report Generation" button.
4. You can also have additional conversations. Please refer to the "Examples" for guidance.

**Notice**: Enabling "do_sample" in the "Parameters" may introduce some randomness to the output.
"""


def load_model(device, dtype):
    # Load Processor and Model
    processor = AutoProcessor.from_pretrained("Deepnoid/M4CXR-TNNLS", trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(
        "Deepnoid/M4CXR-TNNLS",
        trust_remote_code=True,
        torch_dtype=dtype,
        device_map=device,
    )
    return processor, model


def medical_report_generation(history, *args):
    (
        study_images,
        do_sample,
        temperature,
        top_k,
        top_p,
        length_penalty,
        num_beams,
        no_repeat_ngram_size,
        max_new_tokens,
        prior_images,
        prior_report,
    ) = args
    if history:
        raise gr.Error('Please "Clear" the chat history or reload this page.')

    if not study_images:
        raise gr.Error('Please add "Study images".')

    images = [i[0] for i in study_images]

    if prior_images:
        images = [i[0] for i in prior_images] + images
        prior_image_tokens = " ".join("<image>" for _ in prior_images)
        follow_up_image_tokens = " ".join("<image>" for _ in study_images)
        questions = list(templates["multi-study"])
        questions[0] = questions[0].format(
            prior_images=prior_image_tokens,
            prior_report=prior_report,
            images=follow_up_image_tokens,
            findings=findings,
        )
    else:
        if len(images) == 1:
            questions = list(templates["single-image"])
            questions[0] = questions[0].format(findings=findings)
        else:
            image_tokens = " ".join("<image>" for _ in images)
            questions = list(templates["multi-image"])
            questions[0] = questions[0].format(images=image_tokens, findings=findings)

    generator = predict(
        questions[0],
        history,
        study_images,
        do_sample,
        temperature,
        top_k,
        top_p,
        length_penalty,
        num_beams,
        no_repeat_ngram_size,
        max_new_tokens,
        prior_images,
        prior_report,
    )
    for output in generator:
        response = output

    history.append([questions[0], response])
    generator = predict(
        questions[1],
        history,
        study_images,
        do_sample,
        temperature,
        top_k,
        top_p,
        length_penalty,
        num_beams,
        no_repeat_ngram_size,
        max_new_tokens,
        prior_images,
        prior_report,
    )
    for output in generator:
        response = output
    history.append([questions[1], response])

    return history, history


def predict(message, history, *args):
    (
        study_images,
        do_sample,
        temperature,
        top_k,
        top_p,
        length_penalty,
        num_beams,
        no_repeat_ngram_size,
        max_new_tokens,
        prior_images,
        prior_report,
    ) = args

    # build prompts with chat template
    chats = []

    for question, answer in history:
        chats.append({"role": "user", "content": question})
        chats.append({"role": "assistant", "content": answer})

    chats.append({"role": "user", "content": message})

    prompt = processor.apply_chat_template(chats, tokenize=False)
    prompts = [prompt]

    if study_images:
        images = [i[0] for i in study_images]
        # add prior images
        if prior_images:
            images = [i[0] for i in prior_images] + images
    else:
        images = None

    # image, text processing
    inputs = processor(texts=prompts, images=images)

    # prepare inputs
    inputs = {
        k: v.to(model.dtype) if v.dtype == torch.float else v for k, v in inputs.items()
    }
    inputs = {k: v.to(model.device) for k, v in inputs.items()}

    streamer = TextIteratorStreamer(
        processor.tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True
    )

    generate_kwargs = dict(
        inputs,
        streamer=streamer,
        max_new_tokens=max_new_tokens,
        do_sample=do_sample,
        top_p=top_p,
        top_k=top_k,
        temperature=temperature,
        num_beams=num_beams,
        length_penalty=length_penalty,
        no_repeat_ngram_size=no_repeat_ngram_size,
    )
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()

    partial_message = ""
    for new_token in streamer:
        partial_message += new_token
        yield partial_message


def build_demo(model_name: str = "M4CXR"):
    title_model_name = f"""<h1 align="center">{model_name} </h1>"""

    with gr.Blocks(title=model_name) as demo:
        state = gr.State()

        gr.Markdown(title_model_name)
        gr.Markdown(title_markdown)

        with gr.Row():
            with gr.Column(scale=3):

                mrg = gr.Button(value="Medical Report Generation", variant="primary")

                with gr.Row(visible=True) as button_row:
                    prior_images = gr.Gallery(label="Prior study images", type="pil")
                    study_images = gr.Gallery(label="Study images", type="pil")
                    prior_report = gr.Textbox(label="Prior study report")

                with gr.Accordion(
                    "Parameters", open=False, visible=True
                ) as generate_config:
                    do_sample = gr.Checkbox(
                        interactive=True, value=False, label="do_sample"
                    )
                    # gr.Slider(minimum, maximum, value, step, ...)
                    temperature = gr.Slider(
                        0, 1, 1, step=0.1, interactive=True, label="Temperature"
                    )
                    top_k = gr.Slider(1, 5, 3, step=1, interactive=True, label="Top K")
                    top_p = gr.Slider(
                        0, 1, 0.9, step=0.1, interactive=True, label="Top p"
                    )
                    length_penalty = gr.Slider(
                        1, 5, 1, step=0.1, interactive=True, label="length_penalty"
                    )
                    num_beams = gr.Slider(
                        1, 5, 1, step=1, interactive=True, label="Beam Size"
                    )
                    no_repeat_ngram_size = gr.Slider(
                        1, 5, 2, step=1, interactive=True, label="no_repeat_ngram_size"
                    )
                    max_new_tokens = gr.Slider(
                        0,
                        1024,
                        512,
                        step=64,
                        interactive=True,
                        label="Max New tokens",
                    )

            with gr.Column(scale=6):

                chat_interface = gr.ChatInterface(
                    fn=predict,
                    additional_inputs=[
                        study_images,
                        do_sample,
                        temperature,
                        top_k,
                        top_p,
                        length_penalty,
                        num_beams,
                        no_repeat_ngram_size,
                        max_new_tokens,
                        prior_images,
                        prior_report,
                    ],
                    examples=[
                        [templates["summarize"]],
                        [templates["easy-language"]],
                        [templates["recommend"]],
                        [templates["visual-grounding"]],
                    ],
                )

            # Connect the button to the function
            mrg.click(
                medical_report_generation,
                inputs=[
                    chat_interface.chatbot_state,
                    study_images,
                    do_sample,
                    temperature,
                    top_k,
                    top_p,
                    length_penalty,
                    num_beams,
                    no_repeat_ngram_size,
                    max_new_tokens,
                    prior_images,
                    prior_report,
                ],
                outputs=[
                    chat_interface.chatbot,
                    chat_interface.chatbot_state,
                ],
            )

    return demo


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--host", type=str, default="0.0.0.0")
    parser.add_argument("--debug", action="store_true", help="using debug mode")
    parser.add_argument("--port", type=int)
    parser.add_argument("--share", action="store_true", help="share")
    parser.add_argument("--dtype", type=str, default="torch.bfloat16")
    args = parser.parse_args()

    device = torch.device("cuda")
    dtype = eval(args.dtype)
    processor, model = load_model(device, dtype)

    demo = build_demo("M4CXR")
    demo.queue(status_update_rate=10, api_open=False).launch(
        server_name=args.host, debug=args.debug, server_port=args.port, share=args.share
    )