-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (49 loc) · 1.73 KB
/
Copy pathmain.py
File metadata and controls
72 lines (49 loc) · 1.73 KB
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
import uvicorn # ASGI
from fastapi import FastAPI
# modules for serving root files
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
# iput data model class
from customer import Customer
import pandas as pd
import pickle # pickle is built in
# creating the app object
app = FastAPI()
# accessing the pickle
pickle_in = open("churn_predictor.pkl", "rb")
classifier = pickle.load(pickle_in)
# loading the scaler
with open("scaler.pkl", "rb") as scaler_file:
scaler = pickle.load(scaler_file)
# routers
# static folder for the frontend root endpoint
@app.get("/", response_class=HTMLResponse)
async def get_home():
with open("./Frontend/index.html") as f:
return f.read()
app.mount("/static", StaticFiles(directory="Frontend/static"), name="static")
@app.post("/predict")
async def predict_churn(data: Customer):
df = pd.DataFrame(
[data.dict().values()], columns=data.dict().keys()
) # converting input data into a DF
scaled_features = scaler.transform(df)
prediction = classifier.predict(scaled_features)
# Get prediction probabilities
probabilities = classifier.predict_proba(scaled_features)
if prediction == 1:
customer_status = "The customer will churn"
else:
customer_status = "The customer will not churn"
return {
"prediction": customer_status,
"probability": {
"churn": round(probabilities[0][1], 2), # Probability of churn (class 1)
"not_churn": round(
probabilities[0][0], 2
), # Probability of not churn (class 0)
},
}
# running the api with uvicorn on localhost
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)