Update app.py
Browse files
app.py
CHANGED
|
@@ -3,41 +3,69 @@ import os
|
|
| 3 |
from utils import load_dicoms
|
| 4 |
from model import generate_report
|
| 5 |
from utils import load_dicoms, generate_pdf
|
|
|
|
| 6 |
|
| 7 |
-
def process_scan(dicom_files):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
def run_generate(dicom_files):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def reset_on_clear():
|
| 36 |
return (
|
| 37 |
gr.Gallery(value=None, visible=False), # scan_display
|
| 38 |
"", # report_box
|
| 39 |
gr.Button(interactive=False), # generate_btn
|
| 40 |
-
gr.DownloadButton(visible=False) # download_btn
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
# Build the UI
|
|
@@ -46,14 +74,16 @@ with gr.Blocks(title="MRI Brain Report Generator") as app:
|
|
| 46 |
gr.Markdown("# MRI Brain Scan Report Generator")
|
| 47 |
gr.Markdown("### Radiologist Assistant")
|
| 48 |
gr.Markdown("Upload DICOM brain scans to generate a report.")
|
|
|
|
|
|
|
| 49 |
|
| 50 |
with gr.Row():
|
| 51 |
# Left column - image input/display
|
| 52 |
with gr.Column():
|
| 53 |
upload_btn = gr.File(
|
| 54 |
-
label="Upload
|
| 55 |
-
file_types=[".
|
| 56 |
-
file_count="
|
| 57 |
)
|
| 58 |
scan_display = gr.Gallery(
|
| 59 |
label="Scan Preview",
|
|
@@ -84,12 +114,12 @@ with gr.Blocks(title="MRI Brain Report Generator") as app:
|
|
| 84 |
upload_btn.change(
|
| 85 |
fn=process_scan,
|
| 86 |
inputs=upload_btn,
|
| 87 |
-
outputs=[scan_display, generate_btn]
|
| 88 |
)
|
| 89 |
|
| 90 |
generate_btn.click(
|
| 91 |
fn=run_generate,
|
| 92 |
-
inputs=
|
| 93 |
outputs=[report_box, download_btn]
|
| 94 |
)
|
| 95 |
|
|
@@ -102,7 +132,7 @@ with gr.Blocks(title="MRI Brain Report Generator") as app:
|
|
| 102 |
upload_btn.clear(
|
| 103 |
fn=reset_on_clear,
|
| 104 |
inputs=None,
|
| 105 |
-
outputs=[scan_display, report_box, generate_btn, download_btn]
|
| 106 |
)
|
| 107 |
|
| 108 |
app.launch()
|
|
|
|
| 3 |
from utils import load_dicoms
|
| 4 |
from model import generate_report
|
| 5 |
from utils import load_dicoms, generate_pdf
|
| 6 |
+
from utils import convert_dicom_zip_to_nifti, generate_pdf
|
| 7 |
|
| 8 |
+
# def process_scan(dicom_files):
|
| 9 |
+
# if not dicom_files:
|
| 10 |
+
# return gr.Gallery(visible=False), gr.Button(interactive=False)
|
| 11 |
|
| 12 |
+
# images = load_dicoms(dicom_files)
|
| 13 |
+
# if not images:
|
| 14 |
+
# return gr.Gallery(visible=False), gr.Button(interactive=False)
|
| 15 |
|
| 16 |
+
# return gr.Gallery(value=images, visible=True), gr.Button(interactive=True)
|
| 17 |
|
| 18 |
+
# def run_generate(dicom_files):
|
| 19 |
+
# if not dicom_files:
|
| 20 |
+
# return "", gr.DownloadButton(visible=False)
|
| 21 |
|
| 22 |
+
# images = load_dicoms(dicom_files)
|
| 23 |
+
# if not images:
|
| 24 |
+
# return gr.DownloadButton(visible=False)
|
| 25 |
|
| 26 |
+
# report = generate_report(images)
|
| 27 |
|
| 28 |
+
# return report, gr.DownloadButton(visible=True)
|
| 29 |
+
|
| 30 |
+
# def download_report(report_text):
|
| 31 |
+
# if not report_text or not report_text.strip():
|
| 32 |
+
# gr.Warning("No report to download yet.")
|
| 33 |
+
# return None
|
| 34 |
+
# return generate_pdf(report_text)
|
| 35 |
+
|
| 36 |
+
def process_scan(zip_file):
|
| 37 |
+
if zip_file is None:
|
| 38 |
+
return gr.Gallery(visible=False), gr.Button(interactive=False), None
|
| 39 |
|
| 40 |
+
sequence_images = convert_dicom_zip_to_nifti(zip_file)
|
| 41 |
+
|
| 42 |
+
if not sequence_images:
|
| 43 |
+
gr.Warning("No valid sequences found in ZIP.")
|
| 44 |
+
return gr.Gallery(visible=False), gr.Button(interactive=False), None
|
| 45 |
+
|
| 46 |
+
gallery_items = [(img, name) for img, name in sequence_images]
|
| 47 |
+
|
| 48 |
+
return (
|
| 49 |
+
gr.Gallery(value=gallery_items, visible=True),
|
| 50 |
+
gr.Button(interactive=True),
|
| 51 |
+
sequence_images # ← saved into State
|
| 52 |
+
)
|
| 53 |
+
def run_generate(sequence_images):
|
| 54 |
+
if not sequence_images:
|
| 55 |
+
return "", gr.DownloadButton(visible=False)
|
| 56 |
+
|
| 57 |
+
images = [s[1] for s in sequence_images]
|
| 58 |
+
report = generate_report(images)
|
| 59 |
+
|
| 60 |
+
return report, gr.DownloadButton(visible=True)
|
| 61 |
|
| 62 |
def reset_on_clear():
|
| 63 |
return (
|
| 64 |
gr.Gallery(value=None, visible=False), # scan_display
|
| 65 |
"", # report_box
|
| 66 |
gr.Button(interactive=False), # generate_btn
|
| 67 |
+
gr.DownloadButton(visible=False), # download_btn
|
| 68 |
+
None
|
| 69 |
)
|
| 70 |
|
| 71 |
# Build the UI
|
|
|
|
| 74 |
gr.Markdown("# MRI Brain Scan Report Generator")
|
| 75 |
gr.Markdown("### Radiologist Assistant")
|
| 76 |
gr.Markdown("Upload DICOM brain scans to generate a report.")
|
| 77 |
+
|
| 78 |
+
sequence_state = gr.State(value=None)
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
# Left column - image input/display
|
| 82 |
with gr.Column():
|
| 83 |
upload_btn = gr.File(
|
| 84 |
+
label="Upload DICOM Study as ZIP",
|
| 85 |
+
file_types=[".zip"],
|
| 86 |
+
file_count="single"
|
| 87 |
)
|
| 88 |
scan_display = gr.Gallery(
|
| 89 |
label="Scan Preview",
|
|
|
|
| 114 |
upload_btn.change(
|
| 115 |
fn=process_scan,
|
| 116 |
inputs=upload_btn,
|
| 117 |
+
outputs=[scan_display, generate_btn, sequence_state]
|
| 118 |
)
|
| 119 |
|
| 120 |
generate_btn.click(
|
| 121 |
fn=run_generate,
|
| 122 |
+
inputs=sequence_state,
|
| 123 |
outputs=[report_box, download_btn]
|
| 124 |
)
|
| 125 |
|
|
|
|
| 132 |
upload_btn.clear(
|
| 133 |
fn=reset_on_clear,
|
| 134 |
inputs=None,
|
| 135 |
+
outputs=[scan_display, report_box, generate_btn, download_btn, sequence_state]
|
| 136 |
)
|
| 137 |
|
| 138 |
app.launch()
|