GitLab CI commited on
Commit
b41ac05
·
1 Parent(s): 7cad229

Déploiement Dashboard depuis GitLab CI - 2026-05-06 11:39:02

Browse files
Files changed (1) hide show
  1. streamlit_app.py +37 -2
streamlit_app.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
 
2
  import tempfile
 
3
  import requests
4
  import pandas as pd
5
  import json
@@ -22,7 +24,42 @@ from huggingface_hub import hf_hub_download
22
  from functions import most_important_features_min_max
23
  features_min_max = most_important_features_min_max()
24
 
 
 
 
 
 
 
 
 
 
 
 
25
  @st.cache_resource(show_spinner=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def load_shap_explainer():
27
  """Charge l'explainer SHAP depuis le fichier pickle."""
28
  with open("./hgb_shap_explainer.pkl", "rb") as f:
@@ -117,8 +154,6 @@ with tab1:
117
  help="Moyenne (ratio) du nombre de demandes précédentes faites suite à une visite en agence (walk-in)",
118
  key='prev_name_product_type_walk_in_mean')
119
 
120
- # URL de l'API
121
- API_URL = "https://cedm-oc-mlops-projet-2.hf.space"
122
 
123
  if st.button("🔮 Lancer la prédiction", type="primary"):
124
  # Bouton pour lancer la prédiction
 
1
  import os
2
+ import time
3
  import tempfile
4
+ import threading
5
  import requests
6
  import pandas as pd
7
  import json
 
24
  from functions import most_important_features_min_max
25
  features_min_max = most_important_features_min_max()
26
 
27
+ # -----------------------------------------------------------------------
28
+ # Configuration globale – URLs des services HF Spaces
29
+ # -----------------------------------------------------------------------
30
+ API_URL = os.environ.get("API_URL", "https://cedm-oc-mlops-projet-2.hf.space")
31
+ KEEP_ALIVE_INTERVAL = 12 * 3600 # 12 heures en secondes
32
+
33
+
34
+ # -----------------------------------------------------------------------
35
+ # Keep-alive : maintien des containers HF Spaces en état "running"
36
+ # Utilise @st.cache_resource pour ne démarrer le thread qu'une seule fois.
37
+ # -----------------------------------------------------------------------
38
  @st.cache_resource(show_spinner=False)
39
+ def start_keep_alive():
40
+ """
41
+ Démarre un thread démon qui envoie des requêtes croisées
42
+ entre le dashboard et l'API toutes les 12 h, afin de maintenir
43
+ les deux containers HF Spaces actifs.
44
+ """
45
+ def _loop():
46
+ while True:
47
+ time.sleep(KEEP_ALIVE_INTERVAL)
48
+ # Ping de l'API depuis le dashboard (ping croisé suffisant)
49
+ try:
50
+ r = requests.get(f"{API_URL}/health", timeout=30)
51
+ print(f"[keep-alive] Ping API → HTTP {r.status_code}")
52
+ except Exception as e:
53
+ print(f"[keep-alive] Ping API échoué: {e}")
54
+
55
+ thread = threading.Thread(target=_loop, daemon=True, name="keep-alive-api")
56
+ thread.start()
57
+ return thread
58
+
59
+
60
+ start_keep_alive()
61
+
62
+ # -----------------------------------------------------------------------
63
  def load_shap_explainer():
64
  """Charge l'explainer SHAP depuis le fichier pickle."""
65
  with open("./hgb_shap_explainer.pkl", "rb") as f:
 
154
  help="Moyenne (ratio) du nombre de demandes précédentes faites suite à une visite en agence (walk-in)",
155
  key='prev_name_product_type_walk_in_mean')
156
 
 
 
157
 
158
  if st.button("🔮 Lancer la prédiction", type="primary"):
159
  # Bouton pour lancer la prédiction