Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Churn_model_v1.joblib +3 -0
- DockerFile +19 -0
- app.py +56 -0
- requirements.txt +10 -0
Churn_model_v1.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f3a10e003ff70d9caf8dcd74ac5eed30883909e24a19afea6feaa8300194f9df
|
| 3 |
+
size 6361812
|
DockerFile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
%%writefile backend_files/Dockerfile
|
| 3 |
+
FROM python:3.9-slim
|
| 4 |
+
|
| 5 |
+
# Set the working directory inside the container
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
# Copy all files from the current directory to the container's working directory
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
# Install dependencies from the requirements file without using cache to reduce image size
|
| 12 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Define the command to start the application using Gunicorn with 4 worker processes
|
| 15 |
+
# - `-w 4`: Uses 4 worker processes for handling requests
|
| 16 |
+
# - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
|
| 17 |
+
# - `app:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`)
|
| 18 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:ChurnPredictionAPI"]
|
| 19 |
+
|
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib
|
| 4 |
+
from flask import Flask,request,jsonify
|
| 5 |
+
|
| 6 |
+
ChurnPredictionAPI=Flask("Churn Prediction API")
|
| 7 |
+
|
| 8 |
+
model=joblib.load('Churn_model_v1.joblib')
|
| 9 |
+
|
| 10 |
+
# let us create endpoint for home
|
| 11 |
+
|
| 12 |
+
@ChurnPredictionAPI.get('/')
|
| 13 |
+
def home():
|
| 14 |
+
return "Welcome to the Backend API for Customer Churn Prediction"
|
| 15 |
+
|
| 16 |
+
# let us create endpoint for predict data record
|
| 17 |
+
|
| 18 |
+
@ChurnPredictionAPI.post('/Predict/Data')
|
| 19 |
+
def predict_data():
|
| 20 |
+
data=request.get_json()
|
| 21 |
+
|
| 22 |
+
user_input={ 'CreditScore':data['CreditScore'],
|
| 23 |
+
'Geography':data['Geography'],
|
| 24 |
+
'Age':data['Age'],
|
| 25 |
+
'Tenure':data['Tenure'],
|
| 26 |
+
'Balance':data['Balance'],
|
| 27 |
+
'NumOfProducts':data['NumOfProducts'],
|
| 28 |
+
'HasCrCard':data['HasCrCard'],
|
| 29 |
+
'IsActiveMember':data['IsActiveMember'],
|
| 30 |
+
'EstimatedSalary':data['EstimatedSalary']
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
df_user_input=pd.DataFrame([user_input])
|
| 34 |
+
prediction=model.predict_proba(df_user_input)[:,1]>0.21.tolist()[0]
|
| 35 |
+
predict_label='Churn' if prediction==1 else 'Not Churn'
|
| 36 |
+
|
| 37 |
+
return jsonify({'predict_label':predict_label})
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# let us create endpoint for predicting batch data
|
| 41 |
+
|
| 42 |
+
@ChurnPredictionAPI.post('/Predict/Batch')
|
| 43 |
+
def predict_batch():
|
| 44 |
+
|
| 45 |
+
file1=request.files['file']
|
| 46 |
+
df_file=pd.read_csv(file1)
|
| 47 |
+
prediction=model.predict_proba(df_file.drop(['CustomerId','Surname']),axis=1)[:,1]>0.21.tolist()
|
| 48 |
+
predictionlist=['Churn' if x==1 else 'Not Churn' for x in prediction]
|
| 49 |
+
listofids=df_file.CustomerId.values.tolist()
|
| 50 |
+
dictionary1=dict(zip(listofids,predictionlist))
|
| 51 |
+
return dictionary1
|
| 52 |
+
|
| 53 |
+
if __name__=='__main__':
|
| 54 |
+
app.run(debug=True)
|
| 55 |
+
|
| 56 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
xgboost==2.1.4
|
| 5 |
+
joblib==1.4.2
|
| 6 |
+
Werkzeug==2.2.2
|
| 7 |
+
flask==2.2.2
|
| 8 |
+
gunicorn==20.1.0
|
| 9 |
+
requests==2.28.1
|
| 10 |
+
imblearn==0.13.0
|