fix(app): empêcher la traduction automatique qui affichait « Risque ÉCHEC »
Browse filesCause : « ÉCHEC » n'existe ni dans le code ni dans l'historique git ;
l'app émet bien « Risque FAIBLE » (vérifié avec Streamlit AppTest).
Streamlit sert ses pages avec <html lang="en"> : Chrome croit la page
anglaise et la « traduit » en français, ce qui corrompt les libellés
déjà en français (« FAIBLE » → « ÉCHEC », « pics » → « photos »).
- app.py : script exécuté au chargement qui déclare la page lang="fr",
translate="no", classe notranslate et meta google notranslate
- Dockerfile : patch de streamlit/static/index.html au build (lang fr,
meta notranslate) pour que la page soit correcte dès le premier octet ;
le build échoue si le motif n'est plus trouvé
- utils_simple.risk_message() : message sous la jauge centralisé
- Tests : message des 3 niveaux (bornes 15 et 35 comprises), aucun
« ÉCHEC » dans le code affiché, protection anti-traduction présente
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- CHANGELOG.md +1 -0
- Dockerfile +8 -0
- streamlit_app/app.py +7 -6
- streamlit_app/utils_simple.py +27 -0
- tests/test_messages.py +56 -0
|
@@ -18,6 +18,7 @@ Tous les changements notables de ce projet seront documentés dans ce fichier.
|
|
| 18 |
- `pyarrow==17.0.0` fixé dans les requirements (pip installait pyarrow 26, qui exige NumPy 2, ce qui faisait planter `st.dataframe` sur toute installation neuve) ; la CI installe désormais `requirements.txt`
|
| 19 |
- Affichage « < 1 % » pour les probabilités inférieures à 1 % (jauge, carte, tableau, messages) ; valeur calculée et niveau inchangés
|
| 20 |
- Consommation par défaut cohérente avec l'heure : 1 100 MW en pointe (18h-22h), 600 MW sinon ; remise à jour quand l'heure bascule de période, valeur modifiable (l'ancien défaut de 800 MW n'existait dans aucune des deux périodes du CSV)
|
|
|
|
| 21 |
- Fiche de données `docs/DATA.md` : générateur d'origine introuvable, contenu du CSV, taux par quartier, règles observées, limites
|
| 22 |
|
| 23 |
## [1.1.0] - 2026-06-17
|
|
|
|
| 18 |
- `pyarrow==17.0.0` fixé dans les requirements (pip installait pyarrow 26, qui exige NumPy 2, ce qui faisait planter `st.dataframe` sur toute installation neuve) ; la CI installe désormais `requirements.txt`
|
| 19 |
- Affichage « < 1 % » pour les probabilités inférieures à 1 % (jauge, carte, tableau, messages) ; valeur calculée et niveau inchangés
|
| 20 |
- Consommation par défaut cohérente avec l'heure : 1 100 MW en pointe (18h-22h), 600 MW sinon ; remise à jour quand l'heure bascule de période, valeur modifiable (l'ancien défaut de 800 MW n'existait dans aucune des deux périodes du CSV)
|
| 21 |
+
- « Risque ÉCHEC » au lieu de « Risque FAIBLE » : traduction automatique du navigateur (Streamlit déclare la page en anglais). La page est désormais déclarée `lang="fr"` et non traduisible (script dans l'app + patch de `index.html` dans le Dockerfile) ; message de niveau centralisé dans `risk_message()`
|
| 22 |
- Fiche de données `docs/DATA.md` : générateur d'origine introuvable, contenu du CSV, taux par quartier, règles observées, limites
|
| 23 |
|
| 24 |
## [1.1.0] - 2026-06-17
|
|
@@ -16,6 +16,14 @@ COPY .streamlit ./.streamlit
|
|
| 16 |
# Installer les dependances
|
| 17 |
RUN pip install --no-cache-dir -r requirements-hf.txt
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Port 7860 : port attendu par Hugging Face Spaces, identique à .streamlit/config.toml
|
| 20 |
EXPOSE 7860
|
| 21 |
|
|
|
|
| 16 |
# Installer les dependances
|
| 17 |
RUN pip install --no-cache-dir -r requirements-hf.txt
|
| 18 |
|
| 19 |
+
# Déclarer la page en français et non traduisible : Streamlit sert <html lang="en">,
|
| 20 |
+
# ce qui pousse Chrome à « traduire » l'app (ex. « FAIBLE » → « ÉCHEC »).
|
| 21 |
+
# Le build échoue si le motif n'est plus trouvé (version de Streamlit changée).
|
| 22 |
+
RUN IDX="$(python -c 'import pathlib, streamlit; print(pathlib.Path(streamlit.__file__).parent / "static" / "index.html")')" \
|
| 23 |
+
&& sed -i 's#<html lang="en">#<html lang="fr" translate="no">#; s#<head>#<head><meta name="google" content="notranslate">#' "$IDX" \
|
| 24 |
+
&& grep -q '<html lang="fr" translate="no">' "$IDX" \
|
| 25 |
+
&& grep -q 'content="notranslate"' "$IDX"
|
| 26 |
+
|
| 27 |
# Port 7860 : port attendu par Hugging Face Spaces, identique à .streamlit/config.toml
|
| 28 |
EXPOSE 7860
|
| 29 |
|
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import logging
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import plotly.graph_objects as go
|
|
@@ -25,11 +26,15 @@ from streamlit_app.utils_simple import (
|
|
| 25 |
check_training_range,
|
| 26 |
format_risk,
|
| 27 |
sync_conso_default,
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
from src.config import QUARTIERS_DAKAR, classify_risk, RISK_THRESHOLDS, SLIDER_CONFIG, TRAINING_RANGES
|
| 30 |
from src.weather_api import get_current_weather_dakar, get_weather_forecast_7days
|
| 31 |
|
| 32 |
st.set_page_config(page_title="Dakar Power", page_icon="⚡", layout="wide")
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Clear old session state
|
| 35 |
if 'initialized' not in st.session_state:
|
|
@@ -175,12 +180,8 @@ with tab1:
|
|
| 175 |
with col3:
|
| 176 |
st.metric("⚠️ NIVEAU", niveau)
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
elif risque < RISK_THRESHOLDS['eleve']:
|
| 181 |
-
st.warning(f"⚠️ Risque {niveau} ({format_risk(risque)})")
|
| 182 |
-
else:
|
| 183 |
-
st.error(f"🚨 Risque {niveau} ({format_risk(risque)})")
|
| 184 |
st.caption(
|
| 185 |
f"Probabilité de coupure estimée par le modèle (moyenne LightGBM/LSTM × coefficient du quartier). "
|
| 186 |
f"Niveaux : FAIBLE < {RISK_THRESHOLDS['faible']} %, MOYEN < {RISK_THRESHOLDS['eleve']} %, ÉLEVÉ au-delà. "
|
|
|
|
| 1 |
import logging
|
| 2 |
import streamlit as st
|
| 3 |
+
import streamlit.components.v1 as components
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
import plotly.graph_objects as go
|
|
|
|
| 26 |
check_training_range,
|
| 27 |
format_risk,
|
| 28 |
sync_conso_default,
|
| 29 |
+
risk_message,
|
| 30 |
+
NO_TRANSLATE_JS,
|
| 31 |
)
|
| 32 |
from src.config import QUARTIERS_DAKAR, classify_risk, RISK_THRESHOLDS, SLIDER_CONFIG, TRAINING_RANGES
|
| 33 |
from src.weather_api import get_current_weather_dakar, get_weather_forecast_7days
|
| 34 |
|
| 35 |
st.set_page_config(page_title="Dakar Power", page_icon="⚡", layout="wide")
|
| 36 |
+
# Empêche la traduction automatique du navigateur (voir NO_TRANSLATE_JS)
|
| 37 |
+
components.html(NO_TRANSLATE_JS, height=0)
|
| 38 |
|
| 39 |
# Clear old session state
|
| 40 |
if 'initialized' not in st.session_state:
|
|
|
|
| 180 |
with col3:
|
| 181 |
st.metric("⚠️ NIVEAU", niveau)
|
| 182 |
|
| 183 |
+
_kind, _texte = risk_message(risque)
|
| 184 |
+
getattr(st, _kind)(_texte)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
st.caption(
|
| 186 |
f"Probabilité de coupure estimée par le modèle (moyenne LightGBM/LSTM × coefficient du quartier). "
|
| 187 |
f"Niveaux : FAIBLE < {RISK_THRESHOLDS['faible']} %, MOYEN < {RISK_THRESHOLDS['eleve']} %, ÉLEVÉ au-delà. "
|
|
@@ -181,6 +181,33 @@ def format_risk(risque_pct):
|
|
| 181 |
return f"{risque_pct:.1f}%"
|
| 182 |
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
def get_risk_color(risque_pct):
|
| 185 |
if risque_pct < RISK_THRESHOLDS['faible']:
|
| 186 |
return "#2ecc71"
|
|
|
|
| 181 |
return f"{risque_pct:.1f}%"
|
| 182 |
|
| 183 |
|
| 184 |
+
def risk_message(risque_pct):
|
| 185 |
+
"""(type de message Streamlit, texte) affiché sous la jauge pour un risque en %."""
|
| 186 |
+
niveau = classify_risk(risque_pct)
|
| 187 |
+
texte = f"Risque {niveau} ({format_risk(risque_pct)})"
|
| 188 |
+
if niveau == "FAIBLE":
|
| 189 |
+
return 'success', f"✅ {texte}"
|
| 190 |
+
if niveau == "MOYEN":
|
| 191 |
+
return 'warning', f"⚠️ {texte}"
|
| 192 |
+
return 'error', f"🚨 {texte}"
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
# Streamlit sert ses pages avec <html lang="en"> : Chrome propose alors de « traduire »
|
| 196 |
+
# l'app (déjà en français) vers le français, ce qui corrompt les libellés
|
| 197 |
+
# (ex. « FAIBLE » → « ÉCHEC », « pics » → « photos »). Ce script, exécuté dans le
|
| 198 |
+
# document parent, déclare la page en français et non traduisible.
|
| 199 |
+
NO_TRANSLATE_JS = """<script>
|
| 200 |
+
const d = window.parent.document;
|
| 201 |
+
d.documentElement.lang = 'fr';
|
| 202 |
+
d.documentElement.setAttribute('translate', 'no');
|
| 203 |
+
d.documentElement.classList.add('notranslate');
|
| 204 |
+
if (!d.querySelector('meta[name="google"][content="notranslate"]')) {
|
| 205 |
+
const m = d.createElement('meta'); m.name = 'google'; m.content = 'notranslate';
|
| 206 |
+
d.head.appendChild(m);
|
| 207 |
+
}
|
| 208 |
+
</script>"""
|
| 209 |
+
|
| 210 |
+
|
| 211 |
def get_risk_color(risque_pct):
|
| 212 |
if risque_pct < RISK_THRESHOLDS['faible']:
|
| 213 |
return "#2ecc71"
|
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Messages affichés par l'app : libellé de niveau correct et protection contre la
|
| 3 |
+
traduction automatique du navigateur (cause de « Risque ÉCHEC » à la place de
|
| 4 |
+
« Risque FAIBLE »).
|
| 5 |
+
"""
|
| 6 |
+
import re
|
| 7 |
+
import sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from unittest.mock import MagicMock
|
| 10 |
+
|
| 11 |
+
import pytest
|
| 12 |
+
|
| 13 |
+
sys.modules['streamlit'] = MagicMock()
|
| 14 |
+
ROOT = Path(__file__).parent.parent
|
| 15 |
+
sys.path.insert(0, str(ROOT))
|
| 16 |
+
|
| 17 |
+
from streamlit_app.utils_simple import risk_message, NO_TRANSLATE_JS
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@pytest.mark.parametrize('risque,kind,texte', [
|
| 21 |
+
(0.1, 'success', "✅ Risque FAIBLE (< 1 %)"),
|
| 22 |
+
(14.9, 'success', "✅ Risque FAIBLE (14.9%)"),
|
| 23 |
+
(15.0, 'warning', "⚠️ Risque MOYEN (15.0%)"),
|
| 24 |
+
(34.9, 'warning', "⚠️ Risque MOYEN (34.9%)"),
|
| 25 |
+
(35.0, 'error', "🚨 Risque ÉLEVÉ (35.0%)"),
|
| 26 |
+
(100.0, 'error', "🚨 Risque ÉLEVÉ (100.0%)"),
|
| 27 |
+
])
|
| 28 |
+
def test_message_sous_la_jauge(risque, kind, texte):
|
| 29 |
+
assert risk_message(risque) == (kind, texte)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_aucun_echec_dans_les_messages_de_niveau():
|
| 33 |
+
for risque in [0, 0.5, 5, 15, 25, 35, 60, 100]:
|
| 34 |
+
assert 'CHEC' not in risk_message(risque)[1].upper()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_aucun_echec_dans_le_code_de_l_app():
|
| 38 |
+
fichiers = list((ROOT / 'streamlit_app').glob('*.py')) + list((ROOT / 'src').glob('*.py'))
|
| 39 |
+
for f in fichiers:
|
| 40 |
+
code = [l for l in f.read_text(encoding='utf-8-sig').splitlines() if not l.strip().startswith('#')]
|
| 41 |
+
for line in code:
|
| 42 |
+
assert not re.search(r'[ÉE]CHEC', line), f"{f.name}: {line.strip()}"
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_page_declaree_en_francais_et_non_traduisible():
|
| 46 |
+
assert "lang = 'fr'" in NO_TRANSLATE_JS
|
| 47 |
+
assert "'translate', 'no'" in NO_TRANSLATE_JS
|
| 48 |
+
assert 'notranslate' in NO_TRANSLATE_JS
|
| 49 |
+
app = (ROOT / 'streamlit_app' / 'app.py').read_text(encoding='utf-8-sig')
|
| 50 |
+
assert 'components.html(NO_TRANSLATE_JS, height=0)' in app
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def test_dockerfile_desactive_la_traduction():
|
| 54 |
+
dockerfile = (ROOT / 'Dockerfile').read_text(encoding='utf-8')
|
| 55 |
+
assert '<html lang="fr" translate="no">' in dockerfile
|
| 56 |
+
assert 'content="notranslate"' in dockerfile
|