Spaces:
Sleeping
Sleeping
Commit
•
0806746
1
Parent(s):
9bbea73
added fastapi application scripts
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import logging
|
3 |
+
from typing import Union
|
4 |
+
|
5 |
+
import mlflow
|
6 |
+
import numpy as np
|
7 |
+
from fastapi import FastAPI
|
8 |
+
from pydantic import BaseModel
|
9 |
+
|
10 |
+
from config import settings
|
11 |
+
|
12 |
+
try:
|
13 |
+
path_mlflow_model = "trained_models/knn_ada_boost"
|
14 |
+
sklearn_pipeline = mlflow.sklearn.load_model(path_mlflow_model)
|
15 |
+
except:
|
16 |
+
path_mlflow_model = "/data/models/knn_ada_boost"
|
17 |
+
sklearn_pipeline = mlflow.sklearn.load_model(path_mlflow_model)
|
18 |
+
|
19 |
+
app = FastAPI()
|
20 |
+
logging.basicConfig(level=logging.INFO)
|
21 |
+
|
22 |
+
class WaterPotabilityDataItem(BaseModel):
|
23 |
+
ph: Union[float, None] = np.nan
|
24 |
+
Hardness: Union[float, None] = np.nan
|
25 |
+
Solids: Union[float, None] = np.nan
|
26 |
+
Chloramines: Union[float, None] = np.nan
|
27 |
+
Sulfate: Union[float, None] = np.nan
|
28 |
+
Conductivity: Union[float, None] = np.nan
|
29 |
+
Organic_carbon: Union[float, None] = np.nan
|
30 |
+
Trihalomethanes: Union[float, None] = np.nan
|
31 |
+
Turbidity: Union[float, None] = np.nan
|
32 |
+
|
33 |
+
def predict_pipeline(data_sample):
|
34 |
+
pred_sample = sklearn_pipeline.predict(data_sample)
|
35 |
+
return pred_sample
|
36 |
+
|
37 |
+
@app.get("/info")
|
38 |
+
def get_app_info():
|
39 |
+
dict_info = {
|
40 |
+
"app_name": settings.app_name,
|
41 |
+
"version": settings.version
|
42 |
+
}
|
43 |
+
return dict_info
|
44 |
+
|
45 |
+
@app.post("/predict")
|
46 |
+
def predict(wpd_item: WaterPotabilityDataItem):
|
47 |
+
wpd_arr = np.array(
|
48 |
+
[
|
49 |
+
wpd_item.ph,
|
50 |
+
wpd_item.Hardness,
|
51 |
+
wpd_item.Solids,
|
52 |
+
wpd_item.Chloramines,
|
53 |
+
wpd_item.Sulfate,
|
54 |
+
wpd_item.Conductivity,
|
55 |
+
wpd_item.Organic_carbon,
|
56 |
+
wpd_item.Trihalomethanes,
|
57 |
+
wpd_item.Turbidity,
|
58 |
+
]
|
59 |
+
).reshape(1, -1)
|
60 |
+
logging.info("data sample: %s", wpd_arr)
|
61 |
+
pred_sample = predict_pipeline(wpd_arr)
|
62 |
+
logging.info("Potability prediction: %s", pred_sample)
|
63 |
+
return {"Potability": int(pred_sample)}
|
config.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic_settings import BaseSettings
|
2 |
+
|
3 |
+
class Settings(BaseSettings):
|
4 |
+
app_name: str = "Water Potability API"
|
5 |
+
version: str = "2024.02.07"
|
6 |
+
|
7 |
+
settings = Settings()
|