Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import shutil
|
|
| 5 |
import uuid
|
| 6 |
import zipfile
|
| 7 |
import nibabel as nib
|
| 8 |
-
import numpy as np
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
|
| 11 |
def run_segmentation(uploaded_file, modality):
|
|
@@ -19,7 +18,7 @@ def run_segmentation(uploaded_file, modality):
|
|
| 19 |
with open(input_filename, "wb") as f:
|
| 20 |
f.write(uploaded_file.read())
|
| 21 |
else:
|
| 22 |
-
return "Invalid file input"
|
| 23 |
|
| 24 |
command = ["TotalSegmentator", "-i", input_filename, "-o", output_folder]
|
| 25 |
if modality == "MR":
|
|
@@ -28,8 +27,11 @@ def run_segmentation(uploaded_file, modality):
|
|
| 28 |
try:
|
| 29 |
subprocess.run(command, check=True)
|
| 30 |
except subprocess.CalledProcessError as e:
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
zip_filename = f"segmentations_{job_id}.zip"
|
| 34 |
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 35 |
for root, dirs, files in os.walk(output_folder):
|
|
@@ -39,6 +41,7 @@ def run_segmentation(uploaded_file, modality):
|
|
| 39 |
zipf.write(file_path, arcname)
|
| 40 |
|
| 41 |
seg_files = [os.path.join(output_folder, f) for f in os.listdir(output_folder) if f.endswith('.nii.gz')]
|
|
|
|
| 42 |
if seg_files:
|
| 43 |
seg_file = seg_files[0]
|
| 44 |
try:
|
|
@@ -55,13 +58,11 @@ def run_segmentation(uploaded_file, modality):
|
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Error creating preview: {e}")
|
| 57 |
image_filename = None
|
| 58 |
-
else:
|
| 59 |
-
image_filename = None
|
| 60 |
|
| 61 |
os.remove(input_filename)
|
| 62 |
shutil.rmtree(output_folder)
|
| 63 |
|
| 64 |
-
return zip_filename, image_filename
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
gr.Markdown("# TotalSegmentator Gradio App")
|
|
@@ -78,7 +79,9 @@ with gr.Blocks() as demo:
|
|
| 78 |
zip_output = gr.File(label="Download Segmentation Output (zip)")
|
| 79 |
preview_output = gr.Image(label="Segmentation Preview")
|
| 80 |
|
|
|
|
|
|
|
| 81 |
run_btn = gr.Button("Run Segmentation")
|
| 82 |
-
run_btn.click(fn=run_segmentation, inputs=[uploaded_file, modality], outputs=[zip_output, preview_output])
|
| 83 |
|
| 84 |
demo.launch()
|
|
|
|
| 5 |
import uuid
|
| 6 |
import zipfile
|
| 7 |
import nibabel as nib
|
|
|
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
|
| 10 |
def run_segmentation(uploaded_file, modality):
|
|
|
|
| 18 |
with open(input_filename, "wb") as f:
|
| 19 |
f.write(uploaded_file.read())
|
| 20 |
else:
|
| 21 |
+
return None, None, "Invalid file input."
|
| 22 |
|
| 23 |
command = ["TotalSegmentator", "-i", input_filename, "-o", output_folder]
|
| 24 |
if modality == "MR":
|
|
|
|
| 27 |
try:
|
| 28 |
subprocess.run(command, check=True)
|
| 29 |
except subprocess.CalledProcessError as e:
|
| 30 |
+
error_message = f"Error during segmentation: {e}"
|
| 31 |
+
if os.path.exists(input_filename): os.remove(input_filename)
|
| 32 |
+
if os.path.exists(output_folder): shutil.rmtree(output_folder)
|
| 33 |
+
return None, None, error_message
|
| 34 |
+
|
| 35 |
zip_filename = f"segmentations_{job_id}.zip"
|
| 36 |
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 37 |
for root, dirs, files in os.walk(output_folder):
|
|
|
|
| 41 |
zipf.write(file_path, arcname)
|
| 42 |
|
| 43 |
seg_files = [os.path.join(output_folder, f) for f in os.listdir(output_folder) if f.endswith('.nii.gz')]
|
| 44 |
+
image_filename = None
|
| 45 |
if seg_files:
|
| 46 |
seg_file = seg_files[0]
|
| 47 |
try:
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error creating preview: {e}")
|
| 60 |
image_filename = None
|
|
|
|
|
|
|
| 61 |
|
| 62 |
os.remove(input_filename)
|
| 63 |
shutil.rmtree(output_folder)
|
| 64 |
|
| 65 |
+
return zip_filename, image_filename, "Segmentation completed successfully."
|
| 66 |
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
gr.Markdown("# TotalSegmentator Gradio App")
|
|
|
|
| 79 |
zip_output = gr.File(label="Download Segmentation Output (zip)")
|
| 80 |
preview_output = gr.Image(label="Segmentation Preview")
|
| 81 |
|
| 82 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 83 |
+
|
| 84 |
run_btn = gr.Button("Run Segmentation")
|
| 85 |
+
run_btn.click(fn=run_segmentation, inputs=[uploaded_file, modality], outputs=[zip_output, preview_output, status_output])
|
| 86 |
|
| 87 |
demo.launch()
|