flokabukie
commited on
Commit
•
cf60f35
1
Parent(s):
4525bde
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import pickle
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
|
7 |
+
def load_pickle(filename):
|
8 |
+
with open(filename, 'rb') as file:
|
9 |
+
data = pickle.load(file)
|
10 |
+
return data
|
11 |
+
|
12 |
+
pkl_file=load_pickle('modelcomponent1.pkl')
|
13 |
+
numeric_imputer=pkl_file['numerical_imputer']
|
14 |
+
categoric_imputer=pkl_file['categorical_imputer']
|
15 |
+
scaler_=pkl_file['scaler']
|
16 |
+
encoder_=pkl_file['encoder']
|
17 |
+
best_model = pkl_file['model']
|
18 |
+
top_pack=pkl_file['top_pack_values']
|
19 |
+
|
20 |
+
def preprocess_data(Data_Volume, On_Net,Orange, Tigo, Regularity, Freq_Top_Pack,
|
21 |
+
Region, Tenure, Top_Pack):
|
22 |
+
|
23 |
+
df=pd.DataFrame({
|
24 |
+
'Data_Volume':[Data_Volume],
|
25 |
+
'On_Net':[On_Net],
|
26 |
+
'Orange':[Orange],
|
27 |
+
'Tigo' :[Tigo],
|
28 |
+
'Regularity':[Regularity],
|
29 |
+
'Freq_Top_Pack':[Freq_Top_Pack],
|
30 |
+
'Region':[Region],
|
31 |
+
'Tenure' :[Tenure],
|
32 |
+
'Top_Pack':[Top_Pack]
|
33 |
+
})
|
34 |
+
|
35 |
+
# Preprocess the data
|
36 |
+
# Selecting categorical and numerical columns separately
|
37 |
+
cat_columns = df.select_dtypes(include=['object', 'category']).columns.tolist()
|
38 |
+
num_columns = df.select_dtypes(exclude=['object', 'category']).columns.tolist()
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
# Apply the imputers on the input data
|
43 |
+
|
44 |
+
cat_imputer=categoric_imputer.transform(df[cat_columns])
|
45 |
+
num_imputer=numeric_imputer.transform(df[num_columns])
|
46 |
+
|
47 |
+
|
48 |
+
#Encode
|
49 |
+
cat_encode = encoder_.transform(cat_imputer)
|
50 |
+
cat_encoded_df = pd.DataFrame(cat_encode.toarray(), columns=encoder_.get_feature_names_out(cat_columns))
|
51 |
+
print(num_imputer)
|
52 |
+
# Scale the numerical columns
|
53 |
+
|
54 |
+
num_scaler=scaler_.transform( num_imputer)
|
55 |
+
num_scaler_df=pd.DataFrame(num_scaler, columns=num_columns)
|
56 |
+
|
57 |
+
#combined
|
58 |
+
df_final = pd.concat([num_scaler_df, cat_encoded_df], axis=1)
|
59 |
+
#print(df_final)
|
60 |
+
return df_final
|
61 |
+
|
62 |
+
def predict_churn(Data_Volume, On_Net, Orange,Tigo, Regularity, Freq_Top_Pack, Region, Tenure, Top_Pack):
|
63 |
+
|
64 |
+
preprocessed_data = preprocess_data(Data_Volume, On_Net,Orange, Tigo, Regularity, Freq_Top_Pack, Region, Tenure, Top_Pack)
|
65 |
+
|
66 |
+
# Make predictions
|
67 |
+
predictions = best_model.predict_proba(preprocessed_data)
|
68 |
+
#print(predictions)
|
69 |
+
return round(predictions[0][0],2), round(predictions[0][1],2)
|
70 |
+
|
71 |
+
with gr.Blocks() as demo:
|
72 |
+
gr.Markdown('''
|
73 |
+
# 📞Telecom Customer Churn Prediction App ☎️ ''')
|
74 |
+
img=gr.Image('churncss.png')
|
75 |
+
gr.Markdown('''
|
76 |
+
|
77 |
+
## Welcome Cherished User👋
|
78 |
+
|
79 |
+
### Please Predict Customer Churn 🙂''')
|
80 |
+
with gr.Row():
|
81 |
+
Region = gr.Dropdown(label='Region', choices=['DAKAR', 'DIOURBEL', 'FATICK',
|
82 |
+
'KAFFRINE', 'KAOLACK', 'KEDOUGOU',
|
83 |
+
'KOLDA', 'LOUGA', 'MATAM',
|
84 |
+
'SAINT-LOUIS', 'SEDHIOU', 'TAMBACOUNDA', 'THIES', 'ZIGUINCHOR'])
|
85 |
+
Tenure = gr.Dropdown(label='Tenure', choices=['K > 24 month', 'I 18-21 month', 'H 15-18 month' , 'G 12-15 month',
|
86 |
+
'J 21-24 month','F 9-12 month','E 6-9 month','D 3-6 month'])
|
87 |
+
Top_Pack =gr.Dropdown(label='Top_Pack', choices=top_pack)
|
88 |
+
with gr.Row():
|
89 |
+
Data_Volume= gr.Number(label='Data_Volume')
|
90 |
+
On_Net=gr.Number(label='On_Net')
|
91 |
+
|
92 |
+
Orange=gr.Number(label='Orange')
|
93 |
+
Tigo=gr.Number(label='Tigo')
|
94 |
+
Regularity=gr.Slider(label='Regularity', minimum=int(1), maximum=int(62), value=1, step=1)
|
95 |
+
Freq_Top_Pack=gr.Number(label='Freq_Top_Pack')
|
96 |
+
|
97 |
+
submit_button=gr.Button('Predict')
|
98 |
+
|
99 |
+
with gr.Row():
|
100 |
+
with gr.Accordion('Churn Prediction'):
|
101 |
+
output1=gr.Slider(maximum=1,
|
102 |
+
minimum=0,
|
103 |
+
value=1,
|
104 |
+
label='Yes')
|
105 |
+
output2=gr.Slider(maximum=1,
|
106 |
+
minimum=0,
|
107 |
+
value=1,
|
108 |
+
label='No')
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
submit_button.click(fn=predict_churn, inputs=[ Data_Volume, On_Net, Orange, Tigo, Regularity, Freq_Top_Pack, Region, Tenure, Top_Pack ], outputs=[output1, output2])
|
113 |
+
|
114 |
+
demo.launch()
|