Upload 7 files
Browse files- cat_imputer.joblib +3 -0
- encoder.joblib +3 -0
- lr_model_vif_smote.joblib +3 -0
- main.py +135 -0
- numerical_imputer.joblib +3 -0
- requirements.txt +14 -0
- scaler.joblib +3 -0
cat_imputer.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d30ccb53964d4b14131ef8b413a068a1ca399d1668c6d6a53f155b9ebf7b6270
|
| 3 |
+
size 1033
|
encoder.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ce49eceb7e11a32bc8babcc7046b0a5395d11588e2d3abc63a891c6aa441ae0a
|
| 3 |
+
size 1668
|
lr_model_vif_smote.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:99ce78fc5406fe3850259aa8750391dfc4d998092396291a3c4ca744e35c8dee
|
| 3 |
+
size 2271
|
main.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
import uvicorn
|
| 5 |
+
from fastapi import FastAPI, Request, File, UploadFile
|
| 6 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 7 |
+
from fastapi.staticfiles import StaticFiles
|
| 8 |
+
from fastapi.templating import Jinja2Templates
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import numpy as np
|
| 11 |
+
from typing import List
|
| 12 |
+
import joblib
|
| 13 |
+
from fastapi import FastAPI
|
| 14 |
+
from pydantic import BaseModel
|
| 15 |
+
|
| 16 |
+
# Create an instance of FastAPI
|
| 17 |
+
app = FastAPI(debug=True)
|
| 18 |
+
|
| 19 |
+
# Load the trained models and transformers
|
| 20 |
+
num_imputer = joblib.load('numerical_imputer.joblib')
|
| 21 |
+
cat_imputer = joblib.load('cat_imputer.joblib')
|
| 22 |
+
encoder = joblib.load('encoder.joblib')
|
| 23 |
+
scaler = joblib.load('scaler.joblib')
|
| 24 |
+
model = joblib.load('lr_model_vif_smote.joblib')
|
| 25 |
+
|
| 26 |
+
original_feature_names = ['MONTANT', 'FREQUENCE_RECH', 'REVENUE', 'ARPU_SEGMENT', 'FREQUENCE',
|
| 27 |
+
'DATA_VOLUME', 'ON_NET', 'ORANGE', 'TIGO', 'ZONE1', 'ZONE2', 'REGULARITY', 'FREQ_TOP_PACK',
|
| 28 |
+
'REGION_DAKAR', 'REGION_DIOURBEL', 'REGION_FATICK', 'REGION_KAFFRINE', 'REGION_KAOLACK',
|
| 29 |
+
'REGION_KEDOUGOU', 'REGION_KOLDA', 'REGION_LOUGA', 'REGION_MATAM', 'REGION_SAINT-LOUIS',
|
| 30 |
+
'REGION_SEDHIOU', 'REGION_TAMBACOUNDA', 'REGION_THIES', 'REGION_ZIGUINCHOR',
|
| 31 |
+
'TENURE_Long-term', 'TENURE_Medium-term', 'TENURE_Mid-term', 'TENURE_Short-term',
|
| 32 |
+
'TENURE_Very short-term', 'TOP_PACK_data', 'TOP_PACK_international', 'TOP_PACK_messaging',
|
| 33 |
+
'TOP_PACK_other_services', 'TOP_PACK_social_media', 'TOP_PACK_value_added_services',
|
| 34 |
+
'TOP_PACK_voice']
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class InputData(BaseModel):
|
| 38 |
+
MONTANT: float
|
| 39 |
+
FREQUENCE_RECH: float
|
| 40 |
+
REVENUE: float
|
| 41 |
+
ARPU_SEGMENT: float
|
| 42 |
+
FREQUENCE: float
|
| 43 |
+
DATA_VOLUME: float
|
| 44 |
+
ON_NET: float
|
| 45 |
+
ORANGE: float
|
| 46 |
+
TIGO: float
|
| 47 |
+
ZONE1: float
|
| 48 |
+
ZONE2: float
|
| 49 |
+
REGULARITY: float
|
| 50 |
+
FREQ_TOP_PACK: float
|
| 51 |
+
REGION: str
|
| 52 |
+
TENURE: str
|
| 53 |
+
TOP_PACK: str
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def preprocess_input(input_data):
|
| 57 |
+
input_df = pd.DataFrame(input_data, index=[0])
|
| 58 |
+
|
| 59 |
+
cat_columns = ['REGION', 'TENURE', 'TOP_PACK']
|
| 60 |
+
num_columns = [col for col in input_df.columns if col not in cat_columns]
|
| 61 |
+
|
| 62 |
+
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
|
| 63 |
+
input_df_imputed_num = num_imputer.transform(input_df[num_columns])
|
| 64 |
+
|
| 65 |
+
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
|
| 66 |
+
columns=encoder.get_feature_names_out(cat_columns))
|
| 67 |
+
|
| 68 |
+
input_df_scaled = scaler.transform(input_df_imputed_num)
|
| 69 |
+
input_scaled_df = pd.DataFrame(input_df_scaled, columns=num_columns)
|
| 70 |
+
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
|
| 71 |
+
final_df = final_df.reindex(columns=original_feature_names, fill_value=0)
|
| 72 |
+
|
| 73 |
+
return final_df
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def make_prediction(data, model):
|
| 77 |
+
probabilities = model.predict_proba(data)
|
| 78 |
+
churn_labels = ["No Churn" if class_idx == 0 else "Churn" for class_idx in range(len(probabilities[0]))]
|
| 79 |
+
churn_probabilities = probabilities[0]
|
| 80 |
+
|
| 81 |
+
# Get the predicted churn label and its probability
|
| 82 |
+
predicted_class_index = np.argmax(churn_probabilities)
|
| 83 |
+
predicted_churn_label = churn_labels[predicted_class_index]
|
| 84 |
+
predicted_probability = churn_probabilities[predicted_class_index]
|
| 85 |
+
|
| 86 |
+
# Customize the output message based on the predicted churn label and its probability
|
| 87 |
+
if predicted_churn_label == "Churn":
|
| 88 |
+
output_message = f"⚠️ Customer is likely to churn with a probability of {predicted_probability:.2f}. This indicates a high risk of losing the customer. ⚠️"
|
| 89 |
+
else:
|
| 90 |
+
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. ✅"
|
| 91 |
+
|
| 92 |
+
return output_message
|
| 93 |
+
|
| 94 |
+
@app.get("/")
|
| 95 |
+
def read_root():
|
| 96 |
+
|
| 97 |
+
info = """
|
| 98 |
+
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-sepsis-fastapi-prediction-app.hf.space/docs/
|
| 99 |
+
"""
|
| 100 |
+
return info.strip()
|
| 101 |
+
|
| 102 |
+
# Model information endpoint
|
| 103 |
+
@app.post('/model-info')
|
| 104 |
+
async def model_info():
|
| 105 |
+
model_name = model.__class__.__name__ # get model name
|
| 106 |
+
model_params = model.get_params() # get model parameters
|
| 107 |
+
model_information = {
|
| 108 |
+
'model info': {
|
| 109 |
+
'model name': model_name,
|
| 110 |
+
'model parameters': model_params
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
return model_information # return model information
|
| 114 |
+
|
| 115 |
+
@app.post('/predict')
|
| 116 |
+
async def predict(input_data: InputData):
|
| 117 |
+
input_features = input_data.dict()
|
| 118 |
+
preprocessed_data = preprocess_input(input_features)
|
| 119 |
+
prediction = make_prediction(preprocessed_data, model)
|
| 120 |
+
|
| 121 |
+
return {"prediction": prediction}
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
@app.post('/batch_predict')
|
| 125 |
+
async def predict(input_data: List[InputData]):
|
| 126 |
+
preprocessed_data = []
|
| 127 |
+
|
| 128 |
+
for data in input_data:
|
| 129 |
+
input_features = data.dict()
|
| 130 |
+
preprocessed = preprocess_input(input_features)
|
| 131 |
+
preprocessed_data.append(preprocessed)
|
| 132 |
+
|
| 133 |
+
predictions = [make_prediction(data, model) for data in preprocessed_data]
|
| 134 |
+
|
| 135 |
+
return {"predictions": predictions}
|
numerical_imputer.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:84cc3b03cdbe7e804e10073e8a5c1718c078f15e0fe0d87ac0c74a5640d44f05
|
| 3 |
+
size 1103
|
requirements.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
joblib==1.2.0
|
| 2 |
+
numpy==1.22.4
|
| 3 |
+
pandas==1.5.3
|
| 4 |
+
shap==0.41.0
|
| 5 |
+
streamlit==1.22.0
|
| 6 |
+
scikit-learn==1.2.2
|
| 7 |
+
matplotlib==3.7.1
|
| 8 |
+
shap==0.41.0
|
| 9 |
+
fastapi==0.95.1
|
| 10 |
+
uvicorn==0.22.0
|
| 11 |
+
pydantic==1.10.7
|
| 12 |
+
jinja2==3.0.2
|
| 13 |
+
python-multipart==0.0.6
|
| 14 |
+
qrcode==7.4.2
|
scaler.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a342ef9d36377bbc748a53205340fd5e5d386e43a8d3d2497a117db766e23764
|
| 3 |
+
size 1199
|