Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,69 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import tempfile
|
| 3 |
-
import uvicorn
|
| 4 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 5 |
-
from fastapi.responses import JSONResponse
|
| 6 |
-
|
| 7 |
-
# Import your prediction functions
|
| 8 |
-
from prediction import predict_eeg_recording, predict_ensemble_eeg_recording
|
| 9 |
-
|
| 10 |
-
app = FastAPI(title="EEG Epilepsy Prediction API")
|
| 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(
|
| 25 |
-
file: UploadFile = File(...),
|
| 26 |
-
model_choice: str = "2DCNN",
|
| 27 |
-
ensemble_method: str = None
|
| 28 |
-
):
|
| 29 |
-
"""
|
| 30 |
-
|
| 31 |
-
Query parameters:
|
| 32 |
-
- model_choice: Choose one model among "2DCNN", "EEGNet", "EpilepsyNet", or "ensemble".
|
| 33 |
-
- ensemble_method: (Optional, required if model_choice is "ensemble")
|
| 34 |
-
The ensemble method to use ("average" or "voting").
|
| 35 |
-
|
| 36 |
-
"""
|
| 37 |
-
print("Saving uploaded file as temporary file...")
|
| 38 |
-
try:
|
| 39 |
-
suffix = os.path.splitext(file.filename)[1]
|
| 40 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
|
| 41 |
-
tmp.write(await file.read())
|
| 42 |
-
tmp_path = tmp.name
|
| 43 |
-
except Exception as e:
|
| 44 |
-
raise HTTPException(status_code=500, detail="Error saving temporary file")
|
| 45 |
-
|
| 46 |
-
print("Performing prediction using model_choice =", model_choice)
|
| 47 |
-
try:
|
| 48 |
-
if model_choice.lower() == "ensemble":
|
| 49 |
-
if ensemble_method is None:
|
| 50 |
-
raise HTTPException(status_code=400, detail="ensemble_method must be specified when using ensemble model_choice")
|
| 51 |
-
pred_label, mean_prob = predict_ensemble_eeg_recording(tmp_path, ensemble_method=ensemble_method, threshold=0.5)
|
| 52 |
-
else:
|
| 53 |
-
pred_label, mean_prob = predict_eeg_recording(tmp_path, model_name=model_choice, threshold=0.5)
|
| 54 |
-
except Exception as e:
|
| 55 |
-
os.remove(tmp_path)
|
| 56 |
-
raise HTTPException(status_code=400, detail=f"Prediction failed: {e}")
|
| 57 |
-
|
| 58 |
-
os.remove(tmp_path)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
response = {
|
| 62 |
-
"prediction": "epilepsy" if pred_label == 1 else "no epilepsy",
|
| 63 |
-
"confidence": mean_prob
|
| 64 |
-
}
|
| 65 |
-
print("Prediction complete, returning response...")
|
| 66 |
-
return JSONResponse(content=response)
|
| 67 |
-
|
| 68 |
-
if __name__ == "__main__":
|
| 69 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|