Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,14 +11,12 @@ 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 |
-
return message
|
| 22 |
|
| 23 |
@app.post("/predict", tags=["Machine Learning"])
|
| 24 |
async def predict_endpoint(
|
|
@@ -26,14 +24,6 @@ async def predict_endpoint(
|
|
| 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]
|
|
@@ -41,23 +31,26 @@ async def predict_endpoint(
|
|
| 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(
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
-
pred_label, mean_prob = predict_eeg_recording(
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
os.remove(tmp_path)
|
| 56 |
-
raise HTTPException(status_code=
|
| 57 |
|
| 58 |
os.remove(tmp_path)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
response = {
|
| 62 |
"prediction": "epilepsy" if pred_label == 1 else "no epilepsy",
|
| 63 |
"mean_probability": float(mean_prob),
|
|
|
|
| 11 |
|
| 12 |
@app.get("/", tags=["Introduction Endpoints"])
|
| 13 |
async def index():
|
| 14 |
+
return {
|
| 15 |
+
"message": (
|
| 16 |
+
"Hello world! Welcome to the EEG Epilepsy Prediction API. "
|
| 17 |
+
"Submit an EEG recording EDF file to the `/predict` endpoint to receive a prediction."
|
| 18 |
+
)
|
| 19 |
+
}
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.post("/predict", tags=["Machine Learning"])
|
| 22 |
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 |
tmp.write(await file.read())
|
| 32 |
tmp_path = tmp.name
|
| 33 |
except Exception as e:
|
| 34 |
+
raise HTTPException(status_code=500, detail=f"Error saving temporary file: {e}")
|
| 35 |
|
| 36 |
print("Performing prediction using model_choice =", model_choice)
|
| 37 |
try:
|
| 38 |
if model_choice.lower() == "ensemble":
|
| 39 |
if ensemble_method is None:
|
| 40 |
raise HTTPException(status_code=400, detail="ensemble_method must be specified when using ensemble model_choice")
|
| 41 |
+
pred_label, mean_prob, segment_probs = predict_ensemble_eeg_recording(
|
| 42 |
+
tmp_path, ensemble_method=ensemble_method, threshold=0.5
|
| 43 |
+
)
|
| 44 |
else:
|
| 45 |
+
pred_label, mean_prob, segment_probs = predict_eeg_recording(
|
| 46 |
+
tmp_path, model_name=model_choice, threshold=0.5
|
| 47 |
+
)
|
| 48 |
except Exception as e:
|
| 49 |
os.remove(tmp_path)
|
| 50 |
+
raise HTTPException(status_code=500, detail=f"Prediction failed: {e}")
|
| 51 |
|
| 52 |
os.remove(tmp_path)
|
| 53 |
+
|
|
|
|
| 54 |
response = {
|
| 55 |
"prediction": "epilepsy" if pred_label == 1 else "no epilepsy",
|
| 56 |
"mean_probability": float(mean_prob),
|