File size: 1,420 Bytes
5e6aba8
 
a10afeb
 
 
5e6aba8
a10afeb
5e6aba8
 
 
 
a10afeb
a844efb
a10afeb
5e6aba8
 
 
 
a844efb
 
 
5e6aba8
 
a10afeb
 
 
 
 
 
 
 
 
 
 
 
5e6aba8
 
 
 
 
 
 
 
 
a10afeb
 
 
 
 
5e6aba8
 
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
from __future__ import annotations

import os

from fastapi import Depends, FastAPI, HTTPException, Security
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from pydantic import BaseModel

from inference.predict import predict

API_KEY = os.environ.get("API_KEY", "")
CORS_ORIGINS = os.environ.get("CORS_ORIGINS", "http://localhost:3001").split(",")

app = FastAPI(title="M2Predict API")

app.add_middleware(
    CORSMiddleware,
    allow_origins=CORS_ORIGINS,
    allow_methods=["POST"],
    allow_headers=["Content-Type", "Authorization"],
)

security = HTTPBearer()


def verify_api_key(

    credentials: HTTPAuthorizationCredentials = Security(security),

) -> str:
    if not API_KEY:
        raise HTTPException(status_code=500, detail="API_KEY not configured")
    if credentials.credentials != API_KEY:
        raise HTTPException(status_code=403, detail="Invalid API key")
    return credentials.credentials


class PredictRequest(BaseModel):
    code_postal: str
    surface_reelle_bati: float
    nombre_pieces_principales: int
    type_local: str


@app.post("/predict")
def predict_endpoint(

    req: PredictRequest,

    model_version: str = "v1_rf_te",

    _key: str = Depends(verify_api_key),

):
    result = predict(req.model_dump(), model_version=model_version)
    return result