Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from model_loader import load_vChain_model
|
| 2 |
from inference import preprocess_input, segment_all
|
| 3 |
-
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import numpy as np
|
| 6 |
import nibabel as nib
|
|
@@ -26,9 +27,25 @@ async def segment(files: list[UploadFile]):
|
|
| 26 |
print("Segmented Successfuly!", output.shape)
|
| 27 |
output_img = nib.Nifti1Image(output, affine=np.eye(4))
|
| 28 |
|
| 29 |
-
with tempfile.NamedTemporaryFile(suffix=".nii") as tempFile:
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
print(f"Error occurred: {str(e)}")
|
|
|
|
| 1 |
from model_loader import load_vChain_model
|
| 2 |
from inference import preprocess_input, segment_all
|
| 3 |
+
import io
|
| 4 |
+
import gzip
|
| 5 |
import tempfile
|
| 6 |
import numpy as np
|
| 7 |
import nibabel as nib
|
|
|
|
| 27 |
print("Segmented Successfuly!", output.shape)
|
| 28 |
output_img = nib.Nifti1Image(output, affine=np.eye(4))
|
| 29 |
|
| 30 |
+
# with tempfile.NamedTemporaryFile(suffix=".nii") as tempFile:
|
| 31 |
+
# nib.save(output_img, tempFile.name)
|
| 32 |
+
# return FileResponse(tempFile.name, media_type="multipart/form-data")
|
| 33 |
+
|
| 34 |
+
with io.BytesIO() as buffer:
|
| 35 |
+
# Save the NIfTI image to the buffer
|
| 36 |
+
nib.save(nifti_img, buffer)
|
| 37 |
+
buffer.seek(0) # Reset the buffer cursor to the beginning
|
| 38 |
+
|
| 39 |
+
# Compress the buffer content using gzip
|
| 40 |
+
with io.BytesIO() as gz_buffer:
|
| 41 |
+
with gzip.GzipFile(fileobj=gz_buffer, mode="wb") as gz_file:
|
| 42 |
+
gz_file.write(buffer.read())
|
| 43 |
+
|
| 44 |
+
# Get the compressed content
|
| 45 |
+
compressed_content = gz_buffer.getvalue()
|
| 46 |
+
|
| 47 |
+
# Return the compressed content as a streaming response
|
| 48 |
+
return StreamingResponse(content=compressed_content, media_type="application/octet-stream")
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Error occurred: {str(e)}")
|