LouayMagdy commited on
Commit
1029406
·
verified ·
1 Parent(s): 634eadd

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +34 -32
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
- wt_model, tc_model, et_model = load_vChain_model('./wt_chkpt.pth', './tc_chkpt.pth', './et_chkpt.pth')
13
- app = FastAPI()
14
-
15
- class NiftiFile(BaseModel):
16
- file: UploadFile
17
-
18
- @app.post("/segment/")
19
- async def segment(modalities: List[NiftiFile]):
20
- try:
21
- if len(modalities) != 4:
22
- return {"msg": "Invalid Input Format"}
23
- flair, t1, t1ce, t2 = (m.file for m in modalities)
24
- flair_tensor, t1_tensor, t1ce_tensor, t2_tensor = preprocess_input(flair, t1, t1ce, t2)
25
-
26
- output = segment_all(wt_model, tc_model, et_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor)
27
- with tempfile.NamedTemporaryFile(suffix=".nii") as tempFile:
28
- nib.save(output, tempFile.name)
29
- return FileResponse(tempFile.name, media_type="application/octet-stream")
30
-
31
- except Exception as e:
32
- print(f"Error occurred: {str(e)}")
 
 
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."}