Commit
•
9ee75ba
1
Parent(s):
2f7a95a
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Hugging Face's logo
|
2 |
+
Hugging Face
|
3 |
+
Search models, datasets, users...
|
4 |
+
Models
|
5 |
+
Datasets
|
6 |
+
Spaces
|
7 |
+
Docs
|
8 |
+
Solutions
|
9 |
+
Pricing
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
Spaces:
|
14 |
+
|
15 |
+
Abubakari
|
16 |
+
/
|
17 |
+
Expresso_Churn_Prediction_fastapi
|
18 |
+
like
|
19 |
+
0
|
20 |
+
App
|
21 |
+
Files
|
22 |
+
Community
|
23 |
+
Expresso_Churn_Prediction_fastapi
|
24 |
+
/
|
25 |
+
main.py
|
26 |
+
Abubakari's picture
|
27 |
+
Abubakari
|
28 |
+
Update main.py
|
29 |
+
48e0e29
|
30 |
+
1 day ago
|
31 |
+
raw
|
32 |
+
history
|
33 |
+
blame
|
34 |
+
contribute
|
35 |
+
delete
|
36 |
+
5.25 kB
|
37 |
+
import os
|
38 |
+
import sys
|
39 |
+
|
40 |
+
import uvicorn
|
41 |
+
from fastapi import FastAPI, Request, File, UploadFile
|
42 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
43 |
+
from fastapi.staticfiles import StaticFiles
|
44 |
+
from fastapi.templating import Jinja2Templates
|
45 |
+
import pandas as pd
|
46 |
+
import numpy as np
|
47 |
+
from typing import List
|
48 |
+
import joblib
|
49 |
+
from fastapi import FastAPI
|
50 |
+
from pydantic import BaseModel
|
51 |
+
|
52 |
+
# Create an instance of FastAPI
|
53 |
+
app = FastAPI(debug=True)
|
54 |
+
|
55 |
+
# Load the trained models and transformers
|
56 |
+
num_imputer = joblib.load('numerical_imputer.joblib')
|
57 |
+
cat_imputer = joblib.load('cat_imputer.joblib')
|
58 |
+
encoder = joblib.load('encoder.joblib')
|
59 |
+
scaler = joblib.load('scaler.joblib')
|
60 |
+
model = joblib.load('lr_model_vif_smote.joblib')
|
61 |
+
|
62 |
+
original_feature_names = ['MONTANT', 'FREQUENCE_RECH', 'REVENUE', 'ARPU_SEGMENT', 'FREQUENCE',
|
63 |
+
'DATA_VOLUME', 'ON_NET', 'ORANGE', 'TIGO', 'ZONE1', 'ZONE2', 'REGULARITY', 'FREQ_TOP_PACK',
|
64 |
+
'REGION_DAKAR', 'REGION_DIOURBEL', 'REGION_FATICK', 'REGION_KAFFRINE', 'REGION_KAOLACK',
|
65 |
+
'REGION_KEDOUGOU', 'REGION_KOLDA', 'REGION_LOUGA', 'REGION_MATAM', 'REGION_SAINT-LOUIS',
|
66 |
+
'REGION_SEDHIOU', 'REGION_TAMBACOUNDA', 'REGION_THIES', 'REGION_ZIGUINCHOR',
|
67 |
+
'TENURE_Long-term', 'TENURE_Medium-term', 'TENURE_Mid-term', 'TENURE_Short-term',
|
68 |
+
'TENURE_Very short-term', 'TOP_PACK_data', 'TOP_PACK_international', 'TOP_PACK_messaging',
|
69 |
+
'TOP_PACK_other_services', 'TOP_PACK_social_media', 'TOP_PACK_value_added_services',
|
70 |
+
'TOP_PACK_voice']
|
71 |
+
|
72 |
+
|
73 |
+
class InputData(BaseModel):
|
74 |
+
MONTANT: float
|
75 |
+
FREQUENCE_RECH: float
|
76 |
+
REVENUE: float
|
77 |
+
ARPU_SEGMENT: float
|
78 |
+
FREQUENCE: float
|
79 |
+
DATA_VOLUME: float
|
80 |
+
ON_NET: float
|
81 |
+
ORANGE: float
|
82 |
+
TIGO: float
|
83 |
+
ZONE1: float
|
84 |
+
ZONE2: float
|
85 |
+
REGULARITY: float
|
86 |
+
FREQ_TOP_PACK: float
|
87 |
+
REGION: str
|
88 |
+
TENURE: str
|
89 |
+
TOP_PACK: str
|
90 |
+
|
91 |
+
|
92 |
+
def preprocess_input(input_data):
|
93 |
+
input_df = pd.DataFrame(input_data, index=[0])
|
94 |
+
|
95 |
+
cat_columns = ['REGION', 'TENURE', 'TOP_PACK']
|
96 |
+
num_columns = [col for col in input_df.columns if col not in cat_columns]
|
97 |
+
|
98 |
+
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
|
99 |
+
input_df_imputed_num = num_imputer.transform(input_df[num_columns])
|
100 |
+
|
101 |
+
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
|
102 |
+
columns=encoder.get_feature_names_out(cat_columns))
|
103 |
+
|
104 |
+
input_df_scaled = scaler.transform(input_df_imputed_num)
|
105 |
+
input_scaled_df = pd.DataFrame(input_df_scaled, columns=num_columns)
|
106 |
+
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
|
107 |
+
final_df = final_df.reindex(columns=original_feature_names, fill_value=0)
|
108 |
+
|
109 |
+
return final_df
|
110 |
+
|
111 |
+
|
112 |
+
def make_prediction(data, model):
|
113 |
+
probabilities = model.predict_proba(data)
|
114 |
+
churn_labels = ["No Churn" if class_idx == 0 else "Churn" for class_idx in range(len(probabilities[0]))]
|
115 |
+
churn_probabilities = probabilities[0]
|
116 |
+
|
117 |
+
# Get the predicted churn label and its probability
|
118 |
+
predicted_class_index = np.argmax(churn_probabilities)
|
119 |
+
predicted_churn_label = churn_labels[predicted_class_index]
|
120 |
+
predicted_probability = churn_probabilities[predicted_class_index]
|
121 |
+
|
122 |
+
# Customize the output message based on the predicted churn label and its probability
|
123 |
+
if predicted_churn_label == "Churn":
|
124 |
+
output_message = f"⚠️ Customer is likely to churn with a probability of {predicted_probability:.2f}. This indicates a high risk of losing the customer. ⚠️"
|
125 |
+
else:
|
126 |
+
output_message = f"✅ Customer is not likely to churn with a probability of {predicted_probability:.2f}. This indicates a lower risk of losing the customer. ✅"
|
127 |
+
|
128 |
+
return output_message
|
129 |
+
|
130 |
+
@app.get("/")
|
131 |
+
def read_root():
|
132 |
+
|
133 |
+
info = """
|
134 |
+
Welcome to the Expressor Churn Prediction API!. This API provides advanced machine learning predictions for churn. ⚡📊 For more information and to explore the API's capabilities, please visit the documentation: https://abubakari-expresso-churn-prediction-fastapi.hf.space/docs
|
135 |
+
"""
|
136 |
+
return info.strip()
|
137 |
+
|
138 |
+
# Model information endpoint
|
139 |
+
@app.post('/model-info')
|
140 |
+
async def model_info():
|
141 |
+
model_name = model.__class__.__name__ # get model name
|
142 |
+
model_params = model.get_params() # get model parameters
|
143 |
+
model_information = {
|
144 |
+
'model info': {
|
145 |
+
'model name': model_name,
|
146 |
+
'model parameters': model_params
|
147 |
+
}
|
148 |
+
}
|
149 |
+
return model_information # return model information
|
150 |
+
|
151 |
+
@app.post('/predict')
|
152 |
+
async def predict(input_data: InputData):
|
153 |
+
input_features = input_data.dict()
|
154 |
+
preprocessed_data = preprocess_input(input_features)
|
155 |
+
prediction = make_prediction(preprocessed_data, model)
|
156 |
+
|
157 |
+
return {"prediction": prediction}
|
158 |
+
|
159 |
+
|
160 |
+
@app.post('/batch_predict')
|
161 |
+
async def predict(input_data: List[InputData]):
|
162 |
+
preprocessed_data = []
|
163 |
+
|
164 |
+
for data in input_data:
|
165 |
+
input_features = data.dict()
|
166 |
+
preprocessed = preprocess_input(input_features)
|
167 |
+
preprocessed_data.append(preprocessed)
|
168 |
+
|
169 |
+
predictions = [make_prediction(data, model) for data in preprocessed_data]
|
170 |
+
|
171 |
+
return {"predictions": predictions}
|
172 |
+
|