Spaces:
Sleeping
Sleeping
File size: 2,159 Bytes
0977180 c7f17c8 0977180 3d0c2d9 0977180 c7f17c8 0977180 c7f17c8 0977180 6f61606 0977180 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#!/usr/bin/env python3
"""
Module de configuration de l'application.
Charge les variables d'environnement depuis .env et fournit
une interface pour accéder à la configuration de manière sécurisée.
"""
import os
from functools import lru_cache
from dotenv import load_dotenv
# Charger .env au démarrage du module
load_dotenv()
def _str_to_bool(value: str, default: bool = False) -> bool:
"""Convertit une chaîne d'environnement en booléen robuste."""
if value is None:
return default
return value.lower() in {"1", "true", "yes", "y", "on"}
class Settings:
"""
Configuration de l'application.
Toutes les valeurs sensibles (API keys, etc.) sont chargées depuis
les variables d'environnement ou le fichier .env.
"""
# ===== SÉCURITÉ =====
API_KEY: str = os.getenv("API_KEY", "dev-key-change-me-in-production")
# ===== API =====
API_VERSION: str = os.getenv("API_VERSION", "3.3.0")
API_HOST: str = os.getenv("API_HOST", "0.0.0.0")
API_PORT: int = int(os.getenv("API_PORT", "8000"))
# ===== MODÈLE =====
HF_MODEL_REPO: str = os.getenv(
"HF_MODEL_REPO", "ASI-Engineer/employee-turnover-model"
)
MODEL_FILENAME: str = os.getenv("MODEL_FILENAME", "model/model.pkl")
# ===== ENVIRONNEMENT =====
DEBUG: bool = _str_to_bool(os.getenv("DEBUG", "False"))
LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
GRADIO_ENABLED: bool = _str_to_bool(os.getenv("GRADIO_ENABLED", "True"), True)
# ===== BASE DE DONNÉES =====
DATABASE_URL: str = os.getenv(
"DATABASE_URL", "postgresql://ml_user:15975359320@localhost:5432/oc_p5_db"
)
@property
def is_api_key_required(self) -> bool:
"""
Vérifie si l'API key est requise.
Returns:
False en mode DEBUG, True en production.
"""
return not self.DEBUG
@lru_cache()
def get_settings() -> Settings:
"""
Retourne l'instance singleton des settings.
Le décorateur @lru_cache() assure qu'on ne crée qu'une seule instance.
Returns:
Settings: Configuration de l'application.
"""
return Settings()
|