|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
app = Flask("Telecom Customer Churn Predictor") |
|
|
|
|
|
|
|
|
model = joblib.load("churn_prediction_model_v1_0.joblib") |
|
|
|
|
|
|
|
|
@app.get('/') |
|
|
def home(): |
|
|
return "Welcome to the Telecom Customer Churn Prediction API" |
|
|
|
|
|
|
|
|
@app.post('/v1/customer') |
|
|
def predict_churn(): |
|
|
|
|
|
customer_data = request.get_json() |
|
|
|
|
|
|
|
|
sample = { |
|
|
'SeniorCitizen': customer_data['SeniorCitizen'], |
|
|
'Partner': customer_data['Partner'], |
|
|
'Dependents': customer_data['Dependents'], |
|
|
'tenure': customer_data['tenure'], |
|
|
'PhoneService': customer_data['PhoneService'], |
|
|
'InternetService': customer_data['InternetService'], |
|
|
'Contract': customer_data['Contract'], |
|
|
'PaymentMethod': customer_data['PaymentMethod'], |
|
|
'MonthlyCharges': customer_data['MonthlyCharges'], |
|
|
'TotalCharges': customer_data['TotalCharges'] |
|
|
} |
|
|
|
|
|
|
|
|
input_data = pd.DataFrame([sample]) |
|
|
|
|
|
|
|
|
prediction = model.predict(input_data).tolist()[0] |
|
|
|
|
|
|
|
|
prediction_label = "churn" if prediction == 1 else "not churn" |
|
|
|
|
|
|
|
|
return jsonify({'Prediction': prediction_label}) |
|
|
|
|
|
|
|
|
@app.post('/v1/customerbatch') |
|
|
def predict_churn_batch(): |
|
|
|
|
|
file = request.files['file'] |
|
|
|
|
|
|
|
|
input_data = pd.read_csv(file) |
|
|
|
|
|
|
|
|
predictions = [ |
|
|
'Churn' if x == 1 |
|
|
else "Not Churn" |
|
|
for x in model.predict(input_data.drop("customerID",axis=1)).tolist() |
|
|
] |
|
|
|
|
|
cust_id_list = input_data.customerID.values.tolist() |
|
|
output_dict = dict(zip(cust_id_list, predictions)) |
|
|
|
|
|
return output_dict |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(debug=True) |
|
|
|