Spaces:
Sleeping
Sleeping
Commit ·
531bd39
1
Parent(s): c0f7df2
Add application file
Browse files- Churn_Modelling.csv +0 -0
- Dockerfile +16 -0
- label_encoder_gender.pkl +3 -0
- main.py +91 -0
- model.h5 +3 -0
- onehot_encoder_geography.pkl +3 -0
- requirements.txt +7 -0
- scaler.pkl +3 -0
Churn_Modelling.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
label_encoder_gender.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:360bd57973ec93035a912994862bffe697c19e212fddcd4f966fb030af35522a
|
| 3 |
+
size 258
|
main.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel, conint, confloat
|
| 3 |
+
from enum import Enum
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import pickle as pkl
|
| 7 |
+
import tensorflow as tf
|
| 8 |
+
from tensorflow.keras.models import load_model
|
| 9 |
+
|
| 10 |
+
# Initialize FastAPI app
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Load the trained model
|
| 14 |
+
model = load_model("model.h5")
|
| 15 |
+
|
| 16 |
+
# Load the pre-trained scalers and encoders
|
| 17 |
+
with open("scaler.pkl", "rb") as f:
|
| 18 |
+
scaler = pkl.load(f)
|
| 19 |
+
|
| 20 |
+
with open("label_encoder_gender.pkl", "rb") as f:
|
| 21 |
+
gen_encoder = pkl.load(f)
|
| 22 |
+
|
| 23 |
+
with open("onehot_encoder_geography.pkl", "rb") as f:
|
| 24 |
+
geo_encoder = pkl.load(f)
|
| 25 |
+
|
| 26 |
+
# Enums for Gender and Geography
|
| 27 |
+
class GenderEnum(str, Enum):
|
| 28 |
+
Male = "Male"
|
| 29 |
+
Female = "Female"
|
| 30 |
+
|
| 31 |
+
class GeographyEnum(str, Enum):
|
| 32 |
+
France = "France"
|
| 33 |
+
Germany = "Germany"
|
| 34 |
+
Spain = "Spain"
|
| 35 |
+
|
| 36 |
+
# Pydantic model for request validation
|
| 37 |
+
class CustomerData(BaseModel):
|
| 38 |
+
CreditScore: conint(ge=350, le=850)
|
| 39 |
+
Gender: GenderEnum
|
| 40 |
+
Age: conint(ge=18, le=92)
|
| 41 |
+
Tenure: conint(ge=0, le=10)
|
| 42 |
+
Balance: confloat(ge=0)
|
| 43 |
+
NumOfProducts: conint(ge=1, le=4)
|
| 44 |
+
HasCrCard: conint(ge=0, le=1)
|
| 45 |
+
IsActiveMember: conint(ge=0, le=1)
|
| 46 |
+
EstimatedSalary: confloat(ge=0)
|
| 47 |
+
Geography: GeographyEnum
|
| 48 |
+
|
| 49 |
+
# API Endpoint for prediction
|
| 50 |
+
@app.post("/predict/")
|
| 51 |
+
def predict_churn(data: CustomerData):
|
| 52 |
+
# Encode gender
|
| 53 |
+
gender_encoded = gen_encoder.transform([data.Gender.value])[0]
|
| 54 |
+
|
| 55 |
+
# One-hot encode geography
|
| 56 |
+
geo_encoded = geo_encoder.transform([[data.Geography.value]])
|
| 57 |
+
geo_encoded_df = pd.DataFrame(geo_encoded, columns=geo_encoder.categories_[0])
|
| 58 |
+
|
| 59 |
+
# Prepare input data as DataFrame
|
| 60 |
+
input_data = {
|
| 61 |
+
"CreditScore": data.CreditScore,
|
| 62 |
+
"Gender": gender_encoded,
|
| 63 |
+
"Age": data.Age,
|
| 64 |
+
"Tenure": data.Tenure,
|
| 65 |
+
"Balance": data.Balance,
|
| 66 |
+
"NumOfProducts": data.NumOfProducts,
|
| 67 |
+
"HasCrCard": data.HasCrCard,
|
| 68 |
+
"IsActiveMember": data.IsActiveMember,
|
| 69 |
+
"EstimatedSalary": data.EstimatedSalary,
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
input_df = pd.DataFrame([input_data])
|
| 73 |
+
|
| 74 |
+
# Append one-hot encoded geography
|
| 75 |
+
input_df = pd.concat([input_df, geo_encoded_df], axis=1)
|
| 76 |
+
|
| 77 |
+
# Rename columns to match training data
|
| 78 |
+
input_df.rename(
|
| 79 |
+
columns={"France": "Geography_France", "Germany": "Geography_Germany", "Spain": "Geography_Spain"},
|
| 80 |
+
inplace=True,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Scale the input data
|
| 84 |
+
input_scaled = scaler.transform(input_df)
|
| 85 |
+
|
| 86 |
+
# Make prediction
|
| 87 |
+
prediction = model.predict(input_scaled)
|
| 88 |
+
|
| 89 |
+
# Return result
|
| 90 |
+
result = "The customer is likely to churn" if prediction[0][0] > 0.5 else "The customer is not likely to churn"
|
| 91 |
+
return {"prediction": result, "probability": float(prediction[0][0])}
|
model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:168c067e4ea72ad77a5303bd432fc923ac92fa4c9ec8e88080f5228af7c7c3a2
|
| 3 |
+
size 62832
|
onehot_encoder_geography.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:af5952b1a655b7212d54b478b896d6154553276da878171d4a7f0559199789d2
|
| 3 |
+
size 597
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
pydantic
|
| 3 |
+
tensorflow
|
| 4 |
+
numpy
|
| 5 |
+
pandas
|
| 6 |
+
scikit-learn
|
| 7 |
+
uvicorn
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ac0cba24ccc97524d4e79423685898630de704289622df2aaa849e786b1bed44
|
| 3 |
+
size 992
|