File size: 2,094 Bytes
e59e621
 
9796e1e
a9477b4
 
100ddbe
 
e59e621
 
9796e1e
e59e621
 
a9477b4
 
75c7c56
 
 
 
 
a9477b4
 
 
75c7c56
a9477b4
 
 
 
 
 
 
 
e59e621
 
e406ddb
e59e621
100ddbe
 
 
 
 
 
 
 
9796e1e
e59e621
100ddbe
e59e621
 
e406ddb
100ddbe
e406ddb
100ddbe
 
 
 
 
 
 
e406ddb
e59e621
 
 
 
9796e1e
 
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
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.routing import APIRoute
from pydantic import field_validator
from pydantic_settings import BaseSettings
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.routes import predict
from ml_model.loader import load_pipeline
from app.middleware.logging import LoggingMiddleware


class Settings(BaseSettings):
    api_key: str = ""
    db_host: str = ""
    db_port: int = 5432
    db_user: str = ""
    db_password: str = ""
    db_name: str = ""

    model_config = {"env_file": ".env"}


    @field_validator("api_key")
    @classmethod
    def api_key_must_not_be_empty(cls, v: str) -> str:
        if not v:
            raise ValueError("API_KEY doit être défini dans le fichier .env")
        return v


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Charge le pipeline ML, initialise la DB et les chemins loggés au démarrage
    app.state.pipeline, app.state.threshold = load_pipeline()
    settings = Settings()
    app.state.settings = settings
    db_url = (
        f"postgresql+psycopg2://{settings.db_user}:{settings.db_password}"
        f"@{settings.db_host}:{settings.db_port}/{settings.db_name}"
    )
    app.state.engine = create_engine(db_url)
    app.state.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=app.state.engine)
    app.state.logged_paths = {route.path for route in app.routes if isinstance(route, APIRoute)}
    yield
    app.state.engine.dispose()

app = FastAPI(
    title="Futurisys - Prédiction du Risque d'Attrition RH",
    description="""
## API de prédiction du risque d'attrition RH

Déployée pour **Futurisys**, cette API expose un modèle de Machine Learning (Régression Logistique)
entraîné sur des données RH pour prédire la probabilité qu'un employé quitte l'entreprise.

### Authentification
Toutes les routes protégées nécessitent un header `X-API-Key`.
""",
    version="1.3.2",
    lifespan=lifespan,
)

app.include_router(predict.router)
app.add_middleware(LoggingMiddleware)