Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,12 +11,14 @@ app = FastAPI(title="EEG Epilepsy Prediction API")
|
|
| 11 |
|
| 12 |
@app.get("/", tags=["Introduction Endpoints"])
|
| 13 |
async def index():
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.post("/predict", tags=["Machine Learning"])
|
| 22 |
async def predict_endpoint(
|
|
@@ -24,6 +26,12 @@ async def predict_endpoint(
|
|
| 24 |
model_choice: str = "2DCNN",
|
| 25 |
ensemble_method: str = None
|
| 26 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
print("Saving uploaded file as temporary file...")
|
| 28 |
try:
|
| 29 |
suffix = os.path.splitext(file.filename)[1]
|
|
@@ -31,8 +39,8 @@ async def predict_endpoint(
|
|
| 31 |
tmp.write(await file.read())
|
| 32 |
tmp_path = tmp.name
|
| 33 |
except Exception as e:
|
| 34 |
-
raise HTTPException(status_code=500, detail=
|
| 35 |
-
|
| 36 |
print("Performing prediction using model_choice =", model_choice)
|
| 37 |
try:
|
| 38 |
if model_choice.lower() == "ensemble":
|
|
@@ -47,8 +55,8 @@ async def predict_endpoint(
|
|
| 47 |
)
|
| 48 |
except Exception as e:
|
| 49 |
os.remove(tmp_path)
|
| 50 |
-
raise HTTPException(status_code=
|
| 51 |
-
|
| 52 |
os.remove(tmp_path)
|
| 53 |
|
| 54 |
response = {
|
|
|
|
| 11 |
|
| 12 |
@app.get("/", tags=["Introduction Endpoints"])
|
| 13 |
async def index():
|
| 14 |
+
"""
|
| 15 |
+
Simply returns a welcome message!
|
| 16 |
+
"""
|
| 17 |
+
message = (
|
| 18 |
+
"Hello world! Welcome to the EEG Epilepsy Prediction API. "
|
| 19 |
+
"Submit an EEG recording EDF file to the `/predict` endpoint to receive a prediction."
|
| 20 |
+
)
|
| 21 |
+
return message
|
| 22 |
|
| 23 |
@app.post("/predict", tags=["Machine Learning"])
|
| 24 |
async def predict_endpoint(
|
|
|
|
| 26 |
model_choice: str = "2DCNN",
|
| 27 |
ensemble_method: str = None
|
| 28 |
):
|
| 29 |
+
"""
|
| 30 |
+
Query parameters:
|
| 31 |
+
- model_choice: Choose one model among "2DCNN", "EEGNet", "EpilepsyNet", or "ensemble".
|
| 32 |
+
- ensemble_method: (Optional, required if model_choice is "ensemble")
|
| 33 |
+
The ensemble method to use ("average" or "voting").
|
| 34 |
+
"""
|
| 35 |
print("Saving uploaded file as temporary file...")
|
| 36 |
try:
|
| 37 |
suffix = os.path.splitext(file.filename)[1]
|
|
|
|
| 39 |
tmp.write(await file.read())
|
| 40 |
tmp_path = tmp.name
|
| 41 |
except Exception as e:
|
| 42 |
+
raise HTTPException(status_code=500, detail="Error saving temporary file")
|
| 43 |
+
|
| 44 |
print("Performing prediction using model_choice =", model_choice)
|
| 45 |
try:
|
| 46 |
if model_choice.lower() == "ensemble":
|
|
|
|
| 55 |
)
|
| 56 |
except Exception as e:
|
| 57 |
os.remove(tmp_path)
|
| 58 |
+
raise HTTPException(status_code=400, detail=f"Prediction failed: {e}")
|
| 59 |
+
|
| 60 |
os.remove(tmp_path)
|
| 61 |
|
| 62 |
response = {
|