1+ from flask import Flask
2+ from flask import request
3+ from flask import render_template
4+ import pickle
5+ import numpy as np
6+
7+ df = pickle .load (open ('./models/df.pkl' ,'rb' ))
8+ classifier = pickle .load (open ('./models/classifier.pkl' ,'rb' ))
9+
10+ app = Flask (__name__ )
11+
12+ @app .route ('/' ,methods = ['GET' ])
13+ def index ():
14+ # Sort the unique values
15+ SeniorCitizen = sorted (df ['SeniorCitizen' ].unique ())
16+ Dependents = sorted (df ['Dependents' ].unique ())
17+ Contract_MonthToMonth = sorted (df ['Contract_MonthToMonth' ].unique ())
18+ Contract_One_year = sorted (df ['Contract_One_year' ].unique ())
19+ Contract_Two_year = sorted (df ['Contract_Two_year' ].unique ())
20+ tenure_bin_New = sorted (df ['tenure_bin_New' ].unique ())
21+ tenure_bin_Mid = sorted (df ['tenure_bin_Mid' ].unique ())
22+ tenure_bin_Long = sorted (df ['tenure_bin_Long' ].unique ())
23+ InternetService_Fiber_optic = sorted (df ['InternetService_Fiber_optic' ].unique ())
24+ OnlineSecurity = sorted (df ['OnlineSecurity' ].unique ())
25+ TechSupport = sorted (df ['TechSupport' ].unique ())
26+ PaymentMethod_Electronic_check = sorted (df ['PaymentMethod_Electronic_check' ].unique ())
27+ PaperlessBilling = sorted (df ['PaperlessBilling' ].unique ())
28+
29+ return render_template ('index.html' , SeniorCitizen = SeniorCitizen , Dependents = Dependents , Contract_MonthToMonth = Contract_MonthToMonth , Contract_One_year = Contract_One_year , Contract_Two_year = Contract_Two_year , tenure_bin_New = tenure_bin_New , tenure_bin_Mid = tenure_bin_Mid , tenure_bin_Long = tenure_bin_Long , InternetService_Fiber_optic = InternetService_Fiber_optic , OnlineSecurity = OnlineSecurity , TechSupport = TechSupport , PaymentMethod_Electronic_check = PaymentMethod_Electronic_check , PaperlessBilling = PaperlessBilling )
30+
31+ @app .route ('/predict' ,methods = ['POST' ])
32+ def predict ():
33+ SeniorCitizen = int (request .form ['SeniorCitizen' ])
34+ Dependents = int (request .form ['Dependents' ])
35+ Contract_MonthToMonth = float (request .form ['Contract_MonthToMonth' ])
36+ Contract_One_year = float (request .form ['Contract_One_year' ])
37+ Contract_Two_year = float (request .form ['Contract_Two_year' ])
38+ tenure_bin_New = float (request .form ['tenure_bin_New' ])
39+ tenure_bin_Mid = float (request .form ['tenure_bin_Mid' ])
40+ tenure_bin_Long = float (request .form ['tenure_bin_Long' ])
41+ InternetService_Fiber_optic = float (request .form ['InternetService_Fiber_optic' ])
42+ OnlineSecurity = int (request .form ['OnlineSecurity' ])
43+ TechSupport = int (request .form ['TechSupport' ])
44+ PaymentMethod_Electronic_check = float (request .form ['PaymentMethod_Electronic_check' ])
45+ PaperlessBilling = int (request .form ['PaperlessBilling' ])
46+ MonthlyCharges = float (request .form ['MonthlyCharges' ])
47+ TotalCharges = float (request .form ['TotalCharges' ])
48+
49+ # predict
50+ predict_churn = classifier .predict (np .array ([SeniorCitizen , Dependents , Contract_One_year , Contract_MonthToMonth , Contract_Two_year , tenure_bin_New , tenure_bin_Mid , tenure_bin_Long , InternetService_Fiber_optic , OnlineSecurity , TechSupport , PaymentMethod_Electronic_check , PaperlessBilling , MonthlyCharges , TotalCharges ]).reshape (1 ,15 ))
51+
52+ return render_template (
53+ 'index.html' ,
54+ predict_churn = predict_churn ,
55+ SeniorCitizen = SeniorCitizen ,
56+ Dependents = Dependents ,
57+ Contract_MonthToMonth = Contract_MonthToMonth ,
58+ Contract_One_year = Contract_One_year ,
59+ Contract_Two_year = Contract_Two_year ,
60+ tenure_bin_New = tenure_bin_New ,
61+ tenure_bin_Mid = tenure_bin_Mid ,
62+ tenure_bin_Long = tenure_bin_Long ,
63+ InternetService_Fiber_optic = InternetService_Fiber_optic ,
64+ OnlineSecurity = OnlineSecurity ,
65+ TechSupport = TechSupport ,
66+ PaymentMethod_Electronic_check = PaymentMethod_Electronic_check ,
67+ PaperlessBilling = PaperlessBilling ,
68+ MonthlyCharges = MonthlyCharges ,
69+ TotalCharges = TotalCharges
70+ )
71+
72+ if __name__ == '__main__' :
73+ app .run (debug = True ,host = '0.0.0.0' ,port = 5000 )
0 commit comments