modif app pour chrger automatiqueuement new model
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import Literal
|
| 4 |
import pandas as pd
|
|
@@ -117,6 +118,17 @@ def get_latest_model_key():
|
|
| 117 |
except Exception as e:
|
| 118 |
raise RuntimeError(f"Erreur récupération modèle S3 : {e}")
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
@app.on_event("startup")
|
| 122 |
def load_model():
|
|
@@ -156,4 +168,11 @@ def predict(data: InputData):
|
|
| 156 |
print(f"Erreur prédiction : {e}")
|
| 157 |
raise HTTPException(status_code=500, detail=str(e))
|
| 158 |
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi import BackgroundTasks
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import Literal
|
| 5 |
import pandas as pd
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
raise RuntimeError(f"Erreur récupération modèle S3 : {e}")
|
| 120 |
|
| 121 |
+
def load_latest_model():
|
| 122 |
+
global model
|
| 123 |
+
latest_model_key = get_latest_model_key()
|
| 124 |
+
print(f"Rechargement du modèle : {latest_model_key}")
|
| 125 |
+
|
| 126 |
+
response = s3.get_object(Bucket=S3_BUCKET, Key=latest_model_key)
|
| 127 |
+
model_bytes = io.BytesIO(response["Body"].read())
|
| 128 |
+
|
| 129 |
+
model = joblib.load(model_bytes)
|
| 130 |
+
print("Nouveau modèle chargé avec succès")
|
| 131 |
+
return latest_model_key
|
| 132 |
|
| 133 |
@app.on_event("startup")
|
| 134 |
def load_model():
|
|
|
|
| 168 |
print(f"Erreur prédiction : {e}")
|
| 169 |
raise HTTPException(status_code=500, detail=str(e))
|
| 170 |
|
| 171 |
+
@app.post("/reload-model")
|
| 172 |
+
def reload_model(background_tasks: BackgroundTasks):
|
| 173 |
+
"""
|
| 174 |
+
Recharge le dernier modèle S3 sans redémarrer l'API.
|
| 175 |
+
"""
|
| 176 |
+
background_tasks.add_task(load_latest_model)
|
| 177 |
+
latest_key = get_latest_model_key()
|
| 178 |
+
return {"status - Rechargement en arrière-plan modèle :", latest_key}
|