|
|
import mlflow |
|
|
import uvicorn |
|
|
import pandas as pd |
|
|
from pydantic import BaseModel |
|
|
from typing import Literal, List, Union |
|
|
from fastapi import FastAPI, File, UploadFile, HTTPException |
|
|
import joblib |
|
|
import traceback |
|
|
import logging |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
df = pd.DataFrame({ |
|
|
"MedInc": [8.3252], |
|
|
"HouseAge": [41.0], |
|
|
"AveRooms": [6.984126984126984], |
|
|
"AveBedrms": [1.0238095238095237], |
|
|
"Population": [322.0], |
|
|
"AveOccup": [2.5555555555555554], |
|
|
"Latitude": [37.88], |
|
|
"Longitude": [-122.23], |
|
|
}) |
|
|
|
|
|
class PredictionFeatures(BaseModel): |
|
|
MedInc: float |
|
|
HouseAge: float |
|
|
AveRooms: float |
|
|
AveBedrms: float |
|
|
Population: float |
|
|
AveOccup: float |
|
|
Latitude: float |
|
|
Longitude: float |
|
|
|
|
|
@app.get("/") |
|
|
async def root(): |
|
|
logging.debug("Request") |
|
|
return {"message": "L'application FastAPI est déployée avec succès !"} |
|
|
|
|
|
@app.post("/predict", tags=["Machine Learning"]) |
|
|
async def predict(predictionFeatures: PredictionFeatures): |
|
|
""" |
|
|
Prédiction du prix d'une maison. |
|
|
""" |
|
|
try: |
|
|
|
|
|
caracts = pd.DataFrame({"MedInc": [predictionFeatures.MedInc], |
|
|
"HouseAge": [predictionFeatures.HouseAge], |
|
|
"AveRooms": [predictionFeatures.AveRooms], |
|
|
"AveBedrms": [predictionFeatures.AveBedrms], |
|
|
"Population": [predictionFeatures.Population], |
|
|
"AveOccup": [predictionFeatures.AveOccup], |
|
|
"Latitude": [predictionFeatures.Latitude], |
|
|
"Longitude": [predictionFeatures.Longitude], |
|
|
}) |
|
|
|
|
|
|
|
|
logged_model = 'runs:/0abb54594cf24179b220ff2e5acff9d7/best_estimator' |
|
|
loaded_model = mlflow.pyfunc.load_model(logged_model) |
|
|
|
|
|
|
|
|
prediction_price = loaded_model.predict(caracts) |
|
|
|
|
|
|
|
|
response = {"prediction": prediction_price.tolist()[0]} |
|
|
return response |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
error_message = str(e) + "\n" + traceback.format_exc() |
|
|
raise HTTPException(status_code=500, detail=error_message) |
|
|
|
|
|
print("Le model est là !") |
|
|
|
|
|
|