LouayMagdy commited on
Commit
a1a9cb3
·
verified ·
1 Parent(s): 75adf7a

Upload main.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. main.py +8 -33
main.py CHANGED
@@ -1,33 +1,8 @@
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
+ numpy
2
+ nibabel
3
+ torch
4
+ torch.nn
5
+ torch.summary
6
+ fastapi
7
+ uvicorn
8
+ python-multipart