-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
133 lines (122 loc) · 3.54 KB
/
app.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from fastapi.encoders import jsonable_encoder
import joblib
import streamlit as st
import pandas as pd
import numpy as np
from fastapi import FastAPI
from pydantic import BaseModel, Field
from collections import defaultdict
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.impute import KNNImputer
from sklearn.preprocessing import PowerTransformer, StandardScaler, OrdinalEncoder
from imblearn.over_sampling import ADASYN
from fastapi.middleware.cors import CORSMiddleware
from sklearn.ensemble import GradientBoostingClassifier
app = FastAPI()
# Allow all origins with the appropriate parameters
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allow all origins
allow_credentials=True,
allow_methods=["*"], # Allow all methods (GET, POST, etc.)
allow_headers=["*"], # Allow all headers
)
model = joblib.load("model.pkl")
X = joblib.load("myX.pkl")
class InputData(BaseModel):
gender: str
age: float
ever_married: str
heart_disease: int
hypertension: int
work_type: str
Residence_type: str
avg_glucose_level: float
bmi: float
smoking_status: str
smoking_not_found: bool
@app.post("/predict/")
async def predict(data: InputData):
# Create DataFrame from input data
power_tranform_t, num_scaler_t, cat_scaler_t, ordinal_encoder_t = joblib.load(
"transformers_prime.pkl"
)
df = {
"gender": [data.gender],
"age": [data.age],
"hypertension": [data.hypertension],
"heart_disease": [data.heart_disease],
"ever_married": [data.ever_married],
"work_type": [data.work_type],
"Residence_type": [data.Residence_type],
"avg_glucose_level": [data.avg_glucose_level],
"bmi": [data.bmi],
"smoking_status": [data.smoking_status],
"smoking_not_found": [data.smoking_not_found],
}
X = joblib.load("myX.pkl")
# return df
df = pd.DataFrame(df, index=[0])
df[["age", "avg_glucose_level", "bmi"]] = power_tranform_t.transform(
df[["age", "avg_glucose_level", "bmi"]]
)
df[["age", "avg_glucose_level", "bmi"]] = num_scaler_t.transform(
df[["age", "avg_glucose_level", "bmi"]]
)
df[
[
"gender",
"hypertension",
"heart_disease",
"ever_married",
"work_type",
"Residence_type",
"smoking_status",
"smoking_not_found",
]
] = ordinal_encoder_t.transform(
df[
[
"gender",
"hypertension",
"heart_disease",
"ever_married",
"work_type",
"Residence_type",
"smoking_status",
"smoking_not_found",
]
]
)
df[
[
"gender",
"hypertension",
"heart_disease",
"ever_married",
"work_type",
"Residence_type",
"smoking_status",
"smoking_not_found",
]
] = cat_scaler_t.transform(
df[
[
"gender",
"hypertension",
"heart_disease",
"ever_married",
"work_type",
"Residence_type",
"smoking_status",
"smoking_not_found",
]
]
)
prediction = model.predict(df)
# Return prediction
return {"prediction": int(prediction)}
@app.get("/")
async def main():
return {"message": "Hello, World"}