siddhesh1981's picture
Upload folder using huggingface_hub
6104937 verified
import pandas as pd
import numpy as np
import joblib
from flask import Flask,request,jsonify
app=Flask(__name__)
model=joblib.load('Churn_model_v1.joblib')
# let us create endpoint for home
@app.get('/')
def home():
return "Welcome to the Backend API for Customer Churn Prediction"
# let us create endpoint for predict data record
@app.post('/Predict/Data')
def predict_data():
data=request.get_json()
user_input={ 'CreditScore':data['CreditScore'],
'Geography':data['Geography'],
'Age':data['Age'],
'Tenure':data['Tenure'],
'Balance':data['Balance'],
'NumOfProducts':data['NumOfProducts'],
'HasCrCard':data['HasCrCard'],
'IsActiveMember':data['IsActiveMember'],
'EstimatedSalary':data['EstimatedSalary']
}
df_user_input=pd.DataFrame([user_input])
prediction1=model.predict_proba(df_user_input)[:,1]>0.21
prediction=prediction1.tolist()[0]
predict_label='Churn' if prediction==1 else 'Not Churn'
return jsonify({'predict_label':predict_label})
# let us create endpoint for predicting batch data
@app.post('/Predict/Batch')
def predict_batch():
file1=request.files['file']
df_file=pd.read_csv(file1)
prediction1=model.predict_proba(df_file.drop(['CustomerId','Surname'],axis=1))[:,1]>0.21
prediction=prediction1.tolist()
predictionlist=['Churn' if x==1 else 'Not Churn' for x in prediction]
listofids=df_file.CustomerId.values.tolist()
dictionary1=dict(zip(listofids,predictionlist))
return dictionary1
if __name__=='__main__':
app.run(debug=True)