Spaces:
Sleeping
Sleeping
File size: 1,813 Bytes
0806746 |
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 |
import os
import logging
from typing import Union
import mlflow
import numpy as np
from fastapi import FastAPI
from pydantic import BaseModel
from config import settings
try:
path_mlflow_model = "trained_models/knn_ada_boost"
sklearn_pipeline = mlflow.sklearn.load_model(path_mlflow_model)
except:
path_mlflow_model = "/data/models/knn_ada_boost"
sklearn_pipeline = mlflow.sklearn.load_model(path_mlflow_model)
app = FastAPI()
logging.basicConfig(level=logging.INFO)
class WaterPotabilityDataItem(BaseModel):
ph: Union[float, None] = np.nan
Hardness: Union[float, None] = np.nan
Solids: Union[float, None] = np.nan
Chloramines: Union[float, None] = np.nan
Sulfate: Union[float, None] = np.nan
Conductivity: Union[float, None] = np.nan
Organic_carbon: Union[float, None] = np.nan
Trihalomethanes: Union[float, None] = np.nan
Turbidity: Union[float, None] = np.nan
def predict_pipeline(data_sample):
pred_sample = sklearn_pipeline.predict(data_sample)
return pred_sample
@app.get("/info")
def get_app_info():
dict_info = {
"app_name": settings.app_name,
"version": settings.version
}
return dict_info
@app.post("/predict")
def predict(wpd_item: WaterPotabilityDataItem):
wpd_arr = np.array(
[
wpd_item.ph,
wpd_item.Hardness,
wpd_item.Solids,
wpd_item.Chloramines,
wpd_item.Sulfate,
wpd_item.Conductivity,
wpd_item.Organic_carbon,
wpd_item.Trihalomethanes,
wpd_item.Turbidity,
]
).reshape(1, -1)
logging.info("data sample: %s", wpd_arr)
pred_sample = predict_pipeline(wpd_arr)
logging.info("Potability prediction: %s", pred_sample)
return {"Potability": int(pred_sample)}
|