MorganBrizon commited on
Commit
ed44c23
·
verified ·
1 Parent(s): 0272714

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -35
app.py CHANGED
@@ -1,58 +1,35 @@
 
1
  import os
2
  import tempfile
3
- import uvicorn
4
  from fastapi import FastAPI, UploadFile, File, HTTPException
5
  from fastapi.responses import JSONResponse
6
  import tensorflow as tf
7
-
8
- # Import your prediction function from your prediction module.
9
- from prediction import predict_eeg_recording
10
 
11
  app = FastAPI(title="EEG Epilepsy Prediction API")
12
-
13
- # Load the trained model once at startup.
14
  model = tf.keras.models.load_model('model1_2dcnn.h5')
15
 
16
- @app.get("/", tags=["Introduction Endpoints"])
17
- async def index():
18
- """
19
- Simply returns a welcome message!
20
- """
21
- message = (
22
- "Hello world! Welcome to the EEG Epilepsy Prediction API. "
23
- "Submit an EEG recording EDF file to the `/predict` endpoint to receive a prediction."
24
- )
25
- return message
26
-
27
- @app.post("/predict", tags=["Machine Learning"])
28
  async def predict_endpoint(file: UploadFile = File(...)):
29
- """
30
- Accepts an EEG EDF file, processes it using the preprocessing and spectrogram conversion functions,
31
- and returns an aggregated prediction (epilepsy or no epilepsy) along with the mean probability.
32
- """
33
- # Save the uploaded file temporarily.
34
  try:
35
  suffix = os.path.splitext(file.filename)[1]
36
  with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
37
  tmp.write(await file.read())
38
  tmp_path = tmp.name
39
- except Exception as e:
40
- raise HTTPException(status_code=500, detail="Error saving temporary file")
41
-
42
- # Use your prediction function to process the file and obtain an aggregated prediction.
43
  try:
44
- pred_label, mean_prob = predict_eeg_recording(tmp_path, model, threshold=0.5)
45
  except Exception as e:
46
  os.remove(tmp_path)
47
- raise HTTPException(status_code=400, detail=f"Prediction failed: {e}")
48
 
49
  os.remove(tmp_path)
50
-
51
  response = {
52
- "prediction": "epilepsy" if pred_label == 1 else "no epilepsy",
53
- "mean_probability": float(mean_prob)
 
54
  }
55
  return JSONResponse(content=response)
56
-
57
- if __name__ == "__main__":
58
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ # app.py (API FastAPI)
2
  import os
3
  import tempfile
 
4
  from fastapi import FastAPI, UploadFile, File, HTTPException
5
  from fastapi.responses import JSONResponse
6
  import tensorflow as tf
7
+ from prediction import predict_eeg_recording # à adapter selon ton nom de module
 
 
8
 
9
  app = FastAPI(title="EEG Epilepsy Prediction API")
 
 
10
  model = tf.keras.models.load_model('model1_2dcnn.h5')
11
 
12
+ @app.post("/predict")
 
 
 
 
 
 
 
 
 
 
 
13
  async def predict_endpoint(file: UploadFile = File(...)):
 
 
 
 
 
14
  try:
15
  suffix = os.path.splitext(file.filename)[1]
16
  with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
17
  tmp.write(await file.read())
18
  tmp_path = tmp.name
19
+ except Exception:
20
+ raise HTTPException(status_code=500, detail="Erreur lors de la sauvegarde temporaire")
21
+
 
22
  try:
23
+ label, mean_prob, segment_probs = predict_eeg_recording(tmp_path, model, threshold=0.5)
24
  except Exception as e:
25
  os.remove(tmp_path)
26
+ raise HTTPException(status_code=400, detail=f"Erreur prédiction : {e}")
27
 
28
  os.remove(tmp_path)
29
+
30
  response = {
31
+ "prediction": "epilepsy" if label == 1 else "no epilepsy",
32
+ "mean_probability": float(mean_prob),
33
+ "segment_probabilities": [float(p) for p in segment_probs]
34
  }
35
  return JSONResponse(content=response)