Spaces:
Running
Running
Déploiement automatique depuis GitLab CI
Browse files
main.py
CHANGED
|
@@ -5,10 +5,12 @@ Cette API charge un modèle MLflow (pipeline sklearn) au démarrage et expose de
|
|
| 5 |
pour effectuer des prédictions de rendement (hg/ha) à partir de variables explicatives.
|
| 6 |
"""
|
| 7 |
|
|
|
|
| 8 |
import os
|
| 9 |
from contextlib import asynccontextmanager
|
| 10 |
from typing import Dict, List
|
| 11 |
|
|
|
|
| 12 |
import joblib
|
| 13 |
import numpy as np
|
| 14 |
import logfire
|
|
@@ -38,6 +40,9 @@ logfire.configure(
|
|
| 38 |
|
| 39 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
# Chemin vers le modèle MLflow (pipeline sklearn complet avec préprocesseur)
|
| 42 |
MODEL_PATH = os.path.join(BASE_DIR, MODEL_REL_PATH)
|
| 43 |
|
|
@@ -103,10 +108,38 @@ async def lifespan(app: FastAPI):
|
|
| 103 |
pipeline = load_pipeline()
|
| 104 |
available_items, available_areas, available_items_per_area = load_training_data()
|
| 105 |
logfire.info("API initialisée et prête")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
yield
|
|
|
|
|
|
|
| 107 |
logfire.info("API arrêtée")
|
| 108 |
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# =======================================================================================================
|
| 111 |
# Initialisation de l'application FastAPI + instrumentation Logfire
|
| 112 |
# =======================================================================================================
|
|
|
|
| 5 |
pour effectuer des prédictions de rendement (hg/ha) à partir de variables explicatives.
|
| 6 |
"""
|
| 7 |
|
| 8 |
+
import asyncio
|
| 9 |
import os
|
| 10 |
from contextlib import asynccontextmanager
|
| 11 |
from typing import Dict, List
|
| 12 |
|
| 13 |
+
import httpx
|
| 14 |
import joblib
|
| 15 |
import numpy as np
|
| 16 |
import logfire
|
|
|
|
| 40 |
|
| 41 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 42 |
|
| 43 |
+
# URL du frontend à reveiller toutes les 12h (définie via variable d'environnement)
|
| 44 |
+
FRONTEND_URL = os.environ.get("FRONTEND_URL", "https://huggingface.co/spaces/CedM/oc_mlops_projet_4_dashboard")
|
| 45 |
+
|
| 46 |
# Chemin vers le modèle MLflow (pipeline sklearn complet avec préprocesseur)
|
| 47 |
MODEL_PATH = os.path.join(BASE_DIR, MODEL_REL_PATH)
|
| 48 |
|
|
|
|
| 108 |
pipeline = load_pipeline()
|
| 109 |
available_items, available_areas, available_items_per_area = load_training_data()
|
| 110 |
logfire.info("API initialisée et prête")
|
| 111 |
+
|
| 112 |
+
# Lancement de la tâche keep-alive en arrière-plan
|
| 113 |
+
task = asyncio.create_task(keep_alive_task())
|
| 114 |
+
|
| 115 |
yield
|
| 116 |
+
|
| 117 |
+
task.cancel()
|
| 118 |
logfire.info("API arrêtée")
|
| 119 |
|
| 120 |
|
| 121 |
+
async def keep_alive_task():
|
| 122 |
+
"""
|
| 123 |
+
Tâche de keep-alive : ping le frontend toutes les 12h
|
| 124 |
+
afin de le maintenir actif sur les plateformes d'hébergement avec mise en veille.
|
| 125 |
+
"""
|
| 126 |
+
INTERVAL = 12 * 3600 # 12 heures en secondes
|
| 127 |
+
await asyncio.sleep(60) # Délai initial pour laisser le temps au démarrage complet
|
| 128 |
+
|
| 129 |
+
while True:
|
| 130 |
+
if FRONTEND_URL:
|
| 131 |
+
async with httpx.AsyncClient() as client:
|
| 132 |
+
try:
|
| 133 |
+
resp = await client.get(FRONTEND_URL, timeout=15)
|
| 134 |
+
logfire.info("Keep-alive frontend: réponse {status}", status=resp.status_code)
|
| 135 |
+
except Exception as e:
|
| 136 |
+
logfire.warning("Keep-alive frontend échoué: {error}", error=str(e))
|
| 137 |
+
else:
|
| 138 |
+
logfire.warning("Keep-alive: FRONTEND_URL non défini, aucun ping envoyé.")
|
| 139 |
+
|
| 140 |
+
await asyncio.sleep(INTERVAL)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
# =======================================================================================================
|
| 144 |
# Initialisation de l'application FastAPI + instrumentation Logfire
|
| 145 |
# =======================================================================================================
|