Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,33 +1,35 @@
|
|
| 1 |
-
from model_loader import load_vChain_model
|
| 2 |
-
from inference import preprocess_input, segment_all
|
| 3 |
-
|
| 4 |
-
import tempfile
|
| 5 |
-
import nibabel as nib
|
| 6 |
-
from typing import List
|
| 7 |
-
from pydantic import BaseModel
|
| 8 |
-
from fastapi import FastAPI, UploadFile
|
| 9 |
-
from fastapi.responses import FileResponse
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
return {"msg": "An error occurred during segmentation."}
|
|
|
|
| 1 |
+
from model_loader import load_vChain_model
|
| 2 |
+
from inference import preprocess_input, segment_all
|
| 3 |
+
|
| 4 |
+
import tempfile
|
| 5 |
+
import nibabel as nib
|
| 6 |
+
from typing import List
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from fastapi import FastAPI, UploadFile
|
| 9 |
+
from fastapi.responses import FileResponse
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
wt_model, tc_model, et_model = load_vChain_model('./wt_chkpt.pth', './tc_chkpt.pth', './et_chkpt.pth')
|
| 14 |
+
|
| 15 |
+
print("Waiting For Segmentation Requests ...")
|
| 16 |
+
|
| 17 |
+
class NiftiFile(BaseModel):
|
| 18 |
+
file: UploadFile
|
| 19 |
+
|
| 20 |
+
@app.post("/segment/")
|
| 21 |
+
async def segment(modalities: List[NiftiFile]):
|
| 22 |
+
try:
|
| 23 |
+
if len(modalities) != 4:
|
| 24 |
+
return {"msg": "Invalid Input Format"}
|
| 25 |
+
flair, t1, t1ce, t2 = (m.file for m in modalities)
|
| 26 |
+
flair_tensor, t1_tensor, t1ce_tensor, t2_tensor = preprocess_input(flair, t1, t1ce, t2)
|
| 27 |
+
|
| 28 |
+
output = segment_all(wt_model, tc_model, et_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor)
|
| 29 |
+
with tempfile.NamedTemporaryFile(suffix=".nii") as tempFile:
|
| 30 |
+
nib.save(output, tempFile.name)
|
| 31 |
+
return FileResponse(tempFile.name, media_type="application/octet-stream")
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Error occurred: {str(e)}")
|
| 35 |
return {"msg": "An error occurred during segmentation."}
|